diff options
Diffstat (limited to 'azurelinuxagent/common/osutil')
-rw-r--r-- | azurelinuxagent/common/osutil/debian.py | 13 | ||||
-rw-r--r-- | azurelinuxagent/common/osutil/default.py | 77 | ||||
-rw-r--r-- | azurelinuxagent/common/osutil/factory.py | 51 | ||||
-rw-r--r-- | azurelinuxagent/common/osutil/freebsd.py | 2 | ||||
-rw-r--r-- | azurelinuxagent/common/osutil/redhat.py | 29 |
5 files changed, 117 insertions, 55 deletions
diff --git a/azurelinuxagent/common/osutil/debian.py b/azurelinuxagent/common/osutil/debian.py index f455572..b3db921 100644 --- a/azurelinuxagent/common/osutil/debian.py +++ b/azurelinuxagent/common/osutil/debian.py @@ -37,7 +37,7 @@ class DebianOSUtil(DefaultOSUtil): super(DebianOSUtil, self).__init__() def restart_ssh_service(self): - return shellutil.run("service sshd restart", chk_err=False) + return shellutil.run("systemctl --job-mode=ignore-dependencies try-reload-or-restart ssh", chk_err=False) def stop_agent_service(self): return shellutil.run("service azurelinuxagent stop", chk_err=False) @@ -45,3 +45,14 @@ class DebianOSUtil(DefaultOSUtil): def start_agent_service(self): return shellutil.run("service azurelinuxagent start", chk_err=False) + def start_network(self): + pass + + def remove_rules_files(self, rules_files=""): + pass + + def restore_rules_files(self, rules_files=""): + pass + + def get_dhcp_lease_endpoint(self): + return self.get_endpoint_from_leases_path('/var/lib/dhcp/dhclient.*.leases') diff --git a/azurelinuxagent/common/osutil/default.py b/azurelinuxagent/common/osutil/default.py index dc73379..4cd379b 100644 --- a/azurelinuxagent/common/osutil/default.py +++ b/azurelinuxagent/common/osutil/default.py @@ -51,7 +51,8 @@ class DefaultOSUtil(object): def __init__(self): self.agent_conf_file_path = '/etc/waagent.conf' - self.selinux=None + self.selinux = None + self.disable_route_warning = False def get_agent_conf_file_path(self): return self.agent_conf_file_path @@ -438,7 +439,8 @@ class DefaultOSUtil(object): iface=sock[i:i+16].split(b'\0', 1)[0] if len(iface) == 0 or self.is_loopback(iface) or iface != primary: # test the next one - logger.info('interface [{0}] skipped'.format(iface)) + if len(iface) != 0 and not self.disable_route_warning: + logger.info('interface [{0}] skipped'.format(iface)) continue else: # use this one @@ -470,7 +472,8 @@ class DefaultOSUtil(object): primary = None primary_metric = None - logger.info("examine /proc/net/route for primary interface") + if not self.disable_route_warning: + logger.info("examine /proc/net/route for primary interface") with open('/proc/net/route') as routing_table: idx = 0 for header in filter(lambda h: len(h) > 0, routing_table.readline().strip(" \n").split("\t")): @@ -494,11 +497,18 @@ class DefaultOSUtil(object): if primary is None: primary = '' - - logger.info('primary interface is [{0}]'.format(primary)) + if not self.disable_route_warning: + with open('/proc/net/route') as routing_table_fh: + routing_table_text = routing_table_fh.read() + logger.error('could not determine primary interface, ' + 'please ensure /proc/net/route is correct:\n' + '{0}'.format(routing_table_text)) + self.disable_route_warning = True + else: + logger.info('primary interface is [{0}]'.format(primary)) + self.disable_route_warning = False return primary - def is_primary_interface(self, ifname): """ Indicate whether the specified interface is the primary. @@ -507,13 +517,14 @@ class DefaultOSUtil(object): """ return self.get_primary_interface() == ifname - def is_loopback(self, ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) result = fcntl.ioctl(s.fileno(), 0x8913, struct.pack('256s', ifname[:15])) flags, = struct.unpack('H', result[16:18]) isloopback = flags & 8 == 8 - logger.info('interface [{0}] has flags [{1}], is loopback [{2}]'.format(ifname, flags, isloopback)) + if not self.disable_route_warning: + logger.info('interface [{0}] has flags [{1}], ' + 'is loopback [{2}]'.format(ifname, flags, isloopback)) return isloopback def get_dhcp_lease_endpoint(self): @@ -675,6 +686,7 @@ class DefaultOSUtil(object): def publish_hostname(self, hostname): self.set_dhcp_hostname(hostname) + self.set_hostname_record(hostname) ifname = self.get_if_name() self.restart_if(ifname) @@ -725,22 +737,39 @@ class DefaultOSUtil(object): port_id = port_id - 2 device = None path = "/sys/bus/vmbus/devices/" - for vmbus in os.listdir(path): - deviceid = fileutil.read_file(os.path.join(path, vmbus, "device_id")) - guid = deviceid.lstrip('{').split('-') - if guid[0] == g0 and guid[1] == "000" + ustr(port_id): - for root, dirs, files in os.walk(path + vmbus): - if root.endswith("/block"): - device = dirs[0] - break - else : #older distros - for d in dirs: - if ':' in d and "block" == d.split(':')[0]: - device = d.split(':')[1] - break - break + if os.path.exists(path): + for vmbus in os.listdir(path): + deviceid = fileutil.read_file(os.path.join(path, vmbus, "device_id")) + guid = deviceid.lstrip('{').split('-') + if guid[0] == g0 and guid[1] == "000" + ustr(port_id): + for root, dirs, files in os.walk(path + vmbus): + if root.endswith("/block"): + device = dirs[0] + break + else : #older distros + for d in dirs: + if ':' in d and "block" == d.split(':')[0]: + device = d.split(':')[1] + break + break return device + def set_hostname_record(self, hostname): + fileutil.write_file(conf.get_published_hostname(), contents=hostname) + + def get_hostname_record(self): + hostname_record = conf.get_published_hostname() + if not os.path.exists(hostname_record): + # this file is created at provisioning time with agents >= 2.2.3 + hostname = socket.gethostname() + logger.warn('Hostname record does not exist, ' + 'creating [{0}] with hostname [{1}]', + hostname_record, + hostname) + self.set_hostname_record(hostname) + record = fileutil.read_file(hostname_record) + return record + def del_account(self, username): if self.is_sys_user(username): logger.error("{0} is a system user. Will not delete it.", username) @@ -749,10 +778,10 @@ class DefaultOSUtil(object): self.conf_sudoer(username, remove=True) def decode_customdata(self, data): - return base64.b64decode(data) + return base64.b64decode(data).decode('utf-8') def get_total_mem(self): - # Get total memory in bytes and divide by 1024**2 to get the valu in MB. + # Get total memory in bytes and divide by 1024**2 to get the value in MB. return os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') / (1024**2) def get_processor_cores(self): diff --git a/azurelinuxagent/common/osutil/factory.py b/azurelinuxagent/common/osutil/factory.py index 2718ba1..acd7f6e 100644 --- a/azurelinuxagent/common/osutil/factory.py +++ b/azurelinuxagent/common/osutil/factory.py @@ -17,9 +17,7 @@ import azurelinuxagent.common.logger as logger from azurelinuxagent.common.utils.textutil import Version -from azurelinuxagent.common.version import DISTRO_NAME, DISTRO_VERSION, \ - DISTRO_FULL_NAME - +from azurelinuxagent.common.version import * from .default import DefaultOSUtil from .clearlinux import ClearLinuxUtil from .coreos import CoreOSUtil @@ -27,54 +25,65 @@ from .debian import DebianOSUtil from .freebsd import FreeBSDOSUtil from .redhat import RedhatOSUtil, Redhat6xOSUtil from .suse import SUSEOSUtil, SUSE11OSUtil -from .ubuntu import UbuntuOSUtil, Ubuntu12OSUtil, Ubuntu14OSUtil, \ - UbuntuSnappyOSUtil +from .ubuntu import UbuntuOSUtil, Ubuntu12OSUtil, Ubuntu14OSUtil, UbuntuSnappyOSUtil from .alpine import AlpineOSUtil from .bigip import BigIpOSUtil -def get_osutil(distro_name=DISTRO_NAME, distro_version=DISTRO_VERSION, + +def get_osutil(distro_name=DISTRO_NAME, + distro_code_name=DISTRO_CODE_NAME, + distro_version=DISTRO_VERSION, distro_full_name=DISTRO_FULL_NAME): + if distro_name == "clear linux software for intel architecture": return ClearLinuxUtil() + if distro_name == "ubuntu": - if Version(distro_version) == Version("12.04") or \ - Version(distro_version) == Version("12.10"): + if Version(distro_version) == Version("12.04") or Version(distro_version) == Version("12.10"): return Ubuntu12OSUtil() - elif Version(distro_version) == Version("14.04") or \ - Version(distro_version) == Version("14.10"): + elif Version(distro_version) == Version("14.04") or Version(distro_version) == Version("14.10"): return Ubuntu14OSUtil() elif distro_full_name == "Snappy Ubuntu Core": return UbuntuSnappyOSUtil() else: return UbuntuOSUtil() + if distro_name == "alpine": return AlpineOSUtil() + if distro_name == "kali": - return DebianOSUtil() - if distro_name == "coreos": + return DebianOSUtil() + + if distro_name == "coreos" or distro_code_name == "coreos": return CoreOSUtil() + if distro_name == "suse": - if distro_full_name=='SUSE Linux Enterprise Server' and \ - Version(distro_version) < Version('12') or \ - distro_full_name == 'openSUSE' and \ - Version(distro_version) < Version('13.2'): + if distro_full_name == 'SUSE Linux Enterprise Server' \ + and Version(distro_version) < Version('12') \ + or distro_full_name == 'openSUSE' and Version(distro_version) < Version('13.2'): return SUSE11OSUtil() else: return SUSEOSUtil() + elif distro_name == "debian": return DebianOSUtil() - elif distro_name == "redhat" or distro_name == "centos" or \ - distro_name == "oracle": + + elif distro_name == "redhat" \ + or distro_name == "centos" \ + or distro_name == "oracle": if Version(distro_version) < Version("7"): return Redhat6xOSUtil() else: return RedhatOSUtil() + elif distro_name == "freebsd": return FreeBSDOSUtil() + elif distro_name == "bigip": return BigIpOSUtil() + else: - logger.warn("Unable to load distro implementation for {0}.", distro_name) - logger.warn("Use default distro implementation instead.") + logger.warn("Unable to load distro implementation for {0}. Using " + "default distro implementation instead.", + distro_name) return DefaultOSUtil() - diff --git a/azurelinuxagent/common/osutil/freebsd.py b/azurelinuxagent/common/osutil/freebsd.py index 54c7452..d0c40b9 100644 --- a/azurelinuxagent/common/osutil/freebsd.py +++ b/azurelinuxagent/common/osutil/freebsd.py @@ -77,7 +77,7 @@ class FreeBSDOSUtil(DefaultOSUtil): "").format(username, output)) def del_root_password(self): - err = shellutil.run('pw mod user root -w no') + err = shellutil.run('pw usermod root -h -') if err: raise OSUtilError("Failed to delete root password: Failed to update password database.") diff --git a/azurelinuxagent/common/osutil/redhat.py b/azurelinuxagent/common/osutil/redhat.py index 80370a2..5254ea5 100644 --- a/azurelinuxagent/common/osutil/redhat.py +++ b/azurelinuxagent/common/osutil/redhat.py @@ -36,6 +36,7 @@ import azurelinuxagent.common.utils.textutil as textutil from azurelinuxagent.common.utils.cryptutil import CryptUtil from azurelinuxagent.common.osutil.default import DefaultOSUtil + class Redhat6xOSUtil(DefaultOSUtil): def __init__(self): super(Redhat6xOSUtil, self).__init__() @@ -57,7 +58,7 @@ class Redhat6xOSUtil(DefaultOSUtil): def unregister_agent_service(self): return shellutil.run("chkconfig --del waagent", chk_err=False) - + def openssl_to_openssh(self, input_file, output_file): pubkey = fileutil.read_file(input_file) try: @@ -67,7 +68,7 @@ class Redhat6xOSUtil(DefaultOSUtil): raise OSUtilError(ustr(e)) fileutil.write_file(output_file, ssh_rsa_pubkey) - #Override + # Override def get_dhcp_pid(self): ret = shellutil.run_get_output("pidof dhclient", chk_err=False) return ret[1] if ret[0] == 0 else None @@ -84,22 +85,28 @@ class Redhat6xOSUtil(DefaultOSUtil): def set_dhcp_hostname(self, hostname): ifname = self.get_if_name() filepath = "/etc/sysconfig/network-scripts/ifcfg-{0}".format(ifname) - fileutil.update_conf_file(filepath, 'DHCP_HOSTNAME', + fileutil.update_conf_file(filepath, + 'DHCP_HOSTNAME', 'DHCP_HOSTNAME={0}'.format(hostname)) def get_dhcp_lease_endpoint(self): return self.get_endpoint_from_leases_path('/var/lib/dhclient/dhclient-*.leases') + class RedhatOSUtil(Redhat6xOSUtil): def __init__(self): super(RedhatOSUtil, self).__init__() def set_hostname(self, hostname): """ - Set /etc/hostname - Unlike redhat 6.x, redhat 7.x will set hostname to /etc/hostname + Unlike redhat 6.x, redhat 7.x will set hostname via hostnamectl + Due to a bug in systemd in Centos-7.0, if this call fails, fallback + to hostname. """ - DefaultOSUtil.set_hostname(self, hostname) + hostnamectl_cmd = "hostnamectl set-hostname {0}".format(hostname) + if shellutil.run(hostnamectl_cmd, chk_err=False) != 0: + logger.warn("[{0}] failed, attempting fallback".format(hostnamectl_cmd)) + DefaultOSUtil.set_hostname(self, hostname) def publish_hostname(self, hostname): """ @@ -118,5 +125,11 @@ class RedhatOSUtil(Redhat6xOSUtil): DefaultOSUtil.openssl_to_openssh(self, input_file, output_file) def get_dhcp_lease_endpoint(self): - # centos7 has this weird naming with double hyphen like /var/lib/dhclient/dhclient--eth0.lease - return self.get_endpoint_from_leases_path('/var/lib/dhclient/dhclient-*.lease') + # dhclient + endpoint = self.get_endpoint_from_leases_path('/var/lib/dhclient/dhclient-*.lease') + + if endpoint is None: + # NetworkManager + endpoint = self.get_endpoint_from_leases_path('/var/lib/NetworkManager/dhclient-*.lease') + + return endpoint |