8 # This wrapper implements the SFA 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 SFA 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
40 from optparse import OptionParser
41 from sfa.trust.trustedroot import TrustedRootList
42 from sfa.trust.certificate import Keypair, Certificate
43 from sfa.trust.hierarchy import Hierarchy
44 from sfa.util.config import Config
45 from sfa.util.report import trace
47 # after http://www.erlenstar.demon.co.uk/unix/faq_2.html
49 """Daemonize the current process."""
50 if os.fork() != 0: os._exit(0)
52 if os.fork() != 0: os._exit(0)
54 devnull = os.open(os.devnull, os.O_RDWR)
56 # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
57 crashlog = os.open('/var/log/sfa.daemon', os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
61 def init_server_key(server_key_file, server_cert_file, config, hierarchy):
63 subject = config.SFA_INTERFACE_HRN
64 # check if the server's private key exists. If it doesnt,
65 # get the right one from the authorities directory. If it cant be
66 # found in the authorities directory, generate a random one
67 if not os.path.exists(server_key_file):
68 hrn = config.SFA_INTERFACE_HRN.lower()
69 hrn_parts = hrn.split(".")
71 pkey_filename = hrn+".pkey"
73 # sub authority's have "." in their hrn. This must
74 # be converted to os.path separator
75 if len(hrn_parts) > 0:
76 rel_key_path = hrn.replace(".", os.sep)
77 pkey_filename= hrn_parts[-1]+".pkey"
79 key_file = os.sep.join([hierarchy.basedir, rel_key_path, pkey_filename])
80 if not os.path.exists(key_file):
81 # if it doesnt exist then this is probably a fresh interface
82 # with no records. Generate a random keypair for now
83 trace("server's public key not found in %s" % key_file)
84 trace("generating a random server key pair")
85 key = Keypair(create=True)
86 key.save_to_file(server_key_file)
87 cert = Certificate(subject=subject)
88 cert.set_issuer(key=key, subject=subject)
91 cert.save_to_file(server_cert_file, save_parents=True)
94 # the pkey was found in the authorites directory. lets
95 # copy it to where the server key should be and generate
97 key = Keypair(filename=key_file)
98 key.save_to_file(server_key_file)
99 cert = Certificate(subject=subject)
100 cert.set_issuer(key=key, subject=subject)
103 cert.save_to_file(server_cert_file, save_parents=True)
106 # If private key exists and cert doesnt, recreate cert
107 if (os.path.exists(server_key_file)) and (not os.path.exists(server_cert_file)):
108 key = Keypair(filename=server_key_file)
109 cert = Certificate(subject=subject)
110 cert.set_issuer(key=key, subject=subject)
113 cert.save_to_file(server_cert_file)
115 def init_server(options, config):
117 Execute the init method defined in the manager file
119 manager_base = 'sfa.managers'
121 mgr_type = config.SFA_REGISTRY_TYPE
122 manager_module = manager_base + ".registry_manager_%s" % mgr_type
123 try: manager = __import__(manager_module, fromlist=[manager_base])
124 except: manager = None
125 if manager and hasattr(manager, 'init_server'):
126 manager.init_server()
128 mgr_type = config.SFA_AGGREGATE_TYPE
129 manager_module = manager_base + ".aggregate_manager_%s" % mgr_type
130 try: manager = __import__(manager_module, fromlist=[manager_base])
131 except: manager = None
132 if manager and hasattr(manager, 'init_server'):
133 manager.init_server()
135 mgr_type = config.SFA_SM_TYPE
136 manager_module = manager_base + ".slice_manager_%s" % mgr_type
137 try: manager = __import__(manager_module, fromlist=[manager_base])
138 except: manager = None
139 if manager and hasattr(manager, 'init_server'):
140 manager.init_server()
142 mgr_type = config.SFA_CM_TYPE
143 manager_module = manager_base + ".component_manager_%s" % mgr_type
144 try: manager = __import__(manager_module, fromlist=[manager_base])
145 except: manager = None
146 if manager and hasattr(manager, 'init_server'):
147 manager.init_server()
149 mgr_type = config.SFA_GENI_AGGREGATE_TYPE
150 manager_module = manager_base + ".geni_am_%s" % mgr_type
151 try: manager = __import__(manager_module, fromlist=[manager_base])
152 except: manager = None
153 if manager and hasattr(manager, 'init_server'):
154 manager.init_server()
158 # xxx get rid of globals - name consistently CamelCase or under_score
162 global aggregate_port
165 # Generate command line parser
166 parser = OptionParser(usage="sfa-server [options]")
167 parser.add_option("-r", "--registry", dest="registry", action="store_true",
168 help="run registry server", default=False)
169 parser.add_option("-s", "--slicemgr", dest="sm", action="store_true",
170 help="run slice manager", default=False)
171 parser.add_option("-a", "--aggregate", dest="am", action="store_true",
172 help="run aggregate manager", default=False)
173 parser.add_option("-c", "--component", dest="cm", action="store_true",
174 help="run component server", default=False)
175 parser.add_option("-g", "--geniam", dest="gam", action="store_true",
176 help="run GENI aggregate manager", default=False)
177 parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
178 help="verbose mode", default=False)
179 parser.add_option("-d", "--daemon", dest="daemon", action="store_true",
180 help="Run as daemon.", default=False)
181 (options, args) = parser.parse_args()
183 if (options.daemon): daemon()
186 hierarchy = Hierarchy()
187 trusted_roots = TrustedRootList(config.get_trustedroots_dir())
188 server_key_file = os.path.join(hierarchy.basedir, "server.key")
189 server_cert_file = os.path.join(hierarchy.basedir, "server.cert")
191 init_server_key(server_key_file, server_cert_file, config, hierarchy)
192 init_server(options, config)
194 # start registry server
195 if (options.registry):
196 from sfa.server.registry import Registry
197 r = Registry("", registry_port, server_key_file, server_cert_file)
200 # start aggregate manager
202 from sfa.server.aggregate import Aggregate
203 a = Aggregate("", aggregate_port, server_key_file, server_cert_file)
206 # start slice manager
208 from sfa.server.slicemgr import SliceMgr
209 s = SliceMgr("", slicemgr_port, server_key_file, server_cert_file)
213 from sfa.server.component import Component
214 c = Component("", component_port, server_key_file, server_cert_file)
217 # start GENI aggregate manager
219 from sfa.server.geni_aggregate import GENIAggregate
220 g = GENIAggregate("", geni_am_port, server_key_file, server_cert_file)
223 if __name__ == "__main__":