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(
"Trying to add {} with commands {}".format(self.username, str(commands_to_add_user))
)
_ = subprocess.check_output(
commands_to_add_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT
)
try:
_ = subprocess.check_output(
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):
return self # No initialization/logging on needed in Linux
@ -55,7 +58,12 @@ class AutoNewLinuxUser(AutoNewUser):
command_as_new_user = shlex.split(
"sudo -u {username} {command}".format(username=self.username, command=command)
)
return subprocess.call(command_as_new_user, timeout=SHORT_REQUEST_TIMEOUT)
try:
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):
# delete the user.
@ -65,6 +73,9 @@ class AutoNewLinuxUser(AutoNewUser):
self.username, str(commands_to_delete_user)
)
)
_ = subprocess.check_output(
commands_to_delete_user, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT
)
try:
_ = subprocess.check_output(
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)
logger.debug("Trying to add {} with commands {}".format(self.username, str(windows_cmds)))
_ = subprocess.check_output(
windows_cmds, stderr=subprocess.STDOUT, timeout=SHORT_REQUEST_TIMEOUT
)
try:
_ = subprocess.check_output(
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):
try: