fix xmlrpcprotocol import
[sfa.git] / sfa / server / sfa_component_setup.py
1 #!/usr/bin/python
2 import sys
3 import os
4 import tempfile
5 from optparse import OptionParser
6 from sfa.util.config import Config
7 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
8 from sfa.trust.certificate import Keypair, Certificate
9 from sfa.trust.credential import Credential
10
11 CONFIG_DIR='/etc/sfa'
12 TRUSTED_CERTS_DIR = '/etc/sfa/trusted_roots'
13 DATA_DIR = '/var/lib/sfa'
14
15 def create_default_dirs():
16     all_dirs = [CONFIG_DIR, TRUSTED_CERTS_DIR, DATA_DIR]
17     for dir in all_dirs:
18         if not os.path.exists(dir):
19             os.mkdir(dir)
20              
21 def get_node_key(options):
22     if options.verbose:
23         print "Getting the component's pkey"
24     # this call requires no authentication, 
25     # so we can generate a random keypair here
26     subject="component"
27     keyfile = tempfile.mktemp()
28     certfile = tempfile.mktemp()
29     key = Keypair(create=True)
30     key.save_to_file(keyfile)
31     cert = Certificate(subject=subject)
32     cert.set_issuer(key=key, subject=subject)
33     cert.set_pubkey(key)
34     cert.sign()
35     cert.save_to_file(certfile)
36     
37     # get the registry url
38     url = ""   
39     if options.registry:
40         url_parts = options.registry.split(":")
41         if len(url_parts) >1:
42             url = options.registry
43         else:
44             url = "http://%s:12346" % options.registry
45     else:
46         config = Config()
47         addr, port = config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT_
48         url = "http://%(addr)s:%(port)s" % locals()  
49         
50     if options.verbose:
51         print "Contacting registry at: %(url)s" % locals() 
52      
53     registry = xmlrpcprotocol.get_server(url, keyfile, certfile)
54                 
55 def get_credential(options):
56     if options.verbose:
57         print "Getting the component's credential"
58     pass
59
60 def get_trusted_certs(options):
61     if options.verbose:
62         print "Getting the component's trusted certs"
63     pass
64
65 def get_gids(options):
66     if options.verbose:
67         print "Geting the component's GIDs"
68     
69     pass
70
71 def main():
72     args = sys.argv
73     prog_name = args[0]
74     parser = OptionParser(usage="%(prog_name)s [options]" % locals())
75     parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
76                       default=False, help="Be verbose") 
77     parser.add_option("-r", "--registry", dest="registry", default=None,
78                       help="Url of registry to contact")  
79     parser.add_option("-k", "--key", dest="key", action="store_true", 
80                      default=False,  
81                      help="Get the node's pkey from the registry")
82     parser.add_option("-c", "--certs", dest="certs", action="store_true",
83                       default=False,
84                       help="Get the trusted certs from the registry")
85     parser.add_option("-g", "--gids", dest="gids", action="store_true",       
86                       default=False,
87                       help="Get gids for all the slices on the component")
88
89     (options, args) = parser.parse_args()
90
91     create_default_dirs()
92     if options.key:
93         get_node_key(options)
94     if options.certs:
95         get_certs(options)
96     if options.gids:
97         get_gids(options)
98
99 if __name__ == '__main__':
100     main()