use self.get_auth_info
[sfa.git] / sfa / server / sfa-server.py
1 #!/usr/bin/python
2 #
3 ### $Id$
4 ### $URL$
5 #
6 # GENI PLC Wrapper
7 #
8 # This wrapper implements the Geni Registry and Slice Interfaces on PLC.
9 # Depending on command line options, it starts some combination of a
10 # Registry, an Aggregate Manager, and a Slice Manager.
11 #
12 # There are several items that need to be done before starting the wrapper
13 # server.
14 #
15 # NOTE:  Many configuration settings, including the PLC maintenance account
16 # credentials, URI of the PLCAPI, and PLC DB URI and admin credentials are initialized
17 # from your MyPLC configuration (/etc/planetlab/plc_config*).  Please make sure this information
18 # is up to date and accurate.
19 #
20 # 1) Import the existing planetlab database, creating the
21 #    appropriate geni records. This is done by running the "sfa-import-plc.py" tool.
22 #
23 # 2) Create a "trusted_roots" directory and place the certificate of the root
24 #    authority in that directory. Given the defaults in sfa-import-plc.py, this
25 #    certificate would be named "planetlab.gid". For example,
26 #
27 #    mkdir trusted_roots; cp authorities/planetlab.gid trusted_roots/
28 #
29 # TODO: Can all three servers use the same "registry" certificate?
30 ##
31
32 # TCP ports for the three servers
33 registry_port=12345
34 aggregate_port=12346
35 slicemgr_port=12347
36
37 import os, os.path
38 from optparse import OptionParser
39
40 from sfa.trust.trustedroot import TrustedRootList
41 from sfa.trust.certificate import Keypair, Certificate
42
43 from sfa.server.registry import Registry
44 from sfa.server.aggregate import Aggregate
45 from sfa.server.slicemgr import SliceMgr
46
47 from sfa.util.config import Config
48 from sfa.util.hierarchy import Hierarchy
49
50 # after http://www.erlenstar.demon.co.uk/unix/faq_2.html
51 def daemon():
52     """Daemonize the current process."""
53     if os.fork() != 0: os._exit(0)
54     os.setsid()
55     if os.fork() != 0: os._exit(0)
56     os.umask(0)
57     devnull = os.open(os.devnull, os.O_RDWR)
58     os.dup2(devnull, 0)
59     # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
60     crashlog = os.open('/var/log/sfa.daemon', os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
61     os.dup2(crashlog, 1)
62     os.dup2(crashlog, 2)
63
64 def main():
65     # xxx get rid of globals - name consistently CamelCase or under_score
66     global AuthHierarchy
67     global TrustedRoots
68     global registry_port
69     global aggregate_port
70     global slicemgr_port
71
72     # Generate command line parser
73     parser = OptionParser(usage="plc [options]")
74     parser.add_option("-r", "--registry", dest="registry", action="store_true",
75          help="run registry server", default=False)
76     parser.add_option("-s", "--slicemgr", dest="sm", action="store_true",
77          help="run slice manager", default=False)
78     parser.add_option("-a", "--aggregate", dest="am", action="store_true",
79          help="run aggregate manager", default=False)
80     parser.add_option("-v", "--verbose", dest="verbose", action="store_true", 
81          help="verbose mode", default=False)
82     parser.add_option("-d", "--daemon", dest="daemon", action="store_true",
83          help="Run as daemon.", default=False)
84     (options, args) = parser.parse_args()
85
86     config = Config()
87     path = config.basepath 
88     key_file = path + os.sep + "server.key"
89     cert_file = path + os.sep + "server.cert"
90     
91     if (options.daemon):  daemon()
92
93     if (os.path.exists(key_file)) and (not os.path.exists(cert_file)):
94         # If private key exists and cert doesnt, recreate cert
95         key = Keypair(filename=key_file)
96         cert = Certificate(subject="registry")
97         cert.set_issuer(key=key, subject="registry")
98         cert.set_pubkey(key)
99         cert.sign()
100         cert.save_to_file(cert_file)
101
102     elif (not os.path.exists(key_file)) or (not os.path.exists(cert_file)):
103         # if no key is specified, then make one up
104         key = Keypair(create=True)
105         key.save_to_file(key_file)
106         cert = Certificate(subject="registry")
107         cert.set_issuer(key=key, subject="registry")
108         cert.set_pubkey(key)
109         cert.sign()
110         cert.save_to_file(cert_file)
111
112     AuthHierarchy = Hierarchy()
113
114     TrustedRoots = TrustedRootList()
115
116     # start registry server
117     if (options.registry):
118         r = Registry("", registry_port, key_file, cert_file)
119         #r.trusted_cert_list = TrustedRoots.get_list()
120         #r.hierarchy = AuthHierarchy
121         r.start()
122
123     # start aggregate manager
124     if (options.am):
125         a = Aggregate("", aggregate_port, key_file, cert_file)
126         #a.trusted_cert_list = TrustedRoots.get_list()
127         a.start()
128
129     # start slice manager
130     if (options.sm):
131         s = SliceMgr("", slicemgr_port, key_file, cert_file)
132         #s.trusted_cert_list = TrustedRoots.get_list()
133         s.start()
134
135 if __name__ == "__main__":
136     main()