import sys, os import Config import xmlrpclib class ConfigSlice: def __init__(self,slice_spec): self.slice_spec = slice_spec #Connecting self.url="https://%s:443/PLCAPI/"%Config.host self.server=xmlrpclib.Server(self.url,allow_none=True) self.header=Config.header def pi_auth(self): return Config.pi_auth def get_nodes(self): filter={'boot_state':'boot','peer_id':None} return self.server.GetNodes(self.pi_auth(),filter,['hostname','nodenetwork_ids']) #return a file with all the hostnames where we run the slice slice def get_hostnames(self,slice_spec): try: slice_name=slice_spec['slice_fields']['name'] node_ids=self.server.GetSlices(self.pi_auth(),slice_name, ['node_ids']) filtre={'node_id': node_ids[0]['node_ids']} hosts_list = self.server.GetNodes(self.pi_auth(),filtre,['hostname']) file=open("Hostnames_list-%s.txt"%slice_name,"w") for host in hosts_list: print str(host['hostname']) file.write(str(host['hostname'])+"\n") file.close if( not os.path.isfile("Hostnames_list-%s.txt"%slice_name)): print "FAILED to Get slice's hosts list " return False return True except Exception, e: print str(e) ##scan all remote hosts for getting their public keys and the update his local ##know_hosts file to avoid the key popup sigh!! def scan_ips(self, slice_name): try: self.header("Scanning Nodes public Keys") file="Hostnames_list-%s.txt"%slice_name status=os.system("set -x;ssh-keyscan -t rsa -f %s >> ~/.ssh/known_hosts "%file) if (status):#Bon ici il faut un test plus fin car ca c'est rustique print "Failed to get the hostnames public keys" return False return True except Exception, e: print str(e) exit(1) #return all the ips of nodes where the slice is running def get_ip_node(self,all_nodes, slice_name): try: list_ids=[] ips_file = open("IP_list-%s.txt"%slice_name, "w") for ids in all_nodes: for id in ids['nodenetwork_ids']: list_ids.append(id) nodes_ips=self.server.GetNodeNetworks(self.pi_auth(),list_ids,['ip']) for ip in nodes_ips: tt=str(ip['ip'])+"\n" ips_file.write(tt) ips_file.close() if (not ips_file): print "Failed to get IPS @s for slice's hostnames machines" return False return True except Exception, e: print str(e) sys.exit(1) ##add intiscript to a slice def add_intiscriptto_slice(self,slice_spec): self.header("Adding initscript to the slice") print "not already implemented" return True ##add slice to all nodes and return their hostnames def config_slice(self,slice_spec): try: list_hosts = [] print slice_spec hostnames_list = open("Hostnames_list-%s.txt"%slice_spec['slice_fields']['name'], "w") slice_id=self.server.GetSlices(self.pi_auth(),slice_spec['slice_fields']['name'], ['slice_id']) print slice_id all_nodes=self.get_nodes() for node in all_nodes: list_hosts.append(node['hostname']) hostname=str(node['hostname'])+"\n" self.header("Adding slice %s to the node %s"%(slice_spec['slice_fields']['name'], node['hostname'])) hostnames_list.write(hostname) self.server.AddSliceToNodes(self.pi_auth(), slice_id[0]['slice_id'], list_hosts) ips_file=self.get_ip_node(all_nodes,slice_spec['slice_fields']['name']) hostnames_list.close() scan_status=self.scan_ips(slice_spec['slice_fields']['name']) if (not hostnames_list or not ips_file or not scan_status): return False return True except Exception, e: print str(e) sys.exit(1)