convenience script for coupling with the OMF Experiment Controller
[myplc.git] / user-scripts / slice_ssh_keys.py
1 # This utility is designed for use with an OMF Experiment Controller
2
3 import sys
4 import os.path
5 from xmlrpclib import ServerProxy
6 from optparse import OptionParser
7
8 default_host="www.planet-lab.eu"
9 def main ():
10   usage="""Usage: 
11   %prog [--plc plc_hostname] slicename dirname
12 Purpose: 
13   issues a GetSliceSshKeys to the MyPLC instance at <hostname>, and
14   store the keys related to slice <slicename> in <dirname> 
15   in a format suitable for use with the OMF Experiment Controller"""
16
17   parser=OptionParser(usage=usage)
18   parser.add_option ("-p","--plc",action='store',dest='myplc_host',
19                      default=default_host, 
20                      help="the hostname where your myplc is running")
21   (options,args) = parser.parse_args()
22   if len(args) != 2:
23     parser.print_help()
24     sys.exit(1)
25   (slicename, dirname) = args
26   plc_hostname=options.myplc_host
27   plc_url="https://%s/PLCAPI/"%plc_hostname
28   ple=ServerProxy(plc_url,allow_none=True)
29   auth={'AuthMethod':'anonymous'}
30   public_keys=ple.GetSliceSshKeys(auth,slicename)
31   if not public_keys:
32     print ("Cannot find any key for slice %s, check slicename ? "%slicename)
33     return 1
34   for (hostname, pubkey) in public_keys.items(): 
35     filename = os.path.join (dirname, hostname)
36     file = open(filename, "w")
37     filename.write(pubkey)
38     filename.close()
39
40 if __name__ == '__main__':
41   sys.exit(main())
42
43