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