def update(self, name, params): old_t = self.lookup(name) new_t = copy.copy(old_t) # Merge cpu_info settings new_cpu_info = params.get('cpu_info') if new_cpu_info: cpu_info = dict(new_t['cpu_info']) cpu_info.update(new_cpu_info) params['cpu_info'] = cpu_info new_t.update(params) for net_name in params.get(u'networks', []): try: conn = self.conn.get() 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 create(self, params): if 'blk_dev' not in params: raise MissingParameter("GINFS00009E") blk_dev = params['blk_dev'] if 'mount_point' not in params: raise MissingParameter("GINFS00010E") mount_point = params['mount_point'] if 'persistent' not in params: raise MissingParameter("GINFS00011E") persistent = params['persistent'] if type(persistent) != bool: if persistent == u'True': persistent = True elif persistent == u'False': persistent = False else: raise InvalidParameter("GINFS00014E") fs_utils._mount_a_blk_device(blk_dev, mount_point) if persistent: fs_utils.make_persist(blk_dev, mount_point) return mount_point
def getFilteredRecords(self, filter_params): """ Returns a dict containing the filtered list of request log entries (dicts), and an optional uri for downloading results in a text file. """ uri = None results = [] records = self.getRecords() # fail for unrecognized filter options for key in filter_params.keys(): if key not in FILTER_FIELDS: filters = ", ".join(FILTER_FIELDS) raise InvalidParameter("WOKLOG0001E", {"filters": filters}) download = filter_params.pop('download', False) # filter records according to parameters for record in records: if all(key in record and record[key] == val for key, val in filter_params.iteritems()): results.append(record) # download option active: generate text file and provide donwload uri if download and len(results) > 0: uri = self.generateLogFile(results) return {'uri': uri, 'records': results}
def sosreport_collection(name): """ Code for the collection of sosreport in the path /var/tmp as specified in the command. """ path_sosreport = '/var/tmp/' sosreport_file = None if '_' in name: raise InvalidParameter("GGBDR0013E", {'name': name}) command = [ 'sosreport', '--batch', '--name=%s' % name, '--tmp-dir=%s' % path_sosreport ] output, error, retcode = run_command(command) if retcode != 0: raise OperationFailed("GGBDR0003E", {'name': name, 'err': error}) # Checking for sosreport file generation. if output.splitlines(): sosreport_pattern = path_sosreport + 'sosreport-' \ + name + '-' + '*.tar.xz' sosreport_file = glob.glob(sosreport_pattern) if len(sosreport_file) == 0: raise OperationFailed("GGBDR0004E", {'name': name, 'err': retcode}) return sosreport_file[0]
def update(self, vm, mac, params): dom = VMModel.get_vm(vm, self.conn) iface = self._get_vmiface(vm, mac) if iface is None: raise NotFoundError("KCHVMIF0001E", {'name': vm, 'iface': mac}) # cannot change mac address in a running system if DOM_STATE_MAP[dom.info()[0]] != "shutoff": raise InvalidOperation('KCHVMIF0011E') # mac address is a required parameter if 'mac' not in params: raise MissingParameter('KCHVMIF0008E') # new mac address must be unique if self._get_vmiface(vm, params['mac']) is not None: raise InvalidParameter('KCHVMIF0009E', {'name': vm, 'mac': params['mac']}) flags = 0 if dom.isPersistent(): flags |= libvirt.VIR_DOMAIN_AFFECT_CONFIG # remove the current nic xml = etree.tostring(iface) dom.detachDeviceFlags(xml, flags=flags) # add the nic with the desired mac address iface.mac.attrib['address'] = params['mac'] xml = etree.tostring(iface) dom.attachDeviceFlags(xml, flags=flags) return [vm, params['mac']]
def delete_group(groupname): if not isinstance(groupname, str) or not groupname.strip(): raise InvalidParameter('GINUSER0012E', {'group': groupname}) adm = libuser.admin() group_id = int(get_group_gid(groupname)) if group_id <= 1000: wok_log.error('Ignoring deletion of system group "%s" with gid %s' % (groupname, group_id)) return group_obj = adm.lookupGroupById(group_id) if not group_obj: wok_log.error('Could not locate group "%s" with gid %s' % (groupname, group_id)) return if not adm.enumerateUsersByGroup(groupname): # groups prepend with '%' if '%' + groupname in get_sudoers(admin_check=False): raise OperationFailed('GINUSER0017E', {'group': groupname}) try: adm.deleteGroup(group_obj) except Exception as e: raise OperationFailed('GINUSER0029E', { 'group': groupname, 'err': e.__str__() })
def get_list(self, _cap=None, _passthrough=None, _passthrough_affected_by=None, _available_only=None): if _passthrough_affected_by is not None: # _passthrough_affected_by conflicts with _cap and _passthrough if (_cap, _passthrough) != (None, None): raise InvalidParameter("KCHHOST0004E") return sorted( self._get_passthrough_affected_devs(_passthrough_affected_by)) if _cap == 'fc_host': dev_names = self._get_devices_fc_host() else: dev_names = self._get_devices_with_capability(_cap) if _passthrough is not None and _passthrough.lower() == 'true': conn = self.conn.get() passthrough_names = [ dev['name'] for dev in hostdev.get_passthrough_dev_infos(conn) ] dev_names = list(set(dev_names) & set(passthrough_names)) if _available_only is not None and _available_only.lower() \ == 'true': unavailable_devs = self._get_unavailable_devices() dev_names = [ dev for dev in dev_names if dev not in unavailable_devs ] dev_names.sort() return dev_names
def _get_disk_type(path): if check_url_path(path): return 'network' if not os.path.exists(path): raise InvalidParameter("KCHVMSTOR0003E", {'value': path}) # Check if path is a valid local path if os.path.isfile(path): return 'file' r_path = os.path.realpath(path) if stat.S_ISBLK(os.stat(r_path).st_mode): return 'block' raise InvalidParameter("KCHVMSTOR0003E", {'value': path})
def validate_interfaces(interfaces): # # Interfaces only supported on s390x or s390 architecture. # Otherwise FIXME to valid interfaces exist on system. # if os.uname()[4] not in ['s390x', 's390'] and interfaces: raise InvalidParameter("KCHTMPL0039E")
def _set_network_vepa(self, params): for iface in params['interfaces']: if ('vlan_id' in params or not (netinfo.is_bare_nic(iface) or netinfo.is_bonding(iface))): raise InvalidParameter('KCHNET0028E', {'name': iface}) params['forward'] = {'mode': 'vepa', 'devs': params['interfaces']}
def get_list(self, _filter): try: filter = str(_filter) params = filter.split(',') if len(params) != 4: raise InvalidParameter('GINAUD0029E') report = [] cmd = "cat " + audit_summary_report + " |" \ " awk '/^[0-9]/ { " \ "printf \"%s %s\\n\", $" + params[1] + ", $" + params[2] + \ "}'| sort | uniq" p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) out = p.communicate() str(out).split('\n') create_graph(out, params[0], params[3]) report_dict = dict() report_dict['Graph:'] = "/data/logs/" + params[0] + "." + \ params[3] report.append(report_dict) return report except InvalidOperation: raise except Exception, e: raise OperationFailed('GINAUD0028E', {'error': e.message})
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 probe_image(image_path): if not os.path.isfile(image_path): raise InvalidParameter("KCHIMG0004E", {'filename': image_path}) if not os.access(image_path, os.R_OK): raise ImageFormatError("KCHIMG0003E", {'filename': image_path}) g = guestfs.GuestFS(python_return_dict=True) g.add_drive_opts(image_path, readonly=1) g.launch() try: roots = g.inspect_os() except: raise ImageFormatError("KCHIMG0001E") if len(roots) == 0: # If we are unable to detect the OS, still add the image # but make distro and vendor 'unknown' return ("unknown", "unknown") for root in roots: version = "%d.%d" % (g.inspect_get_major_version(root), g.inspect_get_minor_version(root)) distro = "%s" % (g.inspect_get_distro(root)) return (distro, version)
def _set_network_subnet(self, params): netaddr = params.get('subnet', '') # lookup a free network address for nat and isolated automatically if not netaddr: netaddr = self._get_available_address() if not netaddr: raise OperationFailed("KCHNET0009E", {'name': params['name']}) try: ip = ipaddr.IPNetwork(netaddr) except ValueError: raise InvalidParameter("KCHNET0003E", { 'subnet': netaddr, 'network': params['name'] }) if ip.ip == ip.network: ip.ip = ip.ip + 1 dhcp_start = str(ip.ip + ip.numhosts / 2) dhcp_end = str(ip.ip + ip.numhosts - 3) params.update({ 'net': str(ip), 'dhcp': { 'range': { 'start': dhcp_start, 'end': dhcp_end } } })
def _set_network_subnet(self, params): netaddr = params.get('subnet', '') # lookup a free network address for nat and isolated automatically if not netaddr: netaddr = self._get_available_address() if not netaddr: raise OperationFailed('KCHNET0009E', {'name': params['name']}) try: ip = ipaddress.IPv4Network(netaddr, False) except ValueError: raise InvalidParameter('KCHNET0003E', { 'subnet': netaddr, 'network': params['name'] }) ip.network_address = ip.network_address + 1 dhcp_start = str(ip.network_address + int(ip.num_addresses / 2)) dhcp_end = str(ip.network_address + int(ip.num_addresses - 3)) params.update({ 'net': str(ip), 'dhcp': { 'range': { 'start': dhcp_start, 'end': dhcp_end } } })
def delete_user(username): """ method to delete user :param username: user name """ if not isinstance(username, str) or not username.strip(): raise InvalidParameter('GINUSER0010E', {'user': username}) if username in get_sudoers(admin_check=False): raise OperationFailed('GINUSER0016E', {'user': username}) adm = libuser.admin() user_obj = adm.lookupUserByName(username) if not user_obj: raise OperationFailed('GINUSER0011E', {'user': username}) groups = adm.enumerateGroupsByUser(username) for group in groups: # remove user from all groups remove_user_from_group(username, group) f = SUDOERS_FILE % username if os.path.isfile(f): try: os.unlink(f) except Exception as e: raise OperationFailed('GINUSER0013E', {'user': username}) try: adm.deleteUser(user_obj, True, True) except Exception as e: raise OperationFailed('GINUSER0031E', { 'user': username, 'err': e.__str__() })
def probe_image(image_path): if not os.path.isfile(image_path): raise InvalidParameter('KCHIMG0004E', {'filename': image_path}) if not os.access(image_path, os.R_OK): raise ImageFormatError('KCHIMG0003E', {'filename': image_path}) try: import guestfs g = guestfs.GuestFS(python_return_dict=True) g.add_drive_opts(image_path, readonly=1) g.launch() roots = g.inspect_os() except ImportError: return ('unknown', 'unknown') except Exception as e: raise ImageFormatError('KCHIMG0001E', {'err': str(e)}) if len(roots) == 0: # If we are unable to detect the OS, still add the image # but make distro and vendor 'unknown' return ('unknown', 'unknown') for root in roots: version = '%d.%d' % ( g.inspect_get_major_version(root), g.inspect_get_minor_version(root), ) distro = '%s' % (g.inspect_get_distro(root)) return (distro, version)
def create(self, vm, params): conn = self.conn.get() networks = conn.listNetworks() + conn.listDefinedNetworks() networks = map(lambda x: x.decode('utf-8'), networks) if params['type'] == 'network': network = params.get("network") if network is None: raise MissingParameter('KCHVMIF0007E') if network not in networks: raise InvalidParameter('KCHVMIF0002E', {'name': vm, 'network': network}) macs = (iface.mac.get('address') for iface in self.get_vmifaces(vm, self.conn)) # user defined customized mac address if 'mac' in params and params['mac']: # make sure it is unique if params['mac'] in macs: raise InvalidParameter('KCHVMIF0009E', {'name': vm, 'mac': params['mac']}) # otherwise choose a random mac address else: while True: params['mac'] = VMIfacesModel.random_mac() if params['mac'] not in macs: break dom = VMModel.get_vm(vm, self.conn) os_data = VMModel.vm_get_os_metadata(dom) os_version, os_distro = os_data xml = get_iface_xml(params, conn.getInfo()[0], os_distro, os_version) flags = 0 if dom.isPersistent(): flags |= libvirt.VIR_DOMAIN_AFFECT_CONFIG if DOM_STATE_MAP[dom.info()[0]] != "shutoff": flags |= libvirt.VIR_DOMAIN_AFFECT_LIVE dom.attachDeviceFlags(xml, flags) return params['mac']
def _set_network_macvtap(self, params): iface = params['interfaces'][0] if ('vlan_id' in params or not (netinfo.is_bare_nic(iface) or netinfo.is_bonding(iface))): raise InvalidParameter('KCHNET0028E', {'name': iface}) # set macvtap network params['forward'] = {'mode': 'bridge', 'dev': iface}
def _network_validate(self): names = self.info['networks'] for name in names: try: conn = self.conn.get() network = conn.networkLookupByName(name.encode('utf-8')) except libvirt.libvirtError: raise InvalidParameter("KCHTMPL0003E", { 'network': name, 'template': self.name }) if not network.isActive(): raise InvalidParameter("KCHTMPL0007E", { 'network': name, 'template': self.name })
def _network_validate(self): names = self.info.get('networks', []) for name in names: try: conn = self.conn.get() network = conn.networkLookupByName(name) except libvirt.libvirtError: raise InvalidParameter('KCHTMPL0003E', { 'network': name, 'template': self.name }) if not network.isActive(): raise InvalidParameter('KCHTMPL0007E', { 'network': name, 'template': self.name })
def validate_bus_id(bus_id): """ Validate bus ID :param bus_id: bus ID """ pattern = re.compile(r'\d\.\d\.\w{4}') valid = pattern.match(bus_id) if not valid: raise InvalidParameter("GINDASD0011E", {'bus_id': bus_id}) # No need to worry about IndexError exception below becuase # the regex above would have made sure we go through the # split operation on the string smoothly ch_len = bus_id.split(".")[-1] if len(ch_len) > 4: raise InvalidParameter("GINDASD0011E", {'bus_id': bus_id})
def change_bond_configuration(ovsbridge, bond, iface_del, iface_add): def bond_exists_in_ovsbridge(ovsbridge, bond): if not ovsbridge_exists(ovsbridge): return False ports = get_bridge_ports(ovsbridge) if bond not in ports: return False bond_info = get_port_info(bond) if bond_info['type'] != 'bond': return False return True def interface_row_exists(interface): cmd = 'ovs-vsctl --id=@%(iface)s get Interface %(iface)s' cmd = cmd % {'iface': interface} out, err, returncode = run_ovsvsctl_command(cmd.split()) if returncode != 0: return False return True if not bond_exists_in_ovsbridge(ovsbridge, bond): raise InvalidParameter('GINOVS00007E', { 'bridge': ovsbridge, 'bond': bond }) if not interface_row_exists(iface_del): raise InvalidParameter('GINOVS00008E', {'iface': iface_del}) cmd = 'ovs-vsctl --id=@%(iface)s get Interface %(iface)s -- remove ' \ 'Port %(bond)s interfaces @%(iface)s' cmd = cmd % {'iface': iface_del, 'bond': bond} out, err, returncode = run_ovsvsctl_command(cmd.split()) if returncode != 0: raise OperationFailed('GINOVS00002E', {'err': err}) cmd = 'ovs-vsctl --id=@%(iface)s create Interface name=%(iface)s ' \ '-- add Port %(bond)s interfaces @%(iface)s' cmd = cmd % {'iface': iface_add, 'bond': bond} out, err, returncode = run_ovsvsctl_command(cmd.split()) if returncode != 0: raise OperationFailed('GINOVS00002E', {'err': err})
def create_ovsbridge(ovsbridge): if ovsbridge_exists(ovsbridge): raise InvalidParameter('GINOVS00003E', {'name': ovsbridge}) cmd = ['ovs-vsctl', 'add-br', ovsbridge] out, err, returncode = run_ovsvsctl_command(cmd) if returncode != 0: raise OperationFailed('GINOVS00002E', {'err': err})
def _validate_convert_data(value, from_unit, to_unit): if not from_unit: raise InvalidParameter('WOKUTILS0005E', {'unit': from_unit}) if not to_unit: raise InvalidParameter('WOKUTILS0005E', {'unit': to_unit}) # set the default suffix if from_unit[-1] not in SUFFIXES_WITH_MULT: from_unit += DEFAULT_SUFFIX if to_unit[-1] not in SUFFIXES_WITH_MULT: to_unit += DEFAULT_SUFFIX # split prefix and suffix for better parsing from_p = from_unit[:-1] from_s = from_unit[-1] to_p = to_unit[:-1] to_s = to_unit[-1] # validate parameters try: value = float(value) except TypeError: raise InvalidParameter('WOKUTILS0004E', {'value': value}) if from_p != '' and from_p not in (SI_PREFIXES + IEC_PREFIXES): raise InvalidParameter('WOKUTILS0005E', {'unit': from_unit}) if from_s not in SUFFIXES_WITH_MULT: raise InvalidParameter('WOKUTILS0005E', {'unit': from_unit}) if to_p != '' and to_p not in (SI_PREFIXES + IEC_PREFIXES): raise InvalidParameter('WOKUTILS0005E', {'unit': to_unit}) if to_s not in SUFFIXES_WITH_MULT: raise InvalidParameter('WOKUTILS0005E', {'unit': to_unit}) return value, from_unit, to_unit
def validate_memory(memory): # # All checking are made in Mib, so, expects memory values in Mib # current = memory.get('current') maxmem = memory.get('maxmemory') # Check Host Memory if hasattr(psutil, 'virtual_memory'): host_memory = psutil.virtual_memory().total >> 10 >> 10 else: host_memory = psutil.TOTAL_PHYMEM >> 10 >> 10 # Memories must be lesser than 16TiB (PPC) or 4TiB (x86) and the Host # memory limit if (current > (MAX_MEM_LIM >> 10)) or (maxmem > (MAX_MEM_LIM >> 10)): raise InvalidParameter("KCHVM0079E", {'value': str(MAX_MEM_LIM / (1024**3))}) if (current > host_memory) or (maxmem > host_memory): raise InvalidParameter("KCHVM0078E", {'memHost': host_memory}) # Current memory cannot be greater than maxMemory if current > maxmem: raise InvalidParameter("KCHTMPL0031E", { 'mem': str(current), 'maxmem': str(maxmem) }) # make sure memory and Maxmemory are alingned in 256MiB in PowerPC distro, _, _ = platform.linux_distribution() if distro == "IBM_PowerKVM": if current % PPC_MEM_ALIGN != 0: raise InvalidParameter( 'KCHVM0071E', { 'param': "Memory", 'mem': str(current), 'alignment': str(PPC_MEM_ALIGN) }) elif maxmem % PPC_MEM_ALIGN != 0: raise InvalidParameter( 'KCHVM0071E', { 'param': "Maximum Memory", 'mem': str(maxmem), 'alignment': str(PPC_MEM_ALIGN) })
def to_volume_list(self, vm_uuid): ret = [] for i, d in enumerate(self.info['disks']): # Create only .img. If storagepool is (i)SCSI, volumes will be LUNs if 'pool' in d and d['pool']['type'] in ['iscsi', 'scsi']: continue index = d.get('index', i) volume = '%s-%s.img' % (vm_uuid, index) if 'path' in d: storage_path = d['path'] else: storage_path = self._get_storage_path(d['pool']['name']) info = { 'name': volume, 'capacity': d['size'], 'format': d['format'], 'path': '%s/%s' % (storage_path, volume), 'pool': d['pool']['name'] if 'pool' in d else None, } if ('pool' in d and 'logical' == d['pool']['type'] ) or info['format'] not in ['qcow2', 'raw']: info['allocation'] = info['capacity'] else: info['allocation'] = 0 if 'base' in d: info['base'] = dict() base_fmt = imageinfo.probe_img_info(d['base'])['format'] if base_fmt is None: raise InvalidParameter('KCHTMPL0024E', {'path': d['base']}) info['base']['path'] = d['base'] info['base']['format'] = base_fmt v_tree = E.volume(E.name(info['name'])) v_tree.append(E.allocation(str(info['allocation']), unit='G')) v_tree.append(E.capacity(str(info['capacity']), unit='G')) target_fmt = info['format'] if 'base' in d: # target must be qcow2 in order to use a backing file target_fmt = 'qcow2' v_tree.append( E.backingStore( E.path(info['base']['path']), E.format(type=info['base']['format']), )) target = E.target(E.format(type=target_fmt), E.path(info['path'])) v_tree.append(target) info['xml'] = etree.tostring(v_tree) ret.append(info) return ret
def _check_network_interface(self, params): try: # fails if host interface is already in use by a libvirt network iface = params['interface'] if iface in self.get_all_networks_interfaces(): msg_args = {'iface': iface, 'network': params['name']} raise InvalidParameter("KCHNET0006E", msg_args) except KeyError: raise MissingParameter("KCHNET0004E", {'name': params['name']})
def check_topology(self, vcpus, topology): """ param vcpus: should be an integer param iso_path: the path of the guest ISO param topology: {'sockets': x, 'cores': x, 'threads': x} """ sockets = topology['sockets'] cores = topology['cores'] threads = topology['threads'] if not self.guest_threads_enabled: raise InvalidOperation("GGBCPUINF0003E") if vcpus != sockets * cores * threads: raise InvalidParameter("GGBCPUINF0002E") if vcpus > self.cores_available * self.threads_per_core: raise InvalidParameter("GGBCPUINF0001E") if threads > self.threads_per_core: raise InvalidParameter("GGBCPUINF0002E")
def _storage_validate(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 }) if not pool.isActive(): raise InvalidParameter("KCHTMPL0005E", { 'pool': pool_name, 'template': self.name }) return pool