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