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 __store_user_keys(self, users, vms): stored_keys = False try: # Wrap users into list, if not already if not isinstance(users, list): users = [users] for vm in vms: params_list = self.__vm_to_ssh_keys_params_list(vm, users) # Create N entries, each for a specific key for params in params_list: # If SSH key is not yet stored if not VirtualMachineKeys.objects.filter(**params): key_entry = VirtualMachineKeys(**params) key_entry.save() stored_keys = True except Exception as e: logging.error("Could not store user SSH key. Details: %s" % str(e)) return stored_keys
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)