def create(self, params): if utils.is_lun_scan_enabled(): wok_log.error( "Lun scan is enabled. Cannot add/remove LUNs manually.") raise InvalidOperation("GS390XSTG00009") if 'hbaId' not in params: wok_log.error("hbaId is required for adding a LUN") raise MissingParameter("GS390XSTG00010") hbaId = params['hbaId'] utils.validate_hba_id(hbaId) if 'remoteWwpn' not in params: wok_log.error("remoteWwpn is required for adding a LUN") raise MissingParameter("GS390XSTG00011") wwpn = params['remoteWwpn'] utils.validate_wwpn_or_lun(wwpn) if 'lunId' not in params: wok_log.error("lunId is required for adding a LUN") raise MissingParameter("GS390XSTG00012") lunId = params['lunId'] utils.validate_wwpn_or_lun(lunId) utils.add_lun(hbaId, wwpn, lunId) lun_path = hbaId + ":" + wwpn + ":" + lunId return lun_path
def create(self, params): if not params.get('dev_name'): raise MissingParameter("GINDASDPAR0005E") dev_name = params['dev_name'] if 'size' not in params: raise MissingParameter("GINDASDPAR0006E") if params['size']: if type(params['size']) != int: raise InvalidParameter("GINDASDPAR0013E") size = params['size'] devs = dasd_utils.get_dasd_devs() for dev in devs: if dev['size'] != 'Unknown': if size > dev['size']: raise InvalidParameter("GINDASDPAR0015E") try: dasd_utils._create_dasd_part(dev_name, size) except OperationFailed as e: raise OperationFailed("GINDASDPAR0007E", { 'name': dev_name, 'err': e.message }) return dev_name
def addRepo(self, params): """ Add a new APT repository based on <params> """ # To create a APT repository the dist is a required parameter # (in addition to baseurl, verified on controller through API.json) config = params.get('config', None) if config is None: raise MissingParameter('GGBREPOS0019E') if 'dist' not in config.keys(): raise MissingParameter('GGBREPOS0019E') uri = params['baseurl'] dist = config['dist'] comps = config.get('comps', []) validate_repo_url(get_expanded_url(uri)) gingerBaseLock.acquire() try: repos = self._get_repos() source_entry = repos.add('deb', uri, dist, comps, file=self.filename) repos.save() except Exception as e: raise OperationFailed('GGBREPOS0026E', {'err': e.message}) finally: gingerBaseLock.release() return self._get_repo_id(source_entry)
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 create(self, params): if 'vg_name' not in params: raise MissingParameter('GINLV00001E') vgname = params['vg_name'] if 'size' not in params: raise MissingParameter('GINLV00002E') taskid = AsyncTask(u'/lvs/vg_name/%s' % (vgname), self._create_linear_task, params).id return self.task.lookup(taskid)
def construct_fs_rule(self, rule, params): if "rule_info" in params: if "file_to_watch" in params["rule_info"]: rule = rule + " " + params["rule_info"]["file_to_watch"] if params["rule_info"].get("permissions"): rule = rule + " -p " + params["rule_info"]["permissions"] else: raise MissingParameter("GINAUD0031E") if "key" in params["rule_info"]: if params["rule_info"]["key"]: rule = rule + " -k " + params["rule_info"]["key"] else: raise MissingParameter("GINAUD0004E") return rule
def create(self, params): if 'vg_name' not in params: raise MissingParameter("GINVG00013E") vgname = params['vg_name'] if "pv_paths" not in params: raise MissingParameter("GINVG00014E") taskid = AsyncTask(u'/vgs/vg_name/%s' % (vgname), self._create_task, params).id return self.task.lookup(taskid)
def create(self, params): if 'devname' not in params: raise MissingParameter("GINPART00008E") dev_name = params['devname'] if 'partsize' not in params: raise MissingParameter("GINPART00009E") part_size = params['partsize'] try: return utils.create_disk_part(dev_name, part_size) except Exception as e: raise OperationFailed("GINPART00002E", {'err': e.__str__()})
def _format_task(self, cb, params): if 'name' not in params: raise MissingParameter("GINDASD0009E") name = params['name'] if 'blk_size' not in params: raise MissingParameter("GINDASD0010E") blk_size = params['blk_size'] try: dasd_utils._format_dasd(blk_size, name) except OperationFailed: raise OperationFailed('GINDASD0008E', {'name': name}) cb('OK', True)
def _format_task(self, cb, params): if 'name' not in params: raise MissingParameter("GINDASD0009E") name = params['name'] if 'blk_size' not in params: raise MissingParameter("GINDASD0010E") blk_size = params['blk_size'] try: dasd_utils._format_dasd(blk_size, name) except OperationFailed as e: wok_log.error("Formatting of DASD device %s failed" % name) raise OperationFailed('GINDASD0008E', {'err': e}) cb('OK', True)
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 update(self, name, params): """ Modifies the rule. :param name: the existing rule. :param params: The dict of params for the new rule. :return: the new rule. """ try: gingerAuditLock.acquire() if self.is_rule_exists(name): info = self.get_audit_rule_info(name) rule = RulesModel().construct_rules(params) if rule == name: raise OperationFailed("GINAUD0034E", {"name": name}) if info['loaded'] == 'yes': RulesModel().create(params) if info['type'] == "Control Rule": RulesModel().write_to_aucontrol_rules(rule) else: RulesModel().write_to_audit_rules(rule) wok_log.info("Deleting the existing rule %s" % name) self.delete_rule(name) wok_log.info("The rule has been modified successfully.") return rule else: raise MissingParameter("GINAUD0010E", {"name": name}) except MissingParameter: raise except OperationFailed: raise except Exception: raise OperationFailed("GINAUD0011E", {"name": name}) finally: gingerAuditLock.release()
def _create_volume_with_capacity(self, cb, params): pool_name = params.pop('pool') vol_xml = """ <volume> <name>%(name)s</name> <allocation unit='bytes'>%(allocation)s</allocation> <capacity unit='bytes'>%(capacity)s</capacity> <source> </source> <target> <format type='%(format)s'/> </target> </volume> """ allocation = 0 if params['pool_type'] == "logical": allocation = params['capacity'] params.setdefault('allocation', allocation) params.setdefault('format', 'qcow2') name = params['name'] try: pool = StoragePoolModel.get_storagepool(pool_name, self.conn) xml = vol_xml % params except KeyError, item: raise MissingParameter("KCHVOL0004E", { 'item': str(item), 'volume': name })
def addRepo(self, params): """ Add a given repository to YumBase """ # At least one base url, or one mirror, must be given. baseurl = params.get('baseurl', '') config = params.get('config', {}) mirrorlist = config.get('mirrorlist', '') metalink = config.get('metalink', '') if not baseurl and not mirrorlist and not metalink: raise MissingParameter('GGBREPOS0013E') if baseurl: validate_repo_url(get_expanded_url(baseurl)) if mirrorlist: validate_repo_url(get_expanded_url(mirrorlist)) if metalink: validate_repo_url(get_expanded_url(metalink)) if mirrorlist and metalink: raise InvalidOperation('GGBREPOS0030E') repo_id = params.get('repo_id', None) if repo_id is None: repo_id = 'gingerbase_repo_%s' % str(int(time.time() * 1000)) repos = self._get_repos('GGBREPOS0026E') if repo_id in repos.keys(): raise InvalidOperation('GGBREPOS0022E', {'repo_id': repo_id}) repo_name = config.get('repo_name', repo_id) repo = { 'baseurl': baseurl, 'mirrorlist': mirrorlist, 'name': repo_name, 'gpgcheck': 1, 'gpgkey': [], 'enabled': 1, 'metalink': metalink } # write a repo file in the system with repo{} information. parser = SafeConfigParser() parser.add_section(repo_id) for key, value in repo.items(): if value: parser.set(repo_id, key, value) repofile = os.path.join(self._confdir, repo_id + '.repo') try: with open(repofile, 'w') as fd: parser.write(fd) except Exception: raise OperationFailed('GGBREPOS0018E', {'repo_file': repofile}) return repo_id
def _get_os_info(self, args, scan): distro = version = 'unknown' # Identify the cdrom if present iso = args.get('cdrom', '') if len(iso) > 0: if not iso.startswith('/'): self.info.update({'iso_stream': True}) if scan: distro, version = self.get_iso_info(iso) return distro, version # CDROM is not presented: check for base image base_imgs = [] for d in args.get('disks', []): if 'base' in d.keys(): base_imgs.append(d) if scan: distro, version = imageinfo.probe_image(d['base']) if 'size' not in d.keys(): d_info = imageinfo.probe_img_info(d['base']) d['size'] = d_info['virtual-size'] if len(base_imgs) == 0: raise MissingParameter("KCHTMPL0016E") return distro, version
def create(self, params): if 'dev_name' not in params: raise MissingParameter("GINDASDPAR0005E") dev_name = params['dev_name'] if 'size' not in params: raise MissingParameter("GINDASDPAR0006E") size = params['size'] try: dasd_utils._create_dasd_part(dev_name, size) except OperationFailed as e: wok_log.error("Creation of partition failed") raise OperationFailed("GINDASDPAR0007E", {'dev_name': dev_name, 'err': e}) return dev_name
def create(self, params): task_id = None conn = self.conn.get() from_vg = params.get('source', {}).get('from_vg', False) try: name = params['name'] if name == ISO_POOL_NAME: raise InvalidOperation("KCHPOOL0031E") # The user may want to create a logical pool with the same name # used before but a volume group will already exist with this name # So check the volume group does not exist to create the pool if params['type'] == 'logical': self._check_lvm(name, from_vg) if params['type'] == 'kimchi-iso': task_id = self._do_deep_scan(params) if params['type'] == 'scsi': adapter_name = params['source']['adapter_name'] extra_params = self.device.lookup(adapter_name) # Adds name, adapter_type, wwpn and wwnn to source information params['source'].update(extra_params) params['fc_host_support'] = self.caps.fc_host_support poolDef = StoragePoolDef.create(params) poolDef.prepare(conn) xml = poolDef.xml.encode("utf-8") except KeyError, item: raise MissingParameter("KCHPOOL0004E", { 'item': str(item), 'name': name })
def login(self, *args): try: params = parse_request() username = params['username'] password = params['password'] except KeyError, item: e = MissingParameter('WOKAUTH0003E', {'item': str(item)}) raise cherrypy.HTTPError(400, e.message)
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 _create_volume_with_capacity(self, cb, params): pool_name = params.pop('pool') vol_xml = """ <volume> <name>%(name)s</name> <allocation unit='bytes'>%(allocation)s</allocation> <capacity unit='bytes'>%(capacity)s</capacity> <source> </source> <target> <format type='%(format)s'/> </target> </volume> """ allocation = 0 if params['pool_type'] == 'logical': allocation = params['capacity'] params.setdefault('allocation', allocation) params.setdefault('format', 'qcow2') name = params['name'] try: pool = StoragePoolModel.get_storagepool(pool_name, self.conn) xml = vol_xml % params except KeyError as item: raise MissingParameter('KCHVOL0004E', { 'item': str(item), 'volume': name }) try: pool.createXML(xml, 0) except libvirt.libvirtError as e: raise OperationFailed( 'KCHVOL0007E', { 'name': name, 'pool': pool_name, 'err': e.get_error_message() }, ) vol_info = StorageVolumeModel(conn=self.conn, objstore=self.objstore).lookup( pool_name, name) vol_path = vol_info['path'] if params.get('upload', False): upload_volumes[vol_path] = { 'lock': threading.Lock(), 'offset': 0, 'cb': cb, 'expected_vol_size': params['capacity'], } cb('ready for upload') else: cb('OK', True)
def populate_ifcfg_atributes(self, params, cfg_map): if BASIC_INFO in params: cfg_map = self.update_basic_info(cfg_map, params) else: raise MissingParameter('GINNET0024E') if IPV4_ID in params: cfg_map = cfgInterfacesHelper.update_ipv4(cfg_map, params) if IPV6_ID in params: cfg_map = cfgInterfacesHelper.update_ipv6(cfg_map, params) return cfg_map
def create(self, params): if 'pv_name' not in params: raise MissingParameter("GINPV00001E") pvname = params['pv_name'] taskid = AsyncTask(u'/pvs/pv_name/%s' % (pvname), self._create_task, params).id return self.task.lookup(taskid)
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 create(self, params): if utils.is_lun_scan_enabled()['current']: wok_log.error( "Lun scan is enabled. Cannot add/remove LUNs manually.") raise InvalidOperation("GS390XSTG00009") if 'hbaId' not in params: wok_log.error("hbaId is required for adding a LUN") raise MissingParameter("GS390XSTG00010") hbaId = params['hbaId'] utils.validate_hba_id(hbaId) if 'remoteWwpn' not in params: wok_log.error("remoteWwpn is required for adding a LUN") raise MissingParameter("GS390XSTG00011") wwpn = params['remoteWwpn'] utils.validate_wwpn_or_lun(wwpn) if 'lunId' not in params: wok_log.error("lunId is required for adding a LUN") raise MissingParameter("GS390XSTG00012") lunId = params['lunId'] utils.validate_wwpn_or_lun(lunId) try: fc_lock.acquire() utils.add_lun(hbaId, wwpn, lunId) except OperationFailed as e: wok_log.error("Adding LUN failed") raise OperationFailed("GS390XSTG00003", {'err': e}) finally: fc_lock.release() lun_path = hbaId + ":" + wwpn + ":" + lunId return lun_path
def updateRepo(self, repo_id, params): """ Update a given repository in repositories.Repositories() format """ repos = self._get_repos('GGBREPOS0011E') if repo_id not in repos.keys(): raise NotFoundError('GGBREPOS0012E', {'repo_id': repo_id}) entry = repos.get(repo_id) baseurl = params.get('baseurl', entry.baseurl) config = params.get('config', {}) mirrorlist = config.get('mirrorlist', entry.mirrorlist) metalink = config.get('metalink', entry.metalink) if baseurl is not None and len(baseurl.strip()) == 0: baseurl = None if mirrorlist is not None and len(mirrorlist.strip()) == 0: mirrorlist = None if metalink is not None and len(metalink.strip()) == 0: metalink = None if baseurl is None and mirrorlist is None and metalink is None: raise MissingParameter('GGBREPOS0013E') if baseurl is not None: validate_repo_url(get_expanded_url(baseurl)) entry.baseurl = baseurl if mirrorlist is not None: validate_repo_url(get_expanded_url(mirrorlist)) entry.mirrorlist = mirrorlist if metalink is not None: validate_repo_url(get_expanded_url(metalink)) entry.metalink = metalink if mirrorlist and metalink: raise InvalidOperation('GGBREPOS0030E') entry.id = params.get('repo_id', repo_id) entry.name = config.get('repo_name', entry.name) entry.gpgcheck = config.get('gpgcheck', entry.gpgcheck) entry.gpgkey = config.get('gpgkey', entry.gpgkey) gingerBaseLock.acquire() write_repo_to_file(entry) gingerBaseLock.release() return repo_id
def create(self, params): if not params.get('type'): raise MissingParameter("GINFS00016E") if params['type'] == 'local': if not params.get('blk_dev'): raise MissingParameter("GINFS00009E") blk_dev = params['blk_dev'] if not params.get('mount_point'): raise MissingParameter("GINFS00010E") mount_point = params['mount_point'] mount_options = params.get('mount_options', '') try: fs_utils._mount_a_blk_device(blk_dev, mount_point, mount_options) fs_utils.make_persist(blk_dev, mount_point, mount_options) except Exception as e: raise InvalidParameter("GINFS00007E", {"err": e.message}) return mount_point elif params['type'] == 'nfs': if not params.get('server'): raise MissingParameter("GINFS00014E") server = params['server'] if not params.get('share'): raise MissingParameter("GINFS00015E") share = params['share'] if not params.get('mount_point'): raise MissingParameter("GINFS00010E") mount_point = params['mount_point'] mount_options = params.get('mount_options', '') try: fs_utils.nfsmount(server, share, mount_point, mount_options) dev_info = server + ':' + share fs_utils.make_persist(dev_info, mount_point, mount_options) except Exception as e: raise InvalidParameter("GINFS00018E", {"err": e}) return mount_point else: raise InvalidParameter("GINFS00017E")
def _check_network_interface(self, params): if not params.get('interfaces'): raise MissingParameter("KCHNET0004E", {'name': params['name']}) if len(params['interfaces']) == 0: raise InvalidParameter("KCHNET0029E") conn = params['connection'] if conn in ['bridge', 'macvtap'] and len(params['interfaces']) > 1: raise InvalidParameter("KCHNET0030E") for iface in params['interfaces']: if iface in self.get_all_networks_interfaces(): msg_args = {'iface': iface, 'network': params['name']} raise InvalidParameter("KCHNET0006E", msg_args)
def create(self, params): """ """ if ('ipaddr' not in params or 'name' not in params or 'password' not in params): raise MissingParameter('GINSE00001E') params['salt'] = ''.join(random.choice(ALPHABET) for i in range(16)) params['password'] = encrypt(params['password'], params['salt']) serverData = get_server_status(params) if serverData: if 'status' in serverData: del serverData['status'] add_config(serverData) return serverData['name'] else: raise OperationFailed('GINSE00003E', params)
def _set_network_bridge(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']}) # Linux bridges cannot be the trunk device of a VLAN if 'vlan_id' in params and \ (netinfo.is_bridge(iface) or params['connection'] == "bridge"): raise InvalidParameter('KCHNET0019E', {'name': iface}) # User specified bridge interface, simply use it self._ensure_iface_up(iface) params['ovs'] = False if netinfo.is_bridge(iface): params['bridge'] = iface if netinfo.is_ovs_bridge(iface): params['ovs'] = True # OVS bridges don't work with macvtap if params['connection'] != "bridge": raise InvalidParameter('KCHNET0026E') # User wants Linux bridge network, but didn't specify bridge interface elif params['connection'] == "bridge": # create Linux bridge interface first and use it as actual iface iface = self._create_linux_bridge(iface) params['bridge'] = iface # connection == macvtap and iface is not bridge elif netinfo.is_bare_nic(iface) or netinfo.is_bonding(iface): if params.get('vlan_id') is None: params['forward']['dev'] = iface else: params['bridge'] = \ self._create_vlan_tagged_bridge(str(iface), str(params['vlan_id'])) else: raise InvalidParameter("KCHNET0007E")
def create(self, params): task_id = None conn = self.conn.get() try: name = params['name'] if name == ISO_POOL_NAME: raise InvalidOperation("KCHPOOL0031E") # The user may want to create a logical pool with the same name # used before but a volume group will already exist with this name # So check the volume group does not exist to create the pool if params['type'] == 'logical': vgdisplay_cmd = ['vgdisplay', name.encode('utf-8')] output, error, returncode = run_command(vgdisplay_cmd) # From vgdisplay error codes: # 1 error reading VGDA # 2 volume group doesn't exist # 3 not all physical volumes of volume group online # 4 volume group not found # 5 no volume groups found at all # 6 error reading VGDA from lvmtab if returncode not in [2, 4, 5]: raise InvalidOperation("KCHPOOL0036E", {'name': name}) if params['type'] == 'kimchi-iso': task_id = self._do_deep_scan(params) if params['type'] == 'scsi': adapter_name = params['source']['adapter_name'] extra_params = self.device.lookup(adapter_name) # Adds name, adapter_type, wwpn and wwnn to source information params['source'].update(extra_params) params['fc_host_support'] = self.caps.fc_host_support poolDef = StoragePoolDef.create(params) poolDef.prepare(conn) xml = poolDef.xml.encode("utf-8") except KeyError, item: raise MissingParameter("KCHPOOL0004E", { 'item': str(item), 'name': name })