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 _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, *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
echo=True, echo_pool=False ) metadata = sqlalchemy.MetaData(bind=engine) reload_mapper(metadata) metadata.drop_all() metadata.create_all() Session = sqlalchemy.orm.sessionmaker(bind=engine, autoflush=False) session = Session() # INSERT from karesansui.lib.crypt import sha1encrypt (password, salt) = sha1encrypt(u'password') from karesansui.lib.utils import uni_force _m_u = User(u'hoge@localhost', uni_force(password), unicode(salt, 'utf-8'), u'ja', u'ja_JP') session.add(_m_u) session.commit() # SELECT One u = session.query(User).filter(User.email == u'hoge@localhost').one() print _m_u.__repr__() # UPDATE _m_u.email = u'foo@localhost' session.add(_m_u) session.commit() # DELETE _m_u = session.query(User).filter(User.email == u'foo@localhost').one() session.delete(_m_u) session.commit()
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 _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