| 59 | |
| 60 | |
| 61 | class ShellCommandAction(object): |
| 62 | EXPORT_CMD = "export" |
| 63 | |
| 64 | def __init__( |
| 65 | self, |
| 66 | name, |
| 67 | action_exec_id, |
| 68 | command, |
| 69 | user, |
| 70 | env_vars=None, |
| 71 | sudo=False, |
| 72 | timeout=None, |
| 73 | cwd=None, |
| 74 | sudo_password=None, |
| 75 | ): |
| 76 | self.name = name |
| 77 | self.action_exec_id = action_exec_id |
| 78 | self.command = command |
| 79 | self.env_vars = env_vars or {} |
| 80 | self.user = user |
| 81 | self.sudo = sudo |
| 82 | self.timeout = timeout |
| 83 | self.cwd = cwd |
| 84 | self.sudo_password = sudo_password |
| 85 | |
| 86 | def get_full_command_string(self): |
| 87 | # Note: We pass -E to sudo because we want to preserve user provided environment variables |
| 88 | if self.sudo: |
| 89 | command = quote_unix(self.command) |
| 90 | sudo_arguments = " ".join(self._get_common_sudo_arguments()) |
| 91 | command = "sudo %s -- bash -c %s" % (sudo_arguments, command) |
| 92 | else: |
| 93 | if self.user and self.user != LOGGED_USER_USERNAME: |
| 94 | # Need to use sudo to run as a different (requested) user |
| 95 | user = quote_unix(self.user) |
| 96 | sudo_arguments = " ".join(self._get_user_sudo_arguments(user=user)) |
| 97 | command = quote_unix(self.command) |
| 98 | command = "sudo %s -- bash -c %s" % (sudo_arguments, command) |
| 99 | |
| 100 | else: |
| 101 | command = self.command |
| 102 | |
| 103 | return command |
| 104 | |
| 105 | def get_sanitized_full_command_string(self): |
| 106 | """ |
| 107 | Get a command string which can be used inside the log messages (if provided, sudo password |
| 108 | is masked). |
| 109 | |
| 110 | :rtype: ``password`` |
| 111 | """ |
| 112 | command_string = self.get_full_command_string() |
| 113 | |
| 114 | if self.sudo_password: |
| 115 | # Mask sudo password |
| 116 | command_string = "echo -e '%s\n' | %s" % ( |
| 117 | MASKED_ATTRIBUTE_VALUE, |
| 118 | command_string, |
no outgoing calls