def _GET(self, *param, **params): _prep_console() (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() dom = kvc.search_guests(domname)[0] document = XMLParse(dom.XMLDesc(1)) self.view.graphics_port = XMLXpath(document, '/domain/devices/graphics/@port') self.view.xenname = XMLXpath(document, '/domain/name/text()') finally: kvc.close() h_model = findbyhost1(self.orm, host_id) try: from karesansui.lib.utils import get_ifconfig_info device = KVM_BRIDGE_PREFIX + "0" self.view.host_ipaddr = get_ifconfig_info(device)[device]["ipaddr"] except: try: self.view.host_ipaddr = h_model.hostname.split(":")[0].strip() except: self.view.host_ipaddr = socket.gethostbyname(socket.gethostname()) return True
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = kvc.search_kvg_guests(domname)[0] info = virt.get_graphics_info() self.view.checked_listen_all = "" self.view.checked_listen_lo = "checked" try: if info["setting"]["listen"] == "0.0.0.0": self.view.checked_listen_all = "checked" self.view.checked_listen_lo = "" except: pass self.view.VMType = virt.get_info()['VMType'] self.view.keymaps = eval("get_keymaps(%s_KEYMAP_DIR)" % self.view.VMType.upper()) self.view.info = info self.view.guest = model finally: kvc.close() return True
def _DELETE(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if is_int(param[2]) is False: return web.notfound() nic_id = int(param[2]) model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) nic_info = virt.get_interface_info()[nic_id] finally: kvc.close() mac = nic_info["mac"]["address"] self.logger.debug('spinning off delete_nic_job dom=%s, mac=%s' % (domname, mac)) if delete_nic_job(self,model,domname,mac) is True: return web.accepted() else: return False
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = kvc.search_kvg_guests(domname)[0] vcpus_info = virt.get_vcpus_info() self.view.max_vcpus_limit = kvc.get_max_vcpus() self.view.max_vcpus = vcpus_info['bootup_vcpus'] self.view.vcpus_limit = vcpus_info['max_vcpus'] self.view.vcpus = vcpus_info['vcpus'] self.view.cpuTime = virt.get_info()['cpuTime'] self.view.hypervisor = virt.get_info()['hypervisor'] self.view.guest = model finally: kvc.close() return True
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if is_int(param[2]) is False: return web.badrequest() disk_id = int(param[2]) model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) self.view.guest = guest self.view.disk_info = virt.get_disk_info()[disk_id] finally: kvc.close() return True
def _PUT(self, *param, **params): """<comment-ja> Japanese Comment </comment-ja> <comment-en> TODO: English Comment </comment-en> """ (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if is_param(self.input, "memory"): memory = int(self.input.memory) else: memory = None max_memory = int(self.input.max_memory) model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] info = virt.get_info() # maxMem = info["maxMem"] now_memory = info["memory"] mem_info = kvc.get_mem_info() nodeinfo = kvc.get_nodeinfo() finally: kvc.close() # valid # if (mem_info["host_free_mem"] + (now_memory / 1024)) < memory: # return web.badrequest("Memory value is greater than the maximum memory value. - memory=%s" % self.input.memory) options = {} options["name"] = domname options["maxmem"] = max_memory if memory is None: options["memory"] = max_memory else: options["memory"] = memory _cmd = dict2command("%s/%s" % (karesansui.config["application.bin.dir"], VIRT_COMMAND_SET_MEMORY), options) cmdname = "Set memory" _jobgroup = JobGroup(cmdname, karesansui.sheconf["env.uniqkey"]) _jobgroup.jobs.append(Job("%s command" % cmdname, 0, _cmd)) _machine2jobgroup = m2j_new( machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf["env.uniqkey"], created_user=self.me, modified_user=self.me, ) save_job_collaboration(self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup) return web.accepted(url=web.ctx.path)
def _POST(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if not validates_snapshot(self): return web.badrequest(self.view.alert) guest = findbyguest1(self.orm, guest_id) kvs = KaresansuiVirtSnapshot(readonly=False) try: domname = kvs.kvc.uuid_to_domname(guest.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvs.kvc.search_kvg_guests(domname)[0] if virt.is_active() is True: return web.badrequest( _("Guest is running. Please stop and try again. name=%s" % domname)) finally: kvs.finish() id = int(time.time()) notebook = new_notebook(self.input.title, self.input.value) snapshot = new_snapshot(guest, id, self.me, self.me, notebook) save_snapshot(self.orm, snapshot) options = {} options['name'] = domname options['id'] = id _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_TAKE_SNAPSHOT), options) cmdname = 'Take Snapshot' _jobgroup = JobGroup(cmdname, karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('%s command' % cmdname, 0, _cmd)) _machine2jobgroup = m2j_new( machine=guest, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) save_job_collaboration( self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) return web.accepted()
def _POST(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if not validates_snapshot(self): return web.badrequest(self.view.alert) guest = findbyguest1(self.orm, guest_id) kvs = KaresansuiVirtSnapshot(readonly=False) try: domname = kvs.kvc.uuid_to_domname(guest.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvs.kvc.search_kvg_guests(domname)[0] if virt.is_active() is True: return web.badrequest(_("Guest is running. Please stop and try again. name=%s" % domname)) finally: kvs.finish() id = int(time.time()) notebook = new_notebook(self.input.title, self.input.value) snapshot = new_snapshot(guest, id, self.me, self.me, notebook) save_snapshot(self.orm, snapshot) options = {} options['name'] = domname options['id'] = id _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_TAKE_SNAPSHOT), options) cmdname = 'Take Snapshot' _jobgroup = JobGroup(cmdname, karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('%s command' % cmdname, 0, _cmd)) _machine2jobgroup = m2j_new(machine=guest, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) save_job_collaboration(self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) return web.accepted()
def _PUT(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if is_int(param[2]) is False: return web.notfound() nic_id = int(param[2]) if not validates_nicby1(self): return web.badrequest(self.view.alert) model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) old_mac = virt.get_interface_info()[nic_id]["mac"]["address"] nic_info = virt.get_interface_info() finally: kvc.close() new_mac = self.input.mac_address if old_mac != new_mac: f_chk = True for x in nic_info: if x["mac"]["address"] == new_mac: f_chk = False break if f_chk is False: return web.badrequest(_("Specified MAC address is already defined.")) self.logger.debug( "spinning off change_mac_job dom=%s, from_mac=%s, to_mac=%s" % (domname, old_mac, new_mac) ) if change_mac_job(self, model, domname, old_mac, new_mac) is True: return web.accepted(url=web.ctx.path) else: return False else: return web.accepted(url=web.ctx.path)
def _PUT(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if is_int(param[2]) is False: return web.notfound() nic_id = int(param[2]) if not validates_nicby1(self): return web.badrequest(self.view.alert) model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) old_mac = virt.get_interface_info()[nic_id]['mac']['address'] nic_info = virt.get_interface_info() finally: kvc.close() new_mac = self.input.mac_address if old_mac != new_mac: f_chk = True for x in nic_info: if x['mac']['address'] == new_mac: f_chk = False break if f_chk is False: return web.badrequest(_('Specified MAC address is already defined.')) self.logger.debug('spinning off change_mac_job dom=%s, from_mac=%s, to_mac=%s' % (domname, old_mac, new_mac)) if change_mac_job(self, model, domname, old_mac, new_mac) is True: return web.accepted(url=web.ctx.path) else: return False else: return web.accepted(url=web.ctx.path)
def _PUT(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if not validates_guest_edit(self): self.logger.debug( "Update Guest OS is failed, Invalid input value.") return web.badrequest(self.view.alert) guest = findbyguest1(self.orm, guest_id) # notebook if is_param(self.input, "note_title"): guest.notebook.title = self.input.note_title if is_param(self.input, "note_value"): guest.notebook.value = self.input.note_value if is_param(self.input, "m_name"): guest.name = self.input.m_name # Icon icon_filename = None if is_param(self.input, "icon_filename", empty=True): guest.icon = self.input.icon_filename # tag UPDATE if is_param(self.input, "tags"): _tags = [] tag_array = comma_split(self.input.tags) tag_array = uniq_sort(tag_array) for x in tag_array: if t_count(self.orm, x) == 0: _tags.append(t_new(x)) else: _tags.append(t_name(self.orm, x)) guest.tags = _tags guest.modified_user = self.me m_update(self.orm, guest) return web.seeother(web.ctx.path)
def _PUT(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if not validates_guest_edit(self): self.logger.debug("Update Guest OS is failed, Invalid input value.") return web.badrequest(self.view.alert) guest = findbyguest1(self.orm, guest_id) # notebook if is_param(self.input, "note_title"): guest.notebook.title = self.input.note_title if is_param(self.input, "note_value"): guest.notebook.value = self.input.note_value if is_param(self.input, "m_name"): guest.name = self.input.m_name # Icon icon_filename = None if is_param(self.input, "icon_filename", empty=True): guest.icon = self.input.icon_filename # tag UPDATE if is_param(self.input, "tags"): _tags = [] tag_array = comma_split(self.input.tags) tag_array = uniq_sort(tag_array) for x in tag_array: if t_count(self.orm, x) == 0: _tags.append(t_new(x)) else: _tags.append(t_name(self.orm, x)) guest.tags = _tags guest.modified_user = self.me m_update(self.orm, guest) return web.seeother(web.ctx.path)
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = kvc.search_kvg_guests(domname)[0] info = virt.get_graphics_info() self.view.checked_listen_all = "" self.view.checked_listen_lo = "checked" try: if info["setting"]["listen"] == "0.0.0.0": self.view.checked_listen_all = "checked" self.view.checked_listen_lo = "" except: pass self.view.VMType = virt.get_info()['VMType'] self.view.keymaps = eval("get_keymaps(%s_KEYMAP_DIR)" % self.view.VMType.upper()) self.view.info = info self.view.guest = model self.view.checked_graphics_type = {} self.view.graphics_type = ENABLE_GRAPHICS_TYPE for _type in self.view.graphics_type: self.view.checked_graphics_type[_type] = '' if info["info"]["type"] == _type: self.view.checked_graphics_type[_type] = 'checked' finally: kvc.close() return True
def _GET(self, *param, **params): java_dir = karesansui.dirname + '/static/java' self.view.applet_dst_path = java_dir + '/VncViewer.jar' self.view.applet_src_path = _get_applet_source_path() self.view.found_applet_located = os.path.exists( self.view.applet_dst_path) (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() dom = kvc.search_guests(domname)[0] document = XMLParse(dom.XMLDesc(1)) self.view.graphics_port = XMLXpath( document, '/domain/devices/graphics/@port') self.view.xenname = XMLXpath(document, '/domain/name/text()') finally: kvc.close() h_model = findbyhost1(self.orm, host_id) try: from karesansui.lib.utils import get_ifconfig_info device = KVM_BRIDGE_PREFIX + "0" self.view.host_ipaddr = get_ifconfig_info(device)[device]["ipaddr"] except: try: self.view.host_ipaddr = h_model.hostname.split(":")[0].strip() except: self.view.host_ipaddr = socket.gethostbyname( socket.gethostname()) return True
def _GET(self, *param, **params): java_dir = karesansui.dirname + "/static/java" self.view.applet_dst_path = java_dir + "/VncViewer.jar" self.view.applet_src_path = _get_applet_source_path() self.view.found_applet_located = os.path.exists(self.view.applet_dst_path) (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() dom = kvc.search_guests(domname)[0] document = XMLParse(dom.XMLDesc(1)) self.view.graphics_port = XMLXpath(document, "/domain/devices/graphics/@port") self.view.xenname = XMLXpath(document, "/domain/name/text()") finally: kvc.close() h_model = findbyhost1(self.orm, host_id) try: from karesansui.lib.utils import get_ifconfig_info device = KVM_BRIDGE_PREFIX + "0" self.view.host_ipaddr = get_ifconfig_info(device)[device]["ipaddr"] except: try: self.view.host_ipaddr = h_model.hostname.split(":")[0].strip() except: self.view.host_ipaddr = socket.gethostbyname(socket.gethostname()) return True
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = kvc.search_kvg_guests(domname)[0] self.view.info = virt.get_info() #self.view.mem_info = kvc.get_mem_info() self.view.nodeinfo = kvc.get_nodeinfo() self.view.guest = model self.view.hypervisor = self.view.info['hypervisor'] finally: kvc.close() return True
def _GET(self, *param, **params): """<comment-ja> virDomainState - VIR_DOMAIN_NOSTATE = 0 - VIR_DOMAIN_RUNNING = 1 - VIR_DOMAIN_BLOCKED = 2 - VIR_DOMAIN_PAUSED = 3 - VIR_DOMAIN_SHUTDOWN = 4 - VIR_DOMAIN_SHUTOFF = 5 - VIR_DOMAIN_CRASHED = 6 </comment-ja> <comment-en> TODO: English Comment </comment-en> """ (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname) if self.__template__["media"] == 'json': self.view.status = json_dumps(virt[0].status()) else: self.view.status = virt[0].status() finally: kvc.close() #self.__template__.dir = 'guestby1' return True
def _GET(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() kvc = KaresansuiVirtConnection() try: # Storage Pool #inactive_pool = kvc.list_inactive_storage_pool() inactive_pool = [] active_pool = kvc.list_active_storage_pool() pools = inactive_pool + active_pool pools.sort() if self.is_mode_input() is True: # (*.input) if not validates_sid(self): return web.badrequest(self.view.alert) sid = self.input.sid model = findbyguest1(self.orm, sid) if not model: return web.badrequest() domname = kvc.uuid_to_domname(model.uniq_key) src_pools = kvc.get_storage_pool_name_bydomain(domname) if not src_pools: return web.badrequest(_("Source storage pool is not found.")) for src_pool in src_pools : src_pool_type = kvc.get_storage_pool_type(src_pool) if src_pool_type != 'dir': return web.badrequest(_("'%s' disk contains the image.") % src_pool_type) virt = kvc.search_kvg_guests(domname)[0] if virt.is_active() is True: return web.badrequest(_("Guest is running. Please stop and try again. name=%s" % domname)) self.view.domname = virt.get_domain_name() non_iscsi_pool = [] for pool in pools: if kvc.get_storage_pool_type(pool) != 'iscsi': non_iscsi_pool.append(pool) self.view.pools = non_iscsi_pool self.view.sid = sid return True # Exported Guest Info (*.json) exports = {} for pool_name in pools: files = [] pool = kvc.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,)): param = ExportConfigParam() param.load_xml_config(_afile) _dir = os.path.dirname(_afile) uuid = param.get_uuid() name = param.get_domain() created = param.get_created() title = param.get_title() if title != "": title = re.sub("[\r\n]","",title) if title == "": title = _('untitled') if created != "": created_str = time.strftime("%Y/%m/%d %H:%M:%S", \ time.localtime(float(created))) else: created_str = _("N/A") files.append({"dir": _dir, "pool" : pool_name, #"b64dir" : base64_encode(_dir), "uuid" : uuid, "name" : name, "created" : int(created), "created_str" : created_str, "title" : title, }) exports[pool_name] = files # .json if self.is_json() is True: self.view.exports = json_dumps(exports) else: self.view.exports = exports return True finally: kvc.close()
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) # virt self.kvc = KaresansuiVirtConnection() try: domname = self.kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() finally: self.kvc.close() (check, edit) = validates_jobsearch(self) if check is False: return web.badrequest(self.view.alert) if edit is True: # user search users = findbyname_BM(self.orm, self.input.user) users_id = [] for user in users: users_id.append(user.id) if is_empty(self.input.start): start = None else: start = str2datetime(self.input.start, DEFAULT_LANGS[self.me.languages]['DATE_FORMAT'][0]) if is_empty(self.input.end): end = None else: end = str2datetime(self.input.end, DEFAULT_LANGS[self.me.languages]['DATE_FORMAT'][0],True) # machine search m_m2js = m2mj_findbyguest(self.orm, guest_id, start, end, users_id, ) if not m_m2js: self.logger.debug("Search m_m2js failed. " "Did not exist m_m2js that in accord with these query. " "guest_id %s, start %s, end %s, users_id %s" % (guest_id, start, end, users_id)) return web.nocontent() self.view.m_m2js = m_m2js self.view.user = self.input.user self.view.status = self.input.status self.view.start = self.input.start self.view.end = self.input.end jobgroup_ids = [] for m_m2j in m_m2js: jobgroup_ids.append(m_m2j[1].jobgroup_id) jobgroup_status = self.input.status if is_empty(jobgroup_status): jobgroup_status = None jobgroups = jg_findbyalltype(self.pysilhouette.orm, JOBGROUP_TYPE["SERIAL"], jobgroup_ids, jobgroup_status, desc=True) if not jobgroups: self.logger.debug("Search jobgroups failed. " "Did not exist jobgroups that in accord with these query." "jobgroup_ids %s, jobgroup_status %s" % (jobgroup_ids, jobgroup_status)) return web.nocontent() else: m_m2js = m2mj_findbyguest(self.orm, guest_id) self.view.m_m2js = m_m2js self.view.user = '' self.view.status = '' self.view.start = '' self.view.end = '' jobgroup_ids = [] for m_m2j in m_m2js: jobgroup_ids.append(m_m2j[1].jobgroup_id) jobgroups = jg_findbyalltype(self.pysilhouette.orm, JOBGROUP_TYPE["SERIAL"], jobgroup_ids, desc=True) self.view.JOBGROUP_STATUS = JOBGROUP_STATUS self.view.HYPERVISOR = MACHINE_HYPERVISOR if self.input.has_key('p') is True: if validates_page(self) is True: start = int(self.input.p) else: return web.badrequest(self.view.alert) else: start = 0 self.view.date_format = DEFAULT_LANGS[self.me.languages]['DATE_FORMAT'][1] self.view.pager = Pager(jobgroups, start, JOB_LIST_RANGE) return True
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if self.input.has_key('job_id') is True: self.view.job_id = self.input.job_id else: self.view.job_id = None if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) self.kvc = KaresansuiVirtConnection() if self.is_mode_input() is True: try: domname = self.kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = self.kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) self.view.model = guest.info["model"] return True except: self.kvc.close() raise else: try: domname = self.kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = self.kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) guest_info = guest.info["virt"].get_info() info = {} info['memory'] = guest_info["memory"] info['cpu'] = guest_info["cpuTime"] info['os'] = guest_info["OSType"] info['hypervisor'] = guest_info["hypervisor"] info['type'] = guest_info["VMType"] info['hv_version'] = guest_info["hv_version"] disk_info = guest.info["virt"].get_disk_info() interface_info = guest.info["virt"].get_interface_info() net_info = guest.info["virt"].get_netinfo() graphics_info = guest.info["virt"].get_graphics_info() vcpu_info = guest.info["virt"].get_vcpus_info() pool_info = [] pool_vols_info = [] already_vols = [] pools = self.kvc.get_storage_pool_name_bydomain(domname) if len(pools) > 0: for pool in pools: pool_obj = self.kvc.search_kvn_storage_pools(pool)[0] if pool_obj.is_active() is True: pool_info = pool_obj.get_info() vols_obj = pool_obj.search_kvn_storage_volumes(self.kvc) vols_info = {} for vol_obj in vols_obj: vol_name = vol_obj.get_storage_volume_name() vols_info[vol_name] = vol_obj.get_info() pool_vols_info = vols_info if self.__template__["media"] == 'json': json_guest = guest.get_json(self.me.languages) self.view.data = json_dumps( { "model": json_guest["model"], "virt": json_guest["virt"], "autostart": guest.info["virt"].autostart(), "info": info, "disk_info": disk_info, "net_info": net_info, "interface_info": interface_info, "pool_info": pool_info, "pool_vols_info": pool_vols_info, "graphics_info": graphics_info, "vcpu_info": vcpu_info, } ) else: self.view.model = guest.info["model"] self.view.virt = guest.info["virt"] self.view.autostart = guest.info["virt"].autostart() self.view.info = info self.view.disk_info = disk_info self.view.net_info = net_info self.view.interface_info = interface_info self.view.pool_info = pool_info self.view.pool_vols_info = pool_vols_info self.view.graphics_info = graphics_info self.view.vcpu_info = vcpu_info except: self.kvc.close() raise return True
def _POST(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] nic_info = virt.get_interface_info() # -- Nic if self.input.device_type == "nic": if not validates_nic(self): return web.badrequest(self.view.alert) f_chk = True for x in nic_info: if x['mac']['address'] == self.input.mac_address: f_chk = False break if f_chk is False: return web.badrequest(_('Specified MAC address is already defined.')) mac = self.input.mac_address bridge = None network = None if self.input.nic_type == "phydev": bridge = self.input.phydev elif self.input.nic_type == "virnet": network = self.input.virnet self.logger.debug('spinning off create_nic_job dom=%s, mac=%s, bridge=%s, network=%s' \ % (domname, mac, bridge, network)) create_nic_job(self,model,domname,mac,bridge,network) return web.accepted() # -- Disk elif self.input.device_type == "disk": if not validates_disk(self): return web.badrequest(self.view.alert) volume_job = None order = 0 if self.input.pool_type == "dir" or self.input.pool_type == "fs": # create(dir) disk_type = 'file' pool_name = self.input.pool_dir volume_name = string_from_uuid(generate_uuid()) volume_job = create_storage_volume_dir(self, model, domname, volume_name, self.input.pool_dir, self.input.disk_format, self.input.disk_size, self.input.disk_size, 'M', order) order += 1 elif self.input.pool_type == "block": # create(iscsi block) disk_type = 'iscsi' (iscsi_pool, iscsi_volume) = self.input.pool_dir.split("/", 2) pool_name = iscsi_pool volume_name = iscsi_volume else: return badrequest(_("No storage type specified.")) # add disk disk_job = create_disk_job(self, guest=model, domain_name=domname, pool=pool_name, volume=volume_name, bus=self.input.bus_type, format=self.input.disk_format, type=disk_type, order=order) order += 1 if exec_disk_job(obj=self, guest=model, disk_job=disk_job, volume_job=volume_job, order=order ) is True: return web.accepted() else: return False else: # Not Found return False finally: kvc.close()
def _PUT(self, *param, **params): """<comment-ja> Japanese Comment </comment-ja> <comment-en> TODO: English Comment </comment-en> """ (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if not validates_cpu(self): return web.badrequest(self.view.alert) if is_param(self.input, 'vcpus'): vcpus = int(self.input.vcpus) else: vcpus = None max_vcpus = int(self.input.max_vcpus) model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] vcpus_info = virt.get_vcpus_info() max_vcpus_limit = kvc.get_max_vcpus() vcpus_limit = vcpus_info['max_vcpus'] finally: kvc.close() if max_vcpus_limit < max_vcpus: return web.badrequest("%s is greater than the maximum number supported for guest. - max_vcpus_limit=%d max_vcpus=%d" \ % (_('Maximum Number of Virtual CPUs'), self.input.max_vcpus_limit,self.input.max_vcpus,)) if vcpus is not None: if vcpus_limit < vcpus: return web.badrequest("%s is greater than the maximum number supported for guest. - vcpus_limit=%d vcpus=%d" \ % (_('Number of Virtual CPUs'), self.input.vcpus_limit,self.input.vcpus,)) options = {} options["name"] = domname if vcpus is not None: options["vcpus"] = vcpus options["max-vcpus"] = max_vcpus _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_SET_VCPUS), options) cmdname = "Set CPU" _jobgroup = JobGroup(cmdname, karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('%s command' % cmdname, 0, _cmd)) _machine2jobgroup = m2j_new( machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) save_job_collaboration( self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) return web.accepted()
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() guest = findbyguest1(self.orm, guest_id) kvs = KaresansuiVirtSnapshot(readonly=False) try: domname = kvs.kvc.uuid_to_domname(guest.uniq_key) if not domname: return web.notfound() if self.is_mode_input(): virt = kvs.kvc.search_kvg_guests(domname)[0] if virt.is_active() is True: return web.badrequest(_("Guest is running. Please stop and try again. name=%s" % domname)) self.view.is_creatable = kvs.isSupportedDomain(domname) self.view.snapshot_error_msg = '' if self.view.is_creatable is not True and len(kvs.error_msg) > 0: self.view.snapshot_error_msg = ", ".join(kvs.error_msg) try: snapshot_list = kvs.listNames(domname)[domname] except: pass current_snapshot = kvs.getCurrentSnapshotName(domname) finally: kvs.finish() snapshots = [] if snapshot_list: snapshot_list.sort(reverse = True) for snapshot in snapshot_list: model = s_findbyname_guestby1(self.orm, snapshot,guest_id) if model is None: name = snapshot notebook_title = "" created_user = _("N/A") created = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(snapshot))) else: name = model.name notebook_title = model.notebook.title created_user = model.created_user.nickname created = model.created current = False if snapshot == current_snapshot: current = True snapshots.append((name, notebook_title, domname, created_user, created, current )) self.view.snapshots = snapshots self.view.guest = guest return True
def _PUT(self, *param, **params): """<comment-ja> Japanese Comment </comment-ja> <comment-en> TODO: English Comment </comment-en> """ (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if not validates_graphics(self): return web.badrequest(self.view.alert) model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] info = virt.get_graphics_info()["setting"] used_ports = kvc.list_used_graphics_port() origin_port = info["port"] finally: kvc.close() options = {} options["name"] = domname if self.input.change_passwd == "random": options["random-passwd"] = None elif self.input.change_passwd == "empty": options["passwd"] = "" options["port"] = self.input.port options["listen"] = self.input.listen options["keymap"] = self.input.keymap options["type"] = self.input.graphics_type if int(self.input.port) != origin_port and int( self.input.port) in used_ports: return web.badrequest( "Graphics port number has been already used by other service. - port=%s" % (self.input.port, )) _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_SET_GRAPHICS), options) cmdname = "Set Graphics" _jobgroup = JobGroup(cmdname, karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('%s command' % cmdname, 0, _cmd)) _machine2jobgroup = m2j_new( machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) save_job_collaboration( self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) return web.created(None)
def _GET(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() if not validates_src_id(self): self.view.alert = "Failed to get the id of source domain." self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) src_id = self.input.src_id if self.is_mode_input() is False: return web.nomethod() self.view.src_id = src_id self.view.mac_address = generate_mac_address() src_guest = findbyguest1(self.orm, src_id) if not src_guest: self.view.alert = "Failed to get the data of source domain." self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) kvc = KaresansuiVirtConnection() try: # Storage Pool #inactive_pool = self.kvc.list_inactive_storage_pool() inactive_pool = [] active_pool = kvc.list_active_storage_pool() pools = inactive_pool + active_pool pools.sort() domname = kvc.uuid_to_domname(src_guest.uniq_key) # TODO 対応するストレージボリュームにiscsiがあった場合はエラーにする src_pools = kvc.get_storage_pool_name_bydomain(domname) if not src_pools: self.view.alert = _("Source storage pool is not found.") self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) for src_pool in src_pools : src_pool_type = kvc.get_storage_pool_type(src_pool) if src_pool_type != 'dir': self.view.alert = _("'%s' disk contains the image.") % (src_pool_type) self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) non_iscsi_pool = [] for pool in pools: if kvc.get_storage_pool_type(pool) != 'iscsi': non_iscsi_pool.append(pool) self.view.pools = non_iscsi_pool virt = kvc.search_kvg_guests(domname)[0] if virt.is_active() is True: self.view.alert = _("Guest is running. Please stop and try again. name=%s" % domname) self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) self.view.domain_src_name = virt.get_domain_name() used_ports = kvc.list_used_vnc_port() self.view.vnc_port = next_number(VNC_PORT_MIN_NUMBER,PORT_MAX_NUMBER,used_ports) finally: kvc.close() return True
def _PUT(self, *param, **params): """<comment-ja> ステータス更新 - param - create = 0 - shutdown = 1 - destroy = 2 - suspend = 3 - resume = 4 - reboot = 5 </comment-ja> <comment-en> TODO: English Comment </comment-en> """ (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if not validates_guest_status(self): return web.badrequest(self.view.alert) status = int(self.input.status) model = findbyguest1(self.orm, guest_id) kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname) if status == GUEST_ACTION_CREATE: # -- Create cmdname = ["Start Guest", "start guest"] if virt[0].is_creatable() is True: _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_START_GUEST), {"name":domname}) self.view.status = VIRT_COMMAND_START_GUEST else: self.logger.error("Create Action:The state can not run. - %d" % virt[0].status()) elif status == GUEST_ACTION_SHUTDOWN: cmdname = ["Shutdown Guest", "shutdown guest"] if virt[0].is_shutdownable() is True: # -- Shutdown _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_SHUTDOWN_GUEST), {"name":domname}) self.view.status = VIRT_COMMAND_SHUTDOWN_GUEST else: self.logger.error("Shutdown Action:The state can not run. - %d" % virt[0].status()) elif status == GUEST_ACTION_DESTROY: cmdname = ["Destroy Guest", "Destroy guest"] if virt[0].is_destroyable() is True: # -- destroy _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_DESTROY_GUEST), {"name":domname}) self.view.status = VIRT_COMMAND_DESTROY_GUEST else: self.logger.error("Destroy Action:The state can not run. - %d" % virt[0].status()) elif status == GUEST_ACTION_SUSPEND: cmdname = ["Suspend Guest", "suspend guest"] if virt[0].is_suspendable() is True: # -- Suspend _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_SUSPEND_GUEST), {"name":domname}) self.view.status = VIRT_COMMAND_SUSPEND_GUEST else: self.logger.error("Destroy Action:The state can not run. - %d" % virt[0].status()) elif status == GUEST_ACTION_RESUME: cmdname = ["Resume Guest", "resume guest"] if virt[0].is_resumable() is True: # -- Resume _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_RESUME_GUEST), {"name":domname}) self.view.status = VIRT_COMMAND_RESUME_GUEST else: self.logger.error("Resume Action:The state can not run. - %d" % virt[0].status()) elif status == GUEST_ACTION_REBOOT: cmdname = ["Reboot Guest", "reboot guest"] if virt[0].is_shutdownable() is True: # -- Reboot _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_REBOOT_GUEST), {"name":domname}) self.view.status = VIRT_COMMAND_REBOOT_GUEST else: self.logger.error("Reboot Action:The state can not run. - %d" % virt[0].status()) elif status == GUEST_ACTION_ENABLE_AUTOSTART: cmdname = ["Enable Autostart Guest", "enable autostart guest"] # -- Enable autostart guest _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_AUTOSTART_GUEST), {"name":domname, "enable":None}) self.view.status = VIRT_COMMAND_AUTOSTART_GUEST elif status == GUEST_ACTION_DISABLE_AUTOSTART: cmdname = ["Disable Autostart Guest", "disable autostart guest"] # -- Disable autostart guest _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_AUTOSTART_GUEST), {"name":domname, "disable":None}) self.view.status = VIRT_COMMAND_AUTOSTART_GUEST else: self.logger.error("Action:Bad Request. - request status=%d" % status) return web.badrequest() finally: kvc.close() # Job Register _jobgroup = JobGroup(cmdname[0], karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('%s command' % cmdname[1], 0, _cmd)) _machine2jobgroup = m2j_new(machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) # INSERT save_job_collaboration(self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) return web.accepted(url="/host/%d/guest/%d.part" % (host_id, guest_id))
def _PUT(self, *param, **params): """<comment-ja> ステータス更新 - param - create = 0 - shutdown = 1 - destroy = 2 - suspend = 3 - resume = 4 - reboot = 5 </comment-ja> <comment-en> TODO: English Comment </comment-en> """ (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if not validates_guest_status(self): return web.badrequest(self.view.alert) status = int(self.input.status) model = findbyguest1(self.orm, guest_id) kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname) if status == GUEST_ACTION_CREATE: # -- Create cmdname = ["Start Guest", "start guest"] if virt[0].is_creatable() is True: _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_START_GUEST), {"name": domname}) self.view.status = VIRT_COMMAND_START_GUEST else: self.logger.error( "Create Action:The state can not run. - %d" % virt[0].status()) elif status == GUEST_ACTION_SHUTDOWN: cmdname = ["Shutdown Guest", "shutdown guest"] if virt[0].is_shutdownable() is True: # -- Shutdown _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_SHUTDOWN_GUEST), {"name": domname}) self.view.status = VIRT_COMMAND_SHUTDOWN_GUEST else: self.logger.error( "Shutdown Action:The state can not run. - %d" % virt[0].status()) elif status == GUEST_ACTION_DESTROY: cmdname = ["Destroy Guest", "Destroy guest"] if virt[0].is_destroyable() is True: # -- destroy _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_DESTROY_GUEST), {"name": domname}) self.view.status = VIRT_COMMAND_DESTROY_GUEST else: self.logger.error( "Destroy Action:The state can not run. - %d" % virt[0].status()) elif status == GUEST_ACTION_SUSPEND: cmdname = ["Suspend Guest", "suspend guest"] if virt[0].is_suspendable() is True: # -- Suspend _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_SUSPEND_GUEST), {"name": domname}) self.view.status = VIRT_COMMAND_SUSPEND_GUEST else: self.logger.error( "Destroy Action:The state can not run. - %d" % virt[0].status()) elif status == GUEST_ACTION_RESUME: cmdname = ["Resume Guest", "resume guest"] if virt[0].is_resumable() is True: # -- Resume _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_RESUME_GUEST), {"name": domname}) self.view.status = VIRT_COMMAND_RESUME_GUEST else: self.logger.error( "Resume Action:The state can not run. - %d" % virt[0].status()) elif status == GUEST_ACTION_REBOOT: cmdname = ["Reboot Guest", "reboot guest"] if virt[0].is_shutdownable() is True: # -- Reboot _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_REBOOT_GUEST), {"name": domname}) self.view.status = VIRT_COMMAND_REBOOT_GUEST else: self.logger.error( "Reboot Action:The state can not run. - %d" % virt[0].status()) elif status == GUEST_ACTION_ENABLE_AUTOSTART: cmdname = ["Enable Autostart Guest", "enable autostart guest"] # -- Enable autostart guest _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_AUTOSTART_GUEST), { "name": domname, "enable": None }) self.view.status = VIRT_COMMAND_AUTOSTART_GUEST elif status == GUEST_ACTION_DISABLE_AUTOSTART: cmdname = [ "Disable Autostart Guest", "disable autostart guest" ] # -- Disable autostart guest _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_AUTOSTART_GUEST), { "name": domname, "disable": None }) self.view.status = VIRT_COMMAND_AUTOSTART_GUEST else: self.logger.error("Action:Bad Request. - request status=%d" % status) return web.badrequest() finally: kvc.close() # Job Register _jobgroup = JobGroup(cmdname[0], karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('%s command' % cmdname[1], 0, _cmd)) _machine2jobgroup = m2j_new( machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) # INSERT save_job_collaboration( self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) return web.accepted(url="/host/%d/guest/%d.part" % (host_id, guest_id))
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() bridge_prefix = { "XEN":"xenbr", "KVM":"br|bondbr", #"KVM":"eth|bondbr", } model = findbyguest1(self.orm, guest_id) # virt self.kvc = KaresansuiVirtConnection() try: domname = self.kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = self.kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) self.view.guest = guest # Output .input if self.is_mode_input() is True: try: VMType = guest.info["virt"].get_info()["VMType"].upper() except: VMType = "KVM" self.view.VMType = VMType # Network phydev = [] phydev_regex = re.compile(r"%s" % bridge_prefix[VMType]) for dev,dev_info in get_ifconfig_info().iteritems(): try: if phydev_regex.match(dev): phydev.append(dev) except: pass if len(phydev) == 0: phydev.append("%s0" % bridge_prefix[VMType]) phydev.sort() self.view.phydev = phydev # Physical device self.view.virnet = sorted(self.kvc.list_active_network()) # Virtual device self.view.mac_address = generate_mac_address() # new mac address # Disk inactive_pool = [] active_pool = self.kvc.list_active_storage_pool() pools = inactive_pool + active_pool pools.sort() if not pools: return web.badrequest('One can not start a storage pool.') pools_info = {} pools_vols_info = {} pools_iscsi_blocks = {} already_vols = [] guests = [] guests += self.kvc.list_inactive_guest() guests += self.kvc.list_active_guest() for guest in guests: already_vol = self.kvc.get_storage_volume_bydomain(domain=guest, image_type=None, attr='path') if already_vol: already_vols += already_vol.keys() for pool in pools: pool_obj = self.kvc.search_kvn_storage_pools(pool)[0] if pool_obj.is_active() is True: pools_info[pool] = pool_obj.get_info() blocks = None if pools_info[pool]['type'] == 'iscsi': blocks = self.kvc.get_storage_volume_iscsi_block_bypool(pool) if blocks: pools_iscsi_blocks[pool] = [] vols_obj = pool_obj.search_kvn_storage_volumes(self.kvc) vols_info = {} for vol_obj in vols_obj: vol_name = vol_obj.get_storage_volume_name() vols_info[vol_name] = vol_obj.get_info() if blocks: if vol_name in blocks and vol_name not in already_vols: pools_iscsi_blocks[pool].append(vol_obj.get_info()) pools_vols_info[pool] = vols_info self.view.pools = pools self.view.pools_info = pools_info self.view.pools_vols_info = pools_vols_info self.view.pools_iscsi_blocks = pools_iscsi_blocks if VMType == "KVM": self.view.DISK_FORMATS = DISK_QEMU_FORMAT else: self.view.DISK_FORMATS = DISK_NON_QEMU_FORMAT self.view.bus_types = self.kvc.bus_types else: # .part self.view.ifinfo = virt.get_interface_info() # interface info self.view.disk_info = virt.get_disk_info() # Disk info finally: self.kvc.close() return True
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() guest = findbyguest1(self.orm, guest_id) kvs = KaresansuiVirtSnapshot(readonly=False) try: domname = kvs.kvc.uuid_to_domname(guest.uniq_key) if not domname: return web.notfound() if self.is_mode_input(): virt = kvs.kvc.search_kvg_guests(domname)[0] if virt.is_active() is True: return web.badrequest( _("Guest is running. Please stop and try again. name=%s" % domname)) self.view.is_creatable = kvs.isSupportedDomain(domname) self.view.snapshot_error_msg = '' if self.view.is_creatable is not True and len(kvs.error_msg) > 0: self.view.snapshot_error_msg = ", ".join(kvs.error_msg) try: snapshot_list = kvs.listNames(domname)[domname] except: pass current_snapshot = kvs.getCurrentSnapshotName(domname) finally: kvs.finish() snapshots = [] if snapshot_list: snapshot_list.sort(reverse=True) for snapshot in snapshot_list: model = s_findbyname_guestby1(self.orm, snapshot, guest_id) if model is None: name = snapshot notebook_title = "" created_user = _("N/A") created = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(snapshot))) else: name = model.name notebook_title = model.notebook.title created_user = model.created_user.nickname created = model.created current = False if snapshot == current_snapshot: current = True snapshots.append((name, notebook_title, domname, created_user, created, current)) self.view.snapshots = snapshots self.view.guest = guest return True
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() bridge_prefix = { "XEN": "xenbr", "KVM": "br|bondbr", #"KVM":"eth|bondbr", } model = findbyguest1(self.orm, guest_id) # virt self.kvc = KaresansuiVirtConnection() try: domname = self.kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = self.kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) self.view.guest = guest # Output .input if self.is_mode_input() is True: try: VMType = guest.info["virt"].get_info()["VMType"].upper() except: VMType = "KVM" self.view.VMType = VMType # Network phydev = [] phydev_regex = re.compile(r"%s" % bridge_prefix[VMType]) for dev, dev_info in get_ifconfig_info().iteritems(): try: if phydev_regex.match(dev): phydev.append(dev) except: pass if len(phydev) == 0: phydev.append("%s0" % bridge_prefix[VMType]) phydev.sort() self.view.phydev = phydev # Physical device self.view.virnet = sorted( self.kvc.list_active_network()) # Virtual device self.view.mac_address = generate_mac_address( ) # new mac address # Disk inactive_pool = [] active_pool = self.kvc.list_active_storage_pool() pools = inactive_pool + active_pool pools.sort() if not pools: return web.badrequest('One can not start a storage pool.') pools_info = {} pools_vols_info = {} pools_iscsi_blocks = {} already_vols = [] guests = [] guests += self.kvc.list_inactive_guest() guests += self.kvc.list_active_guest() for guest in guests: already_vol = self.kvc.get_storage_volume_bydomain( domain=guest, image_type=None, attr='path') if already_vol: already_vols += already_vol.keys() for pool in pools: pool_obj = self.kvc.search_kvn_storage_pools(pool)[0] if pool_obj.is_active() is True: pools_info[pool] = pool_obj.get_info() blocks = None if pools_info[pool]['type'] == 'iscsi': blocks = self.kvc.get_storage_volume_iscsi_block_bypool( pool) if blocks: pools_iscsi_blocks[pool] = [] vols_obj = pool_obj.search_kvn_storage_volumes( self.kvc) vols_info = {} for vol_obj in vols_obj: vol_name = vol_obj.get_storage_volume_name() vols_info[vol_name] = vol_obj.get_info() if blocks: if vol_name in blocks and vol_name not in already_vols: pools_iscsi_blocks[pool].append( vol_obj.get_info()) pools_vols_info[pool] = vols_info self.view.pools = pools self.view.pools_info = pools_info self.view.pools_vols_info = pools_vols_info self.view.pools_iscsi_blocks = pools_iscsi_blocks if VMType == "KVM": self.view.DISK_FORMATS = DISK_QEMU_FORMAT else: self.view.DISK_FORMATS = DISK_NON_QEMU_FORMAT self.view.bus_types = self.kvc.bus_types else: # .part self.view.ifinfo = virt.get_interface_info() # interface info self.view.disk_info = virt.get_disk_info() # Disk info finally: self.kvc.close() return True
def _POST(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() if not validates_guest_export(self): return web.badrequest(self.view.alert) if not validates_sid(self): return web.badrequest(self.view.alert) model = findbyguest1(self.orm, self.input.sid) if not model: return web.badrequest() kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) src_pools = kvc.get_storage_pool_name_bydomain(domname) if not src_pools: return web.badrequest(_("Source storage pool is not found.")) for src_pool in src_pools : src_pool_type = kvc.get_storage_pool_type(src_pool) if src_pool_type != 'dir': return web.badrequest(_("'%s' disk contains the image.") % src_pool_type) virt = kvc.search_kvg_guests(domname)[0] options = {} options["name"] = virt.get_domain_name() if is_param(self.input, "pool"): # disk check src_pool = kvc.get_storage_pool_name_bydomain(domname, 'os')[0] src_path = kvc.get_storage_pool_targetpath(src_pool) src_disk = "%s/%s/images/%s.img" \ % (src_path, options["name"], options["name"]) dest_path = kvc.get_storage_pool_targetpath(self.input.pool) s_size = os.path.getsize(src_disk) / (1024 * 1024) # a unit 'MB' if os.access(dest_path, os.F_OK): if chk_create_disk(dest_path, s_size) is False: partition = get_partition_info(dest_path, header=False) return web.badrequest( _("No space available to create disk image in '%s' partition.") \ % partition[5][0]) #else: # Permission denied #TODO:check disk space for root options["pool"] = self.input.pool if is_param(self.input, "export_title"): #options["title"] = self.input.export_title options["title"] = "b64:" + base64_encode(self.input.export_title) options["quiet"] = None finally: kvc.close() _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_EXPORT_GUEST), options) # Job Register cmdname = ["Export Guest", "export guest"] _jobgroup = JobGroup(cmdname[0], karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('%s command' % cmdname[1], 0, _cmd)) _machine2jobgroup = m2j_new(machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) # INSERT save_job_collaboration(self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) self.logger.debug("(%s) Job group id==%s" % (cmdname[0],_jobgroup.id)) url = '%s/job/%s.part' % (web.ctx.home, _jobgroup.id) self.logger.debug('Returning Location: %s' % url) return web.accepted()
def _DELETE(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if is_int(param[2]) is False: return web.badrequest() disk_id = int(param[2]) model = findbyguest1(self.orm, guest_id) if not model: return web.notfound() # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) disk_info = virt.get_disk_info()[disk_id] if disk_info['source'].has_key('file'): pool_type = 'file' volume_rpath = disk_info['source']['file'] elif disk_info['source'].has_key('dev'): pool_type = 'iscsi' volume_rpath = disk_info['source']['dev'] pool_name = kvc.get_storage_pool_name_byimage(volume_rpath) if not pool_name: return web.badrequest(_("Storage pool not found.")) else: pool_name = pool_name[0] pool_type = kvc.get_storage_pool_type(pool_name) order = 0 volume_job = None if pool_type != 'iscsi': disk_volumes = kvc.get_storage_volume_bydomain(domname, 'disk', 'key') volume = None for key in disk_volumes.keys(): if volume_rpath == os.path.realpath(disk_volumes[key]): volume = key if volume is None: return web.badrequest(_("Storage volume can not be found.")) volume_job = delete_storage_volume(self, key, pool_name, order, use=DISK_USES["DISK"] ) order += 1 finally: kvc.close() target = disk_info["target"]["dev"] self.logger.debug('spinning off delete_disk_job dom=%s, target=%s' % (domname, target)) disk_job = delete_disk_job(self, domname, target, order) if setexec_delete_disk(self, model, disk_job, volume_job) is True: return web.accepted() else: return False
def _GET(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if self.input.has_key('job_id') is True: self.view.job_id = self.input.job_id else: self.view.job_id = None if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) self.kvc = KaresansuiVirtConnection() if self.is_mode_input() is True: try: domname = self.kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = self.kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) self.view.model = guest.info["model"] return True except: self.kvc.close() raise else: try: domname = self.kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() virt = self.kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) guest_info = guest.info["virt"].get_info() info = {} info['memory'] = guest_info["memory"] info['cpu'] = guest_info["cpuTime"] info['os'] = guest_info["OSType"] info['hypervisor'] = guest_info["hypervisor"] info['type'] = guest_info["VMType"] info['hv_version'] = guest_info["hv_version"] disk_info = guest.info["virt"].get_disk_info() interface_info = guest.info["virt"].get_interface_info() net_info = guest.info["virt"].get_netinfo() graphics_info = guest.info["virt"].get_graphics_info() vcpu_info = guest.info["virt"].get_vcpus_info() pool_info = [] pool_vols_info = [] already_vols = [] pools = self.kvc.get_storage_pool_name_bydomain(domname) if len(pools) > 0: for pool in pools: pool_obj = self.kvc.search_kvn_storage_pools(pool)[0] if pool_obj.is_active() is True: pool_info = pool_obj.get_info() vols_obj = pool_obj.search_kvn_storage_volumes( self.kvc) vols_info = {} for vol_obj in vols_obj: vol_name = vol_obj.get_storage_volume_name() vols_info[vol_name] = vol_obj.get_info() pool_vols_info = vols_info if self.__template__["media"] == 'json': json_guest = guest.get_json(self.me.languages) self.view.data = json_dumps({ "model": json_guest["model"], "virt": json_guest["virt"], "autostart": guest.info["virt"].autostart(), "info": info, "disk_info": disk_info, "net_info": net_info, "interface_info": interface_info, "pool_info": pool_info, "pool_vols_info": pool_vols_info, "graphics_info": graphics_info, "vcpu_info": vcpu_info, }) else: self.view.model = guest.info["model"] self.view.virt = guest.info["virt"] self.view.autostart = guest.info["virt"].autostart() self.view.info = info self.view.disk_info = disk_info self.view.net_info = net_info self.view.interface_info = interface_info self.view.pool_info = pool_info self.view.pool_vols_info = pool_vols_info self.view.graphics_info = graphics_info self.view.vcpu_info = vcpu_info except: self.kvc.close() raise return True
def _POST(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() if not validates_guest_replicate(self): self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) uuid = string_from_uuid(generate_uuid()) # TODO dest_pool valid if not validates_src_id(self): self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) src_guest = findbyguest1(self.orm, self.input.src_id) if not src_guest: self.view.alert = "Failed to get the data of source domain." self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) # Note note_title = None if is_param(self.input, "note_title"): note_title = self.input.note_title note_value = None if is_param(self.input, "note_value"): note_value = self.input.note_value _notebook = n_new(note_title, note_value) # Tag _tags = None if is_param(self.input, "tags"): _tags = [] for x in comma_split(self.input.tags): _tags.append(t_new(x)) # Icon icon_filename = None if is_param(self.input, "icon_filename", empty=True): icon_filename = self.input.icon_filename dest_guest = m_new(created_user=self.me, modified_user=self.me, uniq_key=uni_force(uuid), name=self.input.m_name, attribute=MACHINE_ATTRIBUTE['GUEST'], hypervisor=src_guest.hypervisor, notebook=_notebook, tags=_tags, icon=icon_filename, is_deleted=False, parent=src_guest.parent, ) kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(src_guest.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] options = {} options["src-name"] = virt.get_domain_name() if is_param(self.input, "dest_pool"): options["pool"] = self.input.dest_pool if is_param(self.input, "domain_dest_name"): options["dest-name"] = self.input.domain_dest_name if is_param(self.input, "vm_vncport"): options["vnc-port"] = self.input.vm_vncport if is_param(self.input, "vm_mac"): options["mac"] = self.input.vm_mac options["uuid"] = uuid src_pools = kvc.get_storage_pool_name_bydomain(domname) if not src_pools: self.view.alert = _("Source storage pool is not found.") self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) for src_pool in src_pools : src_pool_type = kvc.get_storage_pool_type(src_pool) if src_pool_type != 'dir': self.view.alert = _("'%s' disk contains the image.") % src_pool_type self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) # disk check target_pool = kvc.get_storage_pool_name_bydomain(domname, 'os')[0] target_path = kvc.get_storage_pool_targetpath(target_pool) src_disk = "%s/%s/images/%s.img" % \ (target_path, options["src-name"], options["src-name"]) s_size = os.path.getsize(src_disk) / (1024 * 1024) # a unit 'MB' if os.access(target_path, os.F_OK): if chk_create_disk(target_path, s_size) is False: partition = get_partition_info(target_path, header=False) self.view.alert = _("No space available to create disk image in '%s' partition.") % partition[5][0] self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) #else: # Permission denied #TODO:check disk space for root active_guests = kvc.list_active_guest() inactive_guests = kvc.list_inactive_guest() used_vnc_ports = kvc.list_used_vnc_port() used_mac_addrs = kvc.list_used_mac_addr() conflict_location = "%s/host/%d/guest/%d.json" \ % (web.ctx.homepath, src_guest.parent_id, src_guest.id) # source guestos if not (options["src-name"] in active_guests or options["src-name"] in inactive_guests): return web.conflict(conflict_location, "Unable to get the source guest.") # Check on whether value has already been used # destination guestos if (options["dest-name"] in active_guests or options["dest-name"] in inactive_guests): return web.conflict(conflict_location, "Destination guest %s is already there." % options["dest-name"]) # VNC port number if(int(self.input.vm_vncport) in used_vnc_ports): return web.conflict(conflict_location, "VNC port %s is already used." % self.input.vm_vncport) # MAC address if(self.input.vm_mac in used_mac_addrs): return web.conflict(conflict_location, "MAC address %s is already used." % self.input.vm_mac) # Replicate Guest order = 0 # job order disk_jobs = [] # Add Disk volume_jobs = [] # Create Storage Volume for disk in virt.get_disk_info(): if disk['type'] != 'file': self.view.alert = _("The type of the storage pool where the disk is to be added must be 'file'. dev=%s") % disk['target']['dev'] self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) disk_pool = kvc.get_storage_pool_name_byimage(disk['source']['file']) if not disk_pool: self.view.alert = _("Can not find the storage pool.") self.logger.debug(self.view.alert) return web.badrequest(self.view.alert) else: disk_pool = disk_pool[0] disk_volumes = kvc.get_storage_volume_bydomain(domname, 'disk', 'key') volume = None for key in disk_volumes.keys(): if disk['source']['file'] == os.path.realpath(disk_volumes[key]): volume = key # disk image if volume is None: # os image continue disk_uuid = string_from_uuid(generate_uuid()) volume_jobs.append(replicate_storage_volume(self, domname, disk_pool, volume, self.input.domain_dest_name, #self.input.dest_pool, disk_pool, # orig disk_uuid, order)) order += 1 disk_jobs.append(create_disk_job(self, dest_guest, self.input.domain_dest_name, disk_pool, disk_uuid, bus=disk['target']['bus'], format=disk['driver']['type'], type=disk['type'], target=disk['target']['dev'], order=-1)) finally: kvc.close() # replicate guest guest_job = replicate_guest(self, dest_guest, VIRT_COMMAND_REPLICATE_GUEST, options, 'Replicate Guest', {"name" : options['dest-name'], "pool" : options["pool"], }, order, ) order += 1 for disk_job in disk_jobs: disk_job.order = order order += 1 ret = exec_replicate_guest(self, dest_guest, icon_filename, 'Replicate Guest', guest_job, disk_jobs, volume_jobs, ) if ret is True: return web.accepted() else: return False
def _DELETE(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) self.kvc = KaresansuiVirtConnection() try: domname = self.kvc.uuid_to_domname(model.uniq_key) if not domname: self.logger.info("Did not exist in libvirt. - guestid=%s" % model.id) logical_delete(self.orm, model) # TODO ファイルシステムにゴミが残るので、delete_guest.pyを実行する必要がある。 self.orm.commit() return web.nocontent() kvg_guest = self.kvc.search_kvg_guests(domname) if not kvg_guest: return web.badrequest( _("Guest not found. - name=%s") % domname) else: kvg_guest = kvg_guest[0] if kvg_guest.is_active(): return web.badrequest( _("Can not delete a running guest OS. - name=%s") % domname) # jobs order order = 0 jobs = [] os_storage = {} for disk in kvg_guest.get_disk_info(): if disk['type'] == 'file': # type="dir" # delete_storage_volume pool = self.kvc.get_storage_pool_name_byimage( disk['source']['file']) if not pool: return web.badrequest( _("Can not find the storage pool image. - target=%s" ) % (disk['source']['file'])) else: pool = pool[0] disk_volumes = self.kvc.get_storage_volume_bydomain( domname, 'disk', 'key') volume = None for key in disk_volumes.keys(): if disk['source']['file'] == os.path.realpath( disk_volumes[key]): volume = key # disk image use = DISK_USES['DISK'] if volume is None: # os image os_volume = self.kvc.get_storage_volume_bydomain( domname, 'os', 'key') if not os_volume: return web.badrequest( _("OS storage volume for guest could not be found. domname=%s" ) % domname) if disk['source']['file'] == os.path.realpath( os_volume.values()[0]): use = DISK_USES['IMAGES'] volume = os_volume.keys()[0] os_storage["pool"] = pool os_storage["volume"] = volume continue # OS delete command to do "VIRT_COMMAND_DELETE_GUEST" image. jobs.append( delete_storage_volume(self, volume, pool, order, use)) order += 1 # delete_disk jobs.append( delete_disk_job(self, domname, disk["target"]["dev"], order)) order += 1 elif disk['type'] == 'block': # type="iscsi" pool = self.kvc.get_storage_pool_name_byimage( disk['source']['dev']) if not pool: return web.badrequest( _("Can not find the storage pool image. - target=%s" ) % disk['source']['dev']) else: pool = pool[0] os_volume = self.kvc.get_storage_volume_bydomain( domname, 'os', 'key') if not os_volume: return web.badrequest( _("OS storage volume for guest could not be found. domname=%s" ) % domname) else: volume = os_volume.values()[0] if disk['source']['dev'] == volume: os_storage["pool"] = pool os_storage["volume"] = volume continue # OS delete command to do "VIRT_COMMAND_DELETE_GUEST" image. # delete_disk jobs.append( delete_disk_job(self, domname, disk["target"]["dev"], order)) order += 1 else: return web.internalerror( _("Not expected storage type. type=%") % disk['type']) # DELETE GUEST cmdname = "Delete Guest" _jobgroup = JobGroup(cmdname, karesansui.sheconf['env.uniqkey']) _jobgroup.jobs = jobs # Set Disk action_cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_DELETE_GUEST), { "name": domname, "pool": os_storage["pool"], "volume": os_storage["volume"], }) _job = Job('%s command' % cmdname, order, action_cmd) _jobgroup.jobs.append(_job) logical_delete(self.orm, model) _machine2jobgroup = m2j_new( machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) save_job_collaboration( self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) return web.accepted(url=web.ctx.path) finally: #self.kvc.close() GuestBy1#_post pass
def _DELETE(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) self.kvc = KaresansuiVirtConnection() try: domname = self.kvc.uuid_to_domname(model.uniq_key) if not domname: self.logger.info("Did not exist in libvirt. - guestid=%s" % model.id) logical_delete(self.orm, model) # TODO ファイルシステムにゴミが残るので、delete_guest.pyを実行する必要がある。 self.orm.commit() return web.nocontent() kvg_guest = self.kvc.search_kvg_guests(domname) if not kvg_guest: return web.badrequest(_("Guest not found. - name=%s") % domname) else: kvg_guest = kvg_guest[0] if kvg_guest.is_active(): return web.badrequest(_("Can not delete a running guest OS. - name=%s") % domname) # jobs order order = 0 jobs = [] os_storage = {} for disk in kvg_guest.get_disk_info(): if disk['type'] == 'file': # type="dir" # delete_storage_volume pool = self.kvc.get_storage_pool_name_byimage(disk['source']['file']) if not pool: return web.badrequest(_("Can not find the storage pool image. - target=%s") % (disk['source']['file'])) else: pool = pool[0] disk_volumes = self.kvc.get_storage_volume_bydomain(domname, 'disk', 'key') volume = None for key in disk_volumes.keys(): if disk['source']['file'] == os.path.realpath(disk_volumes[key]): volume = key # disk image use = DISK_USES['DISK'] if volume is None: # os image os_volume = self.kvc.get_storage_volume_bydomain(domname, 'os', 'key') if not os_volume: return web.badrequest(_("OS storage volume for guest could not be found. domname=%s") % domname) if disk['source']['file'] == os.path.realpath(os_volume.values()[0]): use = DISK_USES['IMAGES'] volume = os_volume.keys()[0] os_storage["pool"] = pool os_storage["volume"] = volume continue # OS delete command to do "VIRT_COMMAND_DELETE_GUEST" image. jobs.append(delete_storage_volume(self, volume, pool, order, use)) order += 1 # delete_disk jobs.append(delete_disk_job(self, domname, disk["target"]["dev"], order)) order += 1 elif disk['type'] == 'block': # type="iscsi" pool = self.kvc.get_storage_pool_name_byimage(disk['source']['dev']) if not pool: return web.badrequest(_("Can not find the storage pool image. - target=%s") % disk['source']['dev']) else: pool = pool[0] os_volume = self.kvc.get_storage_volume_bydomain(domname, 'os', 'key') if not os_volume: return web.badrequest(_("OS storage volume for guest could not be found. domname=%s") % domname) else: volume = os_volume.values()[0] if disk['source']['dev'] == volume: os_storage["pool"] = pool os_storage["volume"] = volume continue # OS delete command to do "VIRT_COMMAND_DELETE_GUEST" image. # delete_disk jobs.append(delete_disk_job(self, domname, disk["target"]["dev"], order)) order += 1 else: return web.internalerror( _("Not expected storage type. type=%") % disk['type']) # DELETE GUEST cmdname = "Delete Guest" _jobgroup = JobGroup(cmdname, karesansui.sheconf['env.uniqkey']) _jobgroup.jobs = jobs # Set Disk action_cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_DELETE_GUEST), {"name" : domname, "pool" : os_storage["pool"], "volume" : os_storage["volume"], } ) _job = Job('%s command' % cmdname, order, action_cmd) _jobgroup.jobs.append(_job) logical_delete(self.orm, model) _machine2jobgroup = m2j_new(machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) save_job_collaboration(self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) return web.accepted(url = web.ctx.path) finally: #self.kvc.close() GuestBy1#_post pass
def _PUT(self, *param, **params): """<comment-ja> Japanese Comment </comment-ja> <comment-en> TODO: English Comment </comment-en> """ (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if not validates_cpu(self): return web.badrequest(self.view.alert) if is_param(self.input, 'vcpus'): vcpus = int(self.input.vcpus) else: vcpus = None max_vcpus = int(self.input.max_vcpus) model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] vcpus_info = virt.get_vcpus_info() max_vcpus_limit = kvc.get_max_vcpus() vcpus_limit = vcpus_info['max_vcpus'] finally: kvc.close() if max_vcpus_limit < max_vcpus: return web.badrequest("%s is greater than the maximum number supported for guest. - max_vcpus_limit=%d max_vcpus=%d" \ % (_('Maximum Number of Virtual CPUs'), self.input.max_vcpus_limit,self.input.max_vcpus,)) if vcpus is not None: if vcpus_limit < vcpus: return web.badrequest("%s is greater than the maximum number supported for guest. - vcpus_limit=%d vcpus=%d" \ % (_('Number of Virtual CPUs'), self.input.vcpus_limit,self.input.vcpus,)) options = {} options["name"] = domname if vcpus is not None: options["vcpus"] = vcpus options["max-vcpus"] = max_vcpus _cmd = dict2command("%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_SET_VCPUS), options) cmdname = 'Set cpu' _jobgroup = JobGroup(cmdname, karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('%s command' % cmdname, 0, _cmd)) _machine2jobgroup = m2j_new(machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) save_job_collaboration(self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) return web.accepted()
def _PUT(self, *param, **params): """<comment-ja> Japanese Comment </comment-ja> <comment-en> TODO: English Comment </comment-en> """ (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if is_param(self.input, 'memory'): memory = int(self.input.memory) else: memory = None max_memory = int(self.input.max_memory) model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] info = virt.get_info() #maxMem = info["maxMem"] now_memory = info["memory"] mem_info = kvc.get_mem_info() nodeinfo = kvc.get_nodeinfo() finally: kvc.close() # valid #if (mem_info["host_free_mem"] + (now_memory / 1024)) < memory: # return web.badrequest("Memory value is greater than the maximum memory value. - memory=%s" % self.input.memory) options = {} options["name"] = domname options["maxmem"] = max_memory if memory is None: options["memory"] = max_memory else: options["memory"] = memory _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_SET_MEMORY), options) cmdname = "Set Memory" _jobgroup = JobGroup(cmdname, karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('%s command' % cmdname, 0, _cmd)) _machine2jobgroup = m2j_new( machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) save_job_collaboration( self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) return web.accepted(url=web.ctx.path)
def _POST(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] nic_info = virt.get_interface_info() # -- Nic if self.input.device_type == "nic": if not validates_nic(self): return web.badrequest(self.view.alert) f_chk = True for x in nic_info: if x['mac']['address'] == self.input.mac_address: f_chk = False break if f_chk is False: return web.badrequest( _('Specified MAC address is already defined.')) mac = self.input.mac_address bridge = None network = None if self.input.nic_type == "phydev": bridge = self.input.phydev elif self.input.nic_type == "virnet": network = self.input.virnet self.logger.debug('spinning off create_nic_job dom=%s, mac=%s, bridge=%s, network=%s' \ % (domname, mac, bridge, network)) create_nic_job(self, model, domname, mac, bridge, network) return web.accepted() # -- Disk elif self.input.device_type == "disk": if not validates_disk(self): return web.badrequest(self.view.alert) volume_job = None order = 0 if self.input.pool_type == "dir" or self.input.pool_type == "fs": # create(dir) disk_type = 'file' pool_name = self.input.pool_dir volume_name = string_from_uuid(generate_uuid()) volume_job = create_storage_volume_dir( self, model, domname, volume_name, self.input.pool_dir, self.input.disk_format, self.input.disk_size, self.input.disk_size, 'M', order) order += 1 elif self.input.pool_type == "block": # create(iscsi block) disk_type = 'iscsi' (iscsi_pool, iscsi_volume) = self.input.pool_dir.split("/", 2) pool_name = iscsi_pool volume_name = iscsi_volume else: return badrequest(_("No storage type specified.")) # add disk disk_job = create_disk_job(self, guest=model, domain_name=domname, pool=pool_name, volume=volume_name, bus=self.input.bus_type, format=self.input.disk_format, type=disk_type, order=order) order += 1 if exec_disk_job(obj=self, guest=model, disk_job=disk_job, volume_job=volume_job, order=order) is True: return web.accepted() else: return False else: # Not Found return False finally: kvc.close()
def _POST(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() if not validates_guest_export(self): return web.badrequest(self.view.alert) if not validates_sid(self): return web.badrequest(self.view.alert) model = findbyguest1(self.orm, self.input.sid) if not model: return web.badrequest() kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) src_pools = kvc.get_storage_pool_name_bydomain(domname) if not src_pools: return web.badrequest(_("Source storage pool is not found.")) for src_pool in src_pools: src_pool_type = kvc.get_storage_pool_type(src_pool) if src_pool_type != 'dir': return web.badrequest( _("'%s' disk contains the image.") % src_pool_type) virt = kvc.search_kvg_guests(domname)[0] options = {} options["name"] = virt.get_domain_name() if is_param(self.input, "pool"): # disk check src_pool = kvc.get_storage_pool_name_bydomain(domname, 'os')[0] src_path = kvc.get_storage_pool_targetpath(src_pool) src_disk = "%s/%s/images/%s.img" \ % (src_path, options["name"], options["name"]) dest_path = kvc.get_storage_pool_targetpath(self.input.pool) s_size = os.path.getsize(src_disk) / (1024 * 1024 ) # a unit 'MB' if os.access(dest_path, os.F_OK): if chk_create_disk(dest_path, s_size) is False: partition = get_partition_info(dest_path, header=False) return web.badrequest( _("No space available to create disk image in '%s' partition.") \ % partition[5][0]) #else: # Permission denied #TODO:check disk space for root options["pool"] = self.input.pool if is_param(self.input, "export_title"): #options["title"] = self.input.export_title options["title"] = "b64:" + base64_encode( self.input.export_title) options["quiet"] = None finally: kvc.close() _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_EXPORT_GUEST), options) # Job Register cmdname = ["Export Guest", "export guest"] _jobgroup = JobGroup(cmdname[0], karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('%s command' % cmdname[1], 0, _cmd)) _machine2jobgroup = m2j_new( machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) # INSERT save_job_collaboration( self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) self.logger.debug("(%s) Job group id==%s" % (cmdname[0], _jobgroup.id)) url = '%s/job/%s.part' % (web.ctx.home, _jobgroup.id) self.logger.debug('Returning Location: %s' % url) return web.accepted()
def _DELETE(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if is_int(param[2]) is False: return web.badrequest() disk_id = int(param[2]) model = findbyguest1(self.orm, guest_id) if not model: return web.notfound() # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] guest = MergeGuest(model, virt) disk_info = virt.get_disk_info()[disk_id] if disk_info['source'].has_key('file'): pool_type = 'file' volume_rpath = disk_info['source']['file'] elif disk_info['source'].has_key('dev'): pool_type = 'iscsi' volume_rpath = disk_info['source']['dev'] disk_type = disk_info['type'] disk_device = disk_info['device'] if disk_device != "cdrom": pool_name = kvc.get_storage_pool_name_byimage(volume_rpath) if not pool_name: return web.badrequest(_("Storage pool not found.")) else: pool_name = pool_name[0] pool_type = kvc.get_storage_pool_type(pool_name) order = 0 volume_job = None if pool_type != 'iscsi' and disk_type != "block": disk_volumes = kvc.get_storage_volume_bydomain( domname, 'disk', 'key') volume = None for key in disk_volumes.keys(): if volume_rpath == os.path.realpath(disk_volumes[key]): volume = key if volume is None: return web.badrequest( _("Storage volume can not be found.")) volume_job = delete_storage_volume(self, key, pool_name, order, use=DISK_USES["DISK"]) order += 1 finally: kvc.close() target = disk_info["target"]["dev"] self.logger.debug('spinning off delete_disk_job dom=%s, target=%s' % (domname, target)) disk_job = delete_disk_job(self, domname, target, order) if setexec_delete_disk(self, model, disk_job, volume_job) is True: return web.accepted() else: return False
def _GET(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() kvc = KaresansuiVirtConnection() try: # Storage Pool #inactive_pool = kvc.list_inactive_storage_pool() inactive_pool = [] active_pool = kvc.list_active_storage_pool() pools = inactive_pool + active_pool pools.sort() if self.is_mode_input() is True: # (*.input) if not validates_sid(self): return web.badrequest(self.view.alert) sid = self.input.sid model = findbyguest1(self.orm, sid) if not model: return web.badrequest() domname = kvc.uuid_to_domname(model.uniq_key) src_pools = kvc.get_storage_pool_name_bydomain(domname) if not src_pools: return web.badrequest( _("Source storage pool is not found.")) for src_pool in src_pools: src_pool_type = kvc.get_storage_pool_type(src_pool) if src_pool_type != 'dir': return web.badrequest( _("'%s' disk contains the image.") % src_pool_type) virt = kvc.search_kvg_guests(domname)[0] if virt.is_active() is True: return web.badrequest( _("Guest is running. Please stop and try again. name=%s" % domname)) self.view.domname = virt.get_domain_name() non_iscsi_pool = [] for pool in pools: if kvc.get_storage_pool_type(pool) != 'iscsi': non_iscsi_pool.append(pool) self.view.pools = non_iscsi_pool self.view.sid = sid return True # Exported Guest Info (*.json) exports = {} for pool_name in pools: files = [] pool = kvc.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, )): param = ExportConfigParam() param.load_xml_config(_afile) _dir = os.path.dirname(_afile) uuid = param.get_uuid() name = param.get_domain() created = param.get_created() title = param.get_title() if title != "": title = re.sub("[\r\n]", "", title) if title == "": title = _('untitled') if created != "": created_str = time.strftime("%Y/%m/%d %H:%M:%S", \ time.localtime(float(created))) else: created_str = _("N/A") files.append({ "dir": _dir, "pool": pool_name, #"b64dir" : base64_encode(_dir), "uuid": uuid, "name": name, "created": int(created), "created_str": created_str, "title": title, }) exports[pool_name] = files # .json if self.is_json() is True: self.view.exports = json_dumps(exports) else: self.view.exports = exports return True finally: kvc.close()
def _PUT(self, *param, **params): (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if is_param(self.input, 'id') is False \ or is_int(self.input.id) is False: return web.badrequest("Request data is invalid.") snapshot_id = str(self.input.id) snapshot = s_findbyname_guestby1(self.orm, snapshot_id, guest_id) if snapshot is None: pass # ignore snapshots that is not in database. #return web.badrequest("Request data is invalid.") model = findbyguest1(self.orm, guest_id) kvs = KaresansuiVirtSnapshot(readonly=False) snapshot_list = [] try: domname = kvs.kvc.uuid_to_domname(model.uniq_key) if not domname: return web.notfound() self.view.is_creatable = kvs.isSupportedDomain(domname) try: snapshot_list = kvs.listNames(domname)[domname] except: pass finally: kvs.finish() if not snapshot_id in snapshot_list: self.logger.debug(_("The specified snapshot does not exist in database. - %s") % snapshot_id) # ignore snapshots that is not in database. #return web.notfound() action_cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_APPLY_SNAPSHOT), {"name" : domname, "id" : snapshot_id}) cmdname = 'Apply Snapshot' _jobgroup = JobGroup(cmdname, karesansui.sheconf['env.uniqkey']) _job = Job('%s command' % cmdname, 0, action_cmd) _jobgroup.jobs.append(_job) _machine2jobgroup = m2j_new(machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) save_job_collaboration(self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) self.view.currentsnapshot = snapshot return web.accepted(url=web.ctx.path)
def _PUT(self, *param, **params): """<comment-ja> Japanese Comment </comment-ja> <comment-en> TODO: English Comment </comment-en> """ (host_id, guest_id) = self.chk_guestby1(param) if guest_id is None: return web.notfound() if not validates_graphics(self): return web.badrequest(self.view.alert) model = findbyguest1(self.orm, guest_id) # virt kvc = KaresansuiVirtConnection() try: domname = kvc.uuid_to_domname(model.uniq_key) if not domname: return web.conflict(web.ctx.path) virt = kvc.search_kvg_guests(domname)[0] info = virt.get_graphics_info()["setting"] used_ports = kvc.list_used_graphics_port() origin_port = info["port"] finally: kvc.close() options = {} options["name"] = domname if self.input.change_passwd == "random": options["random-passwd"] = None elif self.input.change_passwd == "empty": options["passwd"] = "" options["port"] = self.input.port options["listen"] = self.input.listen options["keymap"] = self.input.keymap options["type"] = self.input.graphics_type if int(self.input.port) != origin_port and int(self.input.port) in used_ports: return web.badrequest("Graphics port number has been already used by other service. - port=%s" % (self.input.port,)) _cmd = dict2command("%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_SET_GRAPHICS), options) cmdname = "Set Graphics" _jobgroup = JobGroup(cmdname, karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('%s command' % cmdname, 0, _cmd)) _machine2jobgroup = m2j_new(machine=model, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=self.me, modified_user=self.me, ) save_job_collaboration(self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup, ) return web.created(None)