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