def GET(self, path): web.header('Content-Type', 'text; char=UTF-8') path = str(path) j = web.input() if path == '/': return '\n'.join('%s=(%s, %s)' % (path, str(_locks[path].granted), str(_locks[path].last_used),) for path in sorted(_locks)) elif path not in _locks and 'lock_id' not in j: return 'OK' elif 'lock_id' in j: lock = _locks.get(path, -1) try: if int(j['lock_id']) == lock.lock_id: _update_lock(path) return 'OK' else: raise Exception("Bad lock_id") except (Exception, ValueError) as e: _cancel_lock(path) raise web.conflict() elif _lock_expired(path): _cancel_lock(path) return 'OK' raise web.conflict()
def _setReturnCode(self, code): """Set the return code :param: code :type code: integer or string returns success: [True|False] """ success = False if code in (200, "200", "ok"): web.ok() success = True elif code in (201, "201", "created"): web.created() success = True elif code in (400, "400", "badrequest"): web.badrequest() elif code in (401, "401", "unauthorized"): web.unauthorized() elif code in (404, "404", "notfound"): web.notfound() elif code in (409, "409", "conflict"): web.conflict() elif code in (500, "500", "internalerror"): web.internalerror() if success: logging.debug("[LayMan][_setReturnCode] Code: '%s'" % code) else: logging.error("[LayMan][_setReturnCode] Code: '%s'" % code) return success
def _POST(self, *param, **params): if not validates_icon(self): self.logger.debug("Create Icon is failed, Invalid input value") return web.badrequest(add_prefix(self.view.alert, "400")) icon_filevalue = self.input.multi_icon.value icon_filename = "%s.%s" % (uniq_filename(), imghdr.what(None, icon_filevalue)) if is_path(icon_filename) is True: return web.badrequest("Not to include the path.") icon_realpath = ICON_DIR_TPL % (karesansui.dirname, icon_filename) icon_webpath = ICON_DIR_TPL % (web.ctx.homepath, icon_filename) if os.path.exists(icon_realpath): web.conflict(icon_webpath, add_prefix("icon already exists", "409")) try: create_file(icon_realpath, icon_filevalue) except IOError, ioe: self.logger.error("Failed to write icon file. - filename=%s" % icon_filename) return web.internalerror( add_prefix("Failed to create icon file.", "500"))
def _PUT(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() if not validates_host_edit(self): self.logger.debug("Update Host OS is failed, Invalid input value.") return web.badrequest(self.view.alert) host = findbyhost1(self.orm, host_id) cmp_host = findby1name(self.orm, self.input.m_name) if cmp_host is not None and int(host_id) != cmp_host.id: self.logger.debug("Update Host OS is failed, " "Already exists name" "- %s, %s" % (host, cmp_host)) return web.conflict(web.ctx.path) hostname_check = findby1hostname(self.orm, self.input.m_hostname) if hostname_check is not None and int(host_id) != hostname_check.id: return web.conflict(web.ctx.path) if is_param(self.input, "m_hostname"): host.hostname = self.input.m_hostname if is_param(self.input, "note_title"): host.notebook.title = self.input.note_title if is_param(self.input, "note_value"): host.notebook.value = self.input.note_value if is_param(self.input, "m_name"): host.name = self.input.m_name # Icon icon_filename = None if is_param(self.input, "icon_filename", empty=True): host.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)) host.tags = _tags host.modified_user = self.me m_update(self.orm, host) return web.seeother(web.ctx.path)
def checked_data(self, validate_method=None, **kwargs): try: data = kwargs.pop('data', web.data()) if validate_method: method = validate_method else: method = self.validator.validate valid_data = method(data, **kwargs) except ( errors.InvalidInterfacesInfo, errors.InvalidMetadata ) as exc: notifier.notify("error", str(exc)) raise web.badrequest(message=str(exc)) except ( errors.AlreadyExists ) as exc: err = web.conflict() err.message = exc.message raise err except ( errors.InvalidData, Exception ) as exc: raise web.badrequest(message=str(exc)) return valid_data
def _PUT(self, *param, **params): tag_id = param[0] if not validates_param_id(self, tag_id): self.logger.debug("Failed to update tag. The value of parameter is invalid.") return web.badrequest(self.view.alert) if not validates_tag(self): self.logger.debug("Failed to update tag. The value of input is invalid.") return web.badrequest(self.view.alert) tag = findby1(self.orm, tag_id) if not tag: self.logger.debug("Failed to update tag. No such tag - id=%s" % tag_id) return web.notfound() cmp_tag = findby1name(self.orm, self.input.name) if not cmp_tag is None: if cmp_tag.id != tag.id: self.logger.debug("Failed to update tag. The same tag already exist - id='%s'" % (cmp_tag.id)) return web.conflict(web.ctx.path) tag.name = self.input.name update(self.orm, tag) return web.seeother(web.ctx.path)
def _PUT(self, *param, **params): tag_id = param[0] if not validates_param_id(self, tag_id): self.logger.debug( "Failed to update tag. The value of parameter is invalid.") return web.badrequest(self.view.alert) if not validates_tag(self): self.logger.debug( "Failed to update tag. The value of input is invalid.") return web.badrequest(self.view.alert) tag = findby1(self.orm, tag_id) if not tag: self.logger.debug("Failed to update tag. No such tag - id=%s" % tag_id) return web.notfound() cmp_tag = findby1name(self.orm, self.input.name) if not cmp_tag is None: if cmp_tag.id != tag.id: self.logger.debug( "Failed to update tag. The same tag already exist - id='%s'" % (cmp_tag.id)) return web.conflict(web.ctx.path) tag.name = self.input.name update(self.orm, tag) return web.seeother(web.ctx.path)
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 _PUT(self, *param, **params): user_id = param[0] if not validates_param_id(self, user_id): self.logger.debug("Failed to update account. the value of parameter is invalid.") return web.notfound(self.view.alert) if not validates_user(self): self.logger.debug("Failed to update account. the value of input is invalid.") return web.badrequest(self.view.alert) user = findby1(self.orm, user_id) if not user: self.logger.debug("Failed to update account. No such account - id=%s" % user_id) return web.notfound() cmp_user = findby1email(self.orm, self.input.email) if not cmp_user is None: if int(user_id) != cmp_user.id: self.logger.debug("Failed to update account. The same mail address '%s' already exist - user='******'" % (self.input.email, cmp_user.nickname)) return web.conflict(web.ctx.path) user.nickname = self.input.nickname user.email = self.input.email user.languages = self.input.languages if not is_empty(self.input.new_password): if compare_password(self, user) == False: return web.badrequest(self.view.alert) (password, salt) = sha1encrypt(self.input.new_password) user.password = password user.salt = salt update(self.orm, user) return web.seeother(web.ctx.path)
def _PUT(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() if not validates_server(self): self.logger.debug("Update server setting failed. Did not validate.") return web.badrequest(self.view.alert) try: conf = os.environ.get("KARESANSUI_CONF") _K2V = K2V(conf) config = _K2V.read() tmp_unique_key = config["application.uniqkey"] # make whether unique key is unique uniq_key_check = findbyalluniquekey(self.orm, self.input.uniqkey) if uniq_key_check != [] and config["application.uniqkey"] != self.input.uniqkey: self.logger.debug("Update unique key failed, Already exists Unique key - %s" % uniq_key_check[0].id) return web.conflict(web.ctx.path) lighttpdconf_path = set_server_conf(config, self.input, self.orm) _K2V.write(config) except (IOError, KaresansuiGadgetException), kge: self.logger.debug(kge) raise KaresansuiGadgetException, kge
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 PUT(self, name): if not re.match(r'', name): raise web.conflict() with Transaction(models.Srv4FileStats) as trans: res = models.CatalogRelease.selectBy(name=name, connection=trans) if res.count(): return cjson.encode('%s already exists' % name) models.CatalogRelease(name=name, connection=trans) return cjson.encode('%s has been created' % name)
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 GET(self, filepath): #Return server with directory in which filepath is located. #filepath is the actual file path web.header('Content-Type', 'text/plain; charset=UTF-8') filepath = str(filepath) i = web.input() if filepath == '/': # To print dirs/lock return '\n'.join('%s=(%s, %s)' % (filepath, str(_locks[filepath].granted), # list of files granted str(_locks[filepath].last_used),) # list of last used file for filepath in sorted(_locks)) elif filepath not in _locks and 'lock_id' not in i: return 'OK' # Since no locks, just return OK elif 'lock_id' in i: lock = _locks.get(filepath, -1) # If lock_id is requested and filepath is locked try: if int(i['lock_id']) == lock.lock_id: _update_lock(filepath) #update last used file return 'OK' else: raise Exception("Bad lock_id") #Error except (Exception, ValueError) as e: # logging exception(e) _revoke_lock(filepath) raise web.conflict() elif _lock_expired(filepath): _revoke_lock(filepath) return 'OK' # IF its already locked, or wrong lock_id- error raise web.conflict()
def GET(self, filepath): web.header('Content-Type', 'text/plain; charset=UTF-8') filepath = str(filepath) i = web.input() if filepath == '/': p = lock[filepath] # just a list of file=(granted, last_used) return '\n'.join('%s=(%s, %s)' % ( filepath, str(p.granted), str(p.last_used), ) for filepath in sorted(lock)) elif filepath not in lock and 'lock_id' not in i: return 'OK' elif 'lock_id' in i: locks = lock.get(filepath, -1) try: if int(i['lock_id']) == locks.lock_id: t = datetime.datetime.now() logging.info('Update lock on %s from %s to %s.', filepath, lock[filepath].last_used, t) l = lock[filepath] l = lock(l.lock_id, l.granted, t) lock[filepath] = l return 'OK' else: raise Exception("Bad lock_id") except (Exception, ValueError) as e: # logging.exception(e) if filepath in lock: logging.info('Revoking lock on %s.', filepath) del lock[filepath] raise web.conflict() raise web.conflict()
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 DELETE(self, name): with Transaction(models.Srv4FileStats) as trans: try: o = models.CatalogRelease.selectBy(name=name, connection=trans).getOne() except sqlobject.main.SQLObjectNotFound: raise web.notfound() res = models.Srv4FileInCatalog.select(models.Srv4FileInCatalog.q.catrel==o, connection=trans) if res.count(): # There still are packages in this catalog. We cannot remove it. raise web.conflict() o.destroySelf() return cjson.encode('%s has been deleted' % name)
def POST(self, device_name, from_state, to_state): args, body = templeton.handlers.get_request_parms() if 'pxe_config' in body: pxe_config = body['pxe_config'] boot_config = body.get('boot_config', '') else: pxe_config = None boot_config = None success = mozpool.lifeguard.driver.conditional_state_change( device_name, from_state, to_state, pxe_config, boot_config) if not success: raise web.conflict() return {}
def _POST(self, *param, **params): if not validates_icon(self): self.logger.debug("Create Icon is failed, Invalid input value") return web.badrequest(add_prefix(self.view.alert, "400")) icon_filevalue = self.input.multi_icon.value icon_filename = "%s.%s" % (uniq_filename(), imghdr.what(None, icon_filevalue)) if is_path(icon_filename) is True: return web.badrequest("Not to include the path.") icon_realpath = ICON_DIR_TPL % (karesansui.dirname, icon_filename) icon_webpath = ICON_DIR_TPL % (web.ctx.homepath, icon_filename) if os.path.exists(icon_realpath): web.conflict(icon_webpath, add_prefix("icon already exists", "409")) try: create_file(icon_realpath, icon_filevalue) except IOError, ioe: self.logger.error("Failed to write icon file. - filename=%s" % icon_filename) return web.internalerror(add_prefix("Failed to create icon file.", "500"))
def _POST(self, *param, **params): if not validates_tag(self): self.logger.debug("Failed to create tag. The value of input is invalid.") return web.badrequest(self.view.alert) tag = findby1name(self.orm, self.input.name) if tag: self.logger.debug("Failed to create tag. The same tag already exist - id='%s'" % (tag.id)) return web.conflict(web.ctx.path) new_tag = new(self.input.name) save(self.orm, new_tag) return web.created(None)
def GET(self, filepath): web.header('Content-Type', 'text/plain; charset=UTF-8') filepath = str(filepath) i = web.input() if filepath == '/': return '' elif _lock_expired(filepath): _revoke_lock(filepath) return 'OK' #Raise conflict in case already locked raise web.conflict()
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 checked_data(self, validate_method=None): try: if validate_method: data = validate_method(web.data()) else: data = self.validator.validate(web.data()) except (errors.InvalidInterfacesInfo, errors.InvalidMetadata) as exc: notifier.notify("error", str(exc)) raise web.badrequest(message=str(exc)) except (errors.AlreadyExists) as exc: err = web.conflict() err.message = exc.message raise err except (errors.InvalidData, Exception) as exc: raise web.badrequest(message=str(exc)) return data
def checked_data(self, validate_method=None, **kwargs): try: data = kwargs.pop('data', web.data()) method = validate_method or self.validator.validate valid_data = method(data, **kwargs) except (errors.InvalidInterfacesInfo, errors.InvalidMetadata) as exc: notifier.notify("error", str(exc)) raise web.badrequest(message=str(exc)) except (errors.AlreadyExists) as exc: err = web.conflict() err.message = exc.message raise err except (errors.InvalidData, Exception) as exc: raise web.badrequest(message=str(exc)) return valid_data
def _POST(self, *param, **params): if not validates_tag(self): self.logger.debug( "Failed to create tag. The value of input is invalid.") return web.badrequest(self.view.alert) tag = findby1name(self.orm, self.input.name) if tag: self.logger.debug( "Failed to create tag. The same tag already exist - id='%s'" % (tag.id)) return web.conflict(web.ctx.path) new_tag = new(self.input.name) save(self.orm, new_tag) return web.created(None)
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 _POST(self, *param, **params): if not validates_user(self): self.logger.debug("Failed to create account. the values of input are invalid.") return web.badrequest(self.view.alert) user = findby1email(self.orm, self.input.email) if user: self.logger.debug("Failed to create account. The same mail address '%s' already exist - user='******'" % (self.input.email, user.nickname)) return web.conflict(web.ctx.path) (password, salt) = sha1encrypt(self.input.new_password) new_user = new(self.input.email, password, salt, self.input.nickname, self.input.languages ) save(self.orm, new_user) return web.created(None)
def _PUT(self, *param, **params): user_id = param[0] if not validates_param_id(self, user_id): self.logger.debug( "Failed to update account. the value of parameter is invalid.") return web.notfound(self.view.alert) if not validates_user(self): self.logger.debug( "Failed to update account. the value of input is invalid.") return web.badrequest(self.view.alert) user = findby1(self.orm, user_id) if not user: self.logger.debug( "Failed to update account. No such account - id=%s" % user_id) return web.notfound() cmp_user = findby1email(self.orm, self.input.email) if not cmp_user is None: if int(user_id) != cmp_user.id: self.logger.debug( "Failed to update account. The same mail address '%s' already exist - user='******'" % (self.input.email, cmp_user.nickname)) return web.conflict(web.ctx.path) user.nickname = self.input.nickname user.email = self.input.email user.languages = self.input.languages if not is_empty(self.input.new_password): if compare_password(self, user) == False: return web.badrequest(self.view.alert) (password, salt) = sha1encrypt(self.input.new_password) user.password = password user.salt = salt update(self.orm, user) return web.seeother(web.ctx.path)
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 _PUT(self, *param, **params): if not validates_me(self): return web.badrequest(self.view.alert) if self.me.id != int(self.input.id): self.logger.info("Update account is failed, " "posted ID parameter is different from me ID " "- posted ID %s, me ID %s" % (self.input.id, self.me.id)) return web.badrequest(_('ID is wrong. Your ID is not %s.') % self.input.id) me = findby1(self.orm, self.input.id) if not me: self.logger.debug("Update account is failed, " "Did not exist account - id=%s" % self.input.id) return web.notfound() cmp_user = findby1email(self.orm, self.input.email) if not cmp_user is None: if me.id != cmp_user.id: self.logger.info("Update account is failed, " "Already exists mail address " "- %s, %s" % (me, cmp_user)) return web.conflict(web.ctx.path) if self.input.password: if compare_password(self, self.me) is False: return web.badrequest(self.view.alert) (password, salt) = sha1encrypt(self.input.new_password) me.password = password me.salt = salt me.email = self.input.email me.languages = self.input.languages me.nickname = self.input.nickname dba_update(self.orm, me) self.me = me return web.seeother(web.ctx.path)
def checked_data(self, validate_method=None): try: if validate_method: data = validate_method(web.data()) else: data = self.validator.validate(web.data()) except ( errors.InvalidInterfacesInfo, errors.InvalidMetadata ) as exc: notifier.notify("error", str(exc)) raise web.badrequest(message=str(exc)) except ( errors.AlreadyExists ) as exc: err = web.conflict() err.message = exc.message raise err except ( errors.InvalidData, Exception ) as exc: raise web.badrequest(message=str(exc)) return data
def _PUT(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() if not validates_host_edit(self): self.logger.debug("Update Host OS is failed, Invalid input value.") return web.badrequest(self.view.alert) host = findbyhost1(self.orm, host_id) cmp_host = findby1name(self.orm, self.input.m_name) if cmp_host is not None and int(host_id) != cmp_host.id: self.logger.debug("Update Host OS is failed, " "Already exists name" "- %s, %s" % (host, cmp_host)) return web.conflict(web.ctx.path) if self.input.m_connect_type == "karesansui": hostname_check = findby1hostname(self.orm, self.input.m_hostname) if hostname_check is not None and int( host_id) != hostname_check.id: return web.conflict(web.ctx.path) if self.input.m_connect_type == "karesansui": host.attribute = MACHINE_ATTRIBUTE['HOST'] if is_param(self.input, "m_hostname"): host.hostname = self.input.m_hostname if self.input.m_connect_type == "libvirt": host.attribute = MACHINE_ATTRIBUTE['URI'] if is_param(self.input, "m_uri"): segs = uri_split(self.input.m_uri) if is_param(self.input, "m_auth_user") and self.input.m_auth_user: segs["user"] = self.input.m_auth_user if is_param(self.input, "m_auth_passwd") and self.input.m_auth_passwd: segs["passwd"] = self.input.m_auth_passwd host.hostname = uri_join(segs) if is_param(self.input, "note_title"): host.notebook.title = self.input.note_title if is_param(self.input, "note_value"): host.notebook.value = self.input.note_value if is_param(self.input, "m_name"): host.name = self.input.m_name # Icon icon_filename = None if is_param(self.input, "icon_filename", empty=True): host.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)) host.tags = _tags host.modified_user = self.me m_update(self.orm, host) return web.seeother(web.ctx.path)
def _POST(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() model = findbyhost1(self.orm, host_id) if model.attribute == 0 and model.hypervisor == MACHINE_HYPERVISOR[ "XEN"]: uri = uris["XEN"] elif model.attribute == 0 and model.hypervisor == MACHINE_HYPERVISOR[ "KVM"]: uri = uris["KVM"] else: uri = None if not validates_guest_add(self): return web.badrequest(self.view.alert) try: try: self.kvc = KaresansuiVirtConnection(uri) active_guests = self.kvc.list_active_guest() inactive_guests = self.kvc.list_inactive_guest() used_graphics_ports = self.kvc.list_used_graphics_port() used_mac_addrs = self.kvc.list_used_mac_addr() mem_info = self.kvc.get_mem_info() if is_param(self.input, "vm_mem_size"): if mem_info['host_free_mem'] < int(self.input.vm_mem_size): return web.badrequest( _("Space not enough to allocate guest memory.")) if is_param(self.input, "pool_type") and \ self.input.pool_type != "block" and \ is_param(self.input, "pool_dir"): target_path = self.kvc.get_storage_pool_targetpath( self.input.pool_dir) if target_path: # disk if not chk_create_disk(target_path, self.input.vm_disk_size): partition = get_partition_info(target_path, header=False) return web.badrequest( _("No space available to create disk image in '%s' partition." ) % partition[5][0]) except: raise finally: del self.kvc # Check on whether value has already been used # Guest OS if (self.input.domain_name in active_guests) \ or (self.input.domain_name in inactive_guests): return web.conflict(web.ctx.path, "Guest OS is already there.") # Graphics Port Number if (int(self.input.vm_graphics_port) in used_graphics_ports): return web.conflict(web.ctx.path, "Graphics Port is already there.") # MAC Address if (self.input.vm_mac in used_mac_addrs): return web.conflict(web.ctx.path, "MAC Address is already there.") uuid = string_from_uuid(generate_uuid()) options = {} options['uuid'] = uuid if is_param(self.input, "domain_name"): options['name'] = self.input.domain_name if is_param(self.input, "vm_mem_size"): options['mem-size'] = self.input.vm_mem_size if is_param(self.input, "vm_kernel"): options['kernel'] = self.input.vm_kernel if is_param(self.input, "vm_initrd"): options['initrd'] = self.input.vm_initrd if is_param(self.input, "vm_iso"): options['iso'] = self.input.vm_iso if is_param(self.input, "keymap"): options['keymap'] = self.input.keymap is_create = False if is_param(self.input, "pool_type"): if is_param(self.input, "bus_type"): options['bus'] = self.input.bus_type if self.input.pool_type == "dir" or self.input.pool_type == "fs": # create volume is_create = True options['disk-format'] = self.input.disk_format options["storage-pool"] = self.input.pool_dir options["storage-volume"] = options[ 'name'] # default domain name options['disk-size'] = self.input.vm_disk_size elif self.input.pool_type == "block": # iscsi block device (iscsi_pool, iscsi_volume) = self.input.pool_dir.split("/", 2) options["storage-pool"] = iscsi_pool options["storage-volume"] = iscsi_volume else: return web.badrequest() else: return web.badrequest() if is_param(self.input, "vm_graphics_port"): options['graphics-port'] = self.input.vm_graphics_port if is_param(self.input, "vm_mac"): options['mac'] = self.input.vm_mac if is_param(self.input, "vm_extra"): options['extra'] = self.input.vm_extra if is_param(self.input, "nic_type"): if self.input.nic_type == "phydev": options['interface-format'] = "b:" + self.input.phydev elif self.input.nic_type == "virnet": options['interface-format'] = "n:" + self.input.virnet if int(self.input.m_hypervisor) == MACHINE_HYPERVISOR['XEN']: i_hypervisor = MACHINE_HYPERVISOR['XEN'] options['type'] = u"XEN" elif int(self.input.m_hypervisor) == MACHINE_HYPERVISOR['KVM']: i_hypervisor = MACHINE_HYPERVISOR['KVM'] options['type'] = u"KVM" else: return web.badrequest("This is not the hypervisor.") host = findbyhost1(self.orm, host_id) # notebook 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) # tags _tags = None 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)) # Icon icon_filename = None if is_param(self.input, "icon_filename", empty=True): icon_filename = self.input.icon_filename _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=i_hypervisor, notebook=_notebook, tags=_tags, icon=icon_filename, is_deleted=False, parent=host, ) ret = regist_guest( self, _guest, icon_filename, VIRT_COMMAND_CREATE_GUEST, options, ('Create Guest', 'Create Guest'), { "name": options['name'], "pool": options["storage-pool"], "volume": options["uuid"], }, is_create, ) if ret is True: return web.accepted() else: return False
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 _POST(self, *param, **params): if not validates_host_add(self): return web.badrequest(self.view.alert) uniq_key_check = findby1uniquekey(self.orm, self.input.m_uuid) if uniq_key_check is not None and config['application.uniqkey'] != self.input.m_uuid: return web.conflict(web.ctx.path) hostname_check = findby1hostname(self.orm, self.input.m_hostname) if hostname_check is not None: return web.conflict(web.ctx.path) # notebook 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) # tags _tags = None 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)) uniq_key = self.input.m_uuid name = self.input.m_name hostname = self.input.m_hostname model = findby1uniquekey(self.orm, uniq_key, is_deleted = True) if model is None: host = m_new(created_user=self.me, modified_user=self.me, uniq_key=uni_force(uniq_key), name=name, hostname=hostname, attribute=MACHINE_ATTRIBUTE['HOST'], hypervisor=MACHINE_HYPERVISOR['REAL'], notebook=_notebook, tags=_tags, icon=None, is_deleted=False) m_save(self.orm, host) return web.created(None) else: model.name = name model.hostname = hostname model.uniq_key = uniq_key model.notebook.title = note_title model.notebook.value = note_value model.tags = _tags model.is_deleted = False m_update(self.orm, model) return web.created(None)
def POST(self): messages = [] # The 'srv4_file={}' is necessary for the .filename attribute to work. # Reference: http://webpy.org/cookbook/fileupload x = web.input(srv4_file={}) for field_name in ('srv4_file', 'md5_sum', 'basename'): if field_name not in x: raise web.badrequest('srv4_file not found') web.header( 'Content-type', 'application/x-vnd.opencsw.pkg;type=upload-results') data_md5_sum = FileMd5(x['srv4_file'].file) declared_md5_sum = x['md5_sum'] basename = x['basename'] save_attempt = False error = False if declared_md5_sum != data_md5_sum: raise web.conflict() try: srv4 = models.Srv4FileStats.selectBy(md5_sum=data_md5_sum).getOne() except sqlobject.main.SQLObjectNotFound: messages.append("File %s not found in the db." % data_md5_sum) raise web.preconditionfailed() if not srv4.use_to_generate_catalogs: messages.append( '%r is not a package meant to be in catalogs ' '(e.g. not CSW).' % basename) raise web.preconditionfailed() target_path = os.path.join(ALLPKGS_DIR, basename) lock_path = os.path.join('/tmp', '{0}.lock'.format(basename)) target_file_lock = lockfile.FileLock(lock_path) # Have to get this lock to avoid two processes writing to the same file at # the same time. with target_file_lock: on_disk_md5_sum = data_md5_sum try: with open(target_path, 'r') as fd: on_disk_md5_sum = FileMd5(fd) except IOError: # The file doesn't exist yet. That's OK. pass if data_md5_sum != on_disk_md5_sum: messages.append( '%r already exists in the database with md5 sum %s. ' 'The uploaded file is %s. We cannot overwrite the old filename ' 'with new content.' % (basename, on_disk_md5_sum, data_md5_sum)) raise web.conflict() save_attempt = True # FieldStorage by default unlinks the temporary local file as soon as # it's been opened. Therefore, we have to take care of writing data # to the target location in an atomic way. fd, tmp_filename = tempfile.mkstemp(dir=ALLPKGS_DIR) CopyFile(x['srv4_file'].file, fd) os.close(fd) target_path = os.path.join(ALLPKGS_DIR, basename) os.rename(tmp_filename, target_path) # Since mkstemp creates files with mode 0600 by default: os.chmod(target_path, 0644) messages.append({ "received_md5": data_md5_sum, "declared_md5": declared_md5_sum, "save_attempt": save_attempt, }) response = cjson.encode(messages) web.header('Content-Length', str(len(response))) return response
def GET(self, filepath): """If filepath == '/' just print all the dirs/lock. Else if filepath isn't locked, return 200 OK (and no lock_id is provided) Else If lock_id is in the request and filepath is locked with this id: - return OK - update the last_used field Else: '409 Conflict' """ web.header('Content-Type', 'text/plain; charset=UTF-8') filepath = str(filepath) i = web.input() if filepath == '/': # just a list of file=(granted, last_used) return '\n'.join('%s=(%s, %s)' % (filepath, str(_locks[filepath].granted), str(_locks[filepath].last_used),) for filepath in sorted(_locks)) elif filepath not in _locks and 'lock_id' not in i: return 'OK' elif 'lock_id' in i: lock = _locks.get(filepath, -1) try: if int(i['lock_id']) == lock.lock_id: # GET shouldn't be used to change state of server # but it's a "good" idea to do it here, because # when a file server will ask the LockServer if # a file is locked it's because it'll have to # modify it so instead of: # file server ask if locked (and with the good lock) # and just after ask to update the last_used part # of the lock # we just ask if locked and update at the same time # easier, and reduce network access # # moreover as the lockid is only known by the file # server and the client there's no risk of CSRF # (assuming that the security service is implemented) _update_lock(filepath) return 'OK' else: raise Exception("Bad lock_id") except (Exception, ValueError) as e: # logging.exception(e) _revoke_lock(filepath) raise web.conflict() elif _lock_expired(filepath): # ok GET shouldn't change the state of server BUT in this # case it just update it because now we know that the lock _revoke_lock(filepath) return 'OK' # already locked, or wrong lock_id raise web.conflict()
def PUT(self, catrel_name, arch_name, osrel_name, md5_sum): """Adds package to a catalog. When pycurl calls this function, it often hangs, waiting. A fix for that is to add the 'Content-Length' header. However, it sometimes still gets stuck and I don't know why. """ if catrel_name not in CAN_UPLOAD_TO_CATALOGS: # Updates via web are allowed only for the unstable catalog. # We should return an error message instead. # Sadly, we cannot return a response body due to webpy's API # limitation. raise web.forbidden() try: if arch_name == 'all': raise web.badrequest("There is no 'all' catalog, cannot proceed.") try: srv4 = models.Srv4FileStats.selectBy(md5_sum=md5_sum).getOne() except sqlobject.main.SQLObjectNotFound: try: srv4, _ = relational_util.StatsStructToDatabaseLevelOne(md5_sum) except errors.DataError as exc: logging.warning(exc) # TODO(maciej): Add the exception to the web.conflict() call. # webpy 0.37 apparently doesn't allow that. raise web.conflict() parsed_basename = opencsw.ParsePackageFileName(srv4.basename) if parsed_basename["vendortag"] not in ("CSW", "FAKE"): raise web.badrequest( "Package vendor tag is %s instead of CSW or FAKE." % parsed_basename["vendortag"]) if not srv4.registered_level_two: relational_util.StatsStructToDatabaseLevelTwo(md5_sum, True) # Package needs to be registered for releases # This can throw CatalogDatabaseError if the db user doesn't have # enough permissions. # raise web.internalerror('Package not registered') c = checkpkg_lib.Catalog() sqo_osrel, sqo_arch, sqo_catrel = models.GetSqoTriad( osrel_name, arch_name, catrel_name) # See if there already is a package with that catalogname. res = c.GetConflictingSrv4ByCatalognameResult( srv4, srv4.catalogname, sqo_osrel, sqo_arch, sqo_catrel) if res.count() == 1: # Removing old version of the package from the catalog for pkg_in_catalog in res: srv4_to_remove = pkg_in_catalog.srv4file c.RemoveSrv4(srv4_to_remove, osrel_name, arch_name, catrel_name) # See if there already is a package with that pkgname. res = c.GetConflictingSrv4ByPkgnameResult( srv4, srv4.pkginst.pkgname, sqo_osrel, sqo_arch, sqo_catrel) if res.count() == 1: # Removing old version of the package from the catalog for pkg_in_catalog in res: srv4_to_remove = pkg_in_catalog.srv4file c.RemoveSrv4(srv4_to_remove, osrel_name, arch_name, catrel_name) # This is set by basic HTTP auth. username = web.ctx.env.get('REMOTE_USER') c.AddSrv4ToCatalog(srv4, osrel_name, arch_name, catrel_name, who=username) web.header( 'Content-type', 'application/x-vnd.opencsw.pkg;type=catalog-update') response = cjson.encode([ u"Added to catalog %s %s %s" % (catrel_name, arch_name, osrel_name), u"%s" % srv4.basename, u"%s" % srv4.md5_sum, ]) web.header('Content-Length', len(response)) return response except ( checkpkg_lib.CatalogDatabaseError, sqlobject.dberrors.OperationalError) as exc: web.header( 'Content-Type', 'application/x-vnd.opencsw.pkg;type=error-message') response = cjson.encode({ "error_message": unicode(exc), }) web.header('Content-Length', str(len(response))) raise web.badrequest(response)
def POST(self, device_name, from_state, to_state): success = mozpool.lifeguard.driver.conditional_state_change( device_name, from_state, to_state) if not success: raise web.conflict() return {}
def _POST(self, *param, **params): if not validates_host_add(self): return web.badrequest(self.view.alert) if self.input.m_connect_type == "karesansui": uniq_key_check = findby1uniquekey(self.orm, self.input.m_uuid) if uniq_key_check is not None and config['application.uniqkey'] != self.input.m_uuid: return web.conflict(web.ctx.path) hostname_check = findby1hostname(self.orm, self.input.m_hostname) if hostname_check is not None: return web.conflict(web.ctx.path) # notebook 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) # tags _tags = None 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)) name = self.input.m_name if self.input.m_connect_type == "karesansui": uniq_key = self.input.m_uuid attribute = MACHINE_ATTRIBUTE['HOST'] if is_param(self.input, "m_hostname"): hostname = self.input.m_hostname if self.input.m_connect_type == "libvirt": uniq_key = string_from_uuid(generate_uuid()) attribute = MACHINE_ATTRIBUTE['URI'] if is_param(self.input, "m_uri"): segs = uri_split(self.input.m_uri) if is_param(self.input, "m_auth_user") and self.input.m_auth_user: segs["user"] = self.input.m_auth_user if is_param(self.input, "m_auth_passwd") and self.input.m_auth_passwd: segs["passwd"] = self.input.m_auth_passwd hostname = uri_join(segs) model = findby1uniquekey(self.orm, uniq_key, is_deleted = True) if model is None: host = m_new(created_user=self.me, modified_user=self.me, uniq_key=uni_force(uniq_key), name=name, hostname=hostname, attribute=attribute, hypervisor=MACHINE_HYPERVISOR['REAL'], notebook=_notebook, tags=_tags, icon=None, is_deleted=False) m_save(self.orm, host) return web.created(None) else: model.name = name model.hostname = hostname model.uniq_key = uniq_key model.notebook.title = note_title model.notebook.value = note_value model.tags = _tags model.is_deleted = False m_update(self.orm, model) return web.created(None)
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 _POST(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() model = findbyhost1(self.orm, host_id) uris = available_virt_uris() if model.attribute == 0 and model.hypervisor == 1: uri = uris["XEN"] elif model.attribute == 0 and model.hypervisor == 2: uri = uris["KVM"] else: uri = None if not validates_guest_add(self): return web.badrequest(self.view.alert) try: try: self.kvc = KaresansuiVirtConnection(uri) active_guests = self.kvc.list_active_guest() inactive_guests = self.kvc.list_inactive_guest() used_graphics_ports = self.kvc.list_used_graphics_port() used_mac_addrs = self.kvc.list_used_mac_addr() mem_info = self.kvc.get_mem_info() if is_param(self.input, "vm_mem_size"): if mem_info['host_free_mem'] < int(self.input.vm_mem_size): return web.badrequest(_("Space not enough to allocate guest memory.")) if is_param(self.input, "pool_type") and \ self.input.pool_type != "block" and \ is_param(self.input, "pool_dir"): target_path = self.kvc.get_storage_pool_targetpath(self.input.pool_dir) if target_path: # disk if not chk_create_disk(target_path, self.input.vm_disk_size): partition = get_partition_info(target_path, header=False) return web.badrequest(_("No space available to create disk image in '%s' partition.") % partition[5][0]) except: raise finally: del self.kvc # Check on whether value has already been used # Guest OS if (self.input.domain_name in active_guests) \ or (self.input.domain_name in inactive_guests): return web.conflict(web.ctx.path, "Guest OS is already there.") # Graphics Port Number if(int(self.input.vm_graphics_port) in used_graphics_ports): return web.conflict(web.ctx.path, "Graphics Port is already there.") # MAC Address if(self.input.vm_mac in used_mac_addrs): return web.conflict(web.ctx.path, "MAC Address is already there.") uuid = string_from_uuid(generate_uuid()) options = {} options['uuid'] = uuid if is_param(self.input, "domain_name"): options['name'] = self.input.domain_name if is_param(self.input, "vm_mem_size"): options['mem-size'] = self.input.vm_mem_size if is_param(self.input, "vm_kernel"): options['kernel'] = self.input.vm_kernel if is_param(self.input, "vm_initrd"): options['initrd'] = self.input.vm_initrd if is_param(self.input, "vm_iso"): options['iso'] = self.input.vm_iso if is_param(self.input, "keymap"): options['keymap'] = self.input.keymap is_create = False if is_param(self.input, "pool_type"): if is_param(self.input, "bus_type"): options['bus'] = self.input.bus_type if self.input.pool_type == "dir" or self.input.pool_type == "fs": # create volume is_create = True options['disk-format'] = self.input.disk_format options["storage-pool"] = self.input.pool_dir options["storage-volume"] = options['name'] # default domain name options['disk-size'] = self.input.vm_disk_size elif self.input.pool_type == "block": # iscsi block device (iscsi_pool, iscsi_volume) = self.input.pool_dir.split("/", 2) options["storage-pool"] = iscsi_pool options["storage-volume"] = iscsi_volume else: return web.badrequest() else: return web.badrequest() if is_param(self.input, "vm_graphics_port"): options['graphics-port'] = self.input.vm_graphics_port if is_param(self.input, "vm_mac"): options['mac'] = self.input.vm_mac if is_param(self.input, "vm_extra"): options['extra'] = self.input.vm_extra if is_param(self.input, "nic_type"): if self.input.nic_type == "phydev": options['interface-format'] = "b:" + self.input.phydev elif self.input.nic_type == "virnet": options['interface-format'] = "n:" + self.input.virnet if int(self.input.m_hypervisor) == MACHINE_HYPERVISOR['XEN']: i_hypervisor = MACHINE_HYPERVISOR['XEN'] options['type'] = u"XEN" elif int(self.input.m_hypervisor) == MACHINE_HYPERVISOR['KVM']: i_hypervisor = MACHINE_HYPERVISOR['KVM'] options['type'] = u"KVM" else: return web.badrequest("This is not the hypervisor.") host = findbyhost1(self.orm, host_id) # notebook 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) # tags _tags = None 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)) # Icon icon_filename = None if is_param(self.input, "icon_filename", empty=True): icon_filename = self.input.icon_filename _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=i_hypervisor, notebook=_notebook, tags=_tags, icon=icon_filename, is_deleted=False, parent=host, ) ret = regist_guest(self, _guest, icon_filename, VIRT_COMMAND_CREATE_GUEST, options, ('Create Guest', 'Create Guest'), {"name": options['name'], "pool" : options["storage-pool"], "volume" : options["uuid"], }, is_create, ) if ret is True: return web.accepted() else: return False
def _POST(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() model = findbyhost1(self.orm, host_id) uris = available_virt_uris() if model.attribute == 0 and model.hypervisor == 1: uri = uris["XEN"] elif model.attribute == 0 and model.hypervisor == 2: uri = uris["KVM"] else: uri = None if not validates_network(self): self.logger.debug("Network creation failed. Did not validate.") return web.badrequest(self.view.alert) name = self.input.name cidr = self.input.cidr dhcp_start = self.input.dhcp_start dhcp_end = self.input.dhcp_end bridge = self.input.bridge # We support only 'nat' for forward-mode. forward_mode = '' if is_param(self.input, 'forward_mode'): if self.input.forward_mode == 'nat': forward_mode = 'nat' try: autostart = self.input.autostart except: autostart = "no" # Check if the name is available (not already used). kvc = KaresansuiVirtConnection(uri) try: try: kvc.search_kvn_networks(name) self.logger.debug("Network name '%s' already used." % name) url = '%s/%s/%s.part' % (web.ctx.home, web.ctx.path, name) self.logger.debug("Returning url %s as Location." % url) return web.conflict(url) except KaresansuiVirtException, e: # OK pass finally: kvc.close() # spin off create job options = {'dhcp-start': dhcp_start, 'dhcp-end' : dhcp_end, 'bridge-name' : bridge, 'forward-mode' : forward_mode, 'autostart' : autostart, } self.logger.debug('spinning off network_create_job name=%s, cidr=%s, options=%s' % (name, cidr, options)) host = findbyhost1(self.orm, host_id) #network_create_job(self, name, cidr, host, options) options['name'] = name options['cidr'] = cidr _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_CREATE_NETWORK), options) # Job Registration _jobgroup = JobGroup('Create Network: %s' % name, karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('Create Network', 0, _cmd)) _machine2jobgroup = m2j_new(machine=host, 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.logger.debug('(Create network) Job group id==%s', _jobgroup.id) url = '%s/job/%s.part' % (web.ctx.home, _jobgroup.id) self.logger.debug('Returning Location: %s' % url) 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 _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 _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 _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 _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 PUT(self, catrel_name, arch_name, osrel_name, md5_sum): """Adds package to a catalog. When pycurl calls this function, it often hangs, waiting. A fix for that is to add the 'Content-Length' header. However, it sometimes still gets stuck and I don't know why. """ catspec = checkpkg_lib.CatalogSpec(catrel_name, arch_name, osrel_name) applogger.info('PUT %s %s', catspec, md5_sum) try: if arch_name == 'all': raise web.badrequest("There is no 'all' catalog, cannot proceed.") try: srv4 = models.Srv4FileStats.selectBy(md5_sum=md5_sum).getOne() except sqlobject.main.SQLObjectNotFound: try: srv4, _ = relational_util.StatsStructToDatabaseLevelOne(md5_sum) except errors.DataError as exc: applogger.warning(exc) # TODO(maciej): Add the exception to the web.conflict() call. # webpy 0.37 apparently doesn't allow that. raise web.conflict() parsed_basename = opencsw.ParsePackageFileName(srv4.basename) if parsed_basename["vendortag"] not in ("CSW", "FAKE"): raise web.badrequest( "Package vendor tag is %s instead of CSW or FAKE." % parsed_basename["vendortag"]) if not srv4.registered_level_two: applogger.info('Registering the %s package in the database', srv4.basename) relational_util.StatsStructToDatabaseLevelTwo(md5_sum, True) # Package needs to be registered for releases # This can throw CatalogDatabaseError if the db user doesn't have # enough permissions. # raise web.internalerror('Package not registered') c = checkpkg_lib.Catalog() sqo_osrel, sqo_arch, sqo_catrel = models.GetSqoTriad( osrel_name, arch_name, catrel_name) with Transaction(models.Srv4FileStats) as trans: applogger.info('Looking if catalog %s already contains a package ' 'with catalogname %s', catspec, srv4.catalogname) res = c.GetConflictingSrv4ByCatalognameResult( srv4, srv4.catalogname, sqo_osrel, sqo_arch, sqo_catrel, trans) if res.count() >= 1: for pkg_in_catalog in res: srv4_to_remove = pkg_in_catalog.srv4file applogger.info('Removing previous %s catalogname from the %s catalog, ' 'matched by catalogname', srv4_to_remove.catalogname, catspec) c.RemoveSrv4(srv4_to_remove, osrel_name, arch_name, catrel_name, trans) else: applogger.info('Package with the same catalogname (%s) not found in %s', srv4.catalogname, catspec) # See if there already is a package with that pkgname. res = c.GetConflictingSrv4ByPkgnameResult( srv4, srv4.pkginst.pkgname, sqo_osrel, sqo_arch, sqo_catrel, trans) if res.count() >= 1: # Removing old version of the package from the catalog for pkg_in_catalog in res: srv4_to_remove = pkg_in_catalog.srv4file applogger.info('Removing %s from %s', srv4_to_remove.basename, catspec) c.RemoveSrv4(srv4_to_remove, osrel_name, arch_name, catrel_name, trans) else: applogger.info('Package with the same pkgname (%s) not found in %s', srv4.pkginst.pkgname, catspec) # This is set by basic HTTP auth. username = web.ctx.env.get('REMOTE_USER') applogger.info('Adding %s to the %s catalog', srv4.basename, catspec) c.AddSrv4ToCatalog(srv4, osrel_name, arch_name, catrel_name, username, trans) web.header( 'Content-type', 'application/x-vnd.opencsw.pkg;type=catalog-update') response = cjson.encode([ u"Added to catalog %s %s %s" % (catrel_name, arch_name, osrel_name), u"%s" % srv4.basename, u"%s" % srv4.md5_sum, ]) web.header('Content-Length', len(response)) return response except ( checkpkg_lib.CatalogDatabaseError, sqlobject.dberrors.OperationalError) as exc: applogger.error('Failed while attempting to add %s to the %s catalog', srv4.basename, catspec) web.header( 'Content-Type', 'application/x-vnd.opencsw.pkg;type=error-message') response = cjson.encode({"error_message": unicode(exc)}) web.header('Content-Length', str(len(response))) # If we are in this state, there's a possibility that a package has been # removed from the database, and its replacesment has not been inserted. # In such case we want to make sure that the client sees the failure. # Therefore, we're throwing a HTTP internal error. raise web.internalerror(response)
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 GET(self, filepath): """If filepath == '/' just print all the dirs/lock. Else if filepath isn't locked, return 200 OK (and no lock_id is provided) Else If lock_id is in the request and filepath is locked with this id: - return OK - update the last_used field Else: '409 Conflict' """ web.header('Content-Type', 'text/plain; charset=UTF-8') filepath = str(filepath) i = web.input() if filepath == '/': # just a list of file=(granted, last_used) return '\n'.join('%s=(%s, %s)' % ( filepath, str(_locks[filepath].granted), str(_locks[filepath].last_used), ) for filepath in sorted(_locks)) elif filepath not in _locks and 'lock_id' not in i: return 'OK' elif 'lock_id' in i: lock = _locks.get(filepath, -1) try: if int(i['lock_id']) == lock.lock_id: # GET shouldn't be used to change state of server # but it's a "good" idea to do it here, because # when a file server will ask the LockServer if # a file is locked it's because it'll have to # modify it so instead of: # file server ask if locked (and with the good lock) # and just after ask to update the last_used part # of the lock # we just ask if locked and update at the same time # easier, and reduce network access # # moreover as the lockid is only known by the file # server and the client there's no risk of CSRF # (assuming that the security service is implemented) _update_lock(filepath) return 'OK' else: raise Exception("Bad lock_id") except (Exception, ValueError) as e: # logging.exception(e) _revoke_lock(filepath) raise web.conflict() elif _lock_expired(filepath): # ok GET shouldn't change the state of server BUT in this # case it just update it because now we know that the lock _revoke_lock(filepath) return 'OK' # already locked, or wrong lock_id raise web.conflict()
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 _POST(self, *param, **params): host_id = self.chk_hostby1(param) if host_id is None: return web.notfound() model = findbyhost1(self.orm, host_id) uris = available_virt_uris() if model.attribute == 0 and model.hypervisor == 1: uri = uris["XEN"] elif model.attribute == 0 and model.hypervisor == 2: uri = uris["KVM"] else: uri = None if not validates_network(self): self.logger.debug("Network creation failed. Did not validate.") return web.badrequest(self.view.alert) name = self.input.name cidr = self.input.cidr dhcp_start = self.input.dhcp_start dhcp_end = self.input.dhcp_end bridge = self.input.bridge # We support only 'nat' for forward-mode. forward_mode = '' if is_param(self.input, 'forward_mode'): if self.input.forward_mode == 'nat': forward_mode = 'nat' try: autostart = self.input.autostart except: autostart = "no" # Check if the name is available (not already used). kvc = KaresansuiVirtConnection(uri) try: try: kvc.search_kvn_networks(name) self.logger.debug("Network name '%s' already used." % name) url = '%s/%s/%s.part' % (web.ctx.home, web.ctx.path, name) self.logger.debug("Returning url %s as Location." % url) return web.conflict(url) except KaresansuiVirtException, e: # OK pass finally: kvc.close() # spin off create job options = {'dhcp-start': dhcp_start, 'dhcp-end' : dhcp_end, 'bridge-name' : bridge, 'forward-mode' : forward_mode, 'autostart' : autostart, } self.logger.debug('spinning off network_create_job name=%s, cidr=%s, options=%s' % (name, cidr, options)) host = findbyhost1(self.orm, host_id) #network_create_job(self, name, cidr, host, options) options['name'] = name options['cidr'] = cidr _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], VIRT_COMMAND_CREATE_NETWORK), options) #_jobgroup = JobGroup('Create network', karesansui.sheconf['env.uniqkey']) #_jobgroup.jobs.append(Job('Create network', 0, _cmd)) # Job Registration _jobgroup = JobGroup('Create network: %s' % name, karesansui.sheconf['env.uniqkey']) _jobgroup.jobs.append(Job('Create network', 0, _cmd)) _machine2jobgroup = m2j_new(machine=host, 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.logger.debug('(Create network) Job group id==%s', _jobgroup.id) url = '%s/job/%s.part' % (web.ctx.home, _jobgroup.id) self.logger.debug('Returning Location: %s' % url) return web.accepted()
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()