Agent: Catch subprocess exceptions in utils/*/users.py

This commit is contained in:
Shreya Malviya 2022-04-04 13:03:50 +05:30
parent b312c509ce
commit bb798898c1
2 changed files with 24 additions and 10 deletions

View File

@ -44,9 +44,12 @@ class AutoNewLinuxUser(AutoNewUser):
logger.debug( logger.debug(
"Trying to add {} with commands {}".format(self.username, str(commands_to_add_user)) "Trying to add {} with commands {}".format(self.username, str(commands_to_add_user))
) )
try:
_ = subprocess.check_output( _ = subprocess.check_output(
commands_to_add_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT commands_to_add_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT
) )
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as err:
logger.error(f"An exception occurred when creating a new linux user: {str(err)}")
def __enter__(self): def __enter__(self):
return self # No initialization/logging on needed in Linux return self # No initialization/logging on needed in Linux
@ -55,7 +58,12 @@ class AutoNewLinuxUser(AutoNewUser):
command_as_new_user = shlex.split( command_as_new_user = shlex.split(
"sudo -u {username} {command}".format(username=self.username, command=command) "sudo -u {username} {command}".format(username=self.username, command=command)
) )
try:
return subprocess.call(command_as_new_user, timeout=SHORT_REQUEST_TIMEOUT) return subprocess.call(command_as_new_user, timeout=SHORT_REQUEST_TIMEOUT)
except subprocess.TimeoutExpired as err:
logger.error(
f"An exception occurred when running a command as a new linux user: {str(err)}"
)
def __exit__(self, _exc_type, value, traceback): def __exit__(self, _exc_type, value, traceback):
# delete the user. # delete the user.
@ -65,6 +73,9 @@ class AutoNewLinuxUser(AutoNewUser):
self.username, str(commands_to_delete_user) self.username, str(commands_to_delete_user)
) )
) )
try:
_ = subprocess.check_output( _ = subprocess.check_output(
commands_to_delete_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT commands_to_delete_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT
) )
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as err:
logger.error(f"An exception occurred when deleting the new linux user: {str(err)}")

View File

@ -50,9 +50,12 @@ class AutoNewWindowsUser(AutoNewUser):
windows_cmds = get_windows_commands_to_add_user(self.username, self.password, True) windows_cmds = get_windows_commands_to_add_user(self.username, self.password, True)
logger.debug("Trying to add {} with commands {}".format(self.username, str(windows_cmds))) logger.debug("Trying to add {} with commands {}".format(self.username, str(windows_cmds)))
try:
_ = subprocess.check_output( _ = subprocess.check_output(
windows_cmds, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT windows_cmds, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT
) )
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as err:
logger.error(f"An exception occurred when creating a new windows user: {str(err)}")
def __enter__(self): def __enter__(self):
try: try: