def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: conn.network.set_network_name(opts.name) flag = None if opts.enable is True: flag = True elif opts.disable is True: flag = False else: raise KssCommandException( 'either %s options must be specified.' % '--enable or --disable') self.up_progress(10) ret = conn.autostart_network(flag) if ret is False: raise KssCommandException( 'Failed to set autostart flag. - net=%s flag=%s' % (opts.name, flag)) self.up_progress(40) self.logger.info('Set autostart flag. - net=%s flag=%s' % (opts.name, flag)) print >> sys.stdout, _('Set autostart flag. - net=%s flag=%s') % ( opts.name, flag) return True finally: conn.close()
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: active_networks = conn.list_active_network() inactive_networks = conn.list_inactive_network() if not (opts.name in active_networks or opts.name in inactive_networks): raise KssCommandException( 'Could not find the specified network. - net=%s' % (opts.name)) self.up_progress(10) try: conn.delete_network(opts.name) except: raise KssCommandException( 'Failed to delete network. - net=%s' % (opts.name)) self.up_progress(20) active_networks = conn.list_active_network() inactive_networks = conn.list_inactive_network() if opts.name in active_networks or opts.name in inactive_networks: raise KssCommandException( 'Failed to delete the network. - net=%s' % (opts.name)) self.logger.info('Deleted network. - net=%s' % (opts.name)) print >> sys.stdout, _('Deleted network. - net=%s') % (opts.name) return True finally: conn.close()
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: active_networks = conn.list_active_network() inactive_networks = conn.list_inactive_network() if not (opts.name in active_networks or opts.name in inactive_networks): raise KssCommandException('Network not found. - net=%s' % (opts.name)) self.up_progress(10) conn.stop_network(opts.name) self.up_progress(40) if opts.name in conn.list_active_network(): raise KssCommandException('Failed to stop network. - net=%s' % (opts.name)) self.logger.info('Stopped network. - net=%s' % (opts.name)) print >> sys.stdout, _('Stopped network. - net=%s') % (opts.name) return True finally: conn.close()
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) xml = None if opts.id: xml = "<domainsnapshot><name>%s</name></domainsnapshot>" % opts.id kvs = KaresansuiVirtSnapshot(readonly=False) try: self.up_progress(10) try: xmlDesc = kvs.createSnapshot(opts.name, xml) self.up_progress(50) if xmlDesc is not False: doc = XMLParse(xmlDesc) snapshot_name = XMLXpath(doc, '/domainsnapshot/name/text()') msg = _("Domain snapshot '%s' created. - domain=%s") % (str(snapshot_name),opts.name,) self.logger.info(msg) print >>sys.stdout, msg else: msg = _("Failed to create snapshot. - domain=%s") % (opts.name,) self.logger.error(msg) raise KssCommandException(msg) except KssCommandException, e: raise KssCommandException(''.join(e.args)) except Exception, e: msg = _("Failed to create snapshot. - domain=%s") % (opts.name,) msg += ": detail %s" % ''.join(e.args) self.logger.error(msg) raise KssCommandException(msg)
def process(self): (opts, args) = getopts() chkopts(opts) opts.dest = opts.dest.rstrip('/') self.up_progress(10) tmp_configfiles = { LIGHTTPD_PORT_CONFIG: opts.tmp_port_conf, LIGHTTPD_SSL_CONFIG: opts.tmp_ssl_conf, LIGHTTPD_ACCESS_CONFIG: opts.tmp_access_conf } self.up_progress(10) for srcfile in tmp_configfiles.values(): if os.path.isfile(srcfile) is False: raise KssCommandException( 'Temporary config file is not found. -path=%s' % srcfile) self.up_progress(20) try: for dest, src in tmp_configfiles.items(): copy_file(src, opts.dest + '/' + dest) remove_file(src) self.up_progress(40) except: raise KssCommandException( 'Failed to copy config file -src=%s dest=%s' % (src, dest)) self.logger.info('Applied lighttpd settings.') print >> sys.stdout, _('Applied lighttpd settings.') return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: inactive_storage_pools = conn.list_inactive_storage_pool() active_storage_pools = conn.list_active_storage_pool() self.up_progress(10) if not (opts.name in active_storage_pools or \ opts.name in inactive_storage_pools): raise KssCommandOptException( 'Storage pool does not exist. - pool=%s' % opts.name) conn.storage_pool.set_storage_name(opts.name) self.up_progress(10) flag = None if opts.enable: flag = True elif opts.disable: flag = False else: raise KssCommandException( 'ERROR: Execution status information does not exist. enable,disable=%s,%s' \ % (str(opts.enable), str(opts.disable))) self.up_progress(10) if conn.autostart_storage_pool(flag) is False: raise KssCommandException( 'Failed to autostart storage pool(libvirt). - pool=%s' % (opts.name)) ret = conn.is_autostart_storage_pool() if not (ret is flag): raise KssCommandException( 'Auto-start failed to set the storage pool. - pool=%s, autostart=%s' \ % (opts.name, str(ret))) self.up_progress(40) if opts.enable: self.logger.info('Set autostart storage pool. - pool=%s' % (opts.name)) print >> sys.stdout, _( 'Set autostart storage pool. - pool=%s') % (opts.name) elif opts.disable: self.logger.info('Unset autostart storage pool. - pool=%s' % (opts.name)) print >> sys.stdout, _( 'Unset autostart storage pool. - pool=%s') % (opts.name) return True finally: conn.close()
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: forward = { "dev": opts.forward_dev, "mode": opts.forward_mode, } bridge = opts.bridge_name if opts.autostart == "yes": autostart = True else: autostart = False active_networks = conn.list_active_network() inactive_networks = conn.list_inactive_network() self.up_progress(10) if opts.name in active_networks or opts.name in inactive_networks: raise KssCommandException('network already exists. - net=%s' % (opts.name)) self.up_progress(10) try: conn.create_network(opts.name, opts.cidr, opts.dhcp_start, opts.dhcp_end, forward, bridge, autostart=autostart) except: raise KssCommandException( 'Failed to create network. - net=%s' % (opts.name)) self.up_progress(40) active_networks = conn.list_active_network() inactive_networks = conn.list_inactive_network() if not (opts.name in active_networks or opts.name in inactive_networks): raise KssCommandException( 'Failed to create the network. - net=%s' % (opts.name)) self.logger.info('Created network. - net=%s' % (opts.name)) print >> sys.stdout, _('Created network. - net=%s') % (opts.name) self.up_progress(10) return True finally: conn.close()
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: active_networks = conn.list_active_network() inactive_networks = conn.list_inactive_network() if not (opts.name in active_networks or opts.name in inactive_networks): raise KssCommandException( 'Could not find the specified network. - net=%s' % (opts.name)) self.up_progress(10) try: conn.stop_network(opts.name) except KaresansuiVirtException, e: if opt.force is not True: raise KssCommandException( 'Could not stop the specified network. - net=%s' % (opts.name)) self.up_progress(20) try: conn.start_network(opts.name) except KaresansuiVirtException, e: if opts.force is not True: raise KssCommandException( 'Could not start the specified network. - net=%s' % (opts.name)) # try to bring down existing bridge kvn = conn.search_kvn_networks(opts.name)[0] try: bridge_name = kvn.get_info()['bridge']['name'] except KeyError: pass ret, res = execute_command( [NETWORK_IFCONFIG_COMMAND, bridge_name, 'down']) ret, res = execute_command( [NETWORK_BRCTL_COMMAND, 'delbr', bridge_name]) # try again conn.start_network(opts.name)
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) logout_command_args = [ ISCSI_CMD, ISCSI_CMD_OPTION_MODE, ISCSI_CMD_OPTION_MODE_NODE, ISCSI_CMD_OPTION_TARGETNAME, opts.iqn, ] if opts.host: logout_command_args.append(ISCSI_CMD_OPTION_PORTAL) logout_command_args.append(opts.host) logout_command_args.append(ISCSI_CMD_OPTION_LOGOUT) (logout_rc, logout_res) = execute_command(logout_command_args) self.up_progress(50) if logout_rc != 0: raise KssCommandException( 'Failed to logout iSCSI. - host=%s iqn=%s message=%s' % (opts.host, opts.iqn, logout_res)) for line in logout_res: if not line: continue self.logger.info("%s" % (line)) print >> sys.stdout, _("%s") % (line) return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) delete_command_args = [ ISCSI_CMD, ISCSI_CMD_OPTION_MODE, ISCSI_CMD_OPTION_MODE_NODE, ISCSI_CMD_OPTION_OPERATOR, ISCSI_CMD_OPTION_OPERATOR_DELETE, ISCSI_CMD_OPTION_TARGETNAME, opts.iqn, ] if opts.host: delete_command_args.append(ISCSI_CMD_OPTION_PORTAL) delete_command_args.append(opts.host) (delete_rc, delete_res) = execute_command(delete_command_args) self.up_progress(50) if delete_rc != 0: raise KssCommandException( 'Failed to delete iSCSI. - host=%s iqn=%s message=%s' % (opts.host, opts.iqn, delete_res)) self.logger.info("Delete iSCSI node successful. - host=%s iqn=%s" % (opts.host, opts.iqn)) print >> sys.stdout, _("Delete iSCSI node successful. - host=%s iqn=%s" ) % (opts.host, opts.iqn) return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: try: inactive_storage_pools = conn.list_inactive_storage_pool() active_storage_pools = conn.list_active_storage_pool() self.up_progress(10) if not (opts.name in active_storage_pools or opts.name in inactive_storage_pools): raise KssCommandException( 'Specified storage pool does not exist. - pool=%s' % opts.name) if opts.name in active_storage_pools: raise KssCommandException( 'Storage pool is already running. - pool=%s' % opts.name) self.up_progress(10) if conn.start_storage_pool(opts.name) is False: raise KssCommandException( 'Failed to start storage pool. (libvirt) - pool=%s' % (opts.name)) self.up_progress(40) inactive_storage_pools = conn.list_inactive_storage_pool() active_storage_pools = conn.list_active_storage_pool() if not (opts.name in active_storage_pools): raise KssCommandException( 'Could not start the storage pool. - pool=%s' % (opts.name)) self.logger.info('Start storage pool. - pool=%s' % (opts.name)) print >> sys.stdout, _('Start storage pool. - pool=%s') % ( opts.name) return True except KssCommandException, e: raise e finally: conn.close()
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) kvs = KaresansuiVirtSnapshot(readonly=False) try: self.up_progress(10) try: domain = kvs.whichDomain(opts.id) if len(domain) == 0: msg = _("Snapshot '%s' not found in domain '%s'.") % ( opts.id, opts.name, ) self.logger.error(msg) raise KssCommandException(msg) if not opts.name in domain: msg = _("Snapshot '%s' not found in domain '%s'.") % ( opts.id, opts.name, ) self.logger.error(msg) raise KssCommandException(msg) ret = kvs.revertSnapshot(opts.id) if ret is False: msg = _("Can't revert to snapshot '%s'.") % (opts.id, ) self.logger.error(msg) raise KssCommandException(msg) self.up_progress(50) msg = _("Domain snapshot '%s' reverted.") % (opts.id, ) self.logger.info(msg) print >> sys.stdout, msg except KssCommandException, e: raise KssCommandException(''.join(e.args)) except Exception, e: msg = _("Failed to revert to snapshot '%s'.") % (opts.id, ) msg += ": detail %s" % ''.join(str(e.args)) self.logger.error(msg) raise KssCommandException(msg)
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: forward = {"dev": opts.forward_dev, "mode": opts.forward_mode, } bridge = opts.bridge_name if opts.autostart == "yes": autostart = True else: autostart = False self.up_progress(20) try: conn.update_network(opts.name, opts.cidr, opts.dhcp_start, opts.dhcp_end, forward, bridge, autostart=autostart) except: self.logger.error('Failed to update network. - net=%s' % (opts.name)) raise # The network should be active now. # If not, we're going to start it up. active_networks = conn.list_active_network() if not (opts.name in active_networks): try: conn.start_network(opts.name) except KaresansuiVirtException, e: # try to bring down existing bridge kvn = conn.search_kvn_networks(opts.name)[0] try: bridge_name = kvn.get_info()['bridge']['name'] except KeyError: pass ret, res = execute_command([NETWORK_IFCONFIG_COMMAND, bridge_name, 'down']) ret, res = execute_command([NETWORK_BRCTL_COMMAND, 'delbr', bridge_name]) # try again conn.start_network(opts.name) self.up_progress(10) self.up_progress(10) active_networks = conn.list_active_network() if not (opts.name in active_networks): raise KssCommandException('Updated network but it\'s dead. - net=%s' % (opts.name)) self.logger.info('Updated network. - net=%s' % (opts.name)) print >>sys.stdout, _('Updated network. - net=%s') % (opts.name) return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: try: inactive_storage_pools = conn.list_inactive_storage_pool() active_storage_pools = conn.list_active_storage_pool() self.up_progress(10) if not (opts.name in active_storage_pools or \ opts.name in inactive_storage_pools): raise KssCommandException('Storage pool does not exist. - pool=%s' % (opts.name)) self.up_progress(30) if opts.name in conn.list_active_storage_pool(): if conn.destroy_storage_pool(opts.name) is False: KssCommandException("Failed to stop the storage pool. - pool=%s" % (opts.name)) else: raise KssCommandException('Storage pool is not active. - pool=%s' % (opts.name)) self.up_progress(30) # check if not (opts.name in conn.list_inactive_storage_pool()): KssCommandException("Failed to stop the storage pool. - pool=%s" % (opts.name)) self.logger.info('Stop storage pool. - pool=%s' % (opts.name)) print >>sys.stdout, _('Stop storage pool. - pool=%s') % (opts.name) return True except KssCommandException, e: raise e finally: conn.close()
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) network_restart_cmd = ( NETWORK_COMMAND, "restart", ) (rc, res) = execute_command(network_restart_cmd) if rc != 0: raise KssCommandException('Failure restart network.') return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) config = ServiceConfigParam(SERVICE_XML_FILE) config.load_xml_config() service = config.findby1service(opts.name) if not service: raise KssCommandException("Could not find the service - name=%s" % opts.name) self.up_progress(10) sysv = SysVInit_RH(service['system_name'], service['system_command']) if sysv.start(force=False) is False: raise KssCommandException(str(sysv.error_msg)) self.up_progress(50) self.logger.info('Started service. - service=%s' % (opts.name)) print >> sys.stdout, _('Started service. - service=%s') % (opts.name) return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) config = ServiceConfigParam(SERVICE_XML_FILE) config.load_xml_config() service = config.findby1service(opts.name) if service is None: raise KssCommandException( "Service not found in xml file. service=%s" % (opts.name)) self.up_progress(10) sysv = SysVInit_RH(service['system_name'], service['system_command']) flag = None if opts.enable: flag = True if opts.disable: flag = False retval = sysv.onboot(flag) if not (retval is False) and not (retval is True): raise KssCommandException(str(sysv.error_msg)) self.up_progress(50) if flag is True: message = 'Enable service. - service=%s' % (opts.name) else: message = 'Disable service. - service=%s' % (opts.name) self.logger.info(message) print >> sys.stdout, _(message) return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: inactive_storage_pools = conn.list_inactive_storage_pool() active_storage_pools = conn.list_active_storage_pool() self.up_progress(10) if not (opts.name in active_storage_pools or \ opts.name in inactive_storage_pools): raise KssCommandException( 'Storage pool does not exist. - pool=%s' % (opts.name)) try: self.up_progress(30) if opts.force is True and opts.name in conn.list_active_storage_pool( ): if conn.destroy_storage_pool(opts.name) is False: raise KssCommandException("Failed to stop the storage pool. - pool=%s" \ % (opts.name)) if opts.name in conn.list_active_storage_pool(): raise KssCommandException( "Could not delete storage pool: internal error storage pool is still active' pool=%s" \ % opts.name) if conn.delete_storage_pool(opts.name, False) is False: raise KssCommandException("Failed to remove the storage pool. - pool=%s" \ % (opts.name)) self.up_progress(30) # pool check inactive_storage_pools = conn.list_inactive_storage_pool() active_storage_pools = conn.list_active_storage_pool() if opts.name in active_storage_pools or \ opts.name in inactive_storage_pools: raise KssCommandException('Could not remove a storage pool. - pool=%s' \ % (opts.name)) # .xml path = "%s/%s.xml" % (VIRT_STORAGE_CONFIG_DIR, opts.name) if os.path.isfile(path) is True: raise KssCommandException( "Could not delete the configuration file. - pool=%s, path=%s" \ % (opts.name, path)) self.logger.info('Deleted storage pool. - pool=%s' % (opts.name)) print >> sys.stdout, _('Deleted storage pool. - pool=%s') % ( opts.name) return True except KssCommandException, e: raise e finally: conn.close()
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: try: self.up_progress(10) progresscb = None if opts.debug is True: 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 vol_obj = conn.get_storage_volume(opts.orig_pool, opts.orig_volume) if vol_obj is None: raise KssCommandException( 'Specified storage volume does not exist. - pool=%s, vol=%s' % (opts.orig_pool, opts.orig_volume)) inactive_storage_pools = conn.list_inactive_storage_pool() active_storage_pools = conn.list_active_storage_pool() if not (opts.dest_pool in active_storage_pools or \ opts.dest_pool in inactive_storage_pools): raise KssCommandException( 'Destination storage pool does not exist. - pool=%s' % (opts.dest_pool)) vol_info = vol_obj.info() if vol_info[0] != 0: raise KssCommandException( 'Specified storage volume does not "file" type. - pool=%s, vol=%s' % (opts.orig_pool, opts.orig_volume)) filesize = vol_info[1] / (1024 * 1024) # a unit 'MB' target_path = conn.get_storage_pool_targetpath(opts.dest_pool) if chk_create_disk(target_path, filesize) is False: raise KssCommandException( 'Destination storage pool shortage capacity. - pool=%s' % (opts.dest_pool)) if conn.replicate_storage_volume( opts.orig_name, opts.orig_pool, opts.orig_volume, opts.dest_name, opts.dest_pool, opts.dest_volume, progresscb) is False: raise KssCommandException( _("Failed to copy storage volume.")) self.up_progress(40) self.logger.info( 'Replicate storage volume. - orig_pool=%s, orig_vol=%s, dest_pool=%s' % (opts.orig_pool, opts.orig_volume, opts.dest_pool)) print >> sys.stdout, _( 'Replicate storage volume. - orig_pool=%s, orig_vol=%s, dest_pool=%s' % (opts.orig_pool, opts.orig_volume, opts.dest_pool)) return True except Exception, e: raise e finally: conn.close()
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) already_exist = False session_command_args = (ISCSI_CMD, ISCSI_CMD_OPTION_MODE, ISCSI_CMD_OPTION_MODE_SESSION ) (session_rc, session_res) = execute_command(session_command_args) if session_rc != 0: raise KssCommandException('Failed to get iSCSI session. message=%s' % (session_res)) for session_line in session_res: if not session_line: continue if session_line.find(ISCSI_CMD_RES_NO_ACTIVE_SESSION) != -1: break try: session = iscsi_parse_session(session_line) except: self.logger.warn('Failed to parse iSCSI session command response. message="%s"' % (session_line)) continue if session['iqn'] == opts.iqn: if opts.host: if opts.host != session['hostname']: continue already_exist = True break if already_exist: self.logger.info("[target: %s]: already exists" % (opts.iqn)) print >>sys.stdout, _("[target: %s]: already exists") % (opts.iqn) else: login_command_args = [ISCSI_CMD, ISCSI_CMD_OPTION_MODE, ISCSI_CMD_OPTION_MODE_NODE, ISCSI_CMD_OPTION_TARGETNAME, opts.iqn, ] if opts.host: login_command_args.append(ISCSI_CMD_OPTION_PORTAL) login_command_args.append(opts.host) login_command_args.append(ISCSI_CMD_OPTION_LOGIN) (login_rc,login_res) = execute_command(login_command_args) self.up_progress(50) if login_rc != 0: raise KssCommandException('Failed to login to iSCSI. - host=%s iqn=%s message=%s' % (opts.host, opts.iqn, login_res)) for line in login_res: if not line: continue self.logger.info("%s" % (line)) print >>sys.stdout, _("%s") % (line) return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) try: tmp_dir_name = generate_phrase(12, 'abcdefghijklmnopqrstuvwxyz') tmp_dir_path = "%s/%s" % (KARESANSUI_TMP_DIR, tmp_dir_name) os.mkdir(tmp_dir_path) except: raise KssCommandException('Failed to make tmpdir. path=%s' % (tmp_dir_path)) try: self.up_progress(10) mount_command_args = ( MOUNT_CMD, opts.dev, tmp_dir_path, ) umount_command_args = ( UMOUNT_CMD, tmp_dir_path, ) is_mountable = False try: (mount_cmd_rc, mount_cmd_res) = execute_command(mount_command_args) if mount_cmd_rc == 0: is_mountable = True else: self.logger.debug('Failed to mount. dev=%s' % (opts.dev)) finally: (umount_cmd_rc, umount_cmd_res) = execute_command(umount_command_args) self.up_progress(30) if is_mountable is False and opts.format is True: first_command_args = YES_CMD second_command_args = ( FORMAT_CMD, "-t", opts.type, opts.dev, ) format_command_args = ( first_command_args, second_command_args, ) (format_cmd_rc, format_cmd_res) = pipe_execute_command(format_command_args) if format_cmd_rc != 0: raise KssCommandException( 'Failed to format. dev=%s type=%s res=%s' % (opts.dev, opts.type, format_cmd_res)) try: (mount_cmd_rc, mount_cmd_res) = execute_command(mount_command_args) if mount_cmd_rc == 0: is_mountable = True else: self.logger.debug('Failed to mount. dev=%s' % (opts.dev)) finally: (umount_cmd_rc, umount_cmd_res) = execute_command(umount_command_args) self.up_progress(40) finally: try: os.rmdir(tmp_dir_path) except: raise KssCommandException('Failed to delete tmpdir. path=%s' % (tmp_dir_path)) if is_mountable is True: self.logger.info('Device "%s" is mountable.' % (opts.dev)) print >> sys.stdout, _('Device "%s" is mountable.' % (opts.dev)) else: self.logger.info('Device "%s" is not mountable.' % (opts.dev)) print >> sys.stdout, _('Device "%s" is not mountable.' % (opts.dev)) return is_mountable
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) exist_bond_list = get_ifconfig_info("regex:^bond") if opts.dev not in exist_bond_list: raise KssCommandOptException( 'Target bonding device not found. target=%s' % opts.dev) self.up_progress(10) dop = DictOp() ifcfg_parser = ifcfgParser() dop.addconf("ifcfg", ifcfg_parser.read_conf()) if dop.getconf("ifcfg") == {}: raise KssCommandException('Failure read network config file.') if dop.get("ifcfg", opts.dev) is False: raise KssCommandException('Target device ifcfg file not found.') self.up_progress(10) restore_dev_list = [] for dev in dop.getconf("ifcfg").keys(): if dop.get("ifcfg", [dev, "MASTER"]) == opts.dev: restore_dev_list.append(dev) self.up_progress(10) if opts.succession is True: bond_bridge = dop.get("ifcfg", [opts.dev, "BRIDGE"]) bond_dev = opts.dev if bond_bridge: bond_dev = bond_bridge ipaddr = dop.get("ifcfg", [bond_dev, "IPADDR"]) netmask = dop.get("ifcfg", [bond_dev, "NETMASK"]) gateway = dop.get("ifcfg", [bond_dev, "GATEWAY"]) bonding_opts = dop.get("ifcfg", [opts.dev, "BONDING_OPTS"]) bonding_opts = bonding_opts.strip('"') primary_dev = None for combination in bonding_opts.split(" "): if re.match("primary", combination): (key, val) = combination.split("=") val = val.strip() primary_dev = val self.up_progress(10) for restore_dev in restore_dev_list: if move_file( "%s/ifcfg-%s" % (VENDOR_DATA_BONDING_EVACUATION_DIR, restore_dev), NETWORK_IFCFG_DIR) is False: raise KssCommandException('Failure restore ifcfg file.') if os.path.isfile( "%s/ifcfg-p%s" % (VENDOR_DATA_BONDING_EVACUATION_DIR, restore_dev)): if move_file( "%s/ifcfg-p%s" % (VENDOR_DATA_BONDING_EVACUATION_DIR, restore_dev), NETWORK_IFCFG_DIR) is False: raise KssCommandException('Failure restore ifcfg file.') self.up_progress(10) if opts.succession is True and primary_dev is not None: dop = DictOp() ifcfg_parser = ifcfgParser() dop.addconf("ifcfg", ifcfg_parser.read_conf()) if dop.getconf("ifcfg") == {}: raise KssCommandException('Failure read network config file.') if ipaddr: dop.set("ifcfg", [primary_dev, "IPADDR"], ipaddr) if netmask: dop.set("ifcfg", [primary_dev, "NETMASK"], netmask) if gateway: dop.set("ifcfg", [primary_dev, "GATEWAY"], gateway) if ifcfg_parser.write_conf(dop.getconf("ifcfg")) is False: raise KssCommandException('Failure write network config file.') self.up_progress(10) remove_file("%s/ifcfg-%s" % (NETWORK_IFCFG_DIR, opts.dev)) self.up_progress(10) dop = DictOp() modprobe_parser = modprobe_confParser() dop.addconf("modprobe_conf", modprobe_parser.read_conf()) if dop.getconf("modprobe_conf") == {}: raise KssCommandException('Failure read modprobe config file.') dop.unset("modprobe_conf", ["alias", opts.dev]) if modprobe_parser.write_conf(dop.getconf("modprobe_conf")) is False: raise KssCommandException('Failure write modprobe config file.') self.up_progress(10) # # Delete bridge device # bridge_list = get_bridge_info() bond_bridge = None for bridge in bridge_list: if opts.dev in bridge_list[bridge]: bond_bridge = bridge if bond_bridge: ifdown_cmd = ( NETWORK_IFDOWN_COMMAND, bond_bridge, ) (ifdown_rc, ifdown_res) = execute_command(ifdown_cmd) if ifdown_rc != 0: raise KssCommandException( 'Failure stop interface. interface:%s' % (dev)) for brif in bridge_list[bond_bridge]: brctl_delif_cmd = ( NETWORK_BRCTL_COMMAND, "delif", bond_bridge, brif, ) (brctl_rc, brctl_res) = execute_command(brctl_delif_cmd) if brctl_rc != 0: raise KssCommandException( 'Failure delete bridge port. bridge:%s port:%s' % (dev, brif)) brctl_delbr_cmd = ( NETWORK_BRCTL_COMMAND, "delbr", bond_bridge, ) (brctl_rc, brctl_res) = execute_command(brctl_delbr_cmd) if brctl_rc != 0: raise KssCommandException('Failure delete bridge. bridge:%s' % (dev, brif)) remove_file("%s/ifcfg-%s" % (NETWORK_IFCFG_DIR, bond_bridge)) # # Unload bonding module # remove_bonding_cmd = ( SYSTEM_COMMAND_REMOVE_MODULE, "bonding", ) (rmmod_rc, rmmod_res) = execute_command(remove_bonding_cmd) if rmmod_rc != 0: raise KssCommandException('Failure remove bonding module.') # # Restart network # network_restart_cmd = ( NETWORK_COMMAND, "restart", ) (net_rc, net_res) = execute_command(network_restart_cmd) if net_rc != 0: raise KssCommandException('Failure restart network.') self.logger.info("Deleted bonding device. - bond=%s dev=%s" % (opts.dev, ','.join(restore_dev_list))) print >> sys.stdout, _("Deleted bonding device. - bond=%s dev=%s" % (opts.dev, ','.join(restore_dev_list))) return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) node_command_args = (ISCSI_CMD, ISCSI_CMD_OPTION_MODE, ISCSI_CMD_OPTION_MODE_NODE) (node_rc, node_res) = execute_command(node_command_args) if node_rc != 0: for node_line in node_res: if node_line.lower().find(ISCSI_CMD_RES_NO_NODE) != -1: self.logger.info("iSCSI node not found") return True raise KssCommandException('Failed to get iSCSI node. message=%s' % (node_res)) self.up_progress(20) session_command_args = (ISCSI_CMD, ISCSI_CMD_OPTION_MODE, ISCSI_CMD_OPTION_MODE_SESSION) (session_rc, session_res) = execute_command(session_command_args) if session_rc != 0: raise KssCommandException( 'Failed to get iSCSI session. message=%s' % (session_res)) self.up_progress(20) for node_line in node_res: if not node_line: continue try: node = iscsi_parse_node(node_line) except: self.logger.warn( 'Failed to parse iSCSI node command response. message="%s"' % (node_line)) continue is_active = 0 for session_line in session_res: if not session_line: continue if session_line.find(ISCSI_CMD_RES_NO_ACTIVE_SESSION) != -1: break try: session = iscsi_parse_session(session_line) except: self.logger.warn( 'Failed to parse iSCSI session command response. message="%s"' % (session_line)) continue if iscsi_check_node_status(node, session): is_active = 1 break if iscsi_check_node_autostart(node): autostart = 0 else: autostart = 1 if opts.iqn is None: self.logger.info( "%s %s %s" % (iscsi_print_format_node(node), is_active, autostart)) print >> sys.stdout, _("%s %s %s") % ( iscsi_print_format_node(node), is_active, autostart) else: if opts.iqn == node['iqn']: auth = iscsi_get_auth_type(node) user = iscsi_get_auth_user(node) self.logger.info("%s %s %s %s %s" % (iscsi_print_format_node(node), is_active, autostart, auth, user)) print >> sys.stdout, _("%s %s %s %s %s") % ( iscsi_print_format_node(node), is_active, autostart, auth, user) break return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) config_path = iscsi_get_config_path(opts.host, opts.iqn, opts.port, opts.tpgt) parser = iscsidParser() dop = DictOp() dop.addconf("new", parser.read_conf(config_path)) self.up_progress(10) dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_METHOD, opts.auth) if opts.auth == ISCSI_CONFIG_VALUE_AUTH_METHOD_CHAP: password = "" if opts.password is not None: password = opts.password elif opts.password_file is not None and is_readable( opts.password_file): try: fp = open(opts.password_file, "r") try: fcntl.lockf(fp.fileno(), fcntl.LOCK_SH) try: password = fp.readline().strip("\n") finally: fcntl.lockf(fp.fileno(), fcntl.LOCK_UN) self.up_progress(10) finally: fp.close() except: raise KssCommandException('Failed to read file. - target host=%s password_file=%s' \ % (opts.host,opts.password_file)) try: os.remove(opts.password_file) except: raise KssCommandException('Failed to remove file. - target host=%s password_file=%s' \ % (opts.host,opts.password_file)) dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_USER, opts.user) dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_PASSWORD, password) else: dop.comment("new", ISCSI_CONFIG_KEY_AUTH_USER) dop.comment("new", ISCSI_CONFIG_KEY_AUTH_PASSWORD) self.up_progress(10) if opts.autostart: dop.cdp_set("new", ISCSI_CONFIG_KEY_SATRTUP, ISCSI_CONFIG_VALUE_SATRTUP_ON) else: dop.cdp_set("new", ISCSI_CONFIG_KEY_SATRTUP, ISCSI_CONFIG_VALUE_SATRTUP_OFF) self.up_progress(10) parser.write_conf(dop.getconf("new"), config_path) self.up_progress(30) self.logger.info("Updated iSCSI node. - host=%s iqn=%s" % (opts.host, opts.iqn)) print >> sys.stdout, _("Updated iSCSI node. - host=%s iqn=%s") % ( opts.host, opts.iqn) return True
self.up_progress(40) except Exception, e: self.logger.error('Failed to destroy guest. - dom=%s' % (opts.name)) print >> sys.stderr, _( 'Failed to destroy guest. - dom=%s') % (opts.name) raise e status = conn.guest.status() self.up_progress(10) if status == VIR_DOMAIN_SHUTOFF or status == VIR_DOMAIN_SHUTDOWN: self.logger.info('Succeeded to destroy guest. - dom=%s' % (opts.name)) print >> sys.stdout, _( 'Succeeded to destroy guest. - dom=%s') % (opts.name) else: raise KssCommandException('Guest not found. - dom=%s' % (opts.name)) return True finally: conn.close() if __name__ == "__main__": target = DestroyGuest() sys.exit(target.run())
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: conn.set_interface_format(opts.interface_format) active_guests = conn.list_active_guest() inactive_guests = conn.list_inactive_guest() if opts.name in active_guests or opts.name in inactive_guests: raise KssCommandException('Guest already exists. - dom=%s' % (opts.name)) self.up_progress(10) inactive_storage_pools = conn.list_inactive_storage_pool() active_storage_pools = conn.list_active_storage_pool() if not (opts.storage_pool in active_storage_pools or opts.storage_pool in inactive_storage_pools): raise KssCommandException( 'Storage pool does not exist. - pool=%s' % (opts.storage_pool)) # TODO #if conn.get_storage_volume(opts.storage_pool, opts.uuid) is None: # raise KssCommandException('Specified storage volume does not exist. - pool=%s, vol=%s' # % (opts.storage_pool, opts.uuid)) try: self.up_progress(10) if not conn.create_guest( name=opts.name, type=opts.type.lower(), ram=opts.mem_size, disk=opts.disk, disksize=opts.disk_size, mac=opts.mac, uuid=opts.uuid, kernel=opts.kernel, initrd=opts.initrd, iso=opts.iso, graphics=opts.graphics_port, vcpus=opts.vcpus, extra=opts.extra, keymap=opts.keymap, bus=opts.bus, disk_format=opts.disk_format, storage_pool=opts.storage_pool, storage_volume=opts.storage_volume, ) is True: raise KssCommandException( 'Failed to create guest. - dom=%s' % (opts.name)) except Exception, e: self.logger.error( 'Failed to create guest. - dom=%s - detail %s' % (opts.name, str(e.args))) print >> sys.stderr, _( 'Failed to create guest. - dom=%s - detail %s') % ( opts.name, str(e.args)) raise e self.up_progress(40) active_guests = conn.list_active_guest() inactive_guests = conn.list_inactive_guest() if not (opts.name in active_guests or opts.name in inactive_guests): raise KssCommandException( 'Guest OS is not recognized. - dom=%s' % (opts.name)) self.logger.info('Created guest. - dom=%s' % (opts.name)) print >> sys.stdout, 'Created guest. - dom=%s' % opts.name return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: inactive_storage_pools = conn.list_inactive_storage_pool() active_storage_pools = conn.list_active_storage_pool() self.up_progress(10) if opts.name in active_storage_pools or opts.name in inactive_storage_pools: raise KssCommandException( 'Storage pool already exists. - pool=%s' % opts.name) try: if opts.type == STORAGE_POOL_TYPE["TYPE_DIR"]: if conn.create_storage_pool( opts.name, opts.type, opts.target_path, #allocation=opts.allocation, available=opts.available, #capacity=opts.capacity, target_p_group=opts.group, target_p_label=opts.label, target_p_mode=opts.mode, target_p_owner=opts.owner, ) is False: raise KssCommandException('Failed to create storage pools. - pool=%s' \ % opts.name) elif opts.type == STORAGE_POOL_TYPE["TYPE_ISCSI"]: if conn.create_storage_pool( opts.name, opts.type, target_path="/dev/disk/by-path", source_h_name=opts.host_name, source_dev_path=opts.device_path, #allocation=opts.allocation, available=opts.available, #capacity=opts.capacity, target_p_group=opts.group, target_p_label=opts.label, target_p_mode=opts.mode, target_p_owner=opts.owner, ) is False: raise KssCommandException('Failed to create storage pools. - pool=%s' \ % opts.name) elif opts.type == STORAGE_POOL_TYPE["TYPE_FS"]: if conn.create_storage_pool( opts.name, opts.type, opts.target_path, source_dev_path=opts.device_path, #allocation=opts.allocation, available=opts.available, #capacity=opts.capacity, target_p_group=opts.group, target_p_label=opts.label, target_p_mode=opts.mode, target_p_owner=opts.owner, ) is False: raise KssCommandException('Failed to create storage pools. - pool=%s' \ % opts.name) else: raise KssCommandOptException('ERROR: The type that does not exist. type=%s' \ % opts.type) self.up_progress(20) # pool check inactive_storage_pools = conn.list_inactive_storage_pool() active_storage_pools = conn.list_active_storage_pool() if not (opts.name in active_storage_pools or opts.name in inactive_storage_pools): raise KssCommandException( 'Failed to create storage pools. (Unexplained) - pool=%s' \ % opts.name) self.up_progress(20) # pool autostart check flag = True # autostart on if conn.autostart_storage_pool(flag) is False: raise KssCommandException( 'Failed to autostart storage pool(libvirt). - pool=%s' % (opts.name)) ret = conn.is_autostart_storage_pool() if not (ret is flag): raise KssCommandException( 'Auto-start failed to set the storage pool. - pool=%s, autostart=%s' \ % (opts.name, str(ret))) self.logger.info('Created storage pool. - pool=%s' % (opts.name)) print >> sys.stdout, _('Created storage pool. - pool=%s') % ( opts.name) return True except KssCommandException, e: raise e finally: conn.close()
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: conn.set_domain_name(opts.name) self.up_progress(10) active_storage_pools = conn.list_active_storage_pool() if opts.pool not in active_storage_pools: raise KssCommandException('Storage pool does not exist. - pool=%s' % opts.name) if not conn.get_storage_volume(opts.pool, opts.volume): raise KssCommandException('Storage volume does not exist. - pool=%s, volume=%s' % (opts.name, opts.volume)) self.up_progress(10) if opts.type.lower() == 'iscsi': real_volume_path = conn.get_storage_volume_iscsi_rpath_bystorage(opts.pool, opts.volume) if not real_volume_path: raise KssCommandException('Failure get iSCSI volume real path. - pool=%s, volume=%s' % (opts.name, opts.volume)) format = None disk_type = 'block' elif opts.type.lower() == 'file': real_volume_path = "%s/%s/%s/%s.img" % \ (conn.get_storage_pool_targetpath(opts.pool), opts.name, DISK_USES["DISK"], opts.volume) format = opts.format disk_type = 'file' else: raise KssCommandException('Unknown Storage Type. type=%s' % opts.type) if opts.target: target = opts.target else: target = conn.guest.next_disk_target(opts.bus) self.up_progress(10) already_disks = conn.guest.get_disk_info() for already_disk in already_disks: if already_disk['type'] == 'file': already_path = already_disk['source']['file'] elif already_disk['type'] == 'block': already_path = already_disk['source']['dev'] else: already_path = '' if already_path == real_volume_path: raise KssCommandException('Source disk is already used. path=%s' % real_volume_path) if opts.format is None: format = get_disk_img_info(real_volume_path)['file_format'] conn.guest.append_disk(real_volume_path, target, bus=opts.bus, disk_type=disk_type, driver_name=None, driver_type=format, ) self.up_progress(30) finally: conn.close() self.logger.info('Added disk device. - dom=%s target=%s path=%s' \ % (opts.name, target, real_volume_path)) print >>sys.stdout, 'Added disk device. - dom=%s target=%s path=%s' \ % (opts.name, target, real_volume_path) return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: 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 try: #inactive_pool = conn.list_inactive_storage_pool() inactive_pool = [] active_pool = conn.list_active_storage_pool() pools = inactive_pool + active_pool if not pools: raise KssCommandException("Storage pool does not exist, or has been stopped.") pools.sort() export = [] for pool_name in pools: pool = conn.search_kvn_storage_pools(pool_name) path = pool[0].get_info()["target"]["path"] if os.path.exists(path): for _afile in glob.glob("%s/*/info.dat" % (path,)): e_param = ExportConfigParam() e_param.load_xml_config(_afile) if e_param.get_uuid() != opts.exportuuid: continue export.append({"dir" : os.path.dirname(_afile), "uuid" : opts.exportuuid, }) if len(export) != 1: raise KssCommandException("There are differences in the export data and real data. - uuid=%s" % opts.exportuuid) else: export = export[0] if os.path.isdir(export["dir"]) is False: raise KssCommandException("There is no real data. - dir=%s" % export["dir"]) conn.import_guest(export["dir"], uuid=opts.destuuid, progresscb=progresscb) self.up_progress(40) self.logger.info('Import guest completed. - export=%s, dest=%s' % (opts.exportuuid, opts.destuuid)) print >>sys.stdout, _('Import guest completed. - export=%s, dest=%s' % (opts.exportuuid, opts.destuuid)) return True except KaresansuiVirtException, e: raise KssCommandException('Failed to import guest. - [%s]' \ % (''.join(str(e.args)))) finally: conn.close()
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) conn = KaresansuiVirtConnection(readonly=False) try: # dest storage pool dest_target_path = conn.get_storage_pool_targetpath(opts.pool) if not dest_target_path: raise KssCommandException( "Could not get the target path of the storage pool. - path=%s" % dest_target_path) self.dest_disk = "%s/%s/images/%s.img" \ % (dest_target_path, opts.name,opts.name,) # source storage pool src_pool = conn.get_storage_pool_name_bydomain(opts.src_name, "os") if not src_pool: raise KssCommandException("Source storage pool is not found.") src_pool_type = conn.get_storage_pool_type(src_pool) if src_pool_type == 'dir': raise KssCommandException( "Storage pool type 'dir' is not. - type=%s" % src_pool_type) src_target_path = conn.get_storage_pool_targetpath(src_pool[0]) self.src_disk = "%s/%s/images/%s.img" \ % (src_target_path, opts.src_name,opts.src_name,) if os.path.isfile(self.src_disk) is False: raise KssCommandException( 'source disk image is not found. - src=%s' % (self.src_disk)) if os.path.isfile(self.dest_disk) is True: raise KssCommandException( 'destination disk image already exists. - dest=%s' % (self.dest_disk)) self.up_progress(10) active_storage_pools = conn.list_active_storage_pool() self.up_progress(10) if not (opts.pool in active_storage_pools): raise KssCommandException( 'Storage pool does not exist. - pool=%s' % (opts.pool)) try: active_guests = conn.list_active_guest() inactive_guests = conn.list_inactive_guest() # source guestos if not (opts.src_name in active_guests or opts.src_name in inactive_guests): raise KssCommandException( "Unable to get the source guest OS. - src_name=%s" % opts.src_name) if (opts.name in active_guests or opts.name in inactive_guests): raise KssCommandException( "Destination Guest OS is already there. - dest_name=%s" % opts.name) self.up_progress(10) conn.replicate_guest(opts.name, opts.src_name, opts.pool, opts.mac, opts.uuid, opts.graphics_port) self.up_progress(40) except: self.logger.error( 'Failed to replicate guest. - src=%s dom=%s' % (opts.src_name, opts.name)) raise finally: conn.close() conn1 = KaresansuiVirtConnection(readonly=False) try: self.up_progress(10) active_guests = conn1.list_active_guest() inactive_guests = conn1.list_inactive_guest() if opts.name in active_guests or opts.name in inactive_guests: self.logger.info('Replicated guest. - src=%s dom=%s' % (opts.src_name, opts.name)) print >> sys.stdout, _('Replicated guest. - src=%s dom=%s') % ( opts.src_name, opts.name) return True else: raise KssCommandException( 'Replicate guest not found. - src=%s dom=%s' % (opts.src_name, opts.name)) finally: conn1.close() return True