added --extractgids, --dumpparents
[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.util.config import Config
41 from geni.registry import Registry
42 from geni.aggregate import Aggregate
43 from geni.slicemgr import SliceMgr
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     config = Config()
81     path = config.basepath 
82     key_file = path + os.sep + "server.key"
83     cert_file = path + os.sep + "server.cert"
84     
85     if (options.daemon):  daemon()
86
87     if (os.path.exists(key_file)) and (not os.path.exists(cert_file)):
88         # If private key exists and cert doesnt, recreate cert
89         key = Keypair(filename=key_file)
90         cert = Certificate(subject="registry")
91         cert.set_issuer(key=key, subject="registry")
92         cert.set_pubkey(key)
93         cert.sign()
94         cert.save_to_file(cert_file)
95
96     elif (not os.path.exists(key_file)) or (not os.path.exists(cert_file)):
97         # if no key is specified, then make one up
98         key = Keypair(create=True)
99         key.save_to_file(key_file)
100         cert = Certificate(subject="registry")
101         cert.set_issuer(key=key, subject="registry")
102         cert.set_pubkey(key)
103         cert.sign()
104         cert.save_to_file(cert_file)
105
106     AuthHierarchy = Hierarchy()
107
108     TrustedRoots = TrustedRootList()
109
110     # start registry server
111     if (options.registry):
112         r = Registry("", registry_port, key_file, cert_file)
113         #r.trusted_cert_list = TrustedRoots.get_list()
114         #r.hierarchy = AuthHierarchy
115         r.start()
116
117     # start aggregate manager
118     if (options.am):
119         a = Aggregate("", aggregate_port, key_file, cert_file)
120         #a.trusted_cert_list = TrustedRoots.get_list()
121         a.start()
122
123     # start slice manager
124     if (options.sm):
125         s = SliceMgr("", slicemgr_port, key_file, cert_file)
126         #s.trusted_cert_list = TrustedRoots.get_list()
127         s.start()
128
129 if __name__ == "__main__":
130     main()