def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: uuid = conn.domname_to_uuid(opts.name) try: # physical conn.set_domain_name(opts.name) conn.delete_guest(opts.name, opts.pool, opts.volume) self.up_progress(20) except Exception, e: print >>sys.stderr, '[Warn] Failed to delete the guest OS physical. - dom=%s - detail : %s' \ % (opts.name, str(e.args)) self.logger.warn('Failed to delete the guest OS physical. - dom=%s - detail : %s' \ % (opts.name, str(e.args))) # Check the presence of residual files try: self.up_progress(10) # /etc config = "" hypervisor = conn.get_hypervisor_type() if hypervisor == "XEN": config = "%s/%s" % (XEN_VIRT_CONFIG_DIR, opts.name,) elif hypervisor == "KVM" or hypervisor == "QEMU": config = "%s/%s" % (KVM_VIRT_CONFIG_DIR, opts.name,) if os.path.isfile(config) is True: os.remove(config) self.logger.info("physical config remove. - path=%s" % config) self.up_progress(5) xml_config = '%s/%s.xml' % (VIRT_XML_CONFIG_DIR, opts.name) if os.path.isfile(xml_config) is True: os.remove(xml_config) self.logger.info("physical xml config remove. - path=%s" % xml_config) self.up_progress(5) self.logger.info('To remove the storage volume even more.') tmp_pool = conn.get_storage_pool_name_bydomain(opts.name, 'os') if tmp_pool: domains_dir = conn.get_storage_pool_targetpath(tmp_pool[0]) else: domains_dir = conn.get_storage_pool_targetpath(opts.pool) disk_image = '%s/%s/images/%s.img' % (domains_dir, opts.name, opts.name,) if os.path.isfile(disk_image) is True or os.path.islink(disk_image) is True: os.remove(disk_image) self.logger.info("physical disk image remove. - path=%s" % disk_image) self.up_progress(5) if 0 < len(opts.name.split()): # double check snapshot_dir = '%s/%s/snapshot' % (domains_dir, opts.name,) if os.path.isdir(snapshot_dir) is True: for root, dirs, files in os.walk(snapshot_dir): for fname in files: file_path = os.path.join(root, fname) os.remove(file_path) self.logger.info("physical snapshots file remove. - file=%s" % file_path) os.removedirs(snapshot_dir) self.logger.info("physical snapshots directory remove. - dir=%s" % snapshot_dir) self.up_progress(5) if 0 < len(opts.name.split()): # double check disk_dir = '%s/%s/disk' % (domains_dir, opts.name,) if os.path.isdir(disk_dir) is True: for root, dirs, files in os.walk(disk_dir): for fname in files: file_path = os.path.join(root, fname) os.remove(file_path) self.logger.info("physical disk file remove. - file=%s" % file_path) os.removedirs(disk_dir) self.logger.info("physical disk directory remove. - dir=%s" % disk_dir) self.up_progress(5) # Delete GuestOS directory domain_dir = "%s/%s" % (domains_dir, opts.name) if os.path.isdir(domain_dir) is True: shutil.rmtree(domain_dir) self.up_progress(5) except Exception, e: print >>sys.stderr, '[Warn] Failed to remove the residual file.. - dom=%s - detail : %s' \ % (opts.name, str(e.args)) self.logger.warn('Failed to remove the residual file.. - dom=%s - detail : %s' \ % (opts.name, str(e.args)))
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: uuid = conn.domname_to_uuid(opts.name) try: # physical conn.set_domain_name(opts.name) conn.delete_guest(opts.name, opts.pool, opts.volume) self.up_progress(20) except Exception, e: print >>sys.stderr, '[Warn] Failed to delete the guest OS physical. - dom=%s - detail : %s' \ % (opts.name, str(e.args)) self.logger.warn('Failed to delete the guest OS physical. - dom=%s - detail : %s' \ % (opts.name, str(e.args))) # Check the presence of residual files try: self.up_progress(10) # /etc config = "" hypervisor = conn.get_hypervisor_type() if hypervisor == "XEN": config = "%s/%s" % (XEN_VIRT_CONFIG_DIR, opts.name,) elif hypervisor == "KVM" or hypervisor == "QEMU": config = "%s/%s" % (KVM_VIRT_CONFIG_DIR, opts.name,) if os.path.isfile(config) is True: os.remove(config) self.logger.info("physical config remove. - path=%s" % config) self.up_progress(5) xml_config = '%s/%s.xml' % (VIRT_XML_CONFIG_DIR, opts.name) if os.path.isfile(xml_config) is True: os.remove(xml_config) self.logger.info("physical xml config remove. - path=%s" % xml_config) self.up_progress(5) self.logger.info('To remove the storage volume even more.') tmp_pool = conn.get_storage_pool_name_bydomain(opts.name, 'os') if tmp_pool: domains_dir = conn.get_storage_pool_targetpath(tmp_pool[0]) else: domains_dir = conn.get_storage_pool_targetpath(opts.pool) disk_image = '%s/%s/images/%s.img' % (domains_dir, opts.name, opts.name,) if os.path.isfile(disk_image) is True or os.path.islink(disk_image) is True: os.remove(disk_image) self.logger.info("physical disk image remove. - path=%s" % disk_image) self.up_progress(5) if 0 < len(opts.name.split()): # double check snapshot_dir = '%s/%s/snapshot' % (domains_dir, opts.name,) if os.path.isdir(snapshot_dir) is True: for root, dirs, files in os.walk(snapshot_dir): for fname in files: file_path = os.path.join(root, fname) os.remove(file_path) self.logger.info("physical snapshots file remove. - file=%s" % file_path) os.removedirs(snapshot_dir) self.logger.info("physical snapshots directory remove. - dir=%s" % snapshot_dir) self.up_progress(5) if 0 < len(opts.name.split()): # double check disk_dir = '%s/%s/disk' % (domains_dir, opts.name,) if os.path.isdir(disk_dir) is True: for root, dirs, files in os.walk(disk_dir): for fname in files: file_path = os.path.join(root, fname) os.remove(file_path) self.logger.info("physical disk file remove. - file=%s" % file_path) os.removedirs(disk_dir) self.logger.info("physical disk directory remove. - dir=%s" % disk_dir) self.up_progress(5) # Delete GuestOS directory domain_dir = "%s/%s" % (domains_dir, opts.name) if os.path.isdir(domain_dir) is True: shutil.rmtree(domain_dir) self.up_progress(5) except Exception, e: print >>sys.stderr, '[Warn] Failed to remove the residual file.. - dom=%s - detail : %s' \ % (opts.name, str(e.args)) self.logger.warn('Failed to remove the residual file.. - dom=%s - detail : %s' \ % (opts.name, str(e.args)))
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: try: src_pool = conn.get_storage_pool_name_bydomain(opts.name, "os") if not src_pool: raise KssCommandException("Source storage pool not found. domain=%s" % (opts.name)) if conn.get_storage_pool_type(src_pool) == 'dir': raise KssCommandException("Storage pool type 'dir' is not. domain=%s" % (opts.name)) src_path = conn.get_storage_pool_targetpath(src_pool[0]) self.domain_dir = "%s/%s" % (src_path, opts.name,) if os.path.isdir(self.domain_dir) is False: raise KssCommandException( 'domain directory is not found or not directory. - %s' % (self.domain_dir)) # Model virt_uuid = conn.domname_to_uuid(opts.name) model = findby1uniquekey(self.kss_session, virt_uuid) if not model: raise KssCommandException("Export data does not exist in the database.") database = {} database['attribute'] = model.attribute database['hypervisor'] = model.hypervisor database['icon'] = model.icon database['name'] = model.name database['notebook'] = {"title" : model.notebook.title, "value" : model.notebook.value, } tags = [] for _tag in model.tags: tags.append(_tag.name) database['tags'] = ",".join(tags) database['uniq_key'] = model.uniq_key # Snapshot snapshots = [] kvs = KaresansuiVirtSnapshot(readonly=False) try: guest_id = model.id snapshot_list = kvs.listNames(opts.name)[opts.name] if len(snapshot_list) > 0: for snapshot in snapshot_list: s_model = s_findbyname_guestby1(self.kss_session, snapshot, guest_id) if s_model is not None: name = s_model.name title = s_model.notebook.title value = s_model.notebook.value snapshots.append({"name":name, "title":title, "value":value,}) except: raise KssCommandException("Cannot fetch the information of snapshots correctly.") kvs.finish() # Pool target_dir = "" if opts.pool: inactive_storage_pools = conn.list_inactive_storage_pool() active_storage_pools = conn.list_active_storage_pool() if not (opts.pool in active_storage_pools or opts.pool in inactive_storage_pools): raise KssCommandException('Target storage pool does not exist. - pool=%s' % (opts.pool)) pool = conn.search_kvn_storage_pools(opts.pool) storage_info = pool[0].get_info() if storage_info["type"] == "dir" and storage_info["target"]["path"] != "": target_dir = storage_info["target"]["path"] else: raise KssCommandException("Target storage pool type is not 'dir'. pool=%s" % (opts.pool)) elif opts.dir: target_dir = opts.dir self.up_progress(10) progresscb = None if opts.verbose: try: from karesansui.lib.progress import ProgressMeter progresscb = ProgressMeter(command_object=self) except: pass else: try: from karesansui.lib.progress import ProgressMeter progresscb = ProgressMeter(command_object=self,quiet=True) except: pass if opts.title[0:4] == "b64:": title = base64_decode(opts.title[4:]) else: title = opts.title uuid = StrFromUUID(GenUUID()) conn.export_guest(uuid=uuid, name=opts.name, directory=target_dir, database=database, realicon=model.realicon(), title=title, snapshots=snapshots, progresscb=progresscb) self.up_progress(40) self.logger.info('Export guest completed. - pool=%s, uuid=%s' % (opts.pool, uuid)) print >>sys.stdout, _('Export guest completed. - pool=%s, uuid=%s' % (opts.pool, uuid)) return True except KaresansuiVirtException, e: raise KssCommandException('Failed to export guest. - %s to %s [%s]' \ % (opts.name,target_dir, ''.join(e.args))) except KssCommandException: raise except: raise KssCommandException('Failed to export guest. - %s to %s' \ % (opts.name,target_dir))
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: try: src_pool = conn.get_storage_pool_name_bydomain(opts.name, "os") if not src_pool: raise KssCommandException( "Source storage pool not found. domain=%s" % (opts.name)) if conn.get_storage_pool_type(src_pool) == 'dir': raise KssCommandException( "Storage pool type 'dir' is not. domain=%s" % (opts.name)) src_path = conn.get_storage_pool_targetpath(src_pool[0]) self.domain_dir = "%s/%s" % ( src_path, opts.name, ) if os.path.isdir(self.domain_dir) is False: raise KssCommandException( 'domain directory is not found or not directory. - %s' % (self.domain_dir)) # Model virt_uuid = conn.domname_to_uuid(opts.name) model = findby1uniquekey(self.kss_session, virt_uuid) if not model: raise KssCommandException( "Export data does not exist in the database.") database = {} database['attribute'] = model.attribute database['hypervisor'] = model.hypervisor database['icon'] = model.icon database['name'] = model.name database['notebook'] = { "title": model.notebook.title, "value": model.notebook.value, } tags = [] for _tag in model.tags: tags.append(_tag.name) database['tags'] = ",".join(tags) database['uniq_key'] = model.uniq_key # Snapshot snapshots = [] kvs = KaresansuiVirtSnapshot(readonly=False) try: guest_id = model.id snapshot_list = kvs.listNames(opts.name)[opts.name] if len(snapshot_list) > 0: for snapshot in snapshot_list: s_model = s_findbyname_guestby1( self.kss_session, snapshot, guest_id) if s_model is not None: name = s_model.name title = s_model.notebook.title value = s_model.notebook.value snapshots.append({ "name": name, "title": title, "value": value, }) except: raise KssCommandException( "Cannot fetch the information of snapshots correctly.") kvs.finish() # Pool target_dir = "" if opts.pool: inactive_storage_pools = conn.list_inactive_storage_pool() active_storage_pools = conn.list_active_storage_pool() if not (opts.pool in active_storage_pools or opts.pool in inactive_storage_pools): raise KssCommandException( 'Target storage pool does not exist. - pool=%s' % (opts.pool)) pool = conn.search_kvn_storage_pools(opts.pool) storage_info = pool[0].get_info() if storage_info["type"] == "dir" and storage_info[ "target"]["path"] != "": target_dir = storage_info["target"]["path"] else: raise KssCommandException( "Target storage pool type is not 'dir'. pool=%s" % (opts.pool)) elif opts.dir: target_dir = opts.dir self.up_progress(10) progresscb = None if opts.verbose: try: from karesansui.lib.progress import ProgressMeter progresscb = ProgressMeter(command_object=self) except: pass else: try: from karesansui.lib.progress import ProgressMeter progresscb = ProgressMeter(command_object=self, quiet=True) except: pass if opts.title[0:4] == "b64:": title = base64_decode(opts.title[4:]) else: title = opts.title uuid = StrFromUUID(GenUUID()) conn.export_guest(uuid=uuid, name=opts.name, directory=target_dir, database=database, realicon=model.realicon(), title=title, snapshots=snapshots, progresscb=progresscb) self.up_progress(40) self.logger.info('Export guest completed. - pool=%s, uuid=%s' % (opts.pool, uuid)) print >> sys.stdout, _( 'Export guest completed. - pool=%s, uuid=%s' % (opts.pool, uuid)) return True except KaresansuiVirtException, e: raise KssCommandException('Failed to export guest. - %s to %s [%s]' \ % (opts.name,target_dir, ''.join(e.args))) except KssCommandException: raise except: raise KssCommandException('Failed to export guest. - %s to %s' \ % (opts.name,target_dir))