Example #1
0
 def device_for_ide_port(self, port_id):
     """
     Return device name attached to ide port 'n'.
     """
     if port_id > 3:
         return None
     g0 = "00000000"
     if port_id > 1:
         g0 = "00000001"
         port_id = port_id - 2
     device = None
     path = "/sys/bus/vmbus/devices/"
     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
Example #2
0
 def conf_sudoer(self, username, nopasswd=False, remove=False):
     doas_conf = "/etc/doas.conf"
     doas = None
     if not remove:
         if not os.path.isfile(doas_conf):
             # always allow root to become root
             doas = "permit keepenv nopass root\n"
             fileutil.append_file(doas_conf, doas)
         if nopasswd:
             doas = "permit keepenv nopass {0}\n".format(username)
         else:
             doas = "permit keepenv persist {0}\n".format(username)
         fileutil.append_file(doas_conf, doas)
         fileutil.chmod(doas_conf, 0o644)
     else:
         # Remove user from doas.conf
         if os.path.isfile(doas_conf):
             try:
                 content = fileutil.read_file(doas_conf)
                 doas = content.split("\n")
                 doas = [x for x in doas if username not in x]
                 fileutil.write_file(doas_conf, "\n".join(doas))
             except IOError as err:
                 raise OSUtilError("Failed to remove sudoer: "
                                   "{0}".format(err))
Example #3
0
 def fetch_cache(self, local_file):
     if not os.path.isfile(local_file):
         raise ProtocolError("{0} is missing.".format(local_file))
     try:
         return fileutil.read_file(local_file)
     except IOError as e:
         raise ProtocolError("Failed to read cache: {0}".format(e))
Example #4
0
 def wait_for_ovfenv(self, max_retry=1800, sleep_time=1):
     """
     Wait for cloud-init to copy ovf-env.xml file from provision ISO
     """
     ovf_file_path = os.path.join(conf.get_lib_dir(), OVF_FILE_NAME)
     for retry in range(0, max_retry):
         if os.path.isfile(ovf_file_path):
             try:
                 ovf_env = OvfEnv(fileutil.read_file(ovf_file_path))
                 self.handle_provision_guest_agent(ovf_env.provision_guest_agent)
                 return
             except ProtocolError as pe:
                 raise ProvisionError("OVF xml could not be parsed "
                                      "[{0}]: {1}".format(ovf_file_path,
                                                          ustr(pe)))
         else:
             if retry < max_retry - 1:
                 logger.info(
                     "Waiting for cloud-init to copy ovf-env.xml to {0} "
                     "[{1} retries remaining, "
                     "sleeping {2}s]".format(ovf_file_path,
                                             max_retry - retry,
                                             sleep_time))
                 if not self.validate_cloud_init():
                     logger.warn("cloud-init does not appear to be running")
                 time.sleep(sleep_time)
     raise ProvisionError("Giving up, ovf-env.xml was not copied to {0} "
                          "after {1}s".format(ovf_file_path,
                                              max_retry * sleep_time))
Example #5
0
    def collect_ext_status(self, ext):
        self.logger.verbose("Collect extension status")

        seq_no, ext_status_file = self.get_status_file_path(ext)
        if seq_no == -1:
            return None

        ext_status = ExtensionStatus(seq_no=seq_no)
        try:
            data_str = fileutil.read_file(ext_status_file)
            data = json.loads(data_str)
            parse_ext_status(ext_status, data)
        except IOError as e:
            ext_status.message = u"Failed to get status file {0}".format(e)
            ext_status.code = -1
            ext_status.status = "error"
        except ExtensionError as e:
            ext_status.message = u"Malformed status file {0}".format(e)
            ext_status.code = e.code
            ext_status.status = "error"
        except ValueError as e:
            ext_status.message = u"Malformed status file {0}".format(e)
            ext_status.code = -1
            ext_status.status = "error"

        return ext_status
Example #6
0
    def copy_ovf_env(self):
        """
        Copy ovf env file from dvd to hard disk.
        Remove password before save it to the disk
        """
        dvd_mount_point = conf.get_dvd_mount_point()
        ovf_file_path_on_dvd = os.path.join(dvd_mount_point, OVF_FILE_NAME)
        tag_file_path_on_dvd = os.path.join(dvd_mount_point, TAG_FILE_NAME)
        try:
            self.osutil.mount_dvd()
            ovfxml = fileutil.read_file(ovf_file_path_on_dvd, remove_bom=True)
            ovfenv = OvfEnv(ovfxml)
            ovfxml = re.sub("<UserPassword>.*?<", "<UserPassword>*<", ovfxml)
            ovf_file_path = os.path.join(conf.get_lib_dir(), OVF_FILE_NAME)
            fileutil.write_file(ovf_file_path, ovfxml)
            
            if os.path.isfile(tag_file_path_on_dvd):
                logger.info("Found {0} in provisioning ISO", TAG_FILE_NAME)
                tag_file_path = os.path.join(conf.get_lib_dir(), TAG_FILE_NAME)
                shutil.copyfile(tag_file_path_on_dvd, tag_file_path) 

        except (OSUtilError, IOError) as e:
            raise ProtocolError(ustr(e))

        try:
            self.osutil.umount_dvd()
            self.osutil.eject_dvd()
        except OSUtilError as e:
            logger.warn(ustr(e))

        return ovfenv
Example #7
0
    def collect_ext_status(self, ext):
        self.logger.verbose("Collect extension status")

        seq_no = self.get_largest_seq_no()
        if seq_no == -1:
            return None

        status_dir = self.get_status_dir()
        ext_status_file = "{0}.status".format(seq_no)
        ext_status_file = os.path.join(status_dir, ext_status_file)

        ext_status = ExtensionStatus(seq_no=seq_no)
        try:
            data_str = fileutil.read_file(ext_status_file)
            data = json.loads(data_str)
            parse_ext_status(ext_status, data)
        except IOError as e:
            ext_status.message = u"Failed to get status file {0}".format(e)
            ext_status.code = -1
            ext_status.status = "error"
        except (ExtensionError, ValueError) as e:
            ext_status.message = u"Malformed status file {0}".format(e)
            ext_status.code = -1
            ext_status.status = "error"

        return ext_status
Example #8
0
    def _ensure_no_orphans(self, orphan_wait_interval=ORPHAN_WAIT_INTERVAL):
        previous_pid_file, pid_file = self._write_pid_file()
        if previous_pid_file is not None:
            try:
                pid = fileutil.read_file(previous_pid_file)
                wait_interval = orphan_wait_interval
                while self.osutil.check_pid_alive(pid):
                    wait_interval -= GOAL_STATE_INTERVAL
                    if wait_interval <= 0:
                        logger.warn(
                            u"{0} forcibly terminated orphan process {1}",
                            CURRENT_AGENT,
                            pid)
                        os.kill(pid, signal.SIGKILL)
                        break
                    
                    logger.info(
                        u"{0} waiting for orphan process {1} to terminate",
                        CURRENT_AGENT,
                        pid)
                    time.sleep(GOAL_STATE_INTERVAL)

            except Exception as e:
                logger.warn(
                    u"Exception occurred waiting for orphan agent to terminate: {0}",
                    ustr(e))
        return
Example #9
0
    def is_provisioned(self):
        '''
        A VM is considered provisionend *anytime* the provisioning
        sentinel file exists and not provisioned *anytime* the file
        is absent.

        If the VM was provisioned using an agent that did not record
        the VM unique identifier, the provisioning file will be re-written
        to include the identifier.

        A warning is logged *if* the VM unique identifier has changed
        since VM was provisioned.
        '''
        if not os.path.isfile(self.provisioned_file_path()):
            return False

        s = fileutil.read_file(self.provisioned_file_path()).strip()
        if not self.osutil.is_current_instance_id(s):
            if len(s) > 0:
                logger.warn("VM is provisioned, "
                            "but the VM unique identifier has changed -- "
                            "clearing cached state")
                from azurelinuxagent.pa.deprovision \
                    import get_deprovision_handler
                deprovision_handler = get_deprovision_handler()
                deprovision_handler.run_changed_unique_id()

            self.write_provisioned()
            self.report_ready()

        return True
Example #10
0
    def get_dhcp_pid(self):
        ret = None
        pidfile = "/var/run/dhclient.pid"

        if os.path.isfile(pidfile):
            ret = fileutil.read_file(pidfile, encoding='ascii')
        return ret
Example #11
0
 def _get_trans_cert(self):
     trans_crt_file = os.path.join(conf.get_lib_dir(),
                                   TRANSPORT_CERT_FILE_NAME)
     if not os.path.isfile(trans_crt_file):
         raise ProtocolError("{0} is missing.".format(trans_crt_file))
     content = fileutil.read_file(trans_crt_file)
     return textutil.get_bytes_from_pem(content)
Example #12
0
    def conf_sudoer(self, username, nopasswd=False, remove=False):
        sudoers_dir = conf.get_sudoers_dir()
        sudoers_wagent = os.path.join(sudoers_dir, 'waagent')

        if not remove:
            # for older distros create sudoers.d
            if not os.path.isdir(sudoers_dir):
                sudoers_file = os.path.join(sudoers_dir, '../sudoers')
                # create the sudoers.d directory
                os.mkdir(sudoers_dir)
                # add the include of sudoers.d to the /etc/sudoers
                sudoers = '\n#includedir ' + sudoers_dir + '\n'
                fileutil.append_file(sudoers_file, sudoers)
            sudoer = None
            if nopasswd:
                sudoer = "{0} ALL=(ALL) NOPASSWD: ALL\n".format(username)
            else:
                sudoer = "{0} ALL=(ALL) ALL\n".format(username)
            fileutil.append_file(sudoers_wagent, sudoer)
            fileutil.chmod(sudoers_wagent, 0o440)
        else:
            #Remove user from sudoers
            if os.path.isfile(sudoers_wagent):
                try:
                    content = fileutil.read_file(sudoers_wagent)
                    sudoers = content.split("\n")
                    sudoers = [x for x in sudoers if username not in x]
                    fileutil.write_file(sudoers_wagent, "\n".join(sudoers))
                except IOError as e:
                    raise OSUtilError("Failed to remove sudoer: {0}".format(e))
Example #13
0
def setup_rdma_device():
    logger.verbose("Parsing SharedConfig XML contents for RDMA details")
    xml_doc = parse_doc(
        fileutil.read_file(os.path.join(conf.get_lib_dir(), SHARED_CONF_FILE_NAME)))
    if xml_doc is None:
        logger.error("Could not parse SharedConfig XML document")
        return
    instance_elem = find(xml_doc, "Instance")
    if not instance_elem:
        logger.error("Could not find <Instance> in SharedConfig document")
        return

    rdma_ipv4_addr = getattrib(instance_elem, "rdmaIPv4Address")
    if not rdma_ipv4_addr:
        logger.error(
            "Could not find rdmaIPv4Address attribute on Instance element of SharedConfig.xml document")
        return

    rdma_mac_addr = getattrib(instance_elem, "rdmaMacAddress")
    if not rdma_mac_addr:
        logger.error(
            "Could not find rdmaMacAddress attribute on Instance element of SharedConfig.xml document")
        return

    # add colons to the MAC address (e.g. 00155D33FF1D ->
    # 00:15:5D:33:FF:1D)
    rdma_mac_addr = ':'.join([rdma_mac_addr[i:i+2]
                              for i in range(0, len(rdma_mac_addr), 2)])
    logger.info("Found RDMA details. IPv4={0} MAC={1}".format(
        rdma_ipv4_addr, rdma_mac_addr))

    # Set up the RDMA device with collected informatino
    RDMADeviceHandler(rdma_ipv4_addr, rdma_mac_addr).start()
    logger.info("RDMA: device is set up")
    return
Example #14
0
 def openssl_to_openssh(self, input_file, output_file):
     pubkey = fileutil.read_file(input_file)
     try:
         cryptutil = CryptUtil(conf.get_openssl_cmd())
         ssh_rsa_pubkey = cryptutil.asn1_to_ssh(pubkey)
     except CryptError as e:
         raise OSUtilError(ustr(e))
     fileutil.write_file(output_file, ssh_rsa_pubkey)
Example #15
0
    def test_read_write_file(self):
        test_file=os.path.join(self.tmp_dir, self.test_file)
        content = ustr(uuid.uuid4())
        fileutil.write_file(test_file, content)

        content_read = fileutil.read_file(test_file)
        self.assertEquals(content, content_read)
        os.remove(test_file)
    def test_read_write_file(self):
        test_file=os.path.join(self.tmp_dir, self.test_file)
        content = ustr(uuid.uuid4())
        fileutil.write_file(test_file, content)

        content_read = fileutil.read_file(test_file)
        self.assertEquals(content, content_read)
        os.remove(test_file)
    def test_rw_utf8_file(self):
        test_file=os.path.join(self.tmp_dir, self.test_file)
        content = u"\u6211"
        fileutil.write_file(test_file, content, encoding="utf-8")

        content_read = fileutil.read_file(test_file)
        self.assertEquals(content, content_read)
        os.remove(test_file)
Example #18
0
    def test_rw_utf8_file(self):
        test_file=os.path.join(self.tmp_dir, self.test_file)
        content = u"\u6211"
        fileutil.write_file(test_file, content, encoding="utf-8")

        content_read = fileutil.read_file(test_file)
        self.assertEquals(content, content_read)
        os.remove(test_file)
Example #19
0
 def openssl_to_openssh(self, input_file, output_file):
     pubkey = fileutil.read_file(input_file)
     try:
         cryptutil = CryptUtil(conf.get_openssl_cmd())
         ssh_rsa_pubkey = cryptutil.asn1_to_ssh(pubkey)
     except CryptError as e:
         raise OSUtilError(ustr(e))
     fileutil.write_file(output_file, ssh_rsa_pubkey)
Example #20
0
    def download(self):
        self.logger.verbose("Download extension package")
        self.set_operation(WALAEventOperation.Download)
        if self.pkg is None:
            raise ExtensionError("No package uri found")

        package = None
        for uri in self.pkg.uris:
            try:
                package = self.protocol.download_ext_handler_pkg(uri.uri)
                if package is not None:
                    break
            except Exception as e:
                logger.warn("Error while downloading extension: {0}", e)

        if package is None:
            raise ExtensionError("Failed to download extension")

        self.logger.verbose("Unpack extension package")
        pkg_file = os.path.join(conf.get_lib_dir(),
                                os.path.basename(uri.uri) + ".zip")
        try:
            fileutil.write_file(pkg_file, bytearray(package), asbin=True)
            zipfile.ZipFile(pkg_file).extractall(self.get_base_dir())
        except IOError as e:
            raise ExtensionError(u"Failed to write and unzip plugin", e)

        #Add user execute permission to all files under the base dir
        for file in fileutil.get_all_files(self.get_base_dir()):
            fileutil.chmod(file, os.stat(file).st_mode | stat.S_IXUSR)

        self.report_event(message="Download succeeded")

        self.logger.info("Initialize extension directory")
        #Save HandlerManifest.json
        man_file = fileutil.search_file(self.get_base_dir(),
                                        'HandlerManifest.json')

        if man_file is None:
            raise ExtensionError("HandlerManifest.json not found")

        try:
            man = fileutil.read_file(man_file, remove_bom=True)
            fileutil.write_file(self.get_manifest_file(), man)
        except IOError as e:
            raise ExtensionError(u"Failed to save HandlerManifest.json", e)

        #Create status and config dir
        try:
            status_dir = self.get_status_dir()
            fileutil.mkdir(status_dir, mode=0o700)
            conf_dir = self.get_conf_dir()
            fileutil.mkdir(conf_dir, mode=0o700)
        except IOError as e:
            raise ExtensionError(u"Failed to create status or config dir", e)

        #Save HandlerEnvironment.json
        self.create_handler_env()
Example #21
0
    def download(self):
        self.logger.info("Download extension package")
        self.set_operation(WALAEventOperation.Download)
        if self.pkg is None:
            raise ExtensionError("No package uri found")
        
        package = None
        for uri in self.pkg.uris:
            try:
                package = self.protocol.download_ext_handler_pkg(uri.uri)
                if package is not None:
                    break
            except Exception as e:
                logger.warn("Error while downloading extension: {0}", e)
        
        if package is None:
            raise ExtensionError("Failed to download extension")

        self.logger.info("Unpack extension package")
        pkg_file = os.path.join(conf.get_lib_dir(),
                                os.path.basename(uri.uri) + ".zip")
        try:
            fileutil.write_file(pkg_file, bytearray(package), asbin=True)
            zipfile.ZipFile(pkg_file).extractall(self.get_base_dir())
        except IOError as e:
            raise ExtensionError(u"Failed to write and unzip plugin", e)

        #Add user execute permission to all files under the base dir
        for file in fileutil.get_all_files(self.get_base_dir()):
            fileutil.chmod(file, os.stat(file).st_mode | stat.S_IXUSR)

        self.report_event(message="Download succeeded")

        self.logger.info("Initialize extension directory")
        #Save HandlerManifest.json
        man_file = fileutil.search_file(self.get_base_dir(),
                                        'HandlerManifest.json')

        if man_file is None:
            raise ExtensionError("HandlerManifest.json not found")
        
        try:
            man = fileutil.read_file(man_file, remove_bom=True)
            fileutil.write_file(self.get_manifest_file(), man)
        except IOError as e:
            raise ExtensionError(u"Failed to save HandlerManifest.json", e)

        #Create status and config dir
        try:
            status_dir = self.get_status_dir()
            fileutil.mkdir(status_dir, mode=0o700)
            conf_dir = self.get_conf_dir()
            fileutil.mkdir(conf_dir, mode=0o700)
        except IOError as e:
            raise ExtensionError(u"Failed to create status or config dir", e)

        #Save HandlerEnvironment.json
        self.create_handler_env()
Example #22
0
    def test_create_from_vm_settings_should_assume_block_when_blob_type_is_not_valid(self):
        vm_settings_text = fileutil.read_file(os.path.join(data_dir, "hostgaplugin/vm_settings.json"))
        vm_settings_text =  re.sub(r'"statusBlobType".*:.*"BlockBlob"', '"statusBlobType": "INVALID_BLOB_TYPE"', vm_settings_text)
        if "INVALID_BLOB_TYPE" not in vm_settings_text:
            raise Exception("Failed to inject an invalid blob type in the test data")
        vm_settings = ExtensionsGoalState.create_from_vm_settings("123", vm_settings_text)

        actual = vm_settings.get_status_upload_blob_type()
        self.assertEqual("BlockBlob", actual, 'Expected BlockBob for an invalid statusBlobType')
Example #23
0
    def test_create_from_extensions_config_should_assume_block_when_blob_type_is_not_valid(self):
        extensions_config_text = fileutil.read_file(os.path.join(data_dir, "hostgaplugin/ext_conf.xml"))
        extensions_config_text = re.sub(r'statusBlobType.*=.*"BlockBlob"', 'statusBlobType="INVALID_BLOB_TYPE"', extensions_config_text)
        if "INVALID_BLOB_TYPE" not in extensions_config_text:
            raise Exception("Failed to inject an invalid blob type in the test data")
        extensions_config = ExtensionsGoalState.create_from_extensions_config("123", extensions_config_text)

        actual = extensions_config.get_status_upload_blob_type()
        self.assertEqual("BlockBlob", actual, 'Expected BlockBob for an invalid statusBlobType')
Example #24
0
    def _is_orphaned(self):
        parent_pid = os.getppid()
        if parent_pid in (1, None):
            return True

        if not os.path.isfile(conf.get_agent_pid_file_path()):
            return True

        return fileutil.read_file(conf.get_agent_pid_file_path()) != ustr(parent_pid)
Example #25
0
 def set_block_device_timeout(self, dev, timeout):
     if dev is not None and timeout is not None:
         file_path = "/sys/block/{0}/device/timeout".format(dev)
         content = fileutil.read_file(file_path)
         original = content.splitlines()[0].rstrip()
         if original != timeout:
             fileutil.write_file(file_path, timeout)
             logger.info("Set block dev timeout: {0} with timeout: {1}",
                         dev, timeout)
Example #26
0
 def set_block_device_timeout(self, dev, timeout):
     if dev is not None and timeout is not None:
         file_path = "/sys/block/{0}/device/timeout".format(dev)
         content = fileutil.read_file(file_path)
         original = content.splitlines()[0].rstrip()
         if original != timeout:
             fileutil.write_file(file_path, timeout)
             logger.info("Set block dev timeout: {0} with timeout: {1}",
                         dev, timeout)
Example #27
0
    def _is_orphaned(self):
        parent_pid = os.getppid()
        if parent_pid in (1, None):
            return True

        if not os.path.isfile(conf.get_agent_pid_file_path()):
            return True

        return fileutil.read_file(conf.get_agent_pid_file_path()) != ustr(parent_pid)
Example #28
0
 def test_write_pid_file(self):
     for n in range(1112):
         fileutil.write_file(os.path.join(self.tmp_dir, str(n)+"_waagent.pid"), ustr(n+1))
     with patch('os.getpid', return_value=1112):
         previous_pid_file, pid_file = self.update_handler._write_pid_file()
         self.assertEqual("1111_waagent.pid", os.path.basename(previous_pid_file))
         self.assertEqual("1112_waagent.pid", os.path.basename(pid_file))
         self.assertEqual(fileutil.read_file(pid_file), ustr(1112))
     return
Example #29
0
def is_snappy():
    """
    Add this workaround for detecting Snappy Ubuntu Core temporarily,
    until ubuntu fixed this bug: https://bugs.launchpad.net/snappy/+bug/1481086
    """
    if os.path.exists("/etc/motd"):
        motd = fileutil.read_file("/etc/motd")
        if "snappy" in motd:
            return True
    return False
Example #30
0
    def load_manifest(self):
        man_file = self.get_manifest_file()
        try:
            data = json.loads(fileutil.read_file(man_file))
        except (IOError, OSError) as e:
            raise ExtensionError('Failed to load manifest file ({0}): {1}'.format(man_file, e.strerror))
        except ValueError:
            raise ExtensionError('Malformed manifest file ({0}).'.format(man_file))

        return HandlerManifest(data[0])
Example #31
0
 def del_root_password(self):
     try:
         passwd_file_path = conf.get_passwd_file_path()
         passwd_content = fileutil.read_file(passwd_file_path)
         passwd = passwd_content.split('\n')
         new_passwd = [x for x in passwd if not x.startswith("root:")]
         new_passwd.insert(0, "root:*LOCK*:14600::::::")
         fileutil.write_file(passwd_file_path, "\n".join(new_passwd))
     except IOError as e:
         raise OSUtilError("Failed to delete root password:{0}".format(e))
Example #32
0
 def get_ovf_env(self):
     """
     Load saved ovf-env.xml
     """
     ovf_file_path = os.path.join(conf.get_lib_dir(), OVF_FILE_NAME)
     if os.path.isfile(ovf_file_path):
         xml_text = fileutil.read_file(ovf_file_path)
         return OvfEnv(xml_text)
     else:
         raise ProtocolError("ovf-env.xml is missing.")
Example #33
0
 def _load(self):
     self.distributions = {}
     try:
         if self.path is not None and os.path.isfile(self.path):
             j = json.loads(fileutil.read_file(self.path))
             for d in j:
                 self.distributions[d] = SupportedDistribution(j[d])
     except Exception as e:
         logger.warn("Failed JSON parse of {0}: {1}".format(self.path, e))
     return
Example #34
0
 def get_ovf_env(self):
     """
     Load saved ovf-env.xml
     """
     ovf_file_path = os.path.join(conf.get_lib_dir(), OVF_FILE_NAME)
     if os.path.isfile(ovf_file_path):
         xml_text = fileutil.read_file(ovf_file_path)
         return OvfEnv(xml_text)
     else:
         raise ProtocolError("ovf-env.xml is missing from {0}".format(ovf_file_path))
Example #35
0
 def del_root_password(self):
     try:
         passwd_file_path = conf.get_passwd_file_path()
         passwd_content = fileutil.read_file(passwd_file_path)
         passwd = passwd_content.split('\n')
         new_passwd = [x for x in passwd if not x.startswith("root:")]
         new_passwd.insert(0, "root:*LOCK*:14600::::::")
         fileutil.write_file(passwd_file_path, "\n".join(new_passwd))
     except IOError as e:
         raise OSUtilError("Failed to delete root password:{0}".format(e))
Example #36
0
 def _load(self):
     self.distributions = {}
     try:
         if self.path is not None and os.path.isfile(self.path):
             j = json.loads(fileutil.read_file(self.path))
             for d in j:
                 self.distributions[d] = SupportedDistribution(j[d])
     except Exception as e:
         logger.warn("Failed JSON parse of {0}: {1}".format(self.path, e))
     return
Example #37
0
    def load_manifest(self):
        man_file = self.get_manifest_file()
        try:
            data = json.loads(fileutil.read_file(man_file))
        except IOError as e:
            raise ExtensionError('Failed to load manifest file.')
        except ValueError as e:
            raise ExtensionError('Malformed manifest file.')

        return HandlerManifest(data[0])
Example #38
0
def is_snappy():
    """
    Add this workaround for detecting Snappy Ubuntu Core temporarily,
    until ubuntu fixed this bug: https://bugs.launchpad.net/snappy/+bug/1481086
    """
    if os.path.exists("/etc/motd"):
        motd = fileutil.read_file("/etc/motd")
        if "snappy" in motd:
            return True
    return False
Example #39
0
    def load_manifest(self):
        man_file = self.get_manifest_file()
        try:
            data = json.loads(fileutil.read_file(man_file))
        except IOError as e:
            raise ExtensionError('Failed to load manifest file.')
        except ValueError as e:
            raise ExtensionError('Malformed manifest file.')

        return HandlerManifest(data[0])
Example #40
0
    def load_manifest(self):
        man_file = self.get_manifest_file()
        try:
            data = json.loads(fileutil.read_file(man_file))
        except (IOError, OSError) as e:
            raise ExtensionError('Failed to load manifest file ({0}): {1}'.format(man_file, e.strerror), code=1002)
        except ValueError:
            raise ExtensionError('Malformed manifest file ({0}).'.format(man_file), code=1003)

        return HandlerManifest(data[0])
Example #41
0
    def get_handler_state(self):
        state_dir = self.get_conf_dir()
        state_file = os.path.join(state_dir, "HandlerState")
        if not os.path.isfile(state_file):
            return ExtHandlerState.NotInstalled

        try:
            return fileutil.read_file(state_file)
        except IOError as e:
            self.logger.error("Failed to get state: {0}", e)
            return ExtHandlerState.NotInstalled
Example #42
0
 def get_ovf_env(self):
     """
     Load saved ovf-env.xml
     """
     ovf_file_path = os.path.join(conf.get_lib_dir(), OVF_FILE_NAME)
     if os.path.isfile(ovf_file_path): # pylint: disable=R1705
         xml_text = fileutil.read_file(ovf_file_path)
         return OvfEnv(xml_text)
     else:
         raise ProtocolError(
             "ovf-env.xml is missing from {0}".format(ovf_file_path))
Example #43
0
    def download(self):
        self.logger.info("Download extension package")
        self.set_operation(WALAEventOperation.Download)
        if self.pkg is None:
            raise ExtensionError("No package uri found")
        
        package = None
        for uri in self.pkg.uris:
            try:
                package = self.protocol.download_ext_handler_pkg(uri.uri)
            except ProtocolError as e: 
                logger.warn("Failed download extension: {0}", e)
        
        if package is None:
            raise ExtensionError("Failed to download extension")

        self.logger.info("Unpack extension package")
        pkg_file = os.path.join(conf.get_lib_dir(),
                                os.path.basename(uri.uri) + ".zip")
        try:
            fileutil.write_file(pkg_file, bytearray(package), asbin=True)
            zipfile.ZipFile(pkg_file).extractall(self.get_base_dir())
        except IOError as e:
            raise ExtensionError(u"Failed to write and unzip plugin", e)

        chmod = "find {0} -type f | xargs chmod u+x".format(self.get_base_dir())
        shellutil.run(chmod)
        self.report_event(message="Download succeeded")

        self.logger.info("Initialize extension directory")
        #Save HandlerManifest.json
        man_file = fileutil.search_file(self.get_base_dir(),
                                        'HandlerManifest.json')

        if man_file is None:
            raise ExtensionError("HandlerManifest.json not found")
        
        try:
            man = fileutil.read_file(man_file, remove_bom=True)
            fileutil.write_file(self.get_manifest_file(), man)
        except IOError as e:
            raise ExtensionError(u"Failed to save HandlerManifest.json", e)

        #Create status and config dir
        try:
            status_dir = self.get_status_dir()
            fileutil.mkdir(status_dir, mode=0o700)
            conf_dir = self.get_conf_dir()
            fileutil.mkdir(conf_dir, mode=0o700)
        except IOError as e:
            raise ExtensionError(u"Failed to create status or config dir", e)

        #Save HandlerEnvironment.json
        self.create_handler_env()
Example #44
0
    def test_write_agent_disabled(self):
        """
        Test writing disable_agent is empty
        """
        from azurelinuxagent.pa.provision.default import ProvisionHandler

        disable_file_path = conf.get_disable_agent_file_path(self.conf)
        self.assertFalse(os.path.exists(disable_file_path))
        ProvisionHandler.write_agent_disabled()
        self.assertTrue(os.path.exists(disable_file_path))
        self.assertEqual('', fileutil.read_file(disable_file_path))
Example #45
0
 def conf_sshd(self, disable_password):
     option = "no" if disable_password else "yes"
     conf_file_path = conf.get_sshd_conf_file_path()
     conf_file = fileutil.read_file(conf_file_path).split("\n")
     textutil.set_ssh_config(conf_file, "PasswordAuthentication", option)
     textutil.set_ssh_config(conf_file, "ChallengeResponseAuthentication", option)
     textutil.set_ssh_config(conf_file, "ClientAliveInterval", str(conf.get_ssh_client_alive_interval()))
     fileutil.write_file(conf_file_path, "\n".join(conf_file))
     logger.info("{0} SSH password-based authentication methods."
                 .format("Disabled" if disable_password else "Enabled"))
     logger.info("Configured SSH client probing to keep connections alive.")
Example #46
0
    def _get_file_contents(self, file_name):
        """
        Retrieve the contents to file.

        :param str file_name: Name of file within that metric controller
        :return: Entire contents of the file
        :rtype: str
        """
        parameter_file = self._get_cgroup_file(file_name)

        return fileutil.read_file(parameter_file)
Example #47
0
 def conf_sshd(self, disable_password):
     option = "no" if disable_password else "yes"
     conf_file_path = conf.get_sshd_conf_file_path()
     conf_file = fileutil.read_file(conf_file_path).split("\n")
     textutil.set_ssh_config(conf_file, "PasswordAuthentication", option)
     textutil.set_ssh_config(conf_file, "ChallengeResponseAuthentication", option)
     textutil.set_ssh_config(conf_file, "ClientAliveInterval", "180")
     fileutil.write_file(conf_file_path, "\n".join(conf_file))
     logger.info("{0} SSH password-based authentication methods."
                 .format("Disabled" if disable_password else "Enabled"))
     logger.info("Configured SSH client probing to keep connections alive.")
Example #48
0
    def get_handler_state(self):
        state_dir = self.get_conf_dir()
        state_file = os.path.join(state_dir, "HandlerState")
        if not os.path.isfile(state_file):
            return ExtHandlerState.NotInstalled

        try:
            return fileutil.read_file(state_file)
        except IOError as e:
            self.logger.error("Failed to get state: {0}", e)
            return ExtHandlerState.NotInstalled
Example #49
0
    def download(self):
        self.logger.info("Download extension package")
        self.set_operation(WALAEventOperation.Download)
        if self.pkg is None:
            raise ExtensionError("No package uri found")
        
        package = None
        for uri in self.pkg.uris:
            try:
                package = self.protocol.download_ext_handler_pkg(uri.uri)
            except ProtocolError as e: 
                logger.warn("Failed download extension: {0}", e)
        
        if package is None:
            raise ExtensionError("Failed to download extension")

        self.logger.info("Unpack extension package")
        pkg_file = os.path.join(conf.get_lib_dir(),
                                os.path.basename(uri.uri) + ".zip")
        try:
            fileutil.write_file(pkg_file, bytearray(package), asbin=True)
            zipfile.ZipFile(pkg_file).extractall(self.get_base_dir())
        except IOError as e:
            raise ExtensionError(u"Failed to write and unzip plugin", e)

        chmod = "find {0} -type f | xargs chmod u+x".format(self.get_base_dir())
        shellutil.run(chmod)
        self.report_event(message="Download succeeded")

        self.logger.info("Initialize extension directory")
        #Save HandlerManifest.json
        man_file = fileutil.search_file(self.get_base_dir(),
                                        'HandlerManifest.json')

        if man_file is None:
            raise ExtensionError("HandlerManifest.json not found")
        
        try:
            man = fileutil.read_file(man_file, remove_bom=True)
            fileutil.write_file(self.get_manifest_file(), man)
        except IOError as e:
            raise ExtensionError(u"Failed to save HandlerManifest.json", e)

        #Create status and config dir
        try:
            status_dir = self.get_status_dir()
            fileutil.mkdir(status_dir, mode=0o700)
            conf_dir = self.get_conf_dir()
            fileutil.mkdir(conf_dir, mode=0o700)
        except IOError as e:
            raise ExtensionError(u"Failed to create status or config dir", e)

        #Save HandlerEnvironment.json
        self.create_handler_env()
Example #50
0
    def check_pid(self):
        """Check whether daemon is already running"""
        pid = None
        pid_file = conf.get_agent_pid_file_path()
        if os.path.isfile(pid_file):
            pid = fileutil.read_file(pid_file)

        if self.osutil.check_pid_alive(pid):
            logger.info("Daemon is already running: {0}", pid)
            sys.exit(0)

        fileutil.write_file(pid_file, ustr(os.getpid()))
Example #51
0
    def _is_clean_start(self):
        if not os.path.isfile(self._sentinal_file_path()):
            return True

        try:
            if fileutil.read_file(self._sentinal_file_path()) != CURRENT_AGENT:
                return True
        except Exception as e:
            logger.warn(u"Exception reading sentinal file {0}: {1}",
                        self._sentinal_file_path(), str(e))

        return False
Example #52
0
 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
Example #53
0
    def check_pid(self):
        """Check whether daemon is already running"""
        pid = None
        pid_file = conf.get_agent_pid_file_path()
        if os.path.isfile(pid_file):
            pid = fileutil.read_file(pid_file)

        if self.osutil.check_pid_alive(pid):
            logger.info("Daemon is already running: {0}", pid)
            sys.exit(0)

        fileutil.write_file(pid_file, ustr(os.getpid()))
Example #54
0
 def _get_controller_id(controller):
     """
     Get the ID for a given cgroup controller
     """
     cgroup_states = fileutil.read_file("/proc/cgroups")
     for entry in cgroup_states.splitlines():
         fields = entry.split('\t')
         if fields[0] == controller:
             return fields[1]
     raise CGroupsException(
         "Cgroup controller {0} not found in /proc/cgroups".format(
             controller))
Example #55
0
    def copy_ovf_env(self):
        """
        Copy ovf env file from dvd to hard disk.
        Remove password before save it to the disk
        """
        dvd_mount_point = conf.get_dvd_mount_point()
        ovf_file_path_on_dvd = os.path.join(dvd_mount_point, OVF_FILE_NAME)
        tag_file_path_on_dvd = os.path.join(dvd_mount_point, TAG_FILE_NAME)
        ovf_file_path = os.path.join(conf.get_lib_dir(), OVF_FILE_NAME)
        tag_file_path = os.path.join(conf.get_lib_dir(), TAG_FILE_NAME)

        try:
            self.osutil.mount_dvd()
        except OSUtilError as e:
            raise ProtocolError("[CopyOvfEnv] Error mounting dvd: "
                                "{0}".format(ustr(e)))

        try:
            ovfxml = fileutil.read_file(ovf_file_path_on_dvd, remove_bom=True)
            ovfenv = OvfEnv(ovfxml)
        except IOError as e:
            raise ProtocolError("[CopyOvfEnv] Error reading file "
                                "{0}: {1}".format(ovf_file_path_on_dvd,
                                                  ustr(e)))

        try:
            ovfxml = re.sub(PASSWORD_PATTERN,
                            PASSWORD_REPLACEMENT,
                            ovfxml)
            fileutil.write_file(ovf_file_path, ovfxml)
        except IOError as e:
            raise ProtocolError("[CopyOvfEnv] Error writing file "
                                "{0}: {1}".format(ovf_file_path,
                                                  ustr(e)))

        try:
            if os.path.isfile(tag_file_path_on_dvd):
                logger.info("Found {0} in provisioning ISO", TAG_FILE_NAME)
                shutil.copyfile(tag_file_path_on_dvd, tag_file_path)
        except IOError as e:
            raise ProtocolError("[CopyOvfEnv] Error copying file "
                                "{0} to {1}: {2}".format(tag_file_path,
                                                         tag_file_path,
                                                         ustr(e)))

        try:
            self.osutil.umount_dvd()
            self.osutil.eject_dvd()
        except OSUtilError as e:
            logger.warn(ustr(e))

        return ovfenv
Example #56
0
    def get_handler_status(self):
        state_dir = self.get_conf_dir()
        status_file = os.path.join(state_dir, "HandlerStatus")
        if not os.path.isfile(status_file):
            return None

        try:
            data = json.loads(fileutil.read_file(status_file))
            handler_status = ExtHandlerStatus()
            set_properties("ExtHandlerStatus", handler_status, data)
            return handler_status
        except (IOError, ValueError) as e:
            self.logger.error("Failed to get handler status: {0}", e)
Example #57
0
    def get_hierarchy_id(hierarchy):
        """
        Get the cgroups hierarchy ID for a given hierarchy name

        :param hierarchy:
        :return: str
        """
        cgroup_states = fileutil.read_file("/proc/cgroups")
        for entry in cgroup_states.splitlines():
            fields = entry.split('\t')
            if fields[0] == hierarchy:
                return fields[1]
        raise CGroupsException("Cgroup hierarchy {0} not found in /proc/cgroups".format(hierarchy))
Example #58
0
 def assert_cgroup_created(controller):
     cgroup_path = os.path.join(self.cgroups_file_system_root,
                                controller, VM_AGENT_CGROUP_NAME)
     self.assertTrue(
         any(cgroups.path == cgroup_path for cgroups in agent_cgroups))
     self.assertTrue(
         any(cgroups.name == VM_AGENT_CGROUP_NAME
             for cgroups in agent_cgroups))
     self.assertTrue(os.path.exists(cgroup_path))
     cgroup_task = int(
         fileutil.read_file(os.path.join(cgroup_path, "cgroup.procs")))
     current_process = os.getpid()
     self.assertEqual(cgroup_task, current_process)
Example #59
0
def load_conf_from_file(conf_file_path, conf=__conf__):
    """
    Load conf file from: conf_file_path
    """
    if os.path.isfile(conf_file_path) == False:
        raise AgentConfigError(("Missing configuration in {0}"
                                "").format(conf_file_path))
    try:
        content = fileutil.read_file(conf_file_path)
        conf.load(content)
    except IOError as err:
        raise AgentConfigError(("Failed to load conf file:{0}, {1}"
                                "").format(conf_file_path, err))
Example #60
0
    def _foreach_legacy_cgroup(operation):
        """
        Previous versions of the daemon (2.2.31-2.2.40) wrote their PID to /sys/fs/cgroup/{cpu,memory}/WALinuxAgent/WALinuxAgent;
        starting from version 2.2.41 we track the agent service in walinuxagent.service instead of WALinuxAgent/WALinuxAgent. Also,
        when running under systemd, the PIDs should not be explicitly moved to the cgroup filesystem. The older daemons would
        incorrectly do that under certain conditions.

        This method checks for the existence of the legacy cgroups and, if the daemon's PID has been added to them, executes the
        given operation on the cgroups. After this check, the method attempts to remove the legacy cgroups.

        :param operation:
            The function to execute on each legacy cgroup. It must take 2 arguments: the controller and the daemon's PID
        """
        legacy_cgroups = []
        for controller in ['cpu', 'memory']:
            cgroup = os.path.join(CGROUPS_FILE_SYSTEM_ROOT, controller,
                                  "WALinuxAgent", "WALinuxAgent")
            if os.path.exists(cgroup):
                logger.info('Found legacy cgroup {0}', cgroup)
                legacy_cgroups.append((controller, cgroup))

        try:
            for controller, cgroup in legacy_cgroups:
                procs_file = os.path.join(cgroup, "cgroup.procs")

                if os.path.exists(procs_file):
                    procs_file_contents = fileutil.read_file(
                        procs_file).strip()
                    daemon_pid = fileutil.read_file(
                        get_agent_pid_file_path()).strip()

                    if daemon_pid in procs_file_contents:
                        operation(controller, daemon_pid)
        finally:
            for _, cgroup in legacy_cgroups:
                logger.info('Removing {0}', cgroup)
                shutil.rmtree(cgroup, ignore_errors=True)
        return len(legacy_cgroups)