Beispiel #1
0
 def to_sfa_rspec(in_rspec, content_type=None):
     rspec = RSpec(in_rspec)
     version_manager = VersionManager()
     sfa_version = version_manager._get_version('sfa', '1')
     pg_version = version_manager._get_version('protogeni', '2')
     if rspec.version.type.lower() == sfa_version.type.lower(): 
       return in_rspec
     elif rspec.version.type.lower() == pg_version.type.lower(): 
         return PGRSpecConverter.to_sfa_rspec(in_rspec, content_type)
     else:
         return in_rspec 
Beispiel #2
0
    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()
Beispiel #3
0
    def get_rspec(self, version=None, slice_leaf=None, projectName=None, created_vms=[], new_nodes=[], options={}):

        version_manager = VersionManager()
        version = version_manager.get_version(version)
        if slice_leaf:
            # Manifest RSpec will be used when somebody creates an sliver, returning the resources of this AM and the vm(s) requested by the user.
            rspec_version = version_manager._get_version(version.type, version.version, "manifest")
            options["slice"] = slice_leaf
            nodes = self.get_nodes_in_geni(options, slice_leaf, projectName, created_vms, new_nodes)
            rspec = RSpec(version=rspec_version, user_options=options)
            rspec.version.add_nodes_geni(nodes)

        else:
            rspec_version = version_manager._get_version(version.type, version.version, "ad")
            nodes = self.get_nodes(options, slice_leaf, projectName, created_vms, new_nodes)
            rspec = RSpec(version=rspec_version, user_options=options)
            # nodes = self.get_nodes(options,slice_leaf,projectName,created_vms,new_nodes)
            rspec.version.add_nodes(nodes)
        return rspec.toxml()
Beispiel #4
0
 def __init__(self, rspec="", version=None, user_options={}):
     self.header = '<?xml version="1.0"?>\n'
     self.template = """<RSpec></RSpec>"""
     self.version = None
     self.xml = XML()
     self.version_manager = VersionManager()
     self.user_options = user_options
     self.elements = {}
     if rspec:
         if version:
             self.version = self.version_manager.get_version(version)
             self.parse_xml(rspec, version)
         else:
             self.parse_xml(rspec)
     elif version:
         self.create(version)
     else:
         raise InvalidRSpec("No RSpec or version specified. Must specify a valid rspec string or a valid version") 
Beispiel #5
0
class RSpec:
 
    def __init__(self, rspec="", version=None, user_options={}):
        self.header = '<?xml version="1.0"?>\n'
        self.template = """<RSpec></RSpec>"""
        self.version = None
        self.xml = XML()
        self.version_manager = VersionManager()
        self.user_options = user_options
        self.elements = {}
        if rspec:
            if version:
                self.version = self.version_manager.get_version(version)
                self.parse_xml(rspec, version)
            else:
                self.parse_xml(rspec)
        elif version:
            self.create(version)
        else:
            raise InvalidRSpec("No RSpec or version specified. Must specify a valid rspec string or a valid version") 
    def create(self, version=None):
        """
        Create root element
        """
        self.version = self.version_manager.get_version(version)
        self.namespaces = self.version.namespaces
        self.parse_xml(self.version.template, self.version) 
        # eg. 2011-03-23T19:53:28Z 
        date_format = '%Y-%m-%dT%H:%M:%SZ'
        now = datetime.utcnow()
        generated_ts = now.strftime(date_format)
        expires_ts = (now + timedelta(hours=1)).strftime(date_format) 
        self.xml.set('expires', expires_ts)
        self.xml.set('generated', generated_ts)


    def parse_xml(self, xml, version=None):
        self.xml.parse_xml(xml)
        if not version:
            if self.xml.schema:
                self.version = self.version_manager.get_version_by_schema(self.xml.schema)
            else:
                #raise InvalidRSpec('unknown rspec schema: %s' % schema)
                # TODO: Should start raising an exception once SFA defines a schema.
                # for now we just  default to sfa 
                self.version = self.version_manager.get_version({'type':'sfa','version': '1'})
        self.version.xml = self.xml    
        self.namespaces = self.xml.namespaces
    
    def load_rspec_elements(self, rspec_elements):
        self.elements = {}
        for rspec_element in rspec_elements:
            if isinstance(rspec_element, RSpecElement):
                self.elements[rspec_element.type] = rspec_element

    def register_rspec_element(self, element_type, element_name, element_path):
        if element_type not in RSpecElements:
            raise InvalidRSpecElement(element_type, extra="no such element type: %s. Must specify a valid RSpecElement" % element_type)
        self.elements[element_type] = RSpecElement(element_type, element_name, element_path)

    def get_rspec_element(self, element_type):
        if element_type not in self.elements:
            msg = "ElementType %s not registerd for this rspec" % element_type
            raise InvalidRSpecElement(element_type, extra=msg)
        return self.elements[element_type]

    def get(self, element_type, filter={}, depth=0):
        elements = self.get_elements(element_type, filter)
        elements = [self.xml.get_element_attributes(elem, depth=depth) for elem in elements]
        return elements

    def get_elements(self, element_type, filter={}):
        """
        search for a registered element
        """
        if element_type not in self.elements:
            msg = "Unable to search for element %s in rspec, expath expression not found." % \
                   element_type
            raise InvalidRSpecElement(element_type, extra=msg)
        rspec_element = self.get_rspec_element(element_type)
        xpath = rspec_element.path + XpathFilter.xpath(filter)
        return self.xml.xpath(xpath)

    def merge(self, in_rspec):
        self.version.merge(in_rspec)

    def filter(self, filter):
        if 'component_manager_id' in filter:    
            nodes = self.version.get_node_elements()
            for node in nodes:
                if 'component_manager_id' not in node.attrib or \
                  node.attrib['component_manager_id'] != filter['component_manager_id']:
                    parent = node.getparent()
                    parent.remove(node) 
        

    def toxml(self, header=True):
        if header:
            return self.header + self.xml.toxml()
        else:
            return self.xml.toxml()
    

    def save(self, filename):
        return self.xml.save(filename)
Beispiel #6
0
    def to_pg_rspec(rspec, content_type = None):
        if not isinstance(rspec, RSpec):
            sfa_rspec = RSpec(rspec)
        else:
            sfa_rspec = rspec
  
        if not content_type or content_type not in \
          ['ad', 'request', 'manifest']:
            content_type = sfa_rspec.version.content_type
     
 
        version_manager = VersionManager()
        pg_version = version_manager._get_version('protogeni', '2', 'request')
        pg_rspec = RSpec(version=pg_version)
 
        # get networks
        networks = sfa_rspec.version.get_networks()
        
        for network in networks:
            # get nodes
            sfa_node_elements = sfa_rspec.version.get_node_elements(network=network)
            for sfa_node_element in sfa_node_elements:
                # create node element
                node_attrs = {}
                node_attrs['exclusive'] = 'false'
                if 'component_manager_id' in sfa_node_element.attrib:
                    node_attrs['component_manager_id'] = sfa_node_element.attrib['component_manager_id']
                else:
                    node_attrs['component_manager_id'] = hrn_to_urn(network, 'authority+cm')

                if 'component_id' in sfa_node_element.attrib:
                    node_attrs['compoenent_id'] = sfa_node_element.attrib['component_id']

                if sfa_node_element.find('hostname') != None:
                    hostname = sfa_node_element.find('hostname').text
                    node_attrs['component_name'] = hostname
                    node_attrs['client_id'] = hostname
                node_element = pg_rspec.xml.add_element('node', node_attrs)    
            
                if content_type == 'request':
                    sliver_element = sfa_node_element.find('sliver')
                    sliver_type_elements = sfa_node_element.xpath('./sliver_type', namespaces=sfa_rspec.namespaces)
                    available_sliver_types = [element.attrib['name'] for element in sliver_type_elements]
                    valid_sliver_types = ['emulab-openvz', 'raw-pc']
                   
                    # determine sliver type 
                    requested_sliver_type = 'emulab-openvz'
                    for available_sliver_type in available_sliver_types:
                        if available_sliver_type in valid_sliver_types:
                            requested_sliver_type = available_sliver_type
                                
                    if sliver_element != None:
                        pg_rspec.xml.add_element('sliver_type', {'name': requested_sliver_type}, parent=node_element) 
                else:
                    # create node_type element
                    for hw_type in ['plab-pc', 'pc']:
                        hdware_type_element = pg_rspec.xml.add_element('hardware_type', {'name': hw_type}, parent=node_element)
                    # create available element
                    pg_rspec.xml.add_element('available', {'now': 'true'}, parent=node_element)
                    # create locaiton element
                    # We don't actually associate nodes with a country. 
                    # Set country to "unknown" until we figure out how to make
                    # sure this value is always accurate.
                    location = sfa_node_element.find('location')
                    if location != None:
                        location_attrs = {}      
                        location_attrs['country'] =  location.get('country', 'unknown')
                        location_attrs['latitude'] = location.get('latitude', 'None')
                        location_attrs['longitude'] = location.get('longitude', 'None')
                        pg_rspec.xml.add_element('location', location_attrs, parent=node_element)

        return pg_rspec.toxml()