Beispiel #1
0
class VTSfaDriver:
    def __init__(self, config):
        self.aggregate = VMAggregate()
        self.shell = VTShell()

    def list_resources(self, options):
        version_manager = VersionManager()
        rspec_version = 'OcfVt'  #version_manager.get_version(options.get('geni_rspec_version'))
        version_string = "rspec_%s" % (rspec_version)
        slice_leaf = None
        if options.get("slice"):
            slice_leaf = options['slice']
        rspec = self.aggregate.get_rspec(version=rspec_version,
                                         options=options,
                                         slice_leaf=slice_leaf)
        return rspec

    def crud_slice(self, slice_leaf, authority, action=None):
        slicename = slice_leaf
        try:
            slice = self.shell.GetSlice(slicename, authority)
        except Exception as e:
            raise RecordNotFound(slice_leaf)
        for vm in slice['vms']:
            if action == 'start_slice':
                self.shell.StartSlice(vm['node-id'], vm['vm-id'])
            elif action == 'stop_slice':
                self.shell.StopSlice(vm['node-id'], vm['vm-id'])
            elif action == 'delete_slice':
                self.shell.DeleteSlice(vm['node-id'], vm['vm-id'])
                try:
                    logging.info(
                        "LDAP: deleting project in LDAP of SSH gateway")
                    session = ldapManager()
                    con = session.bind()
                    if con:
                        ldapprj = "%s.%s" % (authority, slicename)
                        ldapprj = ldapprj.replace("\\", "")
                        #logging.error("ldapprj: "+ ldapprj)
                        status = session.delProject(con, ldapprj)
                        if not status:
                            logging.error(
                                "Cannot delete entry in LDAP of the SSH gateway"
                            )
                    else:
                        logging.error("Cannot contact LDAP of the SSH gateway")
                except:
                    logging.error("Cannot contact LDAP of the SSH gateway")
            elif action == 'reset_slice':
                self.shell.RebootSlice(vm['node-id'], vm['vm-id'])
        return 1

    def create_sliver(self, slice_leaf, authority, rspec_string, users,
                      options, expiration_date):
        rspec = RSpec(rspec_string, 'OcfVt')
        requested_attributes = rspec.version.get_slice_attributes()
        requested_attributes = self.shell.convert_to_uuid(
            requested_attributes)  #Converts component_id URNs to UUIDs
        projectName = authority  #users[0]['slice_record']['authority']
        sliceName = slice_leaf
        self.shell.CreateSliver(requested_attributes, projectName, sliceName,
                                expiration_date)
        created_vms = list()
        nodes = list()
        for slivers in requested_attributes:
            node = self.shell.GetNodes(uuid=slivers['component_id'])
            #print "\n\n\n\n\n\nslivers: %s\n\n\n\n\n\n" % slivers['slivers']
            for vm in slivers['slivers']:
                #node = self.shell.GetNodes(uuid=vm['server-id'])
                if not node in nodes:
                    nodes.append(node)
                try:
                    #print "\n\n\n\n\n\nusers: %s\n\n\n\n\n\n" % str(users)
                    for user in users:
                        xrn = Xrn(user['urn'], 'user')
                        user_name = xrn.get_leaf()
                        # Store user SSH key for future use
                        for user_key in user['keys']:
                            if not VirtualMachineKeys.objects.filter(
                                    project_uuid=vm["project-id"],
                                    slice_uuid=vm["slice-id"],
                                    vm_uuid=vm['uuid'],
                                    user_name=user_name,
                                    ssh_key=user_key):
                                key_entry = VirtualMachineKeys(
                                    project_uuid=vm["project-id"],
                                    slice_uuid=vm["slice-id"],
                                    vm_uuid=vm['uuid'],
                                    user_name=user_name,
                                    ssh_key=user_key)
                                key_entry.save()
                except Exception as e:
                    logging.error(
                        "create_sliver > Could not store user SSH key. Details: %s"
                        % str(e))
                #ip = self.shell.get_ip_from_vm(vm_name=vm['name'],slice_name=vm['slice-name'],project=authority)
                #created_vms.append({'vm-name':vm['name'],'vm-ip':ip,'vm-state':'ongoing','slice-name':slice_leaf,'node-name':node.name})

    #add ssh keys to ldap of ssh gateway
        if len(nodes):
            logging.info("create_slice > Connecting to LDAP")
            session = ldapManager()
            con = session.bind()
            logging.info("create_slice > Connected to LDAP. Connection: %s" %
                         str(con))
            if con:
                logging.info("LDAP: trying to create the following users: %s" %
                             str(users))
                for user in users:
                    logging.info("Sending users to LDAP")
                    #logging.error("project: "+str(projectName)+" slicename"+str(sliceName))
                    ldapprj = "%s.%s" % (projectName, sliceName)
                    ldapprj = ldapprj.replace("\\", "")
                    logging.info("LDAP project: " + ldapprj)
                    session.add_modify_project_users(con, user["urn"], ldapprj,
                                                     user["keys"])
                    logging.info("User added to project: " + str(ldapprj) +
                                 ", SSH key: " + str(user['keys']))
                #status=session.add_project(con,projectName+"."+sliceName)
                #if status != 0:
                #logging.error("users"+users)
                #	for index,user in enumerate(users):
                #logging.error(user)
                #		status=session.add_user(con,"user"+str(index),user['urn'],projectName+"."+sliceName,user['keys'])
                #		if status ==0:
                #			logging.error("can't add user to ldap of ssh gateway:"+ str(user))
                #else:
                #	logging.error("can't add project to ldap of ssh gateway: "+projectName+"."+sliceName)
            else:
                logging.error("Cannot contact LDAP of the SSH gateway")
        return self.aggregate.get_rspec(slice_leaf=slice_leaf,
                                        projectName=projectName,
                                        version=rspec.version,
                                        created_vms=created_vms,
                                        new_nodes=nodes)

    def sliver_status(self, slice_leaf, authority, options):
        slice = self.shell.GetSlice(slice_leaf, authority)
        result = dict()
        List = list()
        for vm in slice['vms']:
            List.append({
                'vm-name': vm['vm-name'],
                'vm-ip': vm['vm-ip'],
                'vm-state': vm['vm-state'],
                'node-name': vm['node-name']
            })
        return List

    def get_expiration(self, creds, slice_hrn):
        for cred in creds:
            credential = Credential(string=cred)
            if credential.get_gid_caller().get_hrn() == slice_hrn:
                return credential.get_expiration()
        return None
Beispiel #2
0
class VMAggregate:
    def __init__(self):
        self.shell = VTShell()

    @staticmethod
    def FilterList(myfilter, mylist):
        result = []
        result.extend(mylist)
        for item in mylist:
            for key in myfilter.keys():
                if myfilter[key] != item[key]:
                    result.remove(item)
                    break
        return result

    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()

    def server_iface_to_vm_iface(self, server_iface):
        # XXX Original code in Expedient shows hardcoded VM-to-server interfaces
        # (note: server [eth2, eth3] connected to VM [eth1, eth2] respectively)
        # Since this data is not persisted anywhere, let us use a similar approach
        try:
            if "." not in server_iface:
                server_iface = server_iface.replace(
                    server_iface[-1], str(int(server_iface[-1]) - 1))
        except Exception as e:
            print "sfa.drivers.VMAggregate > could not show adapter server's interface for user: %s" % str(
                e)
        return server_iface

    def get_nodes(self,
                  options={},
                  slice_leaf=None,
                  projectName=None,
                  created_vms=[],
                  new_nodes=[]):
        if 'slice' in options.keys():
            nodes = self.shell.GetNodes(options['slice'], projectName)
            if not nodes:
                nodes = new_nodes
        else:
            nodes = self.shell.GetNodes()

        rspec_nodes = []
        for node in nodes:

            rspec_node = Node()
            site = self.get_testbed_info()
            rspec_node['component_id'] = hrn_to_urn(
                config.HRN + '.' + str(node.name), 'node')
            rspec_node['component_name'] = node.name
            rspec_node[
                'component_manager_id'] = "urn:publicid:IDN+" + config.OCF_ISLAND_AUTHORITY + ":" + config.OCF_AM_TYPE + "+cm"
            rspec_node['hostname'] = str(node.name).lower() + '.ctx.i2cat.net'
            rspec_node['exclusive'] = 'false'
            rspec_node['hardware_types'] = [
                OcfVtServer({
                    'name':
                    str(node.name),
                    'operating_system_type':
                    str(node.operatingSystemType),
                    'operating_system_distribution':
                    str(node.operatingSystemDistribution),
                    'operating_system_version':
                    str(node.operatingSystemVersion),
                    'virtualization_technology':
                    node.virtTech,
                    'cpus_number':
                    str(node.numberOfCPUs),
                    'cpu_frequency':
                    str(node.CPUFrequency),
                    'memory':
                    str(node.memory),
                    'hdd_space_GB':
                    str(node.discSpaceGB),
                    'agent_url':
                    str(node.agentURL),
                })
            ]
            if not slice_leaf:
                ip_ranges = node.subscribedIp4Ranges.all()
                mac_ranges = node.subscribedMacRanges.all()
                network_ifaces = node.networkInterfaces.all()
                rspec_node['services'] = list()
                if ip_ranges:
                    for ip_range in ip_ranges:
                        rspec_node['services'].append(
                            Range({
                                'type': 'IP_Range',
                                'name': ip_range.name,
                                'start_value': ip_range.startIp,
                                'end_value': ip_range.endIp
                            }))
                if mac_ranges:
                    for mac_range in mac_ranges:
                        rspec_node['services'].append(
                            Range({
                                'type': 'MAC_Range',
                                'name': mac_range.name,
                                'start_value': mac_range.startMac,
                                'end_value': mac_range.endMac
                            }))
                if network_ifaces:
                    for network_iface in network_ifaces:
                        #rspec_node['services'].append(NetworkInterface({'from_server_interface_name':network_iface.name,
                        network_iface_name = self.server_iface_to_vm_iface(
                            network_iface.name)
                        rspec_node['services'].append(
                            NetworkInterface({
                                'from_server_interface_name':
                                network_iface_name,
                                'to_network_interface_id':
                                network_iface.switchID,
                                'to_network_interface_port':
                                str(network_iface.port)
                            }))
                if site['longitude'] and site['latitude']:
                    location = Location({
                        'longitude': site['longitude'],
                        'latitude': site['latitude'],
                        'country': 'Spain'
                    })
                    rspec_node['location'] = location

            slices = list()
            cVMs = dict()
            if slice_leaf:
                slices = (self.shell.GetSlice(slice_leaf, projectName))
                slices['vms'].extend(
                    VMAggregate.FilterList(
                        {
                            'slice-name': slice_leaf,
                            'node-name': node.name
                        }, created_vms))
                #cVMs['vms'] = createdVMs
            slivers = list()
            if slices:
                services = list()
                for vm in slices['vms']:
                    if vm['node-name'] == node.name:
                        slivers.append(
                            VM({
                                'name': vm['vm-name'],
                                'state': vm['vm-state'],
                                'ip': vm['vm-ip'],
                            }))
                        services.append(
                            Login({
                                'authentication': "ssh-keys",
                                'hostname': vm['vm-ip'],
                                'port': "22",
                                'username': '******',
                            }))
                rspec_node['services'] = services
                rspec_node['slivers'] = slivers

            rspec_nodes.append(rspec_node)
        return rspec_nodes

    def get_testbed_info(self):
        #TODO: get True Testbed Info from the AM
        return {'longitude': config.LONGITUDE, 'latitude': config.LATITUDE}

    def get_nodes_in_geni(self,
                          options={},
                          slice_leaf=None,
                          projectName=None,
                          created_vms=[],
                          new_nodes=[]):
        if 'slice' in options.keys():
            nodes = self.shell.GetNodes(options['slice'], projectName)
            if not nodes:
                nodes = new_nodes
        else:
            nodes = self.shell.GetNodes()

        rspec_nodes = []
        for node in nodes:
            rspec_node = Node()
            site = self.get_testbed_info()
            rspec_node['component_id'] = hrn_to_urn(
                config.HRN + '.' + str(node.name), 'node')
            rspec_node['component_name'] = node.name
            rspec_node[
                'component_manager_id'] = "urn:publicid:IDN+" + config.OCF_ISLAND_AUTHORITY + ":" + config.OCF_AM_TYPE + "+cm"
            rspec_node['hostname'] = str(node.name).lower() + '.ctx.i2cat.net'
            rspec_node['exclusive'] = 'false'
            rspec_node['hardware_types'] = [
                OcfVtServer({
                    'name':
                    str(node.name),
                    'operating_system_type':
                    str(node.operatingSystemType),
                    'operating_system_distribution':
                    str(node.operatingSystemDistribution),
                    'operating_system_version':
                    str(node.operatingSystemVersion),
                    'virtualization_technology':
                    node.virtTech,
                    'cpus_number':
                    str(node.numberOfCPUs),
                    'cpu_frequency':
                    str(node.CPUFrequency),
                    'memory':
                    str(node.memory),
                    'hdd_space_GB':
                    str(node.discSpaceGB),
                    'agent_url':
                    str(node.agentURL),
                })
            ]
            if not slice_leaf:
                ip_ranges = node.subscribedIp4Ranges.all()
                mac_ranges = node.subscribedMacRanges.all()
                network_ifaces = node.networkInterfaces.all()
                rspec_node['services'] = list()
                if ip_ranges:
                    for ip_range in ip_ranges:
                        rspec_node['services'].append(
                            Range({
                                'type': 'IP_Range',
                                'name': ip_range.name,
                                'start_value': ip_range.startIp,
                                'end_value': ip_range.endIp
                            }))
                if mac_ranges:
                    for mac_range in mac_ranges:
                        rspec_node['services'].append(
                            Range({
                                'type': 'MAC_Range',
                                'name': mac_range.name,
                                'start_value': mac_range.startMac,
                                'end_value': mac_range.endMac
                            }))
                if network_ifaces:
                    for network_iface in network_ifaces:
                        #rspec_node['services'].append(NetworkInterface({'from_server_interface_name':network_iface.name,
                        network_iface_name = self.server_iface_to_vm_iface(
                            network_iface.name)
                        rspec_node['services'].append(
                            NetworkInterface({
                                'from_server_interface_name':
                                network_iface_name,
                                'to_network_interface_id':
                                network_iface.switchID,
                                'to_network_interface_port':
                                str(network_iface.port)
                            }))
                if site['longitude'] and site['latitude']:
                    location = Location({
                        'longitude': site['longitude'],
                        'latitude': site['latitude'],
                        'country': 'Spain'
                    })
                    rspec_node['location'] = location

            slices = list()
            cVMs = dict()
            if slice_leaf:
                slices = (self.shell.GetSlice(slice_leaf, projectName))
                slices['vms'].extend(
                    VMAggregate.FilterList(
                        {
                            'slice-name': slice_leaf,
                            'node-name': node.name
                        }, created_vms))
                #cVMs['vms'] = createdVMs
            if slices:
                for vm in slices['vms']:
                    single_node = rspec_node.copy()
                    if vm['node-name'] == node.name:
                        single_node['slivers'] = [(VM({
                            'name': vm['vm-name'],
                            'state': vm['vm-state'],
                            'ip': vm['vm-ip'],
                        }))]
                        single_node['services'] = [
                            Login({
                                'authentication': "ssh-keys",
                                'hostname': vm['vm-ip'],
                                'port': "22",
                                'username': '******',
                            })
                        ]

                        created_node = single_node.copy()
                        rspec_nodes.append(created_node)
        return rspec_nodes
Beispiel #3
0
class VTSfaDriver:

	def __init__ (self, config):
		self.aggregate = VMAggregate()
		self.shell = VTShell()


	def list_resources (self,options):

		version_manager = VersionManager()
        	rspec_version = 'OcfVt'#version_manager.get_version(options.get('geni_rspec_version'))
        	version_string = "rspec_%s" % (rspec_version)
	        rspec =  self.aggregate.get_rspec(version=rspec_version,options=options)
       		return rspec

	def crud_slice(self,slice_leaf,authority, action=None):

		slicename = slice_leaf 
                try:
                        slice = self.shell.GetSlice(slicename,authority)
                except Exception as e:
                        raise RecordNotFound(slice_leaf)

		for vm in slice['vms']:
			if action == 'start_slice':
				self.shell.StartSlice(vm['node-id'],vm['vm-id'])
			elif action == 'stop_slice':
				self.shell.StopSlice(vm['node-id'],vm['vm-id'])
			elif action == 'delete_slice':
				self.shell.DeleteSlice(vm['node-id'],vm['vm-id'])
			elif action == 'reset_slice':
				self.shell.RebootSlice(vm['node-id'],vm['vm-id'])
		return 1

        def create_sliver (self,slice_leaf,authority,rspec_string, users, options, expiration_date):
		
                rspec = RSpec(rspec_string,'OcfVt')
                requested_attributes = rspec.version.get_slice_attributes()
                requested_attributes = self.shell.convert_to_uuid(requested_attributes) #Converts component_id URNs to UUIDs
		projectName = authority#users[0]['slice_record']['authority']
		sliceName = slice_leaf
		self.shell.CreateSliver(requested_attributes,projectName,sliceName,expiration_date)
		created_vms = list()
		nodes = list()
		for slivers in requested_attributes:
			node = self.shell.GetNodes(uuid=slivers['component_id'])
			for vm in slivers['slivers']:
				#node = self.shell.GetNodes(uuid=vm['server-id'])
				if not node in nodes:
					nodes.append(node)
                                #ip = self.shell.get_ip_from_vm(vm_name=vm['name'],slice_name=vm['slice-name'],project=authority)
				#created_vms.append({'vm-name':vm['name'],'vm-ip':ip,'vm-state':'ongoing','slice-name':slice_leaf,'node-name':node.name})
		
		return self.aggregate.get_rspec(slice_leaf=slice_leaf,projectName=projectName,version=rspec.version,created_vms=created_vms,new_nodes=nodes)
	
	def sliver_status(self,slice_leaf,authority,options):

		slice = self.shell.GetSlice(slice_leaf,authority)
		result = dict()
		List = list()
		for vm in slice['vms']:
    			List.append({'vm-name':vm['vm-name'],'vm-ip':vm['vm-ip'],'vm-state': vm['vm-state'], 'node-name': vm['node-name']})
		return List
			
        def get_expiration(self,creds,slice_hrn):
                for cred in creds:
                	credential = Credential(string=cred)
                    	if credential.get_gid_caller().get_hrn() == slice_hrn:
                        	return credential.get_expiration()
                return None