def _get_volume_path(self, pool_uri, vol): pool = pool_name_from_uri(pool_uri) pool_info = self.storagepool_lookup(pool) if pool_info['type'] == 'scsi': return self._mock_storagevolumes.scsi_volumes[vol]['path'] return MockModel._libvirt_get_vol_path(pool, vol)
def validate_integrity(self): invalid = {} # validate networks integrity invalid_networks = list( set(self.info['networks']) - set(self._get_all_networks_name())) if invalid_networks: invalid['networks'] = invalid_networks # validate storagepools integrity for disk in self.info['disks']: pool_uri = disk['pool']['name'] pool_name = pool_name_from_uri(pool_uri) if pool_name not in self._get_active_storagepools_name(): invalid['storagepools'] = [pool_name] # validate iso integrity # FIXME when we support multiples cdrom devices iso = self.info.get('cdrom') if iso: if os.path.exists(iso): st_mode = os.stat(iso).st_mode if not (stat.S_ISREG(st_mode) or stat.S_ISBLK(st_mode)): invalid['cdrom'] = [iso] elif not check_url_path(iso): invalid['cdrom'] = [iso] self.info['invalid'] = invalid return self.info
def validate_integrity(self): invalid = {} # validate networks integrity invalid_networks = list(set(self.info['networks']) - set(self._get_all_networks_name())) if invalid_networks: invalid['networks'] = invalid_networks # validate storagepools integrity for disk in self.info['disks']: pool_uri = disk['pool']['name'] pool_name = pool_name_from_uri(pool_uri) if pool_name not in self._get_active_storagepools_name(): invalid['storagepools'] = [pool_name] # validate iso integrity # FIXME when we support multiples cdrom devices iso = self.info.get('cdrom') if iso: if os.path.exists(iso): st_mode = os.stat(iso).st_mode if not (stat.S_ISREG(st_mode) or stat.S_ISBLK(st_mode)): invalid['cdrom'] = [iso] elif not check_url_path(iso): invalid['cdrom'] = [iso] self.info['invalid'] = invalid return self.info
def _get_disks_xml(self, vm_uuid): # Current implementation just allows to create disk in one single # storage pool, so we cannot mix the types (scsi volumes vs img file) storage_type = self._get_storage_type() storage_path = self._get_storage_path() base_disk_params = {'type': 'disk', 'disk': 'file', 'bus': self.info['disk_bus']} logical_disk_params = {'format': 'raw'} iscsi_disk_params = {'disk': 'block', 'format': 'raw'} scsi_disk = 'volume' if self.fc_host_support else 'block' scsi_disk_params = {'disk': scsi_disk, 'type': 'lun', 'format': 'raw', 'bus': 'scsi'} disks_xml = '' pool_name = pool_name_from_uri(self.info['storagepool']) for index, disk in enumerate(self.info['disks']): params = dict(base_disk_params) params['format'] = disk['format'] params.update(locals().get('%s_disk_params' % storage_type, {})) params['index'] = index volume = disk.get('volume') if volume is not None: params['path'] = self._get_volume_path(pool_name, volume) else: volume = "%s-%s.img" % (vm_uuid, params['index']) params['path'] = os.path.join(storage_path, volume) disks_xml += get_disk_xml(params)[1] return unicode(disks_xml, 'utf-8')
def _pool_used_by_template(self, pool_name): with self.objstore as session: templates = session.get_list('template') for tmpl in templates: t_info = session.get('template', tmpl) t_pool = pool_name_from_uri(t_info['storagepool']) if t_pool == pool_name: return True return False
def _pool_used_by_template(self, pool_name): with self.objstore as session: templates = session.get_list('template') for tmpl in templates: t_info = session.get('template', tmpl) for disk in t_info['disks']: t_pool = disk['pool']['name'] if pool_name_from_uri(t_pool) == pool_name: return True return False
def _get_storage_pool(self, pool_uri): pool_name = pool_name_from_uri(pool_uri) try: conn = self.conn.get() pool = conn.storagePoolLookupByName(pool_name.encode("utf-8")) except libvirt.libvirtError: raise InvalidParameter("KCHTMPL0004E", {'pool': pool_uri, 'template': self.name}) return pool
def template_volume_validate(self, volume, pool): kwargs = {'conn': self.conn, 'objstore': self.objstore} pool_name = pool_name_from_uri(pool['name']) if pool['type'] in ['iscsi', 'scsi']: if not volume: raise InvalidParameter("KCHTMPL0018E") storagevolumes = __import__( "wok.plugins.kimchi.model.storagevolumes", fromlist=['']) pool_volumes = storagevolumes.StorageVolumesModel( **kwargs).get_list(pool_name) if volume not in pool_volumes: raise InvalidParameter("KCHTMPL0019E", {'pool': pool_name, 'volume': volume})
def _storage_validate(self): pool_uri = self.info['storagepool'] pool_name = pool_name_from_uri(pool_uri) try: conn = self.conn.get() pool = conn.storagePoolLookupByName(pool_name.encode("utf-8")) except libvirt.libvirtError: raise InvalidParameter("KCHTMPL0004E", {'pool': pool_uri, 'template': self.name}) if not pool.isActive(): raise InvalidParameter("KCHTMPL0005E", {'pool': pool_name, 'template': self.name}) return pool
def update(self, name, params): old_t = self.lookup(name) new_t = copy.copy(old_t) new_t.update(params) if not self._validate_updated_cpu_params(new_t): raise InvalidParameter('KCHTMPL0025E') ident = name conn = self.conn.get() pool_uri = new_t.get(u'storagepool', '') if pool_uri: try: pool_name = pool_name_from_uri(pool_uri) pool = conn.storagePoolLookupByName(pool_name.encode("utf-8")) except Exception: raise InvalidParameter("KCHTMPL0004E", { 'pool': pool_uri, 'template': name }) tmp_volumes = [ disk['volume'] for disk in new_t.get('disks', []) if 'volume' in disk ] self.templates.template_volume_validate(tmp_volumes, pool) for net_name in params.get(u'networks', []): try: conn.networkLookupByName(net_name.encode('utf-8')) except Exception: raise InvalidParameter("KCHTMPL0003E", { 'network': net_name, 'template': name }) self.delete(name) try: ident = self.templates.create(new_t) except: ident = self.templates.create(old_t) raise return ident
def _storage_validate(self): pool_uri = self.info['storagepool'] pool_name = pool_name_from_uri(pool_uri) try: conn = self.conn.get() pool = conn.storagePoolLookupByName(pool_name.encode("utf-8")) except libvirt.libvirtError: raise InvalidParameter("KCHTMPL0004E", { 'pool': pool_uri, 'template': self.name }) if not pool.isActive(): raise InvalidParameter("KCHTMPL0005E", { 'pool': pool_name, 'template': self.name }) return pool
def _get_disks_xml(self, vm_uuid): # Current implementation just allows to create disk in one single # storage pool, so we cannot mix the types (scsi volumes vs img file) storage_type = self._get_storage_type() storage_path = self._get_storage_path() base_disk_params = { 'type': 'disk', 'disk': 'file', 'bus': self.info['disk_bus'] } logical_disk_params = {'format': 'raw'} iscsi_disk_params = {'disk': 'block', 'format': 'raw'} scsi_disk = 'volume' if self.fc_host_support else 'block' scsi_disk_params = { 'disk': scsi_disk, 'type': 'lun', 'format': 'raw', 'bus': 'scsi' } disks_xml = '' pool_name = pool_name_from_uri(self.info['storagepool']) for index, disk in enumerate(self.info['disks']): params = dict(base_disk_params) params['format'] = disk['format'] params.update(locals().get('%s_disk_params' % storage_type, {})) params['index'] = index volume = disk.get('volume') if volume is not None: params['path'] = self._get_volume_path(pool_name, volume) else: volume = "%s-%s.img" % (vm_uuid, params['index']) params['path'] = os.path.join(storage_path, volume) disks_xml += get_disk_xml(params)[1] return unicode(disks_xml, 'utf-8')
def update(self, name, params): old_t = self.lookup(name) new_t = copy.copy(old_t) new_t.update(params) if not self._validate_updated_cpu_params(new_t): raise InvalidParameter('KCHTMPL0025E') ident = name conn = self.conn.get() pool_uri = new_t.get(u'storagepool', '') if pool_uri: try: pool_name = pool_name_from_uri(pool_uri) pool = conn.storagePoolLookupByName(pool_name.encode("utf-8")) except Exception: raise InvalidParameter("KCHTMPL0004E", {'pool': pool_uri, 'template': name}) tmp_volumes = [disk['volume'] for disk in new_t.get('disks', []) if 'volume' in disk] self.templates.template_volume_validate(tmp_volumes, pool) for net_name in params.get(u'networks', []): try: conn.networkLookupByName(net_name.encode('utf-8')) except Exception: raise InvalidParameter("KCHTMPL0003E", {'network': net_name, 'template': name}) self.delete(name) try: ident = self.templates.create(new_t) except: ident = self.templates.create(old_t) raise return ident
def validate_integrity(self): invalid = {} # validate networks integrity networks = self.info.get('networks', []) invalid_networks = list( set(networks) - set(self._get_all_networks_name())) if invalid_networks: invalid['networks'] = invalid_networks # validate storagepools and image-based templates integrity for disk in self.info['disks']: if 'pool' in disk: pool_uri = disk['pool']['name'] pool_name = pool_name_from_uri(pool_uri) if pool_name not in self._get_active_storagepools_name(): invalid['storagepools'] = [pool_name] if disk.get("base") is None: continue if os.path.exists(disk.get("base")) is False: invalid['vm-image'] = disk["base"] # validate iso integrity # FIXME when we support multiples cdrom devices iso = self.info.get('cdrom') if iso: if os.path.exists(iso): st_mode = os.stat(iso).st_mode if not (stat.S_ISREG(st_mode) or stat.S_ISBLK(st_mode)): invalid['cdrom'] = [iso] elif not check_url_path(iso): invalid['cdrom'] = [iso] self.info['invalid'] = invalid return self.info
def validate_integrity(self): invalid = {} # validate networks integrity networks = self.info.get('networks', []) invalid_networks = list( set(networks) - set(self._get_all_networks_name())) if invalid_networks: invalid['networks'] = invalid_networks # validate storagepools and image-based templates integrity for disk in self.info['disks']: if 'pool' in disk: pool_uri = disk['pool']['name'] pool_name = pool_name_from_uri(pool_uri) if pool_name not in self._get_active_storagepools_name(): invalid['storagepools'] = [pool_name] if disk.get('base') is None: continue if os.path.exists(disk.get('base')) is False: invalid['vm-image'] = disk['base'] # validate iso integrity # FIXME when we support multiples cdrom devices iso = self.info.get('cdrom') if iso: if os.path.exists(iso): st_mode = os.stat(iso).st_mode if not (stat.S_ISREG(st_mode) or stat.S_ISBLK(st_mode)): invalid['cdrom'] = [iso] elif not check_url_path(iso): invalid['cdrom'] = [iso] self.info['invalid'] = invalid return self.info
def validate_integrity(self): invalid = {} # validate networks integrity networks = self.info.get("networks", []) invalid_networks = list(set(networks) - set(self._get_all_networks_name())) if invalid_networks: invalid["networks"] = invalid_networks # validate storagepools and image-based templates integrity for disk in self.info["disks"]: if "pool" in disk: pool_uri = disk["pool"]["name"] pool_name = pool_name_from_uri(pool_uri) if pool_name not in self._get_active_storagepools_name(): invalid["storagepools"] = [pool_name] if disk.get("base") is None: continue if os.path.exists(disk.get("base")) is False: invalid["vm-image"] = disk["base"] # validate iso integrity # FIXME when we support multiples cdrom devices iso = self.info.get("cdrom") if iso: if os.path.exists(iso): st_mode = os.stat(iso).st_mode if not (stat.S_ISREG(st_mode) or stat.S_ISBLK(st_mode)): invalid["cdrom"] = [iso] elif not check_url_path(iso): invalid["cdrom"] = [iso] self.info["invalid"] = invalid return self.info
def create(self, params): name = params.get('name', '').strip() iso = params.get('cdrom') # check search permission if iso and iso.startswith('/') and os.path.exists(iso): st_mode = os.stat(iso).st_mode if stat.S_ISREG(st_mode) or stat.S_ISBLK(st_mode): user = UserTests().probe_user() run_setfacl_set_attr(iso, user=user) ret, excp = probe_file_permission_as_user(iso, user) if ret is False: raise InvalidParameter('KCHISO0008E', {'filename': iso, 'user': user, 'err': excp}) cpu_info = params.get('cpu_info') if cpu_info: topology = cpu_info.get('topology') # Check, even though currently only topology # is supported. if topology: sockets = topology['sockets'] cores = topology['cores'] threads = topology['threads'] if params.get('cpus') is None: params['cpus'] = sockets * cores * threads # check_topoology will raise the appropriate # exception if a topology is invalid. CPUInfoModel(conn=self.conn).\ check_topology(params['cpus'], topology) conn = self.conn.get() pool_uri = params.get(u'storagepool', '') if pool_uri: try: pool_name = pool_name_from_uri(pool_uri) pool = conn.storagePoolLookupByName(pool_name.encode("utf-8")) except Exception: raise InvalidParameter("KCHTMPL0004E", {'pool': pool_uri, 'template': name}) tmp_volumes = [disk['volume'] for disk in params.get('disks', []) if 'volume' in disk] self.template_volume_validate(tmp_volumes, pool) for net_name in params.get(u'networks', []): try: conn.networkLookupByName(net_name.encode('utf-8')) except Exception: raise InvalidParameter("KCHTMPL0003E", {'network': net_name, 'template': name}) # Creates the template class with necessary information # Checkings will be done while creating this class, so any exception # will be raised here t = LibvirtVMTemplate(params, scan=True, conn=self.conn) name = params['name'] try: with self.objstore as session: if name in session.get_list('template'): raise InvalidOperation("KCHTMPL0001E", {'name': name}) session.store('template', name, t.info) except InvalidOperation: raise except Exception, e: raise OperationFailed('KCHTMPL0020E', {'err': e.message})
def create(self, params): name = params.get('name', '').strip() iso = params.get('cdrom') # check search permission if iso and iso.startswith('/') and os.path.exists(iso): st_mode = os.stat(iso).st_mode if stat.S_ISREG(st_mode) or stat.S_ISBLK(st_mode): user = UserTests().probe_user() run_setfacl_set_attr(iso, user=user) ret, excp = probe_file_permission_as_user(iso, user) if ret is False: raise InvalidParameter('KCHISO0008E', { 'filename': iso, 'user': user, 'err': excp }) cpu_info = params.get('cpu_info') if cpu_info: topology = cpu_info.get('topology') # Check, even though currently only topology # is supported. if topology: sockets = topology['sockets'] cores = topology['cores'] threads = topology['threads'] if params.get('cpus') is None: params['cpus'] = sockets * cores * threads # check_topoology will raise the appropriate # exception if a topology is invalid. CPUInfoModel(conn=self.conn).\ check_topology(params['cpus'], topology) conn = self.conn.get() pool_uri = params.get(u'storagepool', '') if pool_uri: try: pool_name = pool_name_from_uri(pool_uri) pool = conn.storagePoolLookupByName(pool_name.encode("utf-8")) except Exception: raise InvalidParameter("KCHTMPL0004E", { 'pool': pool_uri, 'template': name }) tmp_volumes = [ disk['volume'] for disk in params.get('disks', []) if 'volume' in disk ] self.template_volume_validate(tmp_volumes, pool) for net_name in params.get(u'networks', []): try: conn.networkLookupByName(net_name.encode('utf-8')) except Exception: raise InvalidParameter("KCHTMPL0003E", { 'network': net_name, 'template': name }) # Creates the template class with necessary information # Checkings will be done while creating this class, so any exception # will be raised here t = LibvirtVMTemplate(params, scan=True, conn=self.conn) name = params['name'] try: with self.objstore as session: if name in session.get_list('template'): raise InvalidOperation("KCHTMPL0001E", {'name': name}) session.store('template', name, t.info) except InvalidOperation: raise except Exception, e: raise OperationFailed('KCHTMPL0020E', {'err': e.message})