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