From 5944bddad185fbba04819126b280313fa462b719 Mon Sep 17 00:00:00 2001 From: Robert Schweikert Date: Tue, 24 Mar 2020 16:06:54 -0400 Subject: [PATCH 1/3] - Current versions of openSUSE and SLES are systemd based. Use hostnamectl to set the hostname and make sure it is properly propagated through the system. --- azurelinuxagent/common/osutil/suse.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/azurelinuxagent/common/osutil/suse.py b/azurelinuxagent/common/osutil/suse.py index 3c9b0749f9..5f6ccb7904 100644 --- a/azurelinuxagent/common/osutil/suse.py +++ b/azurelinuxagent/common/osutil/suse.py @@ -34,7 +34,7 @@ class SUSE11OSUtil(DefaultOSUtil): - + # TODO: Remove this class after March 31 2022, EOL of SLES 11 def __init__(self): super(SUSE11OSUtil, self).__init__() self.jit_enabled = True @@ -82,6 +82,11 @@ def __init__(self): super(SUSEOSUtil, self).__init__() self.dhclient_name = 'wickedd-dhcp4' + def set_hostname(self, hostname): + shellutil.run( + "hostnamectl set-hostname {0}".format(hostname), chk_err=False + ) + def stop_dhcp_service(self): cmd = "systemctl stop {0}".format(self.dhclient_name) return shellutil.run(cmd, chk_err=False) From c1e3ecc8d35831657103316e58edd0dc1c8f986b Mon Sep 17 00:00:00 2001 From: Robert Schweikert Date: Thu, 23 Apr 2020 07:49:36 -0400 Subject: [PATCH 2/3] - Switch rom shellutil.run() to shellutil.run_command() + The shellutil.run() is deprecated and should no longer be used --- azurelinuxagent/common/osutil/suse.py | 194 +++++++++++++++++++++----- 1 file changed, 157 insertions(+), 37 deletions(-) diff --git a/azurelinuxagent/common/osutil/suse.py b/azurelinuxagent/common/osutil/suse.py index 5f6ccb7904..5aed961c50 100644 --- a/azurelinuxagent/common/osutil/suse.py +++ b/azurelinuxagent/common/osutil/suse.py @@ -16,25 +16,14 @@ # Requires Python 2.6+ and Openssl 1.0+ # -import os -import re -import pwd -import shutil -import socket -import array -import struct -import fcntl -import time -import azurelinuxagent.common.logger as logger import azurelinuxagent.common.utils.fileutil as fileutil import azurelinuxagent.common.utils.shellutil as shellutil -import azurelinuxagent.common.utils.textutil as textutil -from azurelinuxagent.common.version import DISTRO_NAME, DISTRO_VERSION, DISTRO_FULL_NAME +from azurelinuxagent.common.exception import OSUtilError +from azurelinuxagent.common.future import ustr from azurelinuxagent.common.osutil.default import DefaultOSUtil class SUSE11OSUtil(DefaultOSUtil): - # TODO: Remove this class after March 31 2022, EOL of SLES 11 def __init__(self): super(SUSE11OSUtil, self).__init__() self.jit_enabled = True @@ -42,7 +31,12 @@ def __init__(self): def set_hostname(self, hostname): fileutil.write_file('/etc/HOSTNAME', hostname) - shellutil.run("hostname {0}".format(hostname), chk_err=False) + try: + shellutil.run_command(["hostname", hostname]) + except Exception as e: + raise OSUtilError( + "Failed to set hostname. Error: {}".format(ustr(e)) + ) def get_dhcp_pid(self): return self._get_dhcp_pid(["pidof", self.dhclient_name]) @@ -51,30 +45,92 @@ def is_dhcp_enabled(self): return True def stop_dhcp_service(self): - cmd = "/sbin/service {0} stop".format(self.dhclient_name) - return shellutil.run(cmd, chk_err=False) + try: + shellutil.run_command( + ["/sbin/service", self.dhclient_name, "stop"] + ) + except Exception as e: + raise OSUtilError( + "Failed to stop dhcp client {}. Error {}".format( + self.dhclient_name, ustr(e)) + ) def start_dhcp_service(self): - cmd = "/sbin/service {0} start".format(self.dhclient_name) - return shellutil.run(cmd, chk_err=False) + try: + shellutil.run_command( + ["/sbin/service", self.dhclient_name, "start"] + ) + except Exception as e: + raise OSUtilError( + "Failed to start dhcp client {}. Error {}".format( + self.dhclient_name, ustr(e)) + ) def start_network(self) : - return shellutil.run("/sbin/service start network", chk_err=False) + try: + shellutil.run_command( + ["/sbin/service", "network", "start"] + ) + except Exception as e: + raise OSUtilError( + "Failed to start network client {}. Error {}".format( + self.dhclient_name, ustr(e)) + ) def restart_ssh_service(self): - return shellutil.run("/sbin/service sshd restart", chk_err=False) + try: + shellutil.run_command( + ["/sbin/service", "sshd", "restart"] + ) + except Exception as e: + raise OSUtilError( + "Failed to restart sshd client {}. Error {}".format( + self.dhclient_name, ustr(e)) + ) def stop_agent_service(self): - return shellutil.run("/sbin/service {0} stop".format(self.service_name), chk_err=False) + try: + shellutil.run_command( + ["/sbin/service", self.service_name, "stop"] + ) + except Exception as e: + raise OSUtilError( + "Failed to stop {}. Error {}".format( + self.service_name, ustr(e)) + ) def start_agent_service(self): - return shellutil.run("/sbin/service {0} start".format(self.service_name), chk_err=False) + try: + shellutil.run_command( + ["/sbin/service", self.service_name, "start"] + ) + except Exception as e: + raise OSUtilError( + "Failed to start {}. Error {}".format( + self.service_name, ustr(e)) + ) def register_agent_service(self): - return shellutil.run("/sbin/insserv {0}".format(self.service_name), chk_err=False) + try: + shellutil.run_command( + ["/sbin/insserv", self.service_name] + ) + except Exception as e: + raise OSUtilError( + "Failed to enable {}. Error {}".format( + self.service_name, ustr(e)) + ) def unregister_agent_service(self): - return shellutil.run("/sbin/insserv -r {0}".format(self.service_name), chk_err=False) + try: + shellutil.run_command( + ["/sbin/insserv", "-r", self.service_name] + ) + except Exception as e: + raise OSUtilError( + "Failed to remove {}. Error {}".format( + self.service_name, ustr(e)) + ) class SUSEOSUtil(SUSE11OSUtil): @@ -83,32 +139,96 @@ def __init__(self): self.dhclient_name = 'wickedd-dhcp4' def set_hostname(self, hostname): - shellutil.run( - "hostnamectl set-hostname {0}".format(hostname), chk_err=False - ) + try: + shellutil.run_command(["hostnamectl", "set-hostname", hostname]) + except Exception as e: + raise OSUtilError( + "Failed to set hostname. Error: {}".format(ustr(e)) + ) def stop_dhcp_service(self): - cmd = "systemctl stop {0}".format(self.dhclient_name) - return shellutil.run(cmd, chk_err=False) + try: + shellutil.run_command( + ["systemctl", "stop", "{}.service".format(self.dhclient_name)] + ) + except Exception as e: + raise OSUtilError( + "Failed to stop dhcp client {}. Error {}".format( + self.dhclient_name, ustr(e)) + ) def start_dhcp_service(self): - cmd = "systemctl start {0}".format(self.dhclient_name) - return shellutil.run(cmd, chk_err=False) + try: + shellutil.run_command( + ["systemctl", "start", "{}.service".format(self.dhclient_name)] + ) + except Exception as e: + raise OSUtilError( + "Failed to start dhcp client {}. Error {}".format( + self.dhclient_name, ustr(e)) + ) def start_network(self) : - return shellutil.run("systemctl start network", chk_err=False) + try: + shellutil.run_command( + ["systemctl", "start", "network.service"] + ) + except Exception as e: + raise OSUtilError( + "Failed to start network. Error: {}".format(ustr(e)) + ) def restart_ssh_service(self): - return shellutil.run("systemctl restart sshd", chk_err=False) + try: + shellutil.run_command( + ["systemctl", "restart", "sshd.service"] + ) + except Exception as e: + raise OSUtilError( + "Failed to restart sshd. Error: {}".format(ustr(e)) + ) def stop_agent_service(self): - return shellutil.run("systemctl stop {0}".format(self.service_name), chk_err=False) + try: + shellutil.run_command( + ["systemctl", "stop", "{}.service".format(self.service_name)] + ) + except Exception as e: + raise OSUtilError( + "Failed to stop {}. Error {}".format( + self.service_name, ustr(e)) + ) def start_agent_service(self): - return shellutil.run("systemctl start {0}".format(self.service_name), chk_err=False) + try: + shellutil.run_command( + ["systemctl", "start", "{}.service".format(self.service_name)] + ) + except Exception as e: + raise OSUtilError( + "Failed to start {}. Error {}".format( + self.service_name, ustr(e)) + ) def register_agent_service(self): - return shellutil.run("systemctl enable {0}".format(self.service_name), chk_err=False) + try: + shellutil.run_command( + ["systemctl", "enable", "{}.service".format(self.service_name)] + ) + except Exception as e: + raise OSUtilError( + "Failed to enable {}. Error {}".format( + self.service_name, ustr(e)) + ) def unregister_agent_service(self): - return shellutil.run("systemctl disable {0}".format(self.service_name), chk_err=False) + try: + shellutil.run_command( + ["systemctl", "disable", "{}.service".format(self.service_name)] + ) + except Exception as e: + raise OSUtilError( + "Failed to disable {}. Error {}".format( + self.service_name, ustr(e)) + ) + From c8eb62889d077f47389b7b88c2be93d9dc8db1dc Mon Sep 17 00:00:00 2001 From: Paula Gombar Date: Thu, 23 Apr 2020 16:15:07 -0700 Subject: [PATCH 3/3] keep current behaviour of swallowing exceptions --- azurelinuxagent/common/osutil/default.py | 8 + azurelinuxagent/common/osutil/suse.py | 183 ++++------------------- 2 files changed, 34 insertions(+), 157 deletions(-) diff --git a/azurelinuxagent/common/osutil/default.py b/azurelinuxagent/common/osutil/default.py index 90a6e739ab..f4509d1d56 100644 --- a/azurelinuxagent/common/osutil/default.py +++ b/azurelinuxagent/common/osutil/default.py @@ -1420,3 +1420,11 @@ def _update_nic_state(self, state, ip_command, handler, description): handler(state[interface_name], result.group(2)) else: logger.error("Interface {0} has {1} but no link state".format(interface_name, description)) + + @staticmethod + def _run_command_without_raising(cmd, log_error=True): + try: + shellutil.run_command(cmd, log_error=log_error) + # Original implementation of run() does a blanket catch, so mimicking the behaviour here + except Exception: + pass diff --git a/azurelinuxagent/common/osutil/suse.py b/azurelinuxagent/common/osutil/suse.py index 5aed961c50..2192c67d41 100644 --- a/azurelinuxagent/common/osutil/suse.py +++ b/azurelinuxagent/common/osutil/suse.py @@ -27,16 +27,11 @@ class SUSE11OSUtil(DefaultOSUtil): def __init__(self): super(SUSE11OSUtil, self).__init__() self.jit_enabled = True - self.dhclient_name='dhcpcd' + self.dhclient_name = 'dhcpcd' def set_hostname(self, hostname): fileutil.write_file('/etc/HOSTNAME', hostname) - try: - shellutil.run_command(["hostname", hostname]) - except Exception as e: - raise OSUtilError( - "Failed to set hostname. Error: {}".format(ustr(e)) - ) + self._run_command_without_raising(["hostname", hostname], log_error=False) def get_dhcp_pid(self): return self._get_dhcp_pid(["pidof", self.dhclient_name]) @@ -45,92 +40,28 @@ def is_dhcp_enabled(self): return True def stop_dhcp_service(self): - try: - shellutil.run_command( - ["/sbin/service", self.dhclient_name, "stop"] - ) - except Exception as e: - raise OSUtilError( - "Failed to stop dhcp client {}. Error {}".format( - self.dhclient_name, ustr(e)) - ) + self._run_command_without_raising(["/sbin/service", self.dhclient_name, "stop"], log_error=False) def start_dhcp_service(self): - try: - shellutil.run_command( - ["/sbin/service", self.dhclient_name, "start"] - ) - except Exception as e: - raise OSUtilError( - "Failed to start dhcp client {}. Error {}".format( - self.dhclient_name, ustr(e)) - ) + self._run_command_without_raising(["/sbin/service", self.dhclient_name, "start"], log_error=False) - def start_network(self) : - try: - shellutil.run_command( - ["/sbin/service", "network", "start"] - ) - except Exception as e: - raise OSUtilError( - "Failed to start network client {}. Error {}".format( - self.dhclient_name, ustr(e)) - ) + def start_network(self): + self._run_command_without_raising(["/sbin/service", "network", "start"], log_error=False) def restart_ssh_service(self): - try: - shellutil.run_command( - ["/sbin/service", "sshd", "restart"] - ) - except Exception as e: - raise OSUtilError( - "Failed to restart sshd client {}. Error {}".format( - self.dhclient_name, ustr(e)) - ) + self._run_command_without_raising(["/sbin/service", "sshd", "restart"], log_error=False) def stop_agent_service(self): - try: - shellutil.run_command( - ["/sbin/service", self.service_name, "stop"] - ) - except Exception as e: - raise OSUtilError( - "Failed to stop {}. Error {}".format( - self.service_name, ustr(e)) - ) + self._run_command_without_raising(["/sbin/service", self.service_name, "stop"], log_error=False) def start_agent_service(self): - try: - shellutil.run_command( - ["/sbin/service", self.service_name, "start"] - ) - except Exception as e: - raise OSUtilError( - "Failed to start {}. Error {}".format( - self.service_name, ustr(e)) - ) + self._run_command_without_raising(["/sbin/service", self.service_name, "start"], log_error=False) def register_agent_service(self): - try: - shellutil.run_command( - ["/sbin/insserv", self.service_name] - ) - except Exception as e: - raise OSUtilError( - "Failed to enable {}. Error {}".format( - self.service_name, ustr(e)) - ) + self._run_command_without_raising(["/sbin/insserv", self.service_name], log_error=False) def unregister_agent_service(self): - try: - shellutil.run_command( - ["/sbin/insserv", "-r", self.service_name] - ) - except Exception as e: - raise OSUtilError( - "Failed to remove {}. Error {}".format( - self.service_name, ustr(e)) - ) + self._run_command_without_raising(["/sbin/insserv", "-r", self.service_name], log_error=False) class SUSEOSUtil(SUSE11OSUtil): @@ -139,96 +70,34 @@ def __init__(self): self.dhclient_name = 'wickedd-dhcp4' def set_hostname(self, hostname): - try: - shellutil.run_command(["hostnamectl", "set-hostname", hostname]) - except Exception as e: - raise OSUtilError( - "Failed to set hostname. Error: {}".format(ustr(e)) - ) + self._run_command_without_raising(["hostnamectl", "set-hostname", hostname], log_error=False) def stop_dhcp_service(self): - try: - shellutil.run_command( - ["systemctl", "stop", "{}.service".format(self.dhclient_name)] - ) - except Exception as e: - raise OSUtilError( - "Failed to stop dhcp client {}. Error {}".format( - self.dhclient_name, ustr(e)) - ) + self._run_command_without_raising(["systemctl", "stop", "{}.service".format(self.dhclient_name)], + log_error=False) def start_dhcp_service(self): - try: - shellutil.run_command( - ["systemctl", "start", "{}.service".format(self.dhclient_name)] - ) - except Exception as e: - raise OSUtilError( - "Failed to start dhcp client {}. Error {}".format( - self.dhclient_name, ustr(e)) - ) + self._run_command_without_raising(["systemctl", "start", "{}.service".format(self.dhclient_name)], + log_error=False) def start_network(self) : - try: - shellutil.run_command( - ["systemctl", "start", "network.service"] - ) - except Exception as e: - raise OSUtilError( - "Failed to start network. Error: {}".format(ustr(e)) - ) + self._run_command_without_raising(["systemctl", "start", "network.service"], log_error=False) def restart_ssh_service(self): - try: - shellutil.run_command( - ["systemctl", "restart", "sshd.service"] - ) - except Exception as e: - raise OSUtilError( - "Failed to restart sshd. Error: {}".format(ustr(e)) - ) + self._run_command_without_raising(["systemctl", "restart", "sshd.service"], log_error=False) def stop_agent_service(self): - try: - shellutil.run_command( - ["systemctl", "stop", "{}.service".format(self.service_name)] - ) - except Exception as e: - raise OSUtilError( - "Failed to stop {}. Error {}".format( - self.service_name, ustr(e)) - ) + self._run_command_without_raising(["systemctl", "stop", "{}.service".format(self.service_name)], + log_error=False) def start_agent_service(self): - try: - shellutil.run_command( - ["systemctl", "start", "{}.service".format(self.service_name)] - ) - except Exception as e: - raise OSUtilError( - "Failed to start {}. Error {}".format( - self.service_name, ustr(e)) - ) + self._run_command_without_raising(["systemctl", "start", "{}.service".format(self.service_name)], + log_error=False) def register_agent_service(self): - try: - shellutil.run_command( - ["systemctl", "enable", "{}.service".format(self.service_name)] - ) - except Exception as e: - raise OSUtilError( - "Failed to enable {}. Error {}".format( - self.service_name, ustr(e)) - ) + self._run_command_without_raising(["systemctl", "enable", "{}.service".format(self.service_name)], + log_error=False) def unregister_agent_service(self): - try: - shellutil.run_command( - ["systemctl", "disable", "{}.service".format(self.service_name)] - ) - except Exception as e: - raise OSUtilError( - "Failed to disable {}. Error {}".format( - self.service_name, ustr(e)) - ) - + self._run_command_without_raising(["systemctl", "disable", "{}.service".format(self.service_name)], + log_error=False)