8 # This wrapper implements the Geni Registry and Slice Interfaces on PLC.
9 # Depending on command line options, it starts some combination of a
10 # Registry, an Aggregate Manager, and a Slice Manager.
12 # There are several items that need to be done before starting the wrapper
15 # NOTE: Many configuration settings, including the PLC maintenance account
16 # credentials, URI of the PLCAPI, and PLC DB URI and admin credentials are initialized
17 # from your MyPLC configuration (/etc/planetlab/plc_config*). Please make sure this information
18 # is up to date and accurate.
20 # 1) Import the existing planetlab database, creating the
21 # appropriate geni records. This is done by running the "sfa-import-plc.py" tool.
23 # 2) Create a "trusted_roots" directory and place the certificate of the root
24 # authority in that directory. Given the defaults in sfa-import-plc.py, this
25 # certificate would be named "planetlab.gid". For example,
27 # mkdir trusted_roots; cp authorities/planetlab.gid trusted_roots/
29 # TODO: Can all three servers use the same "registry" certificate?
32 # TCP ports for the three servers
39 from optparse import OptionParser
41 from sfa.trust.trustedroot import TrustedRootList
42 from sfa.trust.certificate import Keypair, Certificate
44 from sfa.server.registry import Registry
45 from sfa.server.aggregate import Aggregate
46 from sfa.server.slicemgr import SliceMgr
47 from sfa.trust.hierarchy import Hierarchy
48 from sfa.util.config import Config
49 from sfa.util.report import trace
51 # after http://www.erlenstar.demon.co.uk/unix/faq_2.html
53 """Daemonize the current process."""
54 if os.fork() != 0: os._exit(0)
56 if os.fork() != 0: os._exit(0)
58 devnull = os.open(os.devnull, os.O_RDWR)
60 # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
61 crashlog = os.open('/var/log/sfa.daemon', os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
65 def init_server_key(server_key_file, server_cert_file, config, hierarchy):
67 subject = config.SFA_INTERFACE_HRN
68 # check if the server's private key exists. If it doesnt,
69 # get the right one from the authorities directory. If it cant be
70 # found in the authorities directory, generate a random one
71 if not os.path.exists(server_key_file):
72 hrn = config.SFA_INTERFACE_HRN.lower()
73 hrn_parts = hrn.split(".")
75 pkey_filename = hrn+".pkey"
77 # sub authority's have "." in their hrn. This must
78 # be converted to os.path separator
79 if len(hrn_parts) > 0:
80 rel_key_path = hrn.replace(".", os.sep)
81 pkey_filename= hrn_parts[-1]+".pkey"
83 key_file = os.sep.join([hierarchy.basedir, rel_key_path, pkey_filename])
84 if not os.path.exists(key_file):
85 # if it doesnt exist then this is probably a fresh interface
86 # with no records. Generate a random keypair for now
87 trace("server's public key not found in %s" % key_file)
88 trace("generating a random server key pair")
89 key = Keypair(create=True)
90 key.save_to_file(server_key_file)
91 cert = Certificate(subject=subject)
92 cert.set_issuer(key=key, subject=subject)
95 cert.save_to_file(server_cert_file)
98 # the pkey was found in the authorites directory. lets
99 # copy it to where the server key should be and generate
101 key = Keypair(filename=key_file)
102 key.save_to_file(server_key_file)
103 cert = Certificate(subject=subject)
104 cert.set_issuer(key=key, subject=subject)
107 cert.save_to_file(server_cert_file)
110 # If private key exists and cert doesnt, recreate cert
111 if (os.path.exists(server_key_file)) and (not os.path.exists(server_cert_file)):
112 key = Keypair(filename=server_key_file)
113 cert = Certificate(subject=subject)
114 cert.set_issuer(key=key, subject=subject)
117 cert.save_to_file(server_cert_file)
120 # xxx get rid of globals - name consistently CamelCase or under_score
124 global aggregate_port
127 # Generate command line parser
128 parser = OptionParser(usage="sfa-server [options]")
129 parser.add_option("-r", "--registry", dest="registry", action="store_true",
130 help="run registry server", default=False)
131 parser.add_option("-s", "--slicemgr", dest="sm", action="store_true",
132 help="run slice manager", default=False)
133 parser.add_option("-a", "--aggregate", dest="am", action="store_true",
134 help="run aggregate manager", default=False)
135 parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
136 help="verbose mode", default=False)
137 parser.add_option("-d", "--daemon", dest="daemon", action="store_true",
138 help="Run as daemon.", default=False)
139 (options, args) = parser.parse_args()
141 if (options.daemon): daemon()
144 hierarchy = Hierarchy()
145 trusted_roots = TrustedRootList(config.get_trustedroots_dir())
146 server_key_file = os.path.join(hierarchy.basedir, "server.key")
147 server_cert_file = os.path.join(hierarchy.basedir, "server.cert")
149 init_server_key(server_key_file, server_cert_file, config, hierarchy)
151 # start registry server
152 if (options.registry):
153 r = Registry("", registry_port, server_key_file, server_cert_file)
156 # start aggregate manager
158 a = Aggregate("", aggregate_port, server_key_file, server_cert_file)
161 # start slice manager
163 s = SliceMgr("", slicemgr_port, server_key_file, server_cert_file)
166 if __name__ == "__main__":