def _GET(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() b64name = param[1] if not (b64name and host_id): return web.badrequest() name = base64_decode(str(b64name)) (target, device) = name.split("@") net = NetworkAddress(target) ipaddr = net.ipaddr netmask = net.netmask netlen = net.netlen gateway = _('N/A') flags = _('N/A') ref = _('N/A') use = _('N/A') metric = _('N/A') parser = Parser() status = parser.do_status() for _k,_v in status.iteritems(): for _k2,_v2 in _v.iteritems(): if name == "%s@%s" % (_k2,_k,): gateway = _v2['gateway'] flags = _v2['flags'] ref = _v2['ref'] use = _v2['use'] metric = _v2['metric'] route = dict(name=name, ipaddr=ipaddr, netmask=netmask, netlen=netlen, device=device, gateway=gateway, flags=flags, ref=ref, use=use, metric=metric, ) self.view.route = route return True
def _DELETE(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() b64name = param[1] if not (b64name and host_id): return web.badrequest() host = findbyhost1(self.orm, host_id) name = base64_decode(str(b64name)) (target, device) = name.split("@") net = NetworkAddress(target) ipaddr = net.ipaddr netmask = net.netmask netlen = net.netlen target = "%s/%s" % (ipaddr,netlen,) modules = ["staticroute"] dop = read_conf(modules, self, host) if dop is False: return web.internalerror('Internal Server Error. (Timeout)') dop.delete("staticroute", [device,target]) from karesansui.lib.parser.staticroute import PARSER_COMMAND_ROUTE if net.netlen == 32: command = "%s del -host %s dev %s" % (PARSER_COMMAND_ROUTE,ipaddr,device,) else: command = "%s del -net %s netmask %s dev %s" % (PARSER_COMMAND_ROUTE,ipaddr,netmask,device,) extra_args = {"post-command": command} retval = write_conf(dop, self, host, extra_args=extra_args) if retval is False: return web.internalerror('Internal Server Error. (Adding Task)') return web.accepted()
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(1) uniq_id = time.strftime("%Y%m%d%H%M%S", time.localtime()) if opts.pre_command is not None: if opts.pre_command[0:4] == "b64:": command = base64_decode(opts.pre_command[4:]) else: command = opts.pre_command self.logger.info("execute command - %s" % command) (_ret,_res) = execute_command(command.split()) if _ret != 0: error_msg = "execute error - %s" % command self.logger.error(error_msg) #raise KssCommandOptException("ERROR: %s" % error_msg) dop = DictOp() modules = opts.module.split(":") files = opts.file.split(":") source_files = [] retval = True cnt = 0 for _mod in modules: _file = files[cnt] try: exec("from karesansui.lib.parser.%s import %sParser as Parser" % (_mod,_mod,)) self.up_progress(5) parser = Parser() # 辞書オペレータに追加 self.up_progress(5) if opts.php is True: conf_arr = php_array_to_python_dict(open(_file).read()) else: exec("conf_arr = %s" % open(_file).read()) dop.addconf(_mod,conf_arr) """ 必要ならここで配列操作 通常は、配列操作後の辞書が_fileに書き込まれているので必要ない dop.add (_mod,"foo","bar") dop.delete(_mod,"foo") """ # 設定ファイル一覧に作成(バックアップ用) self.up_progress(5) source_file = parser.source_file() for _afile in source_file: _bak_afile = "%s.%s" % (_afile,uniq_id) copy_file(_afile,_bak_afile) source_files = source_files + source_file # 辞書に戻す self.up_progress(5) conf_arr = dop.getconf(_mod) #dop.preprint_r(_mod) # 設定ファイルに書き込み self.up_progress(5) extra_args = {} extra_args["include"] = opts.include if opts.early_exit is True: retval = retval and parser.write_conf(conf_arr,extra_args=extra_args) else: retval = parser.write_conf(conf_arr,extra_args=extra_args) and retval if opts.delete is True: os.unlink(_file) finally: cnt = cnt + 1 if retval is False: for _afile in source_files: _bak_afile = "%s.%s" % (_afile,uniq_id) os.unlink(_afile) copy_file(_bak_afile,_afile) os.unlink(_bak_afile) raise KssCommandOptException("ERROR: write configure failure") for _afile in source_files: _bak_afile = "%s.%s" % (_afile,uniq_id) os.unlink(_bak_afile) if opts.post_command is not None: if opts.post_command[0:4] == "b64:": command = base64_decode(opts.post_command[4:]) else: command = opts.post_command self.logger.info("execute command - %s" % command) (_ret,_res) = execute_command(command.split()) if _ret != 0: error_msg = "execute error - %s" % command self.logger.error(error_msg) raise KssCommandOptException("ERROR: %s" % error_msg) self.up_progress(10) return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(1) uniq_id = time.strftime("%Y%m%d%H%M%S", time.localtime()) if opts.pre_command is not None: if opts.pre_command[0:4] == "b64:": command = base64_decode(opts.pre_command[4:]) else: command = opts.pre_command self.logger.info("execute command - %s" % command) if opts.dry_run is True: print "" print ">>>Execute pre command: %s" % command print "" else: (_ret,_res) = execute_command(command.split()) if _ret != 0: error_msg = "execute error - %s" % command self.logger.error(error_msg) #raise KssCommandOptException("ERROR: %s" % error_msg) self.up_progress(5) from karesansui.lib.parser.collectd import collectdParser from karesansui.lib.parser.collectdplugin import collectdpluginParser dop = DictOp() collectd_parser = collectdParser() dop.addconf("collectd",collectd_parser.read_conf()) collectdplugin_parser = collectdpluginParser() extra_args = {"include":"^(%s)$" % "|".join(COLLECTD_PLUGINS)} dop.addconf("collectdplugin",collectdplugin_parser.read_conf(extra_args=extra_args)) initialize_collectd_settings(dop=dop,force=opts.force,reverse=opts.reverse) retval = collectd_parser.write_conf(dop.getconf("collectd"),dryrun=opts.dry_run) retval = collectdplugin_parser.write_conf(dop.getconf("collectdplugin"),extra_args=extra_args,dryrun=opts.dry_run) self.up_progress(30) if opts.post_command is not None: if opts.post_command[0:4] == "b64:": command = base64_decode(opts.post_command[4:]) else: command = opts.post_command self.logger.info("execute command - %s" % command) if opts.dry_run is True: print "" print ">>>Execute post command: %s" % command print "" else: (_ret,_res) = execute_command(command.split()) if _ret != 0: error_msg = "execute error - %s" % command self.logger.error(error_msg) raise KssCommandOptException("ERROR: %s" % error_msg) self.up_progress(10) return True
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))