fix show, list and remove. Add code to write records to disk, reorganize main to...
[sfa.git] / plc / 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 "import.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 import.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 util.hierarchy import Hierarchy
37 from util.trustedroot import TrustedRootList
38 from util.cert import Keypair, Certificate
39 from registry import Registry
40 #from aggregate import Aggregate
41 from 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 no key is specified, then make one up
66     if (not os.path.exists(key_file)) or (not os.path.exists(cert_file)):
67         key = Keypair(create=True)
68         key.save_to_file(key_file)
69
70         cert = Certificate(subject="registry")
71         cert.set_issuer(key=key, subject="registry")
72         cert.set_pubkey(key)
73         cert.sign()
74         cert.save_to_file(cert_file)
75
76     AuthHierarchy = Hierarchy()
77
78     TrustedRoots = TrustedRootList()
79
80     # start registry server
81     if (options.registry):
82         r = Registry("", registry_port, key_file, cert_file)
83         r.trusted_cert_list = TrustedRoots.get_list()
84         r.hierarchy = AuthHierarchy
85         r.start()
86
87     # start aggregate manager
88     if (options.am):
89         a = Aggregate("", aggregate_port, key_file, cert_file)
90         a.trusted_cert_list = TrustedRoots.get_list()
91         a.start()
92
93     # start slice manager
94     if (options.sm):
95         s = SliceMgr("", slicemgr_port, key_file, cert_file)
96         s.trusted_cert_list = TrustedRoots.get_list()
97         s.start()
98
99 if __name__ == "__main__":
100     main()