in get_node_key(), dont forget to call get_key()
[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     registry.get_key()
55                 
56 def get_credential(options):
57     if options.verbose:
58         print "Getting the component's credential"
59     pass
60
61 def get_trusted_certs(options):
62     if options.verbose:
63         print "Getting the component's trusted certs"
64     pass
65
66 def get_gids(options):
67     if options.verbose:
68         print "Geting the component's GIDs"
69     
70     pass
71
72 def main():
73     args = sys.argv
74     prog_name = args[0]
75     parser = OptionParser(usage="%(prog_name)s [options]" % locals())
76     parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
77                       default=False, help="Be verbose") 
78     parser.add_option("-r", "--registry", dest="registry", default=None,
79                       help="Url of registry to contact")  
80     parser.add_option("-k", "--key", dest="key", action="store_true", 
81                      default=False,  
82                      help="Get the node's pkey from the registry")
83     parser.add_option("-c", "--certs", dest="certs", action="store_true",
84                       default=False,
85                       help="Get the trusted certs from the registry")
86     parser.add_option("-g", "--gids", dest="gids", action="store_true",       
87                       default=False,
88                       help="Get gids for all the slices on the component")
89
90     (options, args) = parser.parse_args()
91
92     create_default_dirs()
93     if options.key:
94         get_node_key(options)
95     if options.certs:
96         get_certs(options)
97     if options.gids:
98         get_gids(options)
99
100 if __name__ == '__main__':
101     main()