8829321164a8bee98cd0febb0d134f8824aa3961
[sfa.git] / geni / sfa-plc.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 geni.util.hierarchy import Hierarchy
41 from geni.util.trustedroot import TrustedRootList
42 from geni.util.certificate import Keypair, Certificate
43 from geni.util.config import Config
44
45 from geni.registry import Registry
46 from geni.aggregate import Aggregate
47 from geni.slicemgr import SliceMgr
48
49 # after http://www.erlenstar.demon.co.uk/unix/faq_2.html
50 def daemon():
51     """Daemonize the current process."""
52     if os.fork() != 0: os._exit(0)
53     os.setsid()
54     if os.fork() != 0: os._exit(0)
55     os.umask(0)
56     devnull = os.open(os.devnull, os.O_RDWR)
57     os.dup2(devnull, 0)
58     # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
59     crashlog = os.open('/var/log/geni.daemon', os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
60     os.dup2(crashlog, 1)
61     os.dup2(crashlog, 2)
62
63 def main():
64     global AuthHierarchy
65     global TrustedRoots
66     global registry_port
67     global aggregate_port
68     global slicemgr_port
69
70     # Generate command line parser
71     parser = OptionParser(usage="plc [options]")
72     parser.add_option("-r", "--registry", dest="registry", action="store_true",
73          help="run registry server", default=False)
74     parser.add_option("-s", "--slicemgr", dest="sm", action="store_true",
75          help="run slice manager", default=False)
76     parser.add_option("-a", "--aggregate", dest="am", action="store_true",
77          help="run aggregate manager", default=False)
78     parser.add_option("-v", "--verbose", dest="verbose", action="store_true", 
79          help="verbose mode", default=False)
80     parser.add_option("-d", "--daemon", dest="daemon", action="store_true",
81          help="Run as daemon.", default=False)
82     (options, args) = parser.parse_args()
83
84     config = Config()
85     path = config.basepath 
86     key_file = path + os.sep + "server.key"
87     cert_file = path + os.sep + "server.cert"
88     
89     if (options.daemon):  daemon()
90
91     if (os.path.exists(key_file)) and (not os.path.exists(cert_file)):
92         # If private key exists and cert doesnt, recreate cert
93         key = Keypair(filename=key_file)
94         cert = Certificate(subject="registry")
95         cert.set_issuer(key=key, subject="registry")
96         cert.set_pubkey(key)
97         cert.sign()
98         cert.save_to_file(cert_file)
99
100     elif (not os.path.exists(key_file)) or (not os.path.exists(cert_file)):
101         # if no key is specified, then make one up
102         key = Keypair(create=True)
103         key.save_to_file(key_file)
104         cert = Certificate(subject="registry")
105         cert.set_issuer(key=key, subject="registry")
106         cert.set_pubkey(key)
107         cert.sign()
108         cert.save_to_file(cert_file)
109
110     AuthHierarchy = Hierarchy()
111
112     TrustedRoots = TrustedRootList()
113
114     # start registry server
115     if (options.registry):
116         r = Registry("", registry_port, key_file, cert_file)
117         #r.trusted_cert_list = TrustedRoots.get_list()
118         #r.hierarchy = AuthHierarchy
119         r.start()
120
121     # start aggregate manager
122     if (options.am):
123         a = Aggregate("", aggregate_port, key_file, cert_file)
124         #a.trusted_cert_list = TrustedRoots.get_list()
125         a.start()
126
127     # start slice manager
128     if (options.sm):
129         s = SliceMgr("", slicemgr_port, key_file, cert_file)
130         #s.trusted_cert_list = TrustedRoots.get_list()
131         s.start()
132
133 if __name__ == "__main__":
134     main()