Ejemplo n.º 1
0
    def list_(self):
        """
        List installed packages on host

        Returns:
            list: Installed packages

        Raises:
            NotImplementedError, CommandExecutionFailure
        """
        if not self.list_command_d:
            raise NotImplementedError(
                "There is no 'list_' command defined."
            )
        cmd = self.list_command_d
        self.logger.debug(
            "Getting all instaled packages from host %s", self.host
        )
        rc, out, err = self.host.executor().run_cmd(cmd)
        if not rc:
            return out.split('\n')
        self.logger.error(
            "Failed to get installed packages on host %s, rc: %s, err: %s",
            self.host, rc, err
        )
        raise errors.CommandExecutionFailure(
            cmd=cmd, executor=self.host.executor, rc=rc, err=err
        )
Ejemplo n.º 2
0
    def wget(self, url, output_file, progress_handler=None):
        """
        Download file on the host from given url

        Args:
            url (str): url to file
            output_file (str): full path to output file
            progress_handler (func): progress handler function

        Returns:
            str: absolute path to file
        """
        rc = None
        host_executor = self.host.executor()
        cmd = ["wget", "-O", output_file, "--no-check-certificate", url]
        with host_executor.session() as host_session:
            wget_command = host_session.command(cmd)
            with wget_command.execute() as (_, _, stderr):
                counter = 0
                while rc is None:
                    line = stderr.readline()
                    if counter == 1000 and progress_handler:
                        progress_handler(line)
                        counter = 0
                    counter += 1
                    rc = wget_command.get_rc()
        if rc:
            raise errors.CommandExecutionFailure(
                host_executor, cmd, rc,
                "Failed to download file from url {0}".format(url))
        return output_file
Ejemplo n.º 3
0
 def _exec_command(self, cmd):
     host_executor = self.host.executor()
     rc, out, err = host_executor.run_cmd(cmd)
     if rc:
         raise errors.CommandExecutionFailure(cmd=cmd,
                                              executor=host_executor,
                                              rc=rc,
                                              err=err)
     return out
Ejemplo n.º 4
0
 def _exec_command(self, cmd, err_msg=None):
     host_executor = self.host.executor()
     rc, out, err = host_executor.run_cmd(cmd)
     if err_msg:
         err = "{err_msg}: {err}".format(err_msg=err_msg, err=err)
     if rc:
         raise errors.CommandExecutionFailure(
             executor=host_executor, cmd=cmd, rc=rc, err=err
         )
     return out
Ejemplo n.º 5
0
    def get_release_info(self):
        """
        /etc/os-release became to be standard on systemd based operating
        systems.

        It might raise exception in case the systemd is not deployed on system.

        Raises:
            UnsupportedOperation
        """
        os_release_file = '/etc/os-release'
        cmd = ['cat', os_release_file]
        executor = self.host.executor()
        rc, out, err = executor.run_cmd(cmd)
        if rc:
            try:
                if not self.host.fs.exists(os_release_file):
                    raise errors.UnsupportedOperation(
                        self.host, "OperatingSystem.release_info",
                        "Requires 'systemd' based operating system.",
                    )
            except errors.UnsupportedOperation:
                raise
            except Exception:
                pass
            raise errors.CommandExecutionFailure(
                executor, cmd, rc,
                "Failed to obtain release info, system doesn't follow "
                "systemd standards: {0}".format(err)
            )
        release_info = dict()
        for line in out.strip().splitlines():
            values = line.split("=", 1)
            if len(values) != 2:
                continue
            release_info[values[0].strip()] = values[1].strip(" \"'")
        return release_info