make executable
[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 def main():
45     global AuthHierarchy
46     global TrustedRoots
47     global registry_port
48     global aggregate_port
49     global slicemgr_port
50
51     # Generate command line parser
52     parser = OptionParser(usage="plc [options]")
53     parser.add_option("-r", "--registry", dest="registry", action="store_true",
54          help="run registry server", default=False)
55     parser.add_option("-s", "--slicemgr", dest="sm", action="store_true",
56          help="run slice manager", default=False)
57     parser.add_option("-a", "--aggregate", dest="am", action="store_true",
58          help="run aggregate manager", default=False)
59     parser.add_option("-v", "--verbose", dest="verbose", action="store_true", 
60          help="verbose mode", default=False)
61     (options, args) = parser.parse_args()
62  
63     key_file = "server.key"
64     cert_file = "server.cert"
65
66     if (os.path.exists(key_file)) and (not os.path.exists(cert_file)):
67         # If private key exists and cert doesnt, recreate cert
68         key = Keypair(filename=key_file)
69         cert = Certificate(subject="registry")
70         cert.set_issuer(key=key, subject="registry")
71         cert.set_pubkey(key)
72         cert.sign()
73         cert.save_to_file(cert_file)
74
75     elif (not os.path.exists(key_file)) or (not os.path.exists(cert_file)):
76         # if no key is specified, then make one up
77         key = Keypair(create=True)
78         key.save_to_file(key_file)
79         cert = Certificate(subject="registry")
80         cert.set_issuer(key=key, subject="registry")
81         cert.set_pubkey(key)
82         cert.sign()
83         cert.save_to_file(cert_file)
84
85     AuthHierarchy = Hierarchy()
86
87     TrustedRoots = TrustedRootList()
88
89     # start registry server
90     if (options.registry):
91         r = Registry("", registry_port, key_file, cert_file)
92         r.trusted_cert_list = TrustedRoots.get_list()
93         r.hierarchy = AuthHierarchy
94         r.start()
95
96     # start aggregate manager
97     if (options.am):
98         a = Aggregate("", aggregate_port, key_file, cert_file)
99         a.trusted_cert_list = TrustedRoots.get_list()
100         a.start()
101
102     # start slice manager
103     if (options.sm):
104         s = SliceMgr("", slicemgr_port, key_file, cert_file)
105         s.trusted_cert_list = TrustedRoots.get_list()
106         s.start()
107
108 if __name__ == "__main__":
109     main()