fix missing module imports
[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 # 1) Update util/config.py to match the parameters of your PLC installation.
12 #
13 # 2) Import the existing planetlab database, creating the
14 #    appropriate geni records. This is done by running the "import.py" tool.
15 #
16 # 3) Create a "trusted_roots" directory and place the certificate of the root
17 #    authority in that directory. Given the defaults in import.py, this
18 #    certificate would be named "planetlab.gid". For example,
19 #
20 #    mkdir trusted_roots; cp authorities/planetlab.gid trusted_roots/
21 #
22 # TODO: Can all three servers use the same "registry" certificate?
23 ##
24
25 # TCP ports for the three servers
26 registry_port=12345
27 aggregate_port=12346
28 slicemgr_port=12347
29
30 import os, os.path
31 from optparse import OptionParser
32
33 from util.hierarchy import Hierarchy
34 from util.trustedroot import TrustedRootList
35 from util.cert import Keypair, Certificate
36 from registry import Registry
37 #from aggregate import Aggregate
38 from slicemgr import SliceMgr
39
40 def main():
41     global AuthHierarchy
42     global TrustedRoots
43     global registry_port
44     global aggregate_port
45     global slicemgr_port
46
47     # Generate command line parser
48     parser = OptionParser(usage="plc [options]")
49     parser.add_option("-r", "--registry", dest="registry", action="store_true",
50          help="run registry server", default=False)
51     parser.add_option("-s", "--slicemgr", dest="sm", action="store_true",
52          help="run slice manager", default=False)
53     parser.add_option("-a", "--aggregate", dest="am", action="store_true",
54          help="run aggregate manager", default=False)
55     parser.add_option("-v", "--verbose", dest="verbose", action="store_true", 
56          help="verbose mode", default=False)
57     (options, args) = parser.parse_args()
58  
59     key_file = "server.key"
60     cert_file = "server.cert"
61
62     # if no key is specified, then make one up
63     if (not os.path.exists(key_file)) or (not os.path.exists(cert_file)):
64         key = Keypair(create=True)
65         key.save_to_file(key_file)
66
67         cert = Certificate(subject="registry")
68         cert.set_issuer(key=key, subject="registry")
69         cert.set_pubkey(key)
70         cert.sign()
71         cert.save_to_file(cert_file)
72
73     AuthHierarchy = Hierarchy()
74
75     TrustedRoots = TrustedRootList()
76
77     # start registry server
78     if (options.registry):
79         r = Registry("", registry_port, key_file, cert_file)
80         r.trusted_cert_list = TrustedRoots.get_list()
81         r.hierarchy = AuthHierarchy
82         r.start()
83
84     # start aggregate manager
85     if (options.am):
86         a = Aggregate("", aggregate_port, key_file, cert_file)
87         a.trusted_cert_list = TrustedRoots.get_list()
88         a.start()
89
90     # start slice manager
91     if (options.sm):
92         s = SliceMgr("", slicemgr_port, key_file, cert_file)
93         s.trusted_cert_list = TrustedRoots.get_list()
94         s.start()
95
96 if __name__ == "__main__":
97     main()