Example #1
0
    def merge(self, in_rspec):
        """
        Merge contents for specified rspec with current rspec
        """

        if not in_rspec:
            return

        from sfa.rspecs.rspec import RSpec
        if isinstance(in_rspec, RSpec):
            rspec = in_rspec
        else:
            rspec = RSpec(in_rspec)
        if rspec.version.type.lower() == 'protogeni':
            from sfa.rspecs.rspec_converter import RSpecConverter
            in_rspec = RSpecConverter.to_sfa_rspec(rspec.toxml())
            rspec = RSpec(in_rspec)

        # just copy over all networks
        current_networks = self.get_networks()
        networks = rspec.version.get_networks()
        for network in networks:
            current_network = network.get('name')
            if current_network and current_network not in current_networks:
                self.xml.append(network.element)
                current_networks.append(current_network)
Example #2
0
    def merge(self, in_rspec):
        """
        Merge contents for specified rspec with current rspec
        """

        if not in_rspec:
            return

        from sfa.rspecs.rspec import RSpec
        if isinstance(in_rspec, RSpec):
            rspec = in_rspec
        else:
            rspec = RSpec(in_rspec)
        if rspec.version.type.lower() == 'protogeni':
            from sfa.rspecs.rspec_converter import RSpecConverter
            in_rspec = RSpecConverter.to_sfa_rspec(rspec.toxml())
            rspec = RSpec(in_rspec)

        # just copy over all networks
        current_networks = self.get_networks()
        networks = rspec.version.get_networks()
        for network in networks:
            current_network = network.get('name')
            if current_network and current_network not in current_networks:
                self.xml.append(network.element)
                current_networks.append(current_network)
Example #3
0
    def list_resources(self, parent, typename):
        assert(typename == "SFAPNode" or not typename)
        assert(parent == None)
        list_resources = self.__sfi.bootstrap.server_proxy(self.__sfi.sm_url).ListResources(self.__credentials,self.__options)
        self.__sfi.bootstrap.server_proxy(self.__sfi.sm_url)
        logger.debug( "ListResources at %s returned : %s"%(self.__sfi.sm_url,list_resources['value']))
        rspec = RSpec(list_resources['value'])
        nodes = rspec.version.get_nodes()
        networks = rspec.version.get_networks()
        for node in nodes:
            version = {'namespace': None, 'version': '1', 'type': 'SFA', 'extensions': [], 'schema': None}
            rspec_tmp = RSpec(version = version)
            rspec_tmp.version.add_network(networks[0]['name'])	
            rspec_tmp.version.add_nodes([node])
            component_name = ''
            component_name = node['component_name']

            rspec_tmp.version.add_slivers(hostnames = [component_name])
            rspec_tmp_xml = RSpecConverter.to_sfa_rspec(rspec_tmp.toxml())
 
            self.__instances.add(component_name)
            rspec_xml = {}
            rspec_xml['xmlRspec'] = rspec_tmp_xml
            self.__rspecs['/SFAPNode-%s' %component_name] = rspec_xml
            logger.debug("PNodes %s " %component_name)

        logger.debug(self.__rspecs)
        return [ Identifier("/SFAPNode-%s"  %i) for i in self.__instances ]
Example #4
0
File: sfi.py Project: planetlab/sfa
    def create(self, opts, args):
        server = self.get_server_from_opts(opts)
        server_version = self.get_cached_server_version(server)
        slice_hrn = args[0]
        slice_urn = hrn_to_urn(slice_hrn, 'slice') 
        user_cred = self.get_user_cred()
        slice_cred = self.get_slice_cred(slice_hrn).save_to_string(save_parents=True)
        # delegate the cred to the callers root authority
        delegated_cred = self.delegate_cred(slice_cred, get_authority(self.authority)+'.slicemanager')
        #delegated_cred = self.delegate_cred(slice_cred, get_authority(slice_hrn))
        #creds.append(delegated_cred)
        rspec_file = self.get_rspec_file(args[1])
        rspec = open(rspec_file).read()

        # need to pass along user keys to the aggregate.  
        # users = [
        #  { urn: urn:publicid:IDN+emulab.net+user+alice
        #    keys: [<ssh key A>, <ssh key B>] 
        #  }]
        users = []
        slice_records = self.registry.Resolve(slice_urn, [user_cred.save_to_string(save_parents=True)])
        if slice_records and 'researcher' in slice_records[0] and slice_records[0]['researcher']!=[]:
            slice_record = slice_records[0]
            user_hrns = slice_record['researcher']
            user_urns = [hrn_to_urn(hrn, 'user') for hrn in user_hrns]
            user_records = self.registry.Resolve(user_urns, [user_cred.save_to_string(save_parents=True)])
            
            if 'sfa' not in server_version:
                users = pg_users_arg(user_records)
                rspec = RSpec(rspec)
                rspec.filter({'component_manager_id': server_version['urn']})
                rspec = RSpecConverter.to_pg_rspec(rspec.toxml(), content_type='request')
                creds = [slice_cred]
            else:
                users = sfa_users_arg(user_records, slice_record)
                creds = [slice_cred, delegated_cred]
        call_args = [slice_urn, creds, rspec, users]
        if self.server_supports_call_id_arg(server):
            call_args.append(unique_call_id())
           
        result = server.CreateSliver(*call_args)
        if opts.file is None:
            print result
        else:
            save_rspec_to_file (result, opts.file)
        return result
Example #5
0
 def _CreateSliver(aggregate, server, xrn, credential, rspec, users, options):
     tStart = time.time()
     try:
         # Need to call GetVersion at an aggregate to determine the supported
         # rspec type/format beofre calling CreateSliver at an Aggregate.
         server_version = api.get_cached_server_version(server)
         requested_users = users
         if 'sfa' not in server_version and 'geni_api' in server_version:
             # sfa aggregtes support both sfa and pg rspecs, no need to convert
             # if aggregate supports sfa rspecs. otherwise convert to pg rspec
             rspec = RSpec(RSpecConverter.to_pg_rspec(rspec, 'request'))
             filter = {'component_manager_id': server_version['urn']}
             rspec.filter(filter)
             rspec = rspec.toxml()
             requested_users = sfa_to_pg_users_arg(users)
         rspec = server.CreateSliver(xrn, credential, rspec, requested_users, options)
         return {"aggregate": aggregate, "rspec": rspec, "elapsed": time.time()-tStart, "status": "success"}
     except:
         logger.log_exc('Something wrong in _CreateSliver with URL %s'%server.url)
         return {"aggregate": aggregate, "elapsed": time.time()-tStart, "status": "exception", "exc_info": sys.exc_info()}
Example #6
0
    def add_resource(self, parent_id, name, typename, config, owner = None):
        if not parent_id:
            raise ValueError("Need a parent")
        
        #Creating a new SFI client object
#        sfi = Sfi()
#        sfi.read_config()
#        sfi.bootstrap()
#        credentials = [ sfi.my_credential_string ]
#        bootstrap = sfi.bootstrap.server_proxy(sfi.sm_url)
        
        #Create new options
        version_dict = {'type':'SFA', 'version':'1','schema':None,'namespace':None,'extensions':[]}
        options = {}
        options[ 'geni_rspec_version' ]  = version_dict
           
        #Getting the configuration paramter of the parent resource, in this case an RSpec
        parent = self.__manager.get_resource(parent_id)
        pconfig = parent.get_configuration()
        rspec = pconfig['xmlRspec']
        logger.debug('RSpec of the parent %s'%rspec)

        #Saving the hostname of the parent in order to retrieve the slice later
        hostname = self.fetch_tag(rspec,'hostname')
        logger.debug('Saved the hostname %s'%hostname)        

        #Getting the vctname and creating the slice_hrn        
        slice_hrn = 'raven.fts.%s' %config['vctname']
        slice_urn = hrn_to_urn(slice_hrn, 'slice')
        logger.info('Creating or updating a slice with the name %s'%slice_hrn)

        #Preparing the server_proxy object and getting the server version
        result = self.__bootstrap.GetVersion()
        server_version= ReturnValue.get_value(result)
        logger.debug('Received server version %s'%server_version)	


        #Creating the slice record dict or string
        #recorddict = dict({'hrn': slice_hrn,
        #'url': 'http://planet-lab.org',
        #'type': 'slice',
        #'researcher': ['teagle.teagle.teagle'],
        #'description': 'Teagle slice'})
        slice_str = '<record description="Teagle Slice4" hrn="%s" type="slice" url="http://planet-lab.org"><researcher>teagle.teagle.teagle</researcher></record>' %slice_hrn
        slicerecord = SfaRecord(string = slice_str).as_dict()
        logger.debug('Prepared a slice record to add to the registry %s'%slice_str)
 
        
        #Retrieving the credential of the authority
        auth_cred = self.__sfi.bootstrap.authority_credential_string (self.__sfi.authority)
        #auth_cred = sfi.bootstrap.authority_credential_string (sfi.authority)
        logger.debug('Authority %s credentials %s'%(self.__sfi.authority, auth_cred,))
        #logger.debug('Authority %s credentials %s'%(sfi.authority, auth_cred,))

        #Trying to create the slice
        try:
            records = self.registry().Register(slicerecord, auth_cred)
            #records = self.registry(sfi).Register(slicerecord, auth_cred)
        except ServerException:
            logger.debug("Slice already existing")
            pass
        
        #Saving the slice credential
        creds = [self.__sfi.slice_credential_string(slice_hrn)]
        #creds = [sfi.slice_credential_string(slice_hrn)]
        logger.debug('The slice credential: %s'%creds)
        
        
        # users
        # need to pass along user keys to the aggregate.
        # users = [
        #  { urn: urn:publicid:IDN+emulab.net+user+alice
        #    keys: [<ssh key A>, <ssh key B>]
        #  }]
        users = []
        slice_records = self.registry().Resolve(slice_urn, [self.__sfi.my_credential_string])
        #slice_records = self.registry(sfi).Resolve(slice_urn, [sfi.my_credential_string])
        if slice_records and 'researcher' in slice_records[0] and slice_records[0]['researcher']!=[]:
            slice_record = slice_records[0]
            user_hrns = slice_record['researcher']
            user_urns = [hrn_to_urn(hrn, 'user') for hrn in user_hrns]
            user_records = self.registry().Resolve(user_urns, [self.__sfi.my_credential_string])
            #user_records = self.registry(sfi).Resolve(user_urns, [sfi.my_credential_string])

            if 'sfa' not in server_version:
                users = pg_users_arg(user_records)
                rspec = RSpec(rspec)
                rspec.filter({'component_manager_id': server_version['urn']})
                rspec = RSpecConverter.to_pg_rspec(rspec.toxml(), content_type='request')
            else:
                users = sfa_users_arg(user_records, slice_record)


        logger.debug('Creating the sliver using the RSpec %s'%rspec)
        time.sleep(5)

        #Creating the sliver
        logger.debug("###################slice_urn: %s"%(slice_urn,))
        logger.debug("###################creds: %s"%(creds,))
        logger.debug("###################rspec: %s"%(rspec,))
        logger.debug("###################users: %s"%(users,))
        logger.debug("###################options: %s"%(options,))
        result = self.__bootstrap.CreateSliver(slice_urn, creds, rspec, users, options)
        #result = bootstrap.CreateSliver(slice_urn, creds, rspec, users,options)
        value = ReturnValue.get_value(result)
        logger.debug("###################return value: %s"%(value, ))

        options['geni_slice_urn'] = hrn_to_urn(slice_hrn, 'slice')
        options['call_id'] = unique_call_id()

        slice_credentials = self.__sfi.slice_credential_string(slice_hrn)
        #slice_credentials = sfi.slice_credential_string(slice_hrn)

        list_resources = self.__bootstrap.ListResources(slice_credentials, options)
        #list_resources = bootstrap.ListResources(slice_credentials,options)
        #self.__sfi.bootstrap.server_proxy(self.__sfi.sm_url)
        logger.debug( "ListResources of slice %s returned : %s"%(slice_hrn,list_resources['value']))
        slice_rspec = RSpec(list_resources['value'])
        nodes = slice_rspec.version.get_nodes()
        for node in nodes:
            component_name = ''
            component_name = node['component_name']
            tags = self.convert_tags(node['tags'])
            node_hostname = tags['hostname']
            
            if hostname in node_hostname:
                #store the information of the specific sliver in the config 
                
                ###XXX change the username with the real sliver name
                sliver_name = self.fetch_username(slice_rspec)
                ip = self.fetch_ip(slice_rspec)
                name = '%s.%s'%(hostname,sliver_name,)
                conf = {'public_ip':ip,'username':sliver_name,'password':None}
                #self.__config['/SFAVNode-%s' %name] = conf
                self.__config[name] = conf
        logger.info('Adding the resource instance %s '%name)
        time.sleep(10) 
        if not self.poll_machine(ip, sliver_name):
            raise Exception('Connection with the sliver not possible')

        
        
        
        if name in self.__instances:
                raise DuplicateNameError(parent_id, typename, name)
        self.__instances.add(name)

        return name