def test_copy_image_file_ftp_failed(self): file_path = os.tempnam('/tmp', 'image') remote_path = '/mnt/openstack/images' exp_remote_path = os.path.join(remote_path, os.path.basename(file_path)) exp_cmd = ' '.join(['/usr/bin/rm -f', exp_remote_path]) fake_noop = lambda *args, **kwargs: None fake_op = self.powervm_adapter self.stubs.Set(fake_op, 'run_vios_command', fake_noop) self.stubs.Set(fake_op, '_checksum_local_file', fake_noop) self.mox.StubOutWithMock(common, 'ftp_put_command') self.mox.StubOutWithMock(self.powervm_adapter, 'run_vios_command_as_root') msg_args = {'ftp_cmd': 'PUT', 'source_path': file_path, 'dest_path': remote_path} exp_exception = exception.PowerVMFTPTransferFailed(**msg_args) common.ftp_put_command(self.connection, file_path, remote_path).AndRaise(exp_exception) self.powervm_adapter.run_vios_command_as_root(exp_cmd).AndReturn([]) self.mox.ReplayAll() self.assertRaises(exception.PowerVMFTPTransferFailed, self.powervm_adapter._copy_image_file, file_path, remote_path)
def test_copy_image_file_ftp_failed(self): file_path = os.tempnam('/tmp', 'image') remote_path = '/mnt/openstack/images' exp_remote_path = os.path.join(remote_path, os.path.basename(file_path)) exp_cmd = ' '.join(['/usr/bin/rm -f', exp_remote_path]) fake_noop = lambda *args, **kwargs: None fake_op = self.powervm_adapter self.stubs.Set(fake_op, 'run_vios_command', fake_noop) self.stubs.Set(fake_op, '_checksum_local_file', fake_noop) self.mox.StubOutWithMock(common, 'ftp_put_command') self.mox.StubOutWithMock(self.powervm_adapter, 'run_vios_command_as_root') msg_args = { 'ftp_cmd': 'PUT', 'source_path': file_path, 'dest_path': remote_path } exp_exception = exception.PowerVMFTPTransferFailed(**msg_args) common.ftp_put_command(self.connection, file_path, remote_path).AndRaise(exp_exception) self.powervm_adapter.run_vios_command_as_root(exp_cmd).AndReturn([]) self.mox.ReplayAll() self.assertRaises(exception.PowerVMFTPTransferFailed, self.powervm_adapter._copy_image_file, file_path, remote_path)
def test_copy_image_file_ftp_failed(self): file_path = os.tempnam("/tmp", "image") remote_path = "/mnt/openstack/images" exp_remote_path = os.path.join(remote_path, os.path.basename(file_path)) exp_cmd = " ".join(["/usr/bin/rm -f", exp_remote_path]) fake_noop = lambda *args, **kwargs: None fake_op = self.powervm_adapter self.stubs.Set(fake_op, "run_vios_command", fake_noop) self.stubs.Set(fake_op, "_checksum_local_file", fake_noop) self.mox.StubOutWithMock(common, "ftp_put_command") self.mox.StubOutWithMock(self.powervm_adapter, "run_vios_command_as_root") msg_args = {"ftp_cmd": "PUT", "source_path": file_path, "dest_path": remote_path} exp_exception = exception.PowerVMFTPTransferFailed(**msg_args) common.ftp_put_command(self.connection, file_path, remote_path).AndRaise(exp_exception) self.powervm_adapter.run_vios_command_as_root(exp_cmd).AndReturn([]) self.mox.ReplayAll() self.assertRaises( exception.PowerVMFTPTransferFailed, self.powervm_adapter._copy_image_file, file_path, remote_path )
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
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
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)) 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: # 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
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