Example #1
0
    def get_file(self,
                 source,
                 dest,
                 delete_dest=False,
                 preserve_perm=True,
                 preserve_symlinks=False):
        """
        Copy files from the remote host to a local path.

        Directories will be copied recursively.
        If a source component is a directory with a trailing slash,
        the content of the directory will be copied, otherwise, the
        directory itself and its content will be copied. This
        behavior is similar to that of the program 'rsync'.

        Args:
                source: either
                        1) a single file or directory, as a string
                        2) a list of one or more (possibly mixed)
                                files or directories
                dest: a file or a directory (if source contains a
                        directory or more than one element, you must
                        supply a directory dest)
                delete_dest: if this is true, the command will also clear
                             out any old files at dest that are not in the
                             source
                preserve_perm: tells get_file() to try to preserve the sources
                               permissions on files and dirs
                preserve_symlinks: try to preserve symlinks instead of
                                   transforming them into files/dirs on copy

        Raises:
                AutoservRunError: the scp command failed
        """

        # Start a master SSH connection if necessary.
        self.start_master_ssh()

        if isinstance(source, basestring):
            source = [source]
        dest = os.path.abspath(dest)

        # If rsync is disabled or fails, try scp.
        try_scp = True
        if self.use_rsync():
            try:
                remote_source = self._encode_remote_paths(source)
                local_dest = utils.sh_escape(dest)
                rsync = self._make_rsync_cmd([remote_source], local_dest,
                                             delete_dest, preserve_symlinks)
                utils.run(rsync)
                try_scp = False
            except error.CmdError, e:
                logging.warn("trying scp, rsync failed: %s" % e)
Example #2
0
    def send_file(self,
                  source,
                  dest,
                  delete_dest=False,
                  preserve_symlinks=False):
        """
        Copy files from a local path to the remote host.

        Directories will be copied recursively.
        If a source component is a directory with a trailing slash,
        the content of the directory will be copied, otherwise, the
        directory itself and its content will be copied. This
        behavior is similar to that of the program 'rsync'.

        Args:
                source: either
                        1) a single file or directory, as a string
                        2) a list of one or more (possibly mixed)
                                files or directories
                dest: a file or a directory (if source contains a
                        directory or more than one element, you must
                        supply a directory dest)
                delete_dest: if this is true, the command will also clear
                             out any old files at dest that are not in the
                             source
                preserve_symlinks: controls if symlinks on the source will be
                    copied as such on the destination or transformed into the
                    referenced file/directory

        Raises:
                AutoservRunError: the scp command failed
        """

        # Start a master SSH connection if necessary.
        self.start_master_ssh()

        if isinstance(source, basestring):
            source_is_dir = os.path.isdir(source)
            source = [source]
        remote_dest = self._encode_remote_paths([dest])

        # If rsync is disabled or fails, try scp.
        try_scp = True
        if self.use_rsync():
            try:
                local_sources = [utils.sh_escape(path) for path in source]
                rsync = self._make_rsync_cmd(local_sources, remote_dest,
                                             delete_dest, preserve_symlinks)
                utils.run(rsync)
                try_scp = False
            except error.CmdError, e:
                logging.warn("trying scp, rsync failed: %s" % e)
Example #3
0
 def repair_prepare(self):
     try:
         if self.vm_host is None:
             ret = utils.run('type virt-copy-in', ignore_status=True)
             if ret.exit_status is not 0:
                 utils.run('yum install libguestfs-tools-c -y')
         else:
             ret = self.vm_host.run('type virt-copy-in', ignore_status=True)
             if ret.exit_status is not 0:
                 self.vm_host.run('yum install libguestfs-tools-c -y')
     except Exception, e:
         logging.error("Failed to prepare:\n %s \n %s" %
                       (str(e), traceback.format_exc()))
Example #4
0
 def repair_prepare(self):
     try:
         if self.vm_host is None:
             ret = utils.run('type virt-copy-in', ignore_status=True)
             if ret.exit_status is not 0:
                 utils.run('yum install libguestfs-tools-c -y')
         else:
             ret = self.vm_host.run('type virt-copy-in', ignore_status=True)
             if ret.exit_status is not 0:
                 self.vm_host.run('yum install libguestfs-tools-c -y')
     except Exception, e:
         logging.error("Failed to prepare:\n %s \n %s" %
                       (str(e), traceback.format_exc()))
    def get_file(self, source, dest, delete_dest=False, preserve_perm=True,
                 preserve_symlinks=False):
        """
        Copy files from the remote host to a local path.

        Directories will be copied recursively.
        If a source component is a directory with a trailing slash,
        the content of the directory will be copied, otherwise, the
        directory itself and its content will be copied. This
        behavior is similar to that of the program 'rsync'.

        Args:
                source: either
                        1) a single file or directory, as a string
                        2) a list of one or more (possibly mixed)
                                files or directories
                dest: a file or a directory (if source contains a
                        directory or more than one element, you must
                        supply a directory dest)
                delete_dest: if this is true, the command will also clear
                             out any old files at dest that are not in the
                             source
                preserve_perm: tells get_file() to try to preserve the sources
                               permissions on files and dirs
                preserve_symlinks: try to preserve symlinks instead of
                                   transforming them into files/dirs on copy

        Raises:
                AutoservRunError: the scp command failed
        """

        # Start a master SSH connection if necessary.
        self.start_master_ssh()

        if isinstance(source, basestring):
            source = [source]
        dest = os.path.abspath(dest)

        # If rsync is disabled or fails, try scp.
        try_scp = True
        if self.use_rsync():
            try:
                remote_source = self._encode_remote_paths(source)
                local_dest = utils.sh_escape(dest)
                rsync = self._make_rsync_cmd([remote_source], local_dest,
                                             delete_dest, preserve_symlinks)
                utils.run(rsync)
                try_scp = False
            except error.CmdError, e:
                logging.warn("trying scp, rsync failed: %s" % e)
    def send_file(self, source, dest, delete_dest=False,
                  preserve_symlinks=False):
        """
        Copy files from a local path to the remote host.

        Directories will be copied recursively.
        If a source component is a directory with a trailing slash,
        the content of the directory will be copied, otherwise, the
        directory itself and its content will be copied. This
        behavior is similar to that of the program 'rsync'.

        Args:
                source: either
                        1) a single file or directory, as a string
                        2) a list of one or more (possibly mixed)
                                files or directories
                dest: a file or a directory (if source contains a
                        directory or more than one element, you must
                        supply a directory dest)
                delete_dest: if this is true, the command will also clear
                             out any old files at dest that are not in the
                             source
                preserve_symlinks: controls if symlinks on the source will be
                    copied as such on the destination or transformed into the
                    referenced file/directory

        Raises:
                AutoservRunError: the scp command failed
        """

        # Start a master SSH connection if necessary.
        self.start_master_ssh()

        if isinstance(source, basestring):
            source_is_dir = os.path.isdir(source)
            source = [source]
        remote_dest = self._encode_remote_paths([dest])

        # If rsync is disabled or fails, try scp.
        try_scp = True
        if self.use_rsync():
            try:
                local_sources = [utils.sh_escape(path) for path in source]
                rsync = self._make_rsync_cmd(local_sources, remote_dest,
                                             delete_dest, preserve_symlinks)
                utils.run(rsync)
                try_scp = False
            except error.CmdError, e:
                logging.warn("trying scp, rsync failed: %s" % e)
Example #7
0
    def _run(self, command, timeout, ignore_status, stdout, stderr,
             connect_timeout, env, options, stdin, args):
        """Helper function for run()."""
        ssh_cmd = self.ssh_command(connect_timeout, options)
        if not env.strip():
            env = ""
        else:
            env = "export %s;" % env
        for arg in args:
            command += ' "%s"' % utils.sh_escape(arg)
        full_cmd = '%s "%s %s"' % (ssh_cmd, env, utils.sh_escape(command))
        result = utils.run(full_cmd, timeout, True, stdout, stderr,
                           verbose=False, stdin=stdin,
                           stderr_is_expected=ignore_status)

        # The error messages will show up in band (indistinguishable
        # from stuff sent through the SSH connection), so we have the
        # remote computer echo the message "Connected." before running
        # any command.  Since the following 2 errors have to do with
        # connecting, it's safe to do these checks.
        if result.exit_status == 255:
            if re.search(r'^ssh: connect to host .* port .*: '
                         r'Connection timed out\r$', result.stderr):
                raise error.AutoservSSHTimeout("ssh timed out", result)
            if "Permission denied." in result.stderr:
                msg = "ssh permission denied"
                raise error.AutoservSshPermissionDeniedError(msg, result)

        if not ignore_status and result.exit_status > 0:
            raise error.AutoservRunError("command execution error", result)

        return result
Example #8
0
 def hard_repair_vm(self, vm_node):
     file_name = "/tmp/grub.conf"
     domain_name = vm_node.hostname
     cmd_copy_out = "virt-copy-out -d %s /boot/grub/grub.conf %s" % (domain_name, "/tmp")
     cmd_copy_in = "virt-copy-in -d %s %s /boot/grub/" % (domain_name, file_name)
     conn = self.qemu_start_conn()
     domain = conn.lookupByName(domain_name)
     if domain.isActive():
         domain.destroy()
     conn.close()
     try:
         if self.vm_host is None:
             utils.run(cmd_copy_out)
         else:
             self.vm_host.run(cmd_copy_out)
             self.vm_host.get_file(file_name, file_name)
     except Exception, e:
         logging.error("Failed to copy out grub.cfg of domain [%s].\n %s \n %s" %
                       (domain_name, str(e), traceback.format_exc()))
Example #9
0
    def install(self,
                host,
                label='autotest',
                default=False,
                kernel_args='',
                install_vmlinux=True):
        """
        Install a kernel on the remote host.

        This will also invoke the guest's bootloader to set this
        kernel as the default kernel if default=True.

        Args:
                host: the host on which to install the kernel
                [kwargs]: remaining keyword arguments will be passed
                        to Bootloader.add_kernel()

        Raises:
                AutoservError: no package has yet been obtained. Call
                        RPMKernel.get() with a .rpm package.
        """
        if len(label) > 15:
            raise error.AutoservError("label for kernel is too long \
            (> 15 chars): %s" % label)
        if self.source_material is None:
            raise error.AutoservError("A kernel must first be \
            specified via get()")
        rpm = self.source_material

        remote_tmpdir = host.get_tmp_dir()
        remote_rpm = os.path.join(remote_tmpdir, os.path.basename(rpm))
        rpm_package = utils.run('/usr/bin/rpm -q -p %s' % rpm).stdout
        vmlinuz = self.get_image_name()
        host.send_file(rpm, remote_rpm)
        host.run('rpm -e ' + rpm_package, ignore_status=True)
        host.run('rpm --force -i ' + remote_rpm)

        # Copy over the uncompressed image if there is one
        if install_vmlinux:
            vmlinux = self.get_vmlinux_name()
            host.run('cd /;rpm2cpio %s | cpio -imuv .%s' %
                     (remote_rpm, vmlinux))
            host.run('ls ' + vmlinux)  # Verify

        host.bootloader.remove_kernel(label)
        host.bootloader.add_kernel(vmlinuz,
                                   label,
                                   args=kernel_args,
                                   default=default)
        if kernel_args:
            host.bootloader.add_args(label, kernel_args)
        if not default:
            host.bootloader.boot_once(label)
Example #10
0
 def hard_repair_vm(self, vm_node):
     file_name = "/tmp/grub.conf"
     domain_name = vm_node.hostname
     cmd_copy_out = "virt-copy-out -d %s /boot/grub/grub.conf %s" % (
         domain_name, "/tmp")
     cmd_copy_in = "virt-copy-in -d %s %s /boot/grub/" % (domain_name,
                                                          file_name)
     conn = self.qemu_start_conn()
     domain = conn.lookupByName(domain_name)
     if domain.isActive():
         domain.destroy()
     conn.close()
     try:
         if self.vm_host is None:
             utils.run(cmd_copy_out)
         else:
             self.vm_host.run(cmd_copy_out)
             self.vm_host.get_file(file_name, file_name)
     except Exception, e:
         logging.error(
             "Failed to copy out grub.cfg of domain [%s].\n %s \n %s" %
             (domain_name, str(e), traceback.format_exc()))
Example #11
0
    def install(self, host, label='autotest',
                default=False, kernel_args = '', install_vmlinux=True):
        """
        Install a kernel on the remote host.

        This will also invoke the guest's bootloader to set this
        kernel as the default kernel if default=True.

        Args:
                host: the host on which to install the kernel
                [kwargs]: remaining keyword arguments will be passed
                        to Bootloader.add_kernel()

        Raises:
                AutoservError: no package has yet been obtained. Call
                        RPMKernel.get() with a .rpm package.
        """
        if len(label) > 15:
            raise error.AutoservError("label for kernel is too long \
            (> 15 chars): %s" % label)
        if self.source_material is None:
            raise error.AutoservError("A kernel must first be \
            specified via get()")
        rpm = self.source_material

        remote_tmpdir = host.get_tmp_dir()
        remote_rpm = os.path.join(remote_tmpdir, os.path.basename(rpm))
        rpm_package = utils.run('/usr/bin/rpm -q -p %s' % rpm).stdout
        vmlinuz = self.get_image_name()
        host.send_file(rpm, remote_rpm)
        host.run('rpm -e ' + rpm_package, ignore_status = True)
        host.run('rpm --force -i ' + remote_rpm)

        # Copy over the uncompressed image if there is one
        if install_vmlinux:
            vmlinux = self.get_vmlinux_name()
            host.run('cd /;rpm2cpio %s | cpio -imuv .%s'
                    % (remote_rpm, vmlinux))
            host.run('ls ' + vmlinux) # Verify

        host.bootloader.remove_kernel(label)
        host.bootloader.add_kernel(vmlinuz, label,
                                   args=kernel_args, default=default)
        if kernel_args:
            host.bootloader.add_args(label, kernel_args)
        if not default:
            host.bootloader.boot_once(label)
Example #12
0
    def get_version(self):
        """Get the version of the kernel to be installed.

        Returns:
                The version string, as would be returned
                by 'make kernelrelease'.

        Raises:
                AutoservError: no package has yet been obtained. Call
                        DEBKernel.get() with a .deb package.
        """
        if self.source_material is None:
            raise error.AutoservError("A kernel must first be "
                                      "specified via get()")

        retval = utils.run('dpkg-deb -f "%s" version' %
                           utils.sh_escape(self.source_material),)
        return retval.stdout.strip()
Example #13
0
    def get_version(self):
        """Get the version of the kernel to be installed.

        Returns:
                The version string, as would be returned
                by 'make kernelrelease'.

        Raises:
                AutoservError: no package has yet been obtained. Call
                        DEBKernel.get() with a .deb package.
        """
        if self.source_material is None:
            raise error.AutoservError("A kernel must first be "
                                      "specified via get()")

        retval= utils.run('dpkg-deb -f "%s" version' %
                utils.sh_escape(self.source_material),)
        return retval.stdout.strip()
Example #14
0
    def get_image_name(self):
        """Get the name of the kernel image to be installed.

        Returns:
                The full path to the kernel image file as it will be
                installed on the host.

        Raises:
                AutoservError: no package has yet been obtained. Call
                        RPMKernel.get() with a .rpm package.
        """
        if self.source_material is None:
            raise error.AutoservError("A kernel must first be \
            specified via get()")

        vmlinuz = utils.run('rpm -q -l -p %s | grep /boot/vmlinuz' %
                            self.source_material).stdout.strip()
        return vmlinuz
Example #15
0
    def get_version(self):
        """Get the version of the kernel to be installed.

        Returns:
                The version string, as would be returned
                by 'make kernelrelease'.

        Raises:
                AutoservError: no package has yet been obtained. Call
                        RPMKernel.get() with a .rpm package.
        """
        if self.source_material is None:
            raise error.AutoservError("A kernel must first be \
            specified via get()")

        retval = utils.run('rpm -qpi %s | grep Version | awk \'{print($3);}\''
            % utils.sh_escape(self.source_material))
        return retval.stdout.strip()
Example #16
0
    def get_image_name(self):
        """Get the name of the kernel image to be installed.

        Returns:
                The full path to the kernel image file as it will be
                installed on the host.

        Raises:
                AutoservError: no package has yet been obtained. Call
                        RPMKernel.get() with a .rpm package.
        """
        if self.source_material is None:
            raise error.AutoservError("A kernel must first be \
            specified via get()")

        vmlinuz = utils.run('rpm -q -l -p %s | grep /boot/vmlinuz'
            % self.source_material).stdout.strip()
        return vmlinuz
Example #17
0
    def get_version(self):
        """Get the version of the kernel to be installed.

        Returns:
                The version string, as would be returned
                by 'make kernelrelease'.

        Raises:
                AutoservError: no package has yet been obtained. Call
                        RPMKernel.get() with a .rpm package.
        """
        if self.source_material is None:
            raise error.AutoservError("A kernel must first be \
            specified via get()")

        retval = utils.run(
            'rpm -qpi %s | grep Version | awk \'{print($3);}\'' %
            utils.sh_escape(self.source_material))
        return retval.stdout.strip()
Example #18
0
 def validate_files_on_host(self, host, *files):
     if host:
         if not host.is_up():
             logging.warning("Host %s is not up!\n" % host.hostname)
             return False
         for f in files:
             result = host.run('test -f %s' % f, ignore_status=True)
             if result.exit_status is not 0:
                 logging.error("Not found file:%s on host[%s]." %
                               (f, host.hostname))
                 return False
         return True
     else:
         for f in files:
             result = utils.run('test -f %s' % f, ignore_status=True)
             if result.exit_status is not 0:
                 logging.error("Not found file:%s on host[%s]." %
                               (f, host.hostname))
                 return False
     return True
Example #19
0
 def validate_files_on_host(self, host, *files):
     if host:
         if not host.is_up():
             logging.warning("Host %s is not up!\n" % host.hostname)
             return False
         for f in files:
             result = host.run('test -f %s' % f, ignore_status=True)
             if result.exit_status is not 0:
                 logging.error("Not found file:%s on host[%s]." %
                               (f, host.hostname))
                 return False
         return True
     else:
         for f in files:
             result = utils.run('test -f %s' % f, ignore_status=True)
             if result.exit_status is not 0:
                 logging.error("Not found file:%s on host[%s]." %
                               (f, host.hostname))
                 return False
     return True
Example #20
0
    def get_initrd_name(self):
        """Get the name of the initrd file to be installed.

        Returns:
                The full path to the initrd file as it will be
                installed on the host. If the package includes no
                initrd file, None is returned

        Raises:
                AutoservError: no package has yet been obtained. Call
                        RPMKernel.get() with a .rpm package.
        """
        if self.source_material is None:
            raise error.AutoservError("A kernel must first be \
            specified via get()")

        res = utils.run('rpm -q -l -p %s | grep /boot/initrd'
            % self.source_material, ignore_status=True)
        if res.exit_status:
            return None
        return res.stdout.strip()
Example #21
0
    def get_initrd_name(self):
        """Get the name of the initrd file to be installed.

        Returns:
                The full path to the initrd file as it will be
                installed on the host. If the package includes no
                initrd file, None is returned

        Raises:
                AutoservError: no package has yet been obtained. Call
                        RPMKernel.get() with a .rpm package.
        """
        if self.source_material is None:
            raise error.AutoservError("A kernel must first be \
            specified via get()")

        res = utils.run('rpm -q -l -p %s | grep /boot/initrd' %
                        self.source_material,
                        ignore_status=True)
        if res.exit_status:
            return None
        return res.stdout.strip()
Example #22
0
                if len(source) > 1 or source_is_dir:
                    dest_is_dir = True

                if dest_exists and dest_is_dir:
                    cmd = "rm -rf %s && mkdir %s" % (dest, dest)
                    self.run(cmd)

                elif not dest_exists and dest_is_dir:
                    cmd = "mkdir %s" % dest
                    self.run(cmd)

            local_sources = self._make_rsync_compatible_source(source, True)
            if local_sources:
                scp = self._make_scp_cmd(local_sources, remote_dest)
                try:
                    utils.run(scp)
                except error.CmdError, e:
                    raise error.AutoservRunError(e.args[0], e.args[1])

    def ssh_ping(self, timeout=60):
        try:
            # Complex inheritance confuses pylint here
            # pylint: disable=E1123
            self.run("true", timeout=timeout, connect_timeout=timeout)
        except error.AutoservSSHTimeout:
            msg = "Host (ssh) verify timed out (timeout = %d)" % timeout
            raise error.AutoservSSHTimeout(msg)
        except error.AutoservSshPermissionDeniedError:
            # let AutoservSshPermissionDeniedError be visible to the callers
            raise
        except error.AutoservRunError, e:
Example #23
0
class AbstractSSHHost(SiteHost):
    """
    This class represents a generic implementation of most of the
    framework necessary for controlling a host via ssh. It implements
    almost all of the abstract Host methods, except for the core
    Host.run method.
    """
    def _initialize(self,
                    hostname,
                    user="******",
                    port=22,
                    password="",
                    *args,
                    **dargs):
        super(AbstractSSHHost, self)._initialize(hostname=hostname,
                                                 *args,
                                                 **dargs)
        self.ip = socket.getaddrinfo(self.hostname, None)[0][4][0]
        self.user = user
        self.port = port
        self.password = password
        self._use_rsync = None
        self.known_hosts_file = tempfile.mkstemp()[1]
        """
        Master SSH connection background job, socket temp directory and socket
        control path option. If master-SSH is enabled, these fields will be
        initialized by start_master_ssh when a new SSH connection is initiated.
        """
        self.master_ssh_job = None
        self.master_ssh_tempdir = None
        self.master_ssh_option = ''

    def use_rsync(self):
        if self._use_rsync is not None:
            return self._use_rsync

        # Check if rsync is available on the remote host. If it's not,
        # don't try to use it for any future file transfers.
        self._use_rsync = self._check_rsync()
        if not self._use_rsync:
            logging.warn("rsync not available on remote host %s -- disabled",
                         self.hostname)
        return self._use_rsync

    def _check_rsync(self):
        """
        Check if rsync is available on the remote host.
        """
        try:
            self.run("rsync --version", stdout_tee=None, stderr_tee=None)
        except error.AutoservRunError:
            return False
        return True

    def _encode_remote_paths(self, paths, escape=True):
        """
        Given a list of file paths, encodes it as a single remote path, in
        the style used by rsync and scp.
        """
        if escape:
            paths = [utils.scp_remote_escape(path) for path in paths]
        return '%s@%s:"%s"' % (self.user, self.hostname, " ".join(paths))

    def _make_rsync_cmd(self, sources, dest, delete_dest, preserve_symlinks):
        """
        Given a list of source paths and a destination path, produces the
        appropriate rsync command for copying them. Remote paths must be
        pre-encoded.
        """
        ssh_cmd = make_ssh_command(user=self.user,
                                   port=self.port,
                                   opts=self.master_ssh_option,
                                   hosts_file=self.known_hosts_file)
        if delete_dest:
            delete_flag = "--delete"
        else:
            delete_flag = ""
        if preserve_symlinks:
            symlink_flag = ""
        else:
            symlink_flag = "-L"
        command = "rsync %s %s --timeout=1800 --rsh='%s' -az %s %s"
        return command % (symlink_flag, delete_flag, ssh_cmd,
                          " ".join(sources), dest)

    def _make_ssh_cmd(self, cmd):
        """
        Create a base ssh command string for the host which can be used
        to run commands directly on the machine
        """
        base_cmd = make_ssh_command(user=self.user,
                                    port=self.port,
                                    opts=self.master_ssh_option,
                                    hosts_file=self.known_hosts_file)

        return '%s %s "%s"' % (base_cmd, self.hostname, utils.sh_escape(cmd))

    def _make_scp_cmd(self, sources, dest):
        """
        Given a list of source paths and a destination path, produces the
        appropriate scp command for encoding it. Remote paths must be
        pre-encoded.
        """
        command = ("scp -rq %s -o StrictHostKeyChecking=no "
                   "-o UserKnownHostsFile=%s -P %d %s '%s'")
        return command % (self.master_ssh_option, self.known_hosts_file,
                          self.port, " ".join(sources), dest)

    def _make_rsync_compatible_globs(self, path, is_local):
        """
        Given an rsync-style path, returns a list of globbed paths
        that will hopefully provide equivalent behaviour for scp. Does not
        support the full range of rsync pattern matching behaviour, only that
        exposed in the get/send_file interface (trailing slashes).

        The is_local param is flag indicating if the paths should be
        interpreted as local or remote paths.
        """

        # non-trailing slash paths should just work
        if len(path) == 0 or path[-1] != "/":
            return [path]

        # make a function to test if a pattern matches any files
        if is_local:

            def glob_matches_files(path, pattern):
                return len(glob.glob(path + pattern)) > 0
        else:

            def glob_matches_files(path, pattern):
                result = self.run("ls \"%s\"%s" %
                                  (utils.sh_escape(path), pattern),
                                  stdout_tee=None,
                                  ignore_status=True)
                return result.exit_status == 0

        # take a set of globs that cover all files, and see which are needed
        patterns = ["*", ".[!.]*"]
        patterns = [p for p in patterns if glob_matches_files(path, p)]

        # convert them into a set of paths suitable for the commandline
        if is_local:
            return [
                "\"%s\"%s" % (utils.sh_escape(path), pattern)
                for pattern in patterns
            ]
        else:
            return [
                utils.scp_remote_escape(path) + pattern for pattern in patterns
            ]

    def _make_rsync_compatible_source(self, source, is_local):
        """
        Applies the same logic as _make_rsync_compatible_globs, but
        applies it to an entire list of sources, producing a new list of
        sources, properly quoted.
        """
        return sum((self._make_rsync_compatible_globs(path, is_local)
                    for path in source), [])

    def _set_umask_perms(self, dest):
        """
        Given a destination file/dir (recursively) set the permissions on
        all the files and directories to the max allowed by running umask.
        """

        # now this looks strange but I haven't found a way in Python to _just_
        # get the umask, apparently the only option is to try to set it
        umask = os.umask(0)
        os.umask(umask)

        max_privs = 0777 & ~umask

        def set_file_privs(filename):
            file_stat = os.stat(filename)

            file_privs = max_privs
            # if the original file permissions do not have at least one
            # executable bit then do not set it anywhere
            if not file_stat.st_mode & 0111:
                file_privs &= ~0111

            os.chmod(filename, file_privs)

        # try a bottom-up walk so changes on directory permissions won't cut
        # our access to the files/directories inside it
        for root, dirs, files in os.walk(dest, topdown=False):
            # when setting the privileges we emulate the chmod "X" behaviour
            # that sets to execute only if it is a directory or any of the
            # owner/group/other already has execute right
            for dirname in dirs:
                os.chmod(os.path.join(root, dirname), max_privs)

            for filename in files:
                set_file_privs(os.path.join(root, filename))

        # now set privs for the dest itself
        if os.path.isdir(dest):
            os.chmod(dest, max_privs)
        else:
            set_file_privs(dest)

    def get_file(self,
                 source,
                 dest,
                 delete_dest=False,
                 preserve_perm=True,
                 preserve_symlinks=False):
        """
        Copy files from the remote host to a local path.

        Directories will be copied recursively.
        If a source component is a directory with a trailing slash,
        the content of the directory will be copied, otherwise, the
        directory itself and its content will be copied. This
        behavior is similar to that of the program 'rsync'.

        Args:
                source: either
                        1) a single file or directory, as a string
                        2) a list of one or more (possibly mixed)
                                files or directories
                dest: a file or a directory (if source contains a
                        directory or more than one element, you must
                        supply a directory dest)
                delete_dest: if this is true, the command will also clear
                             out any old files at dest that are not in the
                             source
                preserve_perm: tells get_file() to try to preserve the sources
                               permissions on files and dirs
                preserve_symlinks: try to preserve symlinks instead of
                                   transforming them into files/dirs on copy

        Raises:
                AutoservRunError: the scp command failed
        """

        # Start a master SSH connection if necessary.
        self.start_master_ssh()

        if isinstance(source, basestring):
            source = [source]
        dest = os.path.abspath(dest)

        # If rsync is disabled or fails, try scp.
        try_scp = True
        if self.use_rsync():
            try:
                remote_source = self._encode_remote_paths(source)
                local_dest = utils.sh_escape(dest)
                rsync = self._make_rsync_cmd([remote_source], local_dest,
                                             delete_dest, preserve_symlinks)
                utils.run(rsync)
                try_scp = False
            except error.CmdError, e:
                logging.warn("trying scp, rsync failed: %s" % e)

        if try_scp:
            # scp has no equivalent to --delete, just drop the entire dest dir
            if delete_dest and os.path.isdir(dest):
                shutil.rmtree(dest)
                os.mkdir(dest)

            remote_source = self._make_rsync_compatible_source(source, False)
            if remote_source:
                # _make_rsync_compatible_source() already did the escaping
                remote_source = self._encode_remote_paths(remote_source,
                                                          escape=False)
                local_dest = utils.sh_escape(dest)
                scp = self._make_scp_cmd([remote_source], local_dest)
                try:
                    utils.run(scp)
                except error.CmdError, e:
                    raise error.AutoservRunError(e.args[0], e.args[1])
Example #24
0
    def get_file(self,
                 source,
                 dest,
                 delete_dest=False,
                 preserve_perm=True,
                 preserve_symlinks=False):
        """
        Copy files from the remote host to a local path.

        Directories will be copied recursively.
        If a source component is a directory with a trailing slash,
        the content of the directory will be copied, otherwise, the
        directory itself and its content will be copied. This
        behavior is similar to that of the program 'rsync'.

        Args:
                source: either
                        1) a single file or directory, as a string
                        2) a list of one or more (possibly mixed)
                                files or directories
                dest: a file or a directory (if source contains a
                        directory or more than one element, you must
                        supply a directory dest)
                delete_dest: if this is true, the command will also clear
                             out any old files at dest that are not in the
                             source
                preserve_perm: tells get_file() to try to preserve the sources
                               permissions on files and dirs
                preserve_symlinks: try to preserve symlinks instead of
                                   transforming them into files/dirs on copy

        Raises:
                AutoservRunError: the scp command failed
        """

        # Start a master SSH connection if necessary.
        self.start_master_ssh()

        if isinstance(source, str):
            source = [source]
        dest = os.path.abspath(dest)

        # If rsync is disabled or fails, try scp.
        try_scp = True
        if self.use_rsync():
            try:
                remote_source = self._encode_remote_paths(source)
                local_dest = utils.sh_escape(dest)
                rsync = self._make_rsync_cmd([remote_source], local_dest,
                                             delete_dest, preserve_symlinks)
                utils.run(rsync)
                try_scp = False
            except error.CmdError as e:
                logging.warn("trying scp, rsync failed: %s" % e)

        if try_scp:
            # scp has no equivalent to --delete, just drop the entire dest dir
            if delete_dest and os.path.isdir(dest):
                shutil.rmtree(dest)
                os.mkdir(dest)

            remote_source = self._make_rsync_compatible_source(source, False)
            if remote_source:
                # _make_rsync_compatible_source() already did the escaping
                remote_source = self._encode_remote_paths(remote_source,
                                                          escape=False)
                local_dest = utils.sh_escape(dest)
                scp = self._make_scp_cmd([remote_source], local_dest)
                try:
                    utils.run(scp)
                except error.CmdError as e:
                    raise error.AutoservRunError(e.args[0], e.args[1])

        if not preserve_perm:
            # we have no way to tell scp to not try to preserve the
            # permissions so set them after copy instead.
            # for rsync we could use "--no-p --chmod=ugo=rwX" but those
            # options are only in very recent rsync versions
            self._set_umask_perms(dest)
Example #25
0
    def send_file(self,
                  source,
                  dest,
                  delete_dest=False,
                  preserve_symlinks=False):
        """
        Copy files from a local path to the remote host.

        Directories will be copied recursively.
        If a source component is a directory with a trailing slash,
        the content of the directory will be copied, otherwise, the
        directory itself and its content will be copied. This
        behavior is similar to that of the program 'rsync'.

        Args:
                source: either
                        1) a single file or directory, as a string
                        2) a list of one or more (possibly mixed)
                                files or directories
                dest: a file or a directory (if source contains a
                        directory or more than one element, you must
                        supply a directory dest)
                delete_dest: if this is true, the command will also clear
                             out any old files at dest that are not in the
                             source
                preserve_symlinks: controls if symlinks on the source will be
                    copied as such on the destination or transformed into the
                    referenced file/directory

        Raises:
                AutoservRunError: the scp command failed
        """

        # Start a master SSH connection if necessary.
        self.start_master_ssh()

        if isinstance(source, str):
            source_is_dir = os.path.isdir(source)
            source = [source]
        remote_dest = self._encode_remote_paths([dest])

        # If rsync is disabled or fails, try scp.
        try_scp = True
        if self.use_rsync():
            try:
                local_sources = [utils.sh_escape(path) for path in source]
                rsync = self._make_rsync_cmd(local_sources, remote_dest,
                                             delete_dest, preserve_symlinks)
                utils.run(rsync)
                try_scp = False
            except error.CmdError as e:
                logging.warn("trying scp, rsync failed: %s" % e)

        if try_scp:
            # scp has no equivalent to --delete, just drop the entire dest dir
            if delete_dest:
                dest_exists = False
                try:
                    self.run("test -x %s" % dest)
                    dest_exists = True
                except error.AutoservRunError:
                    pass

                dest_is_dir = False
                if dest_exists:
                    try:
                        self.run("test -d %s" % dest)
                        dest_is_dir = True
                    except error.AutoservRunError:
                        pass

                # If there is a list of more than one path, destination *has*
                # to be a dir. If there's a single path being transferred and
                # it is a dir, the destination also has to be a dir. Therefore
                # it has to be created on the remote machine in case it doesn't
                # exist, otherwise we will have an scp failure.
                if len(source) > 1 or source_is_dir:
                    dest_is_dir = True

                if dest_exists and dest_is_dir:
                    cmd = "rm -rf %s && mkdir %s" % (dest, dest)
                    self.run(cmd)

                elif not dest_exists and dest_is_dir:
                    cmd = "mkdir %s" % dest
                    self.run(cmd)

            local_sources = self._make_rsync_compatible_source(source, True)
            if local_sources:
                scp = self._make_scp_cmd(local_sources, remote_dest)
                try:
                    utils.run(scp)
                except error.CmdError as e:
                    raise error.AutoservRunError(e.args[0], e.args[1])
Example #26
0
                if len(source) > 1 or source_is_dir:
                    dest_is_dir = True

                if dest_exists and dest_is_dir:
                    cmd = "rm -rf %s && mkdir %s" % (dest, dest)
                    self.run(cmd)

                elif not dest_exists and dest_is_dir:
                    cmd = "mkdir %s" % dest
                    self.run(cmd)

            local_sources = self._make_rsync_compatible_source(source, True)
            if local_sources:
                scp = self._make_scp_cmd(local_sources, remote_dest)
                try:
                    utils.run(scp)
                except error.CmdError, e:
                    raise error.AutoservRunError(e.args[0], e.args[1])

    def ssh_ping(self, timeout=60):
        try:
            self.run("true", timeout=timeout, connect_timeout=timeout)
        except error.AutoservSSHTimeout:
            msg = "Host (ssh) verify timed out (timeout = %d)" % timeout
            raise error.AutoservSSHTimeout(msg)
        except error.AutoservSshPermissionDeniedError:
            #let AutoservSshPermissionDeniedError be visible to the callers
            raise
        except error.AutoservRunError, e:
            # convert the generic AutoservRunError into something more
            # specific for this context
Example #27
0
                exit(-1)
            if image_index == 0 and lustre_img_index == 0:
                logging.error("No other valid kernel image!\n")
                fd.close()
                exit(0)

            for index in range(image_index + 1):
                if index not in [lustre_img_index, old_boot_index]:
                    fd = open(file_name, "w")
                    lines[default_line_index] = "default=%d\n" % index
                    # print lines
                    fd.writelines(lines)
                    fd.close()
                    try:
                        if self.vm_host is None:
                            utils.run(cmd_copy_in)
                        else:
                            self.vm_host.send_file(file_name, file_name)
                            self.vm_host.run(cmd_copy_in)
                    except Exception, e:
                        logging.error("Failed to copy in grub.cfg to domain [%s].\n %s \n %s" %
                                      (domain_name, str(e), traceback.format_exc()))
                    if self.vm_start_and_check(vm_node):
                        break
        except Exception, e:
            print "Failed, error [%s]\n" % e
            print traceback.format_exc()

    def force_repair_vm(self, vm_node):
        """
        fork a transient and run install kernel in it to repair the target vm
Example #28
0
                exit(-1)
            if image_index == 0 and lustre_img_index == 0:
                logging.error("No other valid kernel image!\n")
                fd.close()
                exit(0)

            for index in range(image_index + 1):
                if index not in [lustre_img_index, old_boot_index]:
                    fd = open(file_name, "w")
                    lines[default_line_index] = "default=%d\n" % index
                    # print lines
                    fd.writelines(lines)
                    fd.close()
                    try:
                        if self.vm_host is None:
                            utils.run(cmd_copy_in)
                        else:
                            self.vm_host.send_file(file_name, file_name)
                            self.vm_host.run(cmd_copy_in)
                    except Exception, e:
                        logging.error(
                            "Failed to copy in grub.cfg to domain [%s].\n %s \n %s"
                            % (domain_name, str(e), traceback.format_exc()))
                    if self.vm_start_and_check(vm_node):
                        break
        except Exception, e:
            print "Failed, error [%s]\n" % e
            print traceback.format_exc()

    def force_repair_vm(self, vm_node):
        """