def get_slice_and_slivers(self, slice_xrn, login=None): """ Returns a dict of slivers keyed on the sliver's node_id """ slivers = {} sfa_slice = None if not slice_xrn: return (sfa_slice, slivers) slice_urn = hrn_to_urn(slice_xrn, 'slice') slice_hrn, _ = urn_to_hrn(slice_xrn) slice_name = slice_hrn slices = self.driver.GetSlices(slice_filter= str(slice_name), \ slice_filter_type = 'slice_hrn', login=login) logger.debug("Slabaggregate api \tget_slice_and_slivers \ sfa_slice %s \r\n slices %s self.driver.hrn %s" \ %(sfa_slice, slices, self.driver.hrn)) if not slices: return (sfa_slice, slivers) #if isinstance(sfa_slice, list): #sfa_slice = slices[0] #else: #sfa_slice = slices # sort slivers by node id , if there is a job #and therfore, node allocated to this slice for sfa_slice in slices: try: node_ids_list = sfa_slice['node_ids'] except KeyError: logger.log_exc("SLABAGGREGATE \t \ get_slice_and_slivers KeyError ") continue for node in node_ids_list: sliver_xrn = Xrn(slice_urn, type='sliver', id=node) sliver_xrn.set_authority(self.driver.hrn) #node_id = self.driver.root_auth + '.' + node_id sliver = Sliver({'sliver_id':sliver_xrn.urn, 'name': sfa_slice['hrn'], 'type': 'slab-node', 'tags': []}) slivers[node] = sliver #Add default sliver attribute : #connection information for senslab if get_authority (sfa_slice['hrn']) == self.driver.root_auth: tmp = sfa_slice['hrn'].split('.') ldap_username = tmp[1].split('_')[0] vmaddr = 'ssh ' + ldap_username + '@grenoble.senslab.info' slivers['default_sliver'] = {'vm': vmaddr , 'login': ldap_username} #TODO get_slice_and_slivers Find the login of the external user logger.debug("SLABAGGREGATE api get_slice_and_slivers slivers %s "\ %(slivers)) return (slices, slivers)
def GetVersion(api): # peers explicitly in aggregates.xml peers =dict ([ (peername,get_serverproxy_url(v)) for (peername,v) in api.aggregates.iteritems() if peername != api.hrn]) version_manager = VersionManager() ad_rspec_versions = [] request_rspec_versions = [] for rspec_version in version_manager.versions: if rspec_version.content_type in ['*', 'ad']: ad_rspec_versions.append(rspec_version.to_dict()) if rspec_version.content_type in ['*', 'request']: request_rspec_versions.append(rspec_version.to_dict()) default_rspec_version = version_manager.get_version("sfa 1").to_dict() xrn=Xrn(api.hrn, 'authority+sa') version_more = {'interface':'slicemgr', 'hrn' : xrn.get_hrn(), 'urn' : xrn.get_urn(), 'peers': peers, 'request_rspec_versions': request_rspec_versions, 'ad_rspec_versions': ad_rspec_versions, 'default_ad_rspec': default_rspec_version } sm_version=version_core(version_more) # local aggregate if present needs to have localhost resolved if api.hrn in api.aggregates: local_am_url=get_serverproxy_url(api.aggregates[api.hrn]) sm_version['peers'][api.hrn]=local_am_url.replace('localhost',sm_version['hostname']) return sm_version
def describe(self, urns, version=None, options=None): if options is None: options={} version_manager = VersionManager() version = version_manager.get_version(version) rspec_version = version_manager._get_version(version.type, version.version, 'manifest') rspec = RSpec(version=rspec_version, user_options=options) # Update connection for the current user xrn = Xrn(urns[0], type='slice') user_name = xrn.get_authority_hrn() + '.' + xrn.leaf.split('-')[0] tenant_name = OSXrn(xrn=urns[0], type='slice').get_hrn() self.driver.shell.compute_manager.connect(username=user_name, tenant=tenant_name, password=user_name) # For delay to collect instance info time.sleep(3) # Get instances from the Openstack instances = self.get_instances(xrn) # Add sliver(s) from instance(s) geni_slivers = [] rspec.xml.set( 'expires', datetime_to_string(utcparse(time.time())) ) rspec_nodes = [] for instance in instances: rspec_nodes.append(self.instance_to_rspec_node(instance)) geni_sliver = self.instance_to_geni_sliver(instance) geni_slivers.append(geni_sliver) rspec.version.add_nodes(rspec_nodes) result = { 'geni_urn': xrn.get_urn(), 'geni_rspec': rspec.toxml(), 'geni_slivers': geni_slivers } return result
def GetVersion(self, api, options): # peers explicitly in aggregates.xml peers =dict ([ (peername,interface.get_url()) for (peername,interface) in api.aggregates.iteritems() if peername != api.hrn]) version_manager = VersionManager() ad_rspec_versions = [] request_rspec_versions = [] for rspec_version in version_manager.versions: if rspec_version.content_type in ['*', 'ad']: ad_rspec_versions.append(rspec_version.to_dict()) if rspec_version.content_type in ['*', 'request']: request_rspec_versions.append(rspec_version.to_dict()) xrn=Xrn(api.hrn, 'authority+sa') version_more = { 'interface':'slicemgr', 'sfa': 2, 'geni_api': 2, 'geni_api_versions': {'2': 'http://%s:%s' % (api.config.SFA_SM_HOST, api.config.SFA_SM_PORT)}, 'hrn' : xrn.get_hrn(), 'urn' : xrn.get_urn(), 'peers': peers, 'geni_request_rspec_versions': request_rspec_versions, 'geni_ad_rspec_versions': ad_rspec_versions, } sm_version=version_core(version_more) # local aggregate if present needs to have localhost resolved if api.hrn in api.aggregates: local_am_url=api.aggregates[api.hrn].get_url() sm_version['peers'][api.hrn]=local_am_url.replace('localhost',sm_version['hostname']) return sm_version
def call(self, xrn, creds, rspec, options): xrn = Xrn(xrn, type='slice') self.api.logger.info("interface: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, xrn.get_hrn(), self.name)) (speaking_for, _) = urn_to_hrn(options.get('geni_speaking_for')) # Find the valid credentials valid_creds = self.api.auth.checkCredentials(creds, 'createsliver', xrn.get_hrn(), speaking_for_hrn=speaking_for) # use the expiration from the first valid credential to determine when # the slivers should expire. expiration = datetime_to_string(Credential(cred=valid_creds[0]).expiration) # make sure request is not empty slivers = RSpec(rspec).version.get_nodes_with_slivers() if not slivers: raise InvalidRSpec("Missing <sliver_type> or <sliver> element. Request rspec must explicitly allocate slivers") # flter rspec through sfatables if self.api.interface in ['aggregate']: chain_name = 'INCOMING' elif self.api.interface in ['slicemgr']: chain_name = 'FORWARD-INCOMING' self.api.logger.debug("Allocate: sfatables on chain %s"%chain_name) origin_hrn = Credential(cred=valid_creds[0]).get_gid_caller().get_hrn() self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, origin_hrn, xrn, self.name)) rspec = run_sfatables(chain_name, xrn.get_hrn(), origin_hrn, rspec) slivers = RSpec(rspec).version.get_nodes_with_slivers() if not slivers: raise SfatablesRejected(slice_xrn) result = self.api.manager.Allocate(self.api, xrn.get_urn(), creds, rspec, expiration, options) return result
def __init__ (self, auth=None, hostname=None, slicename=None, email=None, interface=None, **kwargs): #def hostname_to_hrn(auth_hrn, login_base, hostname): if hostname is not None: self.type='node' # keep only the first part of the DNS name #self.hrn='.'.join( [auth,hostname.split(".")[0] ] ) # escape the '.' in the hostname self.hrn='.'.join( [auth,Xrn.escape(hostname)] ) self.hrn_to_urn() #def slicename_to_hrn(auth_hrn, slicename): elif slicename is not None: self.type='slice' # split at the first _ parts = slicename.split("_",1) self.hrn = ".".join([auth] + parts ) self.hrn_to_urn() #def email_to_hrn(auth_hrn, email): elif email is not None: self.type='person' # keep only the part before '@' and replace special chars into _ self.hrn='.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")]) self.hrn_to_urn() elif interface is not None: self.type = 'interface' self.hrn = auth + '.' + interface self.hrn_to_urn() else: Xrn.__init__ (self,**kwargs)
def GetVersion(self, api, options): # peers explicitly in aggregates.xml peers =dict ([ (peername,interface.get_url()) for (peername,interface) in api.aggregates.iteritems() if peername != api.hrn]) version_manager = VersionManager() ad_rspec_versions = [] request_rspec_versions = [] cred_types = [{'geni_type': 'geni_sfa', 'geni_version': str(i)} for i in range(4)[-2:]] for rspec_version in version_manager.versions: if rspec_version.content_type in ['*', 'ad']: ad_rspec_versions.append(rspec_version.to_dict()) if rspec_version.content_type in ['*', 'request']: request_rspec_versions.append(rspec_version.to_dict()) xrn=Xrn(api.hrn, 'authority+sm') version_more = { 'interface':'slicemgr', 'sfa': 2, 'geni_api': 3, 'geni_api_versions': {'3': 'http://%s:%s' % (api.config.SFA_SM_HOST, api.config.SFA_SM_PORT)}, 'hrn' : xrn.get_hrn(), 'urn' : xrn.get_urn(), 'peers': peers, 'geni_single_allocation': 0, # Accept operations that act on as subset of slivers in a given state. 'geni_allocate': 'geni_many',# Multiple slivers can exist and be incrementally added, including those which connect or overlap in some way. 'geni_credential_types': cred_types, } sm_version=version_core(version_more) # local aggregate if present needs to have localhost resolved if api.hrn in api.aggregates: local_am_url=api.aggregates[api.hrn].get_url() sm_version['peers'][api.hrn]=local_am_url.replace('localhost',sm_version['hostname']) return sm_version
def register_user(self, sfa_record, hrn, pub_key): # add person roles, projects and keys email = sfa_record.get('email', None) xrn = Xrn(hrn) name = xrn.get_leaf() auth_hrn = xrn.get_authority_hrn() tenant_name = OSXrn(xrn=auth_hrn, type='authority').get_tenant_name() tenant = self.shell.auth_manager.tenants.find(name=tenant_name) self.shell.auth_manager.users.create(name, email=email, tenant_id=tenant.id) user = self.shell.auth_manager.users.find(name=name) slices = sfa_records.get('slices', []) for slice in projects: slice_tenant_name = OSXrn(xrn=slice, type='slice').get_tenant_name() slice_tenant = self.shell.auth_manager.tenants.find( name=slice_tenant_name) self.shell.auth_manager.roles.add_user_role( user, slice_tenant, 'user') keys = sfa_records.get('keys', []) for key in keys: keyname = OSXrn(xrn=hrn, type='user').get_slicename() self.shell.nova_client.keypairs.create(keyname, key) return user
def allocate(self, urn, rspec_string, expiration, options={}): xrn = Xrn(urn) aggregate = DummyAggregate(self) slices = DummySlices(self) slice_record = None users = options.get('geni_users', []) if users: slice_record = users[0].get('slice_record', {}) # parse rspec rspec = RSpec(rspec_string) requested_attributes = rspec.version.get_slice_attributes() # ensure slice record exists slice = slices.verify_slice(xrn.hrn, slice_record, expiration=expiration, options=options) # ensure person records exists #persons = slices.verify_persons(xrn.hrn, slice, users, peer, sfa_peer, options=options) # add/remove slice from nodes request_nodes = rspec.version.get_nodes_with_slivers() nodes = slices.verify_slice_nodes(urn, slice, request_nodes) return aggregate.describe([xrn.get_urn()], version=rspec.version)
def _record_dict(self, xrn=None, type=None, url=None, description=None, email='', key=None, slices=[], researchers=[], pis=[]): record_dict = {} if xrn: if type: xrn = Xrn(xrn, type) else: xrn = Xrn(xrn) record_dict['urn'] = xrn.get_urn() record_dict['hrn'] = xrn.get_hrn() record_dict['type'] = xrn.get_type() if url: record_dict['url'] = url if description: record_dict['description'] = description if key: try: pubkey = open(key, 'r').read() except IOError: pubkey = key record_dict['keys'] = [pubkey] if slices: record_dict['slices'] = slices if researchers: record_dict['researcher'] = researchers if email: record_dict['email'] = email if pis: record_dict['pi'] = pis return record_dict
def __init__ (self, auth=None, hostname=None, slicename=None, slivername=None, username=None, **kwargs): if hostname is not None: self.type = 'node' self.hrn = '.'.join( [auth, Xrn.escape(hostname)] ) self.hrn_to_urn() elif slicename is not None: self.type = 'slice' self.hrn = '.'.join([auth, slicename]) self.hrn_to_urn() elif slivername is not None: self.type = 'sliver' # Reformat sliver name xrn_slivername = clab_slivername_to_xrn_slivername(slivername) self.hrn = '.'.join([auth, xrn_slivername]) self.hrn_to_urn() elif username is not None: self.type = 'user' self.hrn = '.'.join([auth, username]) self.hrn_to_urn() else: Xrn.__init__ (self, **kwargs)
def provision(self, xrn): """Provision slivers""" xrn = Xrn(xrn, 'slice') slice_urn=xrn.get_urn() options = {'geni_rspec_version': 'KOREN'} manifest = self.api.manager.Provision(self.api, [slice_urn], [], options) print manifest
def allocate(self, urn, rspec_string, expiration, options={}): xrn = Xrn(urn) aggregate = OSAggregate(self) # assume first user is the caller and use their context # for the ec2/euca api connection. Also, use the first users # key as the project key. key_name = None if len(users) > 1: key_name = aggregate.create_instance_key(xrn.get_hrn(), users[0]) # collect public keys users = options.get('geni_users', []) pubkeys = [] for user in users: pubkeys.extend(user['keys']) rspec = RSpec(rspec_string) instance_name = hrn_to_os_slicename(slice_hrn) tenant_name = OSXrn(xrn=slice_hrn, type='slice').get_tenant_name() slivers = aggregate.run_instances(instance_name, tenant_name, \ rspec_string, key_name, pubkeys) # update all sliver allocation states setting then to geni_allocated sliver_ids = [sliver.id for sliver in slivers] dbsession = self.api.dbsession() SliverAllocation.set_allocations(sliver_ids, 'geni_provisioned', dbsession) return aggregate.describe(urns=[urn], version=rspec.version)
def allocate (self, urn, rspec_string, expiration, options=None): if options is None: options={} xrn = Xrn(urn) aggregate = OSAggregate(self) # assume first user is the caller and use their context # for the ec2/euca api connection. Also, use the first users # key as the project key. key_name = None if len(users) > 1: key_name = aggregate.create_instance_key(xrn.get_hrn(), users[0]) # collect public keys users = options.get('geni_users', []) pubkeys = [] for user in users: pubkeys.extend(user['keys']) rspec = RSpec(rspec_string) instance_name = hrn_to_os_slicename(slice_hrn) tenant_name = OSXrn(xrn=slice_hrn, type='slice').get_tenant_name() slivers = aggregate.run_instances(instance_name, tenant_name, \ rspec_string, key_name, pubkeys) # update all sliver allocation states setting then to geni_allocated sliver_ids = [sliver.id for sliver in slivers] dbsession=self.api.dbsession() SliverAllocation.set_allocations(sliver_ids, 'geni_provisioned',dbsession) return aggregate.describe(urns=[urn], version=rspec.version)
def update(self, old_sfa_record, new_sfa_record, hrn, new_key): type = new_sfa_record['type'] # new_key implemented for users only if new_key and type not in ['user']: raise UnknownSfaType(type) elif type == "slice": # can update project manager and description name = hrn_to_os_slicename(hrn) researchers = sfa_record.get('researchers', []) pis = sfa_record.get('pis', []) project_manager = None description = sfa_record.get('description', None) if pis: project_manager = Xrn(pis[0], 'user').get_leaf() elif researchers: project_manager = Xrn(researchers[0], 'user').get_leaf() self.shell.auth_manager.modify_project(name, project_manager, description) elif type == "user": # can techinally update access_key and secret_key, # but that is not in our scope, so we do nothing. pass return True
def provision(self, urns, options=None): if options is None: options={} # update sliver allocation states and set them to geni_provisioned aggregate = OSAggregate(self) # Update connection for the current client xrn = Xrn(urns[0], type='slice') user_name = xrn.get_authority_hrn() + '.' + xrn.leaf.split('-')[0] tenant_name = OSXrn(xrn=urns[0], type='slice').get_hrn() self.shell.compute_manager.connect(username=user_name, tenant=tenant_name, password=user_name) instances = aggregate.get_instances(xrn) # Allocate new floating IP per the instance servers = aggregate.check_floatingip(instances, True) aggregate.create_floatingip(tenant_name, servers) sliver_ids=[] for instance in instances: sliver_id = OSXrn(name=('koren'+'.'+ instance.name), id=instance.id, type='node+openstack').get_urn() sliver_ids.append(sliver_id) dbsession=self.api.dbsession() SliverAllocation.set_allocations(sliver_ids, 'geni_provisioned', dbsession) version_manager = VersionManager() rspec_version = version_manager.get_version(options['geni_rspec_version']) return self.describe(urns, rspec_version, options=options)
def delete_instance(self, instance): def _delete_security_group(inst): security_group = inst.metadata.get('security_groups', '') if security_group: manager = SecurityGroup(self.driver) timeout = 10.0 # wait a maximum of 10 seconds before forcing the security group delete start_time = time.time() instance_deleted = False while instance_deleted == False and (time.time() - start_time) < timeout: tmp_inst = self.driver.shell.compute_manager.servers.findall(id=inst.id) if not tmp_inst: instance_deleted = True time.sleep(.5) manager.delete_security_group(security_group) multiclient = MultiClient() tenant = self.driver.shell.auth_manager.tenants.find(id=instance.tenant_id) # Update connection for the current client xrn = Xrn(tenant.name) user_name = xrn.get_authority_hrn() + '.' + xrn.leaf.split('-')[0] self.driver.shell.compute_manager.connect(username=user_name, tenant=tenant.name, password=user_name) args = { 'name': instance.name, 'id': instance.id } instances = self.driver.shell.compute_manager.servers.findall(**args) security_group_manager = SecurityGroup(self.driver) for instance in instances: # destroy instance self.driver.shell.compute_manager.servers.delete(instance) # deleate this instance's security groups multiclient.run(_delete_security_group, instance) return 1
def _record_dict(self, xrn, type, email, key, slices, researchers, pis, url, description, extras): record_dict = {} if xrn: if type: xrn = Xrn(xrn, type) else: xrn = Xrn(xrn) record_dict['urn'] = xrn.get_urn() record_dict['hrn'] = xrn.get_hrn() record_dict['type'] = xrn.get_type() if url: record_dict['url'] = url if description: record_dict['description'] = description if key: try: pubkey = open(key, 'r').read() except IOError: pubkey = key record_dict['reg-keys'] = [pubkey] if slices: record_dict['slices'] = slices if researchers: record_dict['reg-researchers'] = researchers if email: record_dict['email'] = email if pis: record_dict['reg-pis'] = pis if extras: record_dict.update(extras) return record_dict
def provision(self, xrn): """Provision slivers""" xrn = Xrn(xrn, 'slice') slice_urn=xrn.get_urn() options={} manifest = self.api.manager.provision(self.api, [slice_urn], [], options) print manifest
def register_slice(self, sfa_record, hrn): # add slice description, name, researchers, PI name = hrn_to_os_tenant_name(hrn) description = sfa_record.get('description', None) self.shell.auth_manager.tenants.create(name, description) tenant = self.shell.auth_manager.tenants.find(name=name) auth_hrn = OSXrn(xrn=hrn, type='slice').get_authority_hrn() parent_tenant_name = OSXrn(xrn=auth_hrn, type='slice').get_tenant_name() parent_tenant = self.shell.auth_manager.tenants.find( name=parent_tenant_name) researchers = sfa_record.get('researchers', []) for researcher in researchers: name = Xrn(researcher).get_leaf() user = self.shell.auth_manager.users.find(name=name) self.shell.auth_manager.roles.add_user_role(user, 'Member', tenant) self.shell.auth_manager.roles.add_user_role(user, 'user', tenant) pis = sfa_record.get('pis', []) for pi in pis: name = Xrn(pi).get_leaf() user = self.shell.auth_manager.users.find(name=name) self.shell.auth_manager.roles.add_user_role(user, 'pi', tenant) self.shell.auth_manager.roles.add_user_role( user, 'pi', parent_tenant) return tenant
def allocate(self, xrn, rspec): """Allocate slivers""" xrn = Xrn(xrn, 'slice') slice_urn=xrn.get_urn() rspec_string = open(rspec).read() options={} manifest = self.api.manager.Allocate(self.api, slice_urn, [], rspec_string, options) print manifest
def RenewSliver(self, api, xrn, creds, expiration_time, options): call_id = options.get('call_id') if Callids().already_handled(call_id): return True xrn = Xrn(xrn, 'slice') slice_urn=xrn.get_urn() slice_hrn=xrn.get_hrn() return self.driver.renew_sliver (slice_urn, slice_hrn, creds, expiration_time, options)
def provision(self, xrn): """Provision slivers""" xrn = Xrn(xrn, 'slice') slice_urn = xrn.get_urn() options = {} manifest = self.api.manager.provision(self.api, [slice_urn], [], options) print manifest
def DeleteSliver(self, api, xrn, creds, options): call_id = options.get('call_id') if Callids().already_handled(call_id): return True xrn = Xrn(xrn, 'slice') slice_urn=xrn.get_urn() slice_hrn=xrn.get_hrn() return self.driver.delete_sliver (slice_urn, slice_hrn, creds, options)
def GetVersion(api): peers =dict ([ (peername,v._ServerProxy__host) for (peername,v) in api.registries.iteritems() if peername != api.hrn]) xrn=Xrn(api.hrn) return version_core({'interface':'registry', 'hrn':xrn.get_hrn(), 'urn':xrn.get_urn(), 'peers':peers})
def SliverStatus (self, api, xrn, creds, options): call_id = options.get('call_id') if Callids().already_handled(call_id): return {} xrn = Xrn(xrn,'slice') slice_urn=xrn.get_urn() slice_hrn=xrn.get_hrn() return self.driver.sliver_status (slice_urn, slice_hrn)
def list(self, xrn, type=None, recursive=False): """List names registered at a given authority - possibly filtered by type""" xrn = Xrn(xrn, type) options = {'recursive': recursive} records = self.api.manager.List(self.api, xrn.get_hrn(), options=options) for record in records: if not type or record['type'] == type: print "%s (%s)" % (record['hrn'], record['type'])
def GetVersion(self, api, options): peers = dict ( [ (hrn,interface.get_url()) for (hrn,interface) in api.registries.iteritems() if hrn != api.hrn]) xrn=Xrn(api.hrn,type='authority') return version_core({'interface':'registry', 'sfa': 3, 'hrn':xrn.get_hrn(), 'urn':xrn.get_urn(), 'peers':peers})
def GetTicket(self, api, xrn, creds, rspec, users, options): xrn = Xrn(xrn) slice_urn=xrn.get_urn() slice_hrn=xrn.get_hrn() # xxx sounds like GetTicket is dead, but if that gets resurrected we might wish # to pass 'users' over to the driver as well return self.driver.get_ticket (slice_urn, slice_hrn, creds, rspec, options)
def allocate(self, xrn, rspec): """Allocate slivers""" xrn = Xrn(xrn, 'slice') slice_urn = xrn.get_urn() rspec_string = open(rspec).read() options = {} manifest = self.api.manager.Allocate(self.api, slice_urn, [], rspec_string, options) print manifest
def restart_instances(self, instacne_name, tenant_name, id=None): # Update connection for the current client xrn = Xrn(tenant_name) user_name = xrn.get_authority_hrn() + '.' + xrn.leaf.split('-')[0] self.driver.shell.compute_manager.connect(username=user_name, tenant=tenant_name, password=user_name) self.stop_instances(instance_name, tenant_name, id) self.start_instances(instance_name, tenant_name, id) return 1
def verify_slice_links(self, slice, requested_links, nodes): if not requested_links: return # exit if links are not supported here topology = Topology() if not topology: return # build dict of nodes nodes_dict = {} interface_ids = [] for node in nodes: nodes_dict[node['node_id']] = node interface_ids.extend(node['interface_ids']) # build dict of interfaces interfaces = self.driver.shell.GetInterfaces(interface_ids) interfaces_dict = {} for interface in interfaces: interfaces_dict[interface['interface_id']] = interface slice_tags = [] # set egre key slice_tags.append({'name': 'egre_key', 'value': self.free_egre_key()}) # set netns slice_tags.append({'name': 'netns', 'value': '1'}) # set cap_net_admin # need to update the attribute string? slice_tags.append({'name': 'capabilities', 'value': 'CAP_NET_ADMIN'}) for link in requested_links: # get the ip address of the first node in the link ifname1 = Xrn(link['interface1']['component_id']).get_leaf() if ifname1: ifname_parts = ifname1.split(':') node_raw = ifname_parts[0] device = None if len(ifname_parts) > 1: device = ifname_parts[1] node_id = int(node_raw.replace('node', '')) node = nodes_dict[node_id] if1 = interfaces_dict[node['interface_ids'][0]] ipaddr = if1['ip'] topo_rspec = VLink.get_topo_rspec(link, ipaddr) # set topo_rspec tag slice_tags.append({'name': 'topo_rspec', 'value': str([topo_rspec]), 'node_id': node_id}) # set vini_topo tag slice_tags.append({'name': 'vini_topo', 'value': 'manual', 'node_id': node_id}) #self.driver.shell.AddSliceTag(slice['name'], 'topo_rspec', str([topo_rspec]), node_id) self.verify_slice_attributes(slice, slice_tags, {'append': True}, admin=True)
def GetVersion(self, api, options): peers = dict ( [ (hrn,interface.get_url()) for (hrn,interface) in api.registries.iteritems() if hrn != api.hrn]) xrn=Xrn(api.hrn) return version_core({'interface':'registry', 'sfa': 2, 'geni_api': 2, 'hrn':xrn.get_hrn(), 'urn':xrn.get_urn(), 'peers':peers})
def xrn_to_hostname(xrn): """Returns a node's hostname from its xrn. :param xrn: The nodes xrn identifier. :type xrn: Xrn (from sfa.util.xrn) :returns: node's hostname. :rtype: string """ return Xrn.unescape(Xrn(xrn=xrn, type='node').get_leaf())
def create(self, xrn, rspec, user, key): """Allocate slivers""" xrn = Xrn(xrn, 'slice') slice_urn=xrn.get_urn() rspec_string = open(rspec).read() user_xrn = Xrn(user, 'user') user_urn = user_xrn.get_urn() user_key_string = open(key).read() users = [{'urn': user_urn, 'keys': [user_key_string]}] options={} self.api.manager.CreateSliver(self, slice_urn, [], rspec_string, users, options)
def __hrn(self,h,t,exp_urn): if verbose: print 'testing (',h,t,') expecting',exp_urn xrn=Xrn(h,type=t) if verbose: print xrn.dump_string() urn=xrn.get_urn() (h1,t1) = Xrn(urn).get_hrn_type() if h1!=h or t1!=t or urn!=exp_urn: print "hrn->urn->hrn : MISMATCH with in=(%s,%s) -- out=(%s,%s) -- urn=%s"%(h,t,h1,t1,urn) self.assertEqual(h1,h) self.assertEqual(t1,t) self.assertEqual(urn,exp_urn)
def __init__(self, name=None, type=None, **kwds): config = Config() if name is not None: self.type = type self.hrn = config.SFA_INTERFACE_HRN + "." + name self.hrn_to_urn() else: Xrn.__init__(self, **kwds) self.name = self.get_name()
def list(self, xrn, type=None, recursive=False, verbose=False): """List names registered at a given authority - possibly filtered by type""" xrn = Xrn(xrn, type) options_dict = {'recursive': recursive} records = self.api.manager.List(self.api, xrn.get_hrn(), options=options_dict) list = filter_records(type, records) # terminal_render expects an options object class Options: pass options=Options() options.verbose=verbose terminal_render (list, options)
def GetVersion(api): xrn=Xrn(api.hrn) request_rspec_versions = [dict(sfa_rspec_version)] ad_rspec_versions = [dict(sfa_rspec_version)] version_more = {'interface':'aggregate', 'testbed':'myplc', 'hrn':xrn.get_hrn(), 'request_rspec_versions': request_rspec_versions, 'ad_rspec_versions': ad_rspec_versions, 'default_ad_rspec': dict(sfa_rspec_version) } return version_core(version_more)
def xrn_to_hostname(xrn): """ Returns a node's hostname from its xrn NOTE: If it is a URN, it must include the 'type' field :param xrn: The nodes xrn identifier :type Xrn (from sfa.util.xrn) :returns: node's hostname :rtype: string """ unescaped_hostname = unescape_testbed_obj_names(Xrn(xrn=xrn, type='node').get_leaf()) return Xrn.unescape(unescaped_hostname)
def test_void(self): void = Xrn(xrn='', type=None) expected = 'urn:publicid:IDN++' self.assertEqual(void.get_hrn(), '') self.assertEqual(void.get_type(), None) self.assertEqual(void.get_urn(), expected) loop = Xrn(xrn=expected) self.assertEqual(loop.get_hrn(), '') # xxx - this is not quite right as the first object has type None self.assertEqual(loop.get_type(), '')
def xrn_object(root_auth, hostname): """Creates a valid xrn object from the node's hostname and the authority of the SFA server. :param hostname: the node's hostname. :param root_auth: the SFA root authority. :type hostname: string :type root_auth: string :returns: the iotlab node's xrn :rtype: Xrn """ return Xrn('.'.join([root_auth, Xrn.escape(hostname)]), type='node')
def call(self, xrn, creds, type): xrn = Xrn(xrn, type=type) # validate the cred valid_creds = self.api.auth.checkCredentials(creds, "remove") self.api.auth.verify_object_permission(xrn.get_hrn()) #log the call origin_hrn = Credential( string=valid_creds[0]).get_gid_caller().get_hrn() self.api.logger.info( "interface: %s\tmethod-name: %s\tcaller-hrn: %s\ttarget-urn: %s" % (self.api.interface, self.name, origin_hrn, xrn.get_urn())) return self.api.manager.Remove(self.api, xrn)
def describe(self, urns, version=None, options={}): # update nova connection tenant_name = OSXrn(xrn=urns[0], type='slice').get_tenant_name() self.driver.shell.nova_manager.connect(tenant=tenant_name) instances = self.get_instances(urns) # lookup the sliver allocations sliver_ids = [sliver['sliver_id'] for sliver in slivers] constraint = SliverAllocation.sliver_id.in_(sliver_ids) sliver_allocations = self.driver.api.dbsession().query(SliverAllocation).filter(constraint) sliver_allocation_dict = {} for sliver_allocation in sliver_allocations: sliver_allocation_dict[sliver_allocation.sliver_id] = sliver_allocation geni_slivers = [] rspec_nodes = [] for instance in instances: rspec_nodes.append(self.instance_to_rspec_node(instance)) geni_sliver = self.instance_to_geni_sliver(instance, sliver_sllocation_dict) geni_slivers.append(geni_sliver) version_manager = VersionManager() version = version_manager.get_version(version) rspec_version = version_manager._get_version(version.type, version.version, 'manifest') rspec = RSpec(version=rspec_version, user_options=options) rspec.xml.set('expires', datetime_to_string(utcparse(time.time()))) rspec.version.add_nodes(rspec_nodes) result = {'geni_urn': Xrn(urns[0]).get_urn(), 'geni_rspec': rspec.toxml(), 'geni_slivers': geni_slivers} return result
def check_sliver_credentials(self, creds, urns): # build list of cred object hrns slice_cred_names = [] for cred in creds: slice_cred_hrn = Credential(cred=cred).get_gid_object().get_hrn() slice_cred_names.append( DummyXrn(xrn=slice_cred_hrn).dummy_slicename()) # look up slice name of slivers listed in urns arg slice_ids = [] for urn in urns: sliver_id_parts = Xrn(xrn=urn).get_sliver_id_parts() try: slice_ids.append(int(sliver_id_parts[0])) except ValueError: pass if not slice_ids: raise Forbidden("sliver urn not provided") slices = self.shell.GetSlices({'slice_ids': slice_ids}) sliver_names = [slice['slice_name'] for slice in slices] # make sure we have a credential for every specified sliver ierd for sliver_name in sliver_names: if sliver_name not in slice_cred_names: msg = "Valid credential not found for target: %s" % sliver_name raise Forbidden(msg)
def list(self, xrn, type=None, recursive=False, verbose=False): """List names registered at a given authority - possibly filtered by type""" xrn = Xrn(xrn, type) options_dict = {'recursive': recursive} records = self.api.manager.List(self.api, xrn.get_hrn(), options=options_dict) list = filter_records(type, records) # terminal_render expects an options object class Options: pass options = Options() options.verbose = verbose terminal_render(list, options)
def _process_node(cls, node): node['type'] = 'node' node['network_hrn'] = Xrn(node['component_id']).authority[0] # network ? XXX node['hrn'] = urn_to_hrn(node['component_id'])[0] node['urn'] = node['component_id'] node['hostname'] = node['component_name'] node['initscripts'] = node.pop('pl_initscripts') # All Labora nodes are exclusive = true node['exclusive'] = 'true' if 'granularity' in node: node['granularity'] = node['granularity']['grain'] # XXX This should use a MAP as before if 'position' in node: # iotlab node['x'] = node['position']['posx'] node['y'] = node['position']['posy'] node['z'] = node['position']['posz'] del node['position'] if 'location' in node: if node['location']: node['latitude'] = node['location']['latitude'] node['longitude'] = node['location']['longitude'] del node['location'] # Flatten tags if 'tags' in node: if node['tags']: for tag in node['tags']: node[tag['tagname']] = tag['value'] del node['tags'] return node
def xrn_object(root_auth, hostname): """ Creates a valid xrn object from the node's hostname and the authority of the SFA server. :param hostname: the node's hostname :type string :param root_auth: the SFA root authority :type string :returns: the C-Lab node's xrn :rtype: Xrn """ escaped_hostname = escape_testbed_obj_names(hostname) return Xrn('.'.join([root_auth, Xrn.escape(escaped_hostname)]), type='node')
def create_instance_key(self, slice_hrn, user): slice_name = Xrn(slice_hrn).leaf user_name = Xrn(user['urn']).leaf key_name = "%s_%s" % (slice_name, user_name) pubkey = user['keys'][0] key_found = False existing_keys = self.driver.shell.nova_manager.keypairs.findall(name=key_name) for existing_key in existing_keys: if existing_key.public_key != pubkey: self.driver.shell.nova_manager.keypairs.delete(existing_key) elif existing_key.public_key == pubkey: key_found = True if not key_found: self.driver.shell.nova_manager.keypairs.create(key_name, pubkey) return key_name
def check_sliver_credentials(self, creds, urns): # build list of cred object hrns slice_cred_names = [] for cred in creds: slice_cred_hrn = Credential(cred=cred).get_gid_object().get_hrn() slice_cred_names.append(OSXrn(xrn=slice_cred_hrn).get_slicename()) # look up slice name of slivers listed in urns arg slice_ids = [] for urn in urns: sliver_id_parts = Xrn(xrn=urn).get_sliver_id_parts() slice_ids.append(sliver_id_parts[0]) if not slice_ids: raise Forbidden("sliver urn not provided") sliver_names = [] for slice_id in slice_ids: slice = self.shell.auth_manager.tenants.find(slice_id) sliver_names.append(slice['name']) # make sure we have a credential for every specified sliver ierd for sliver_name in sliver_names: if sliver_name not in slice_cred_names: msg = "Valid credential not found for target: %s" % sliver_name raise Forbidden(msg)
def delete(self, urns, options={}): # collect sliver ids so we can update sliver allocation states after # we remove the slivers. aggregate = OSAggregate(self) instances = aggregate.get_instances(urns) sliver_ids = [] for instance in instances: sliver_hrn = "%s.%s" % (self.driver.hrn, instance.id) sliver_ids.append(Xrn(sliver_hrn, type='sliver').urn) # delete the instance aggregate.delete_instance(instance) # delete sliver allocation states dbsession = self.api.dbsession() SliverAllocation.delete_allocations(sliver_ids, dbsession) # return geni_slivers geni_slivers = [] for sliver_id in sliver_ids: geni_slivers.append({ 'geni_sliver_urn': sliver['sliver_id'], 'geni_allocation_status': 'geni_unallocated', 'geni_expires': None }) return geni_slivers
def instance_to_geni_sliver(self, instance, sliver_allocations = {}): sliver_hrn = '%s.%s' % (self.driver.hrn, instance.id) sliver_id = Xrn(sliver_hrn, type='sliver').urn # set sliver allocation and operational status sliver_allocation = sliver_allocations[sliver_id] if sliver_allocation: allocation_status = sliver_allocation.allocation_state if allocation_status == 'geni_allocated': op_status = 'geni_pending_allocation' elif allocation_status == 'geni_provisioned': state = instance.state.lower() if state == 'active': op_status = 'geni_ready' elif state == 'building': op_status = 'geni_notready' elif state == 'failed': op_status =' geni_failed' else: op_status = 'geni_unknown' else: allocation_status = 'geni_unallocated' # required fields geni_sliver = {'geni_sliver_urn': sliver_id, 'geni_expires': None, 'geni_allocation_status': allocation_status, 'geni_operational_status': op_status, 'geni_error': None, 'plos_created_at': datetime_to_string(utcparse(instance.created)), 'plos_sliver_type': self.shell.nova_manager.flavors.find(id=instance.flavor['id']).name, } return geni_sliver
def urn_to_properties(value): # urn:publicid:IDN+omf:paris.fit-nitos.fr+node+node38 # node38.paris.fit-nitos.fr #import pdb #pdb.set_trace() urn = value['component_id'] hrn = Xrn(urn).get_hrn() t_urn = urn.split('+') authority = t_urn[1] if ':' in authority: t_authority = authority.split(':') authority = t_authority[1] host = t_urn[-1] hostname = host + '.' + authority if value['type'] == 'node' and 'available.now' in value: if value['available.now'] == 'true': boot = "boot" else: boot = "disabled" return { 'exclusive': True, 'hostname': hostname, 'hrn': hrn, 'urn': urn, 'boot_state': boot } else: return { 'exclusive': True, 'hostname': hostname, 'hrn': hrn, 'urn': urn }
def get_aggregate_nodes(self): zones = self.get_availability_zones() # available sliver/instance/vm types instances = self.driver.shell.nova_manager.flavors.list() if isinstance(instances, dict): instances = instances.values() # available images images = self.driver.shell.image_manager.get_images_detailed() disk_images = [image_to_rspec_disk_image(img) for img in images if img['container_format'] in ['ami', 'ovf']] rspec_nodes = [] for zone in zones: rspec_node = Node() xrn = OSXrn(zone, type='node') rspec_node['component_id'] = xrn.urn rspec_node['component_name'] = xrn.name rspec_node['component_manager_id'] = Xrn(self.driver.hrn, 'authority+cm').get_urn() rspec_node['exclusive'] = 'false' rspec_node['hardware_types'] = [HardwareType({'name': 'plos-pc'}), HardwareType({'name': 'pc'})] slivers = [] for instance in instances: sliver = self.instance_to_sliver(instance) sliver['disk_image'] = disk_images slivers.append(sliver) rspec_node['available'] = 'true' rspec_node['slivers'] = slivers rspec_nodes.append(rspec_node) return rspec_nodes
def __init__(self, name=None, auth=None, **kwds): config = Config() self.id = id if name is not None: Xrn.__init__(self, **kwds) if 'type' in kwds: self.type = kwds['type'] if auth is not None: self.hrn = '.'.join([auth, cleanup_name(name)]) else: self.hrn = name.replace('_', '.') self.hrn_to_urn() else: Xrn.__init__(self, **kwds) self.name = self.get_name()
def sliver_to_slice_xrn(self, xrn): sliver_id_parts = Xrn(xrn).get_sliver_id_parts() slice = self.shell.auth_manager.tenants.find(id=sliver_id_parts[0]) if not slice: raise Forbidden("Unable to locate slice record for sliver: %s" % xrn) slice_xrn = OSXrn(name=slice.name, type='slice') return slice_xrn
def to_sfa_rspec(rspec, content_type=None): if not isinstance(rspec, RSpec): pg_rspec = RSpec(rspec) else: pg_rspec = rspec version_manager = VersionManager() sfa_version = version_manager._get_version('sfa', '1') sfa_rspec = RSpec(version=sfa_version) #nodes = pg_rspec.version.get_nodes() #sfa_rspec.version.add_nodes(nodes()) #sfa_rspec.version.add_links(pg_rspec.version.get_links()) #return sfa_rspec.toxml() # get network networks = pg_rspec.version.get_networks() network_hrn = networks[0]["name"] network_element = sfa_rspec.xml.add_element('network', name=network_hrn, id=network_hrn) # get nodes pg_nodes_elements = pg_rspec.version.get_nodes() nodes_with_slivers = pg_rspec.version.get_nodes_with_slivers() i = 1 for pg_node in pg_nodes_elements: attribs = dict(pg_node.items()) attribs['id'] = 'n' + str(i) node_element = network_element.add_element('node') for attrib in attribs: if type(attribs[attrib]) == str: node_element.set(attrib, attribs[attrib]) urn = pg_node["component_id"] if urn: if type(urn) == list: # legacy code, not sure if urn is ever a list... urn = urn[0] hostname = Xrn.urn_split(urn)[-1] hostname_element = node_element.add_element('hostname') hostname_element.set_text(hostname) if hostname in nodes_with_slivers: node_element.add_element('sliver') for hardware_type in pg_node["hardware_types"]: if "name" in hardware_type: node_element.add_element("hardware_type", name=hardware_type["name"]) # just copy over remaining child elements #for child in pg_node_element.getchildren(): # node_element.append(transform(child).getroot()) i = i + 1 return sfa_rspec.toxml()