Ejemplo n.º 1
0
    def _copy_image_file_from_host(self,
                                   remote_source_path,
                                   local_dest_dir,
                                   compress=False):
        """
        Copy a file from IVM to the nova-compute host,
        and return the location of the copy

        :param remote_source_path remote source file path
        :param local_dest_dir local destination directory
        :param compress: if True, compress the file before transfer;
                         if False (default), copy the file as is
        """

        temp_str = common.aix_path_join(local_dest_dir,
                                        os.path.basename(remote_source_path))
        local_file_path = temp_str + '.gz'

        if compress:
            copy_from_path = remote_source_path + '.gz'
        else:
            copy_from_path = remote_source_path

        if compress:
            # Gzip the file
            cmd = "/usr/bin/gzip %s" % remote_source_path
            self.run_vios_command_as_root(cmd)

            # Cleanup uncompressed remote file
            cmd = "/usr/bin/rm -f %s" % remote_source_path
            self.run_vios_command_as_root(cmd)

        # Get file checksum
        output = self._md5sum_remote_file(copy_from_path)
        if not output:
            LOG.error(_("Unable to get checksum"))
            msg_args = {'file_path': copy_from_path}
            raise exception.PowerVMFileTransferFailed(**msg_args)
        else:
            source_chksum = output.split(' ')[0]

        # Copy file to host
        common.ftp_get_command(self.connection_data, copy_from_path,
                               local_file_path)

        # Calculate copied image checksum
        dest_chksum = self._checksum_local_file(local_file_path)

        # do comparison
        if source_chksum and dest_chksum != source_chksum:
            LOG.error(_("Image checksums do not match"))
            raise exception.PowerVMFileTransferFailed(
                file_path=local_file_path)

        # Cleanup transferred remote file
        cmd = "/usr/bin/rm -f %s" % copy_from_path
        output = self.run_vios_command_as_root(cmd)

        return local_file_path
Ejemplo n.º 2
0
def ftp_put_command(connection, local_path, remote_dir):
    """Method to transfer a file via ftp.

    :param connection: a Connection object.
    :param local_path: path to the local file
    :param remote_dir: path to remote destination
    :raises: PowerVMFileTransferFailed
    """
    try:
        ftp = ftplib.FTP(host=connection.host,
                         user=connection.username,
                         passwd=connection.password)
        ftp.cwd(remote_dir)
        name = os.path.split(local_path)[1]
        f = open(local_path, "rb")
        ftp.storbinary("STOR " + name, f)
        f.close()
        ftp.close()
    except Exception:
        LOG.exception(_('File transfer to PowerVM manager failed'))
        raise exception.PowerVMFileTransferFailed(file_path=local_path)
Ejemplo n.º 3
0
    def _copy_image_file(self, source_path, remote_path, decompress=False):
        """Copy file to VIOS, decompress it, and return its new size and name.

        :param source_path: source file path
        :param remote_path remote file path
        :param decompress: if True, decompressess the file after copying;
                           if False (default), just copies the file
        """
        # Calculate source image checksum
        source_cksum = self._checksum_local_file(source_path)

        comp_path = os.path.join(remote_path, os.path.basename(source_path))
        if comp_path.endswith(".gz"):
            uncomp_path = os.path.splitext(comp_path)[0]
        else:
            uncomp_path = comp_path
        if not decompress:
            final_path = comp_path
        else:
            final_path = uncomp_path

        # Check whether the image is already on IVM
        output = self.run_vios_command("ls %s" % final_path,
                                       check_exit_code=False)

        # If the image does not exist already
        if not output:
            try:
                # Copy file to IVM
                common.ftp_put_command(self.connection_data, source_path,
                                       remote_path)
            except exception.PowerVMFTPTransferFailed:
                with excutils.save_and_reraise_exception():
                    cmd = "/usr/bin/rm -f %s" % final_path
                    self.run_vios_command_as_root(cmd)

            # Verify image file checksums match
            output = self._md5sum_remote_file(final_path)
            if not output:
                LOG.error(_("Unable to get checksum"))
                # Cleanup inconsistent remote file
                cmd = "/usr/bin/rm -f %s" % final_path
                self.run_vios_command_as_root(cmd)

                raise exception.PowerVMFileTransferFailed(file_path=final_path)
            if source_cksum != output.split(' ')[0]:
                LOG.error(_("Image checksums do not match"))
                # Cleanup inconsistent remote file
                cmd = "/usr/bin/rm -f %s" % final_path
                self.run_vios_command_as_root(cmd)

                raise exception.PowerVMFileTransferFailed(file_path=final_path)

            if decompress:
                # Unzip the image
                cmd = "/usr/bin/gunzip %s" % comp_path
                output = self.run_vios_command_as_root(cmd)

                # Remove existing image file
                cmd = "/usr/bin/rm -f %s.*" % uncomp_path
                output = self.run_vios_command_as_root(cmd)

                # Rename unzipped image
                cmd = "/usr/bin/mv %s %s" % (uncomp_path, final_path)
                output = self.run_vios_command_as_root(cmd)

                # Remove compressed image file
                cmd = "/usr/bin/rm -f %s" % comp_path
                output = self.run_vios_command_as_root(cmd)

        else:
            LOG.debug(_("Image found on host at '%s'") % final_path)

        # Calculate file size in multiples of 512 bytes
        output = self.run_vios_command("ls -o %s|awk '{print $4}'" %
                                       final_path,
                                       check_exit_code=False)
        if output:
            size = int(output[0])
        else:
            LOG.error(_("Uncompressed image file not found"))
            raise exception.PowerVMFileTransferFailed()
        if (size % 512 != 0):
            size = (int(size / 512) + 1) * 512

        return final_path, size
Ejemplo n.º 4
0
    def copy_image_file(self, source_path, remote_path):
        """Copy file to VIOS, decompress it, and return its new size and name.

        :param source_path: source file path
        :param remote_path remote file path
        """
        # Calculate source image checksum
        hasher = hashlib.md5()
        block_size = 0x10000
        img_file = file(source_path, 'r')
        buf = img_file.read(block_size)
        while len(buf) > 0:
            hasher.update(buf)
            buf = img_file.read(block_size)
        source_cksum = hasher.hexdigest()

        comp_path = remote_path + os.path.basename(source_path)
        uncomp_path = comp_path.rstrip(".gz")
        final_path = "%s.%s" % (uncomp_path, source_cksum)

        # Check whether the uncompressed image is already on IVM
        output = self.run_command("ls %s" % final_path, check_exit_code=False)

        # If the image does not exist already
        if not len(output):
            # Copy file to IVM
            common.ftp_put_command(self.connection_data, source_path,
                                   remote_path)

            # Verify image file checksums match
            cmd = ("/usr/bin/csum -h MD5 %s |"
                   "/usr/bin/awk '{print $1}'" % comp_path)
            output = self.run_command_as_root(cmd)
            if not len(output):
                LOG.error(_("Unable to get checksum"))
                raise exception.PowerVMFileTransferFailed()
            if source_cksum != output[0]:
                LOG.error(_("Image checksums do not match"))
                raise exception.PowerVMFileTransferFailed()

            # Unzip the image
            cmd = "/usr/bin/gunzip %s" % comp_path
            output = self.run_command_as_root(cmd)

            # Remove existing image file
            cmd = "/usr/bin/rm -f %s.*" % uncomp_path
            output = self.run_command_as_root(cmd)

            # Rename unzipped image
            cmd = "/usr/bin/mv %s %s" % (uncomp_path, final_path)
            output = self.run_command_as_root(cmd)

            # Remove compressed image file
            cmd = "/usr/bin/rm -f %s" % comp_path
            output = self.run_command_as_root(cmd)

        # Calculate file size in multiples of 512 bytes
        output = self.run_command("ls -o %s|awk '{print $4}'" % final_path,
                                  check_exit_code=False)
        if len(output):
            size = int(output[0])
        else:
            LOG.error(_("Uncompressed image file not found"))
            raise exception.PowerVMFileTransferFailed()
        if (size % 512 != 0):
            size = (int(size / 512) + 1) * 512

        return final_path, size
Ejemplo n.º 5
0
    def _copy_image_file(self, source_path, remote_path, decompress=False):
        """Copy file to VIOS, decompress it, and return its new size and name.

        :param source_path: source file path
        :param remote_path remote file path
        :param decompress: if True, decompressess the file after copying;
                           if False (default), just copies the file
        """
        # Calculate source image checksum
        hasher = hashlib.md5()
        block_size = 0x10000
        img_file = file(source_path, 'r')
        buf = img_file.read(block_size)
        while len(buf) > 0:
            hasher.update(buf)
            buf = img_file.read(block_size)
        source_cksum = hasher.hexdigest()

        comp_path = os.path.join(remote_path, os.path.basename(source_path))
        uncomp_path = comp_path.rstrip(".gz")
        if not decompress:
            final_path = comp_path
        else:
            final_path = uncomp_path

        # Check whether the image is already on IVM
        output = self.run_vios_command("ls %s" % final_path,
                                       check_exit_code=False)

        # If the image does not exist already
        if not output:
            # Copy file to IVM
            common.ftp_put_command(self.connection_data, source_path,
                                   remote_path)

            # Verify image file checksums match
            output = self._md5sum_remote_file(final_path)
            if not output:
                LOG.error(_("Unable to get checksum"))
                raise exception.PowerVMFileTransferFailed()
            if source_cksum != output.split(' ')[0]:
                LOG.error(_("Image checksums do not match"))
                raise exception.PowerVMFileTransferFailed()

            if decompress:
                # Unzip the image
                cmd = "/usr/bin/gunzip %s" % comp_path
                output = self.run_vios_command_as_root(cmd)

                # Remove existing image file
                cmd = "/usr/bin/rm -f %s.*" % uncomp_path
                output = self.run_vios_command_as_root(cmd)

                # Rename unzipped image
                cmd = "/usr/bin/mv %s %s" % (uncomp_path, final_path)
                output = self.run_vios_command_as_root(cmd)

                # Remove compressed image file
                cmd = "/usr/bin/rm -f %s" % comp_path
                output = self.run_vios_command_as_root(cmd)

        else:
            LOG.debug(_("Image found on host at '%s'") % final_path)

        # Calculate file size in multiples of 512 bytes
        output = self.run_vios_command("ls -o %s|awk '{print $4}'" %
                                       final_path,
                                       check_exit_code=False)
        if output:
            size = int(output[0])
        else:
            LOG.error(_("Uncompressed image file not found"))
            raise exception.PowerVMFileTransferFailed()
        if (size % 512 != 0):
            size = (int(size / 512) + 1) * 512

        return final_path, size