def get_distribution(self):
        """
        Get OS info (Distro, version and code name)

        :return: Results tuple(distname, version, id}
            example:
            Distribution(
              distname='Red Hat Enterprise Linux Server',
              id='Maipo',
              version'='7.1'
            )
        :rtype: namedtuple Distribution
        """
        values = ["distname", "version", "id"]
        cmd = [
            "python", "-c",
            "import platform;print(','.join(platform.linux_distribution()))"
        ]
        executor = self.host.executor()
        rc, out, err = executor.run_cmd(cmd)
        if rc:
            raise errors.CommandExecutionFailure(
                executor, cmd, rc,
                "Failed to obtain release info: {0}".format(err))
        Distribution = namedtuple('Distribution', values)
        return Distribution(*[i.strip() for i in out.split(",")])
Esempio n. 2
0
    def wget(self, url, output_file, progress_handler=None):
        """
        Download file on the host from given url

        :param url: url to file
        :type url: str
        :param output_file: full path to output file
        :type output_file: str
        :param progress_handler: progress handler function
        :type progress_handler: func
        :return: absolute path to file
        :rtype: str
        """
        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
    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
        )
Esempio n. 4
0
 def _exec_command(self, cmd):
     host_executor = self.host.executor()
     rc, _, err = host_executor.run_cmd(cmd)
     if rc:
         raise errors.CommandExecutionFailure(cmd=cmd,
                                              executor=host_executor,
                                              rc=rc,
                                              err=err)
 def get_release_str(self):
     cmd = ['cat', '/etc/system-release']
     executor = self.host.executor()
     rc, out, err = executor.run_cmd(cmd)
     if rc:
         raise errors.CommandExecutionFailure(
             executor, cmd, rc,
             "Failed to obtain release string: {0}".format(err))
     return out.strip()
Esempio n. 6
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
Esempio n. 7
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