[6679c122] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import os,sys |
---|
| 4 | |
---|
| 5 | import re |
---|
| 6 | import random |
---|
| 7 | import string |
---|
| 8 | import subprocess |
---|
| 9 | import tempfile |
---|
| 10 | import copy |
---|
[eee2b2e] | 11 | import pickle |
---|
[c971895] | 12 | import logging |
---|
[79b6596] | 13 | import signal |
---|
| 14 | import time |
---|
[6679c122] | 15 | |
---|
[3df9b33] | 16 | import os.path |
---|
| 17 | |
---|
[3441fe3] | 18 | import traceback |
---|
[c971895] | 19 | # For parsing visualization output and splitter output |
---|
| 20 | import xml.parsers.expat |
---|
[3441fe3] | 21 | |
---|
[6c57fe9] | 22 | from threading import Lock, Thread, Condition |
---|
| 23 | from subprocess import call, Popen, PIPE |
---|
[c573278] | 24 | from string import join |
---|
[6679c122] | 25 | |
---|
[db6b092] | 26 | from urlparse import urlparse |
---|
| 27 | from urllib2 import urlopen |
---|
| 28 | |
---|
[ec4fb42] | 29 | from util import * |
---|
[6bedbdba] | 30 | from deter import fedid, generate_fedid |
---|
[9460b1e] | 31 | from remote_service import xmlrpc_handler, soap_handler, service_caller |
---|
[c971895] | 32 | from service_error import service_error |
---|
[2761484] | 33 | from synch_store import synch_store |
---|
[73e7f5c] | 34 | from experiment_partition import experiment_partition |
---|
[1d73342] | 35 | from experiment_control_legacy import experiment_control_legacy |
---|
[7206e5a] | 36 | from authorizer import abac_authorizer |
---|
[faea607] | 37 | from thread_pool import thread_pool, pooled_thread |
---|
[ab3d6c5] | 38 | from experiment_info import experiment_info, allocation_info, federated_service |
---|
[22a1a77] | 39 | from operation_status import operation_status |
---|
[6679c122] | 40 | |
---|
[6bedbdba] | 41 | from deter import topdl |
---|
[044dd20] | 42 | from deter import ip_allocator |
---|
| 43 | from deter import ip_addr |
---|
[f07fa49] | 44 | import list_log |
---|
[db6b092] | 45 | |
---|
[11a08b0] | 46 | |
---|
| 47 | class nullHandler(logging.Handler): |
---|
| 48 | def emit(self, record): pass |
---|
| 49 | |
---|
| 50 | fl = logging.getLogger("fedd.experiment_control") |
---|
| 51 | fl.addHandler(nullHandler()) |
---|
| 52 | |
---|
[1d73342] | 53 | class experiment_control_local(experiment_control_legacy): |
---|
[0ea11af] | 54 | """ |
---|
| 55 | Control of experiments that this system can directly access. |
---|
| 56 | |
---|
| 57 | Includes experiment creation, termination and information dissemination. |
---|
| 58 | Thred safe. |
---|
| 59 | """ |
---|
[79b6596] | 60 | |
---|
| 61 | class ssh_cmd_timeout(RuntimeError): pass |
---|
[6679c122] | 62 | |
---|
[f069052] | 63 | call_RequestAccess = service_caller('RequestAccess') |
---|
| 64 | call_ReleaseAccess = service_caller('ReleaseAccess') |
---|
[cc8d8e9] | 65 | call_StartSegment = service_caller('StartSegment') |
---|
[db974ed] | 66 | call_TerminateSegment = service_caller('TerminateSegment') |
---|
[6e33086] | 67 | call_InfoSegment = service_caller('InfoSegment') |
---|
[22a1a77] | 68 | call_OperationSegment = service_caller('OperationSegment') |
---|
[5f6929a] | 69 | call_Ns2Topdl = service_caller('Ns2Topdl') |
---|
[058f58e] | 70 | |
---|
[3f6bc5f] | 71 | def __init__(self, config=None, auth=None): |
---|
[866c983] | 72 | """ |
---|
| 73 | Intialize the various attributes, most from the config object |
---|
| 74 | """ |
---|
| 75 | |
---|
| 76 | def parse_tarfile_list(tf): |
---|
| 77 | """ |
---|
| 78 | Parse a tarfile list from the configuration. This is a set of |
---|
| 79 | paths and tarfiles separated by spaces. |
---|
| 80 | """ |
---|
| 81 | rv = [ ] |
---|
| 82 | if tf is not None: |
---|
| 83 | tl = tf.split() |
---|
| 84 | while len(tl) > 1: |
---|
| 85 | p, t = tl[0:2] |
---|
| 86 | del tl[0:2] |
---|
| 87 | rv.append((p, t)) |
---|
| 88 | return rv |
---|
| 89 | |
---|
[f07fa49] | 90 | self.list_log = list_log.list_log |
---|
[866c983] | 91 | |
---|
| 92 | self.cert_file = config.get("experiment_control", "cert_file") |
---|
| 93 | if self.cert_file: |
---|
| 94 | self.cert_pwd = config.get("experiment_control", "cert_pwd") |
---|
| 95 | else: |
---|
| 96 | self.cert_file = config.get("globals", "cert_file") |
---|
| 97 | self.cert_pwd = config.get("globals", "cert_pwd") |
---|
| 98 | |
---|
| 99 | self.trusted_certs = config.get("experiment_control", "trusted_certs") \ |
---|
| 100 | or config.get("globals", "trusted_certs") |
---|
| 101 | |
---|
[6c57fe9] | 102 | self.repodir = config.get("experiment_control", "repodir") |
---|
[7183b48] | 103 | self.repo_url = config.get("experiment_control", "repo_url", |
---|
| 104 | "https://users.isi.deterlab.net:23235"); |
---|
[cc8d8e9] | 105 | |
---|
[866c983] | 106 | self.exp_stem = "fed-stem" |
---|
| 107 | self.log = logging.getLogger("fedd.experiment_control") |
---|
| 108 | set_log_level(config, "experiment_control", self.log) |
---|
| 109 | self.muxmax = 2 |
---|
[35a4c01] | 110 | self.nthreads = 10 |
---|
[866c983] | 111 | self.randomize_experiments = False |
---|
| 112 | |
---|
| 113 | self.splitter = None |
---|
| 114 | self.ssh_keygen = "/usr/bin/ssh-keygen" |
---|
| 115 | self.ssh_identity_file = None |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | self.debug = config.getboolean("experiment_control", "create_debug") |
---|
[69692a9] | 119 | self.cleanup = not config.getboolean("experiment_control", |
---|
| 120 | "leave_tmpfiles") |
---|
[866c983] | 121 | self.state_filename = config.get("experiment_control", |
---|
| 122 | "experiment_state") |
---|
[2761484] | 123 | self.store_filename = config.get("experiment_control", |
---|
| 124 | "synch_store") |
---|
| 125 | self.store_url = config.get("experiment_control", "store_url") |
---|
[5f6929a] | 126 | self.splitter_url = config.get("experiment_control", "ns2topdl_uri") |
---|
[866c983] | 127 | self.fedkit = parse_tarfile_list(\ |
---|
| 128 | config.get("experiment_control", "fedkit")) |
---|
| 129 | self.gatewaykit = parse_tarfile_list(\ |
---|
| 130 | config.get("experiment_control", "gatewaykit")) |
---|
| 131 | accessdb_file = config.get("experiment_control", "accessdb") |
---|
| 132 | |
---|
[175b444] | 133 | dt = config.get("experiment_control", "direct_transit") |
---|
[7206e5a] | 134 | self.auth_type = config.get('experiment_control', 'auth_type') \ |
---|
| 135 | or 'legacy' |
---|
| 136 | self.auth_dir = config.get('experiment_control', 'auth_dir') |
---|
[6e33086] | 137 | # XXX: document this! |
---|
| 138 | self.info_cache_limit = \ |
---|
| 139 | config.getint('experiment_control', 'info_cache', 600) |
---|
[139e2e2] | 140 | if dt: self.direct_transit = [ tb.strip() for tb in dt.split(",")] |
---|
| 141 | else: self.direct_transit = [ ] |
---|
[866c983] | 142 | # NB for internal master/slave ops, not experiment setup |
---|
| 143 | self.ssh_type = config.get("experiment_control", "sshkeytype", "rsa") |
---|
[ca489e8] | 144 | |
---|
[db6b092] | 145 | self.overrides = set([]) |
---|
| 146 | ovr = config.get('experiment_control', 'overrides') |
---|
| 147 | if ovr: |
---|
| 148 | for o in ovr.split(","): |
---|
| 149 | o = o.strip() |
---|
| 150 | if o.startswith('fedid:'): o = o[len('fedid:'):] |
---|
| 151 | self.overrides.add(fedid(hexstr=o)) |
---|
[ca489e8] | 152 | |
---|
[866c983] | 153 | self.state = { } |
---|
| 154 | self.state_lock = Lock() |
---|
| 155 | self.tclsh = "/usr/local/bin/otclsh" |
---|
[5f6929a] | 156 | self.tcl_splitter = config.get("ns2topdl", "tcl_splitter") or \ |
---|
[866c983] | 157 | config.get("experiment_control", "tcl_splitter", |
---|
| 158 | "/usr/testbed/lib/ns2ir/parse.tcl") |
---|
| 159 | mapdb_file = config.get("experiment_control", "mapdb") |
---|
| 160 | self.trace_file = sys.stderr |
---|
| 161 | |
---|
| 162 | self.def_expstart = \ |
---|
| 163 | "sudo -H /bin/sh /usr/local/federation/bin/federate.sh >& " +\ |
---|
| 164 | "/tmp/federate"; |
---|
| 165 | self.def_mexpstart = "sudo -H /usr/local/federation/bin/make_hosts " +\ |
---|
| 166 | "FEDDIR/hosts"; |
---|
| 167 | self.def_gwstart = \ |
---|
| 168 | "sudo -H /usr/local/federation/bin/fed-tun.pl -f GWCONF>& " +\ |
---|
| 169 | "/tmp/bridge.log"; |
---|
| 170 | self.def_mgwstart = \ |
---|
| 171 | "sudo -H /usr/local/federation/bin/fed-tun.pl -f GWCONF >& " +\ |
---|
| 172 | "/tmp/bridge.log"; |
---|
| 173 | self.def_gwimage = "FBSD61-TUNNEL2"; |
---|
| 174 | self.def_gwtype = "pc"; |
---|
| 175 | self.local_access = { } |
---|
| 176 | |
---|
[7206e5a] | 177 | if self.auth_type == 'legacy': |
---|
| 178 | if auth: |
---|
| 179 | self.auth = auth |
---|
| 180 | else: |
---|
| 181 | self.log.error( "[access]: No authorizer initialized, " +\ |
---|
| 182 | "creating local one.") |
---|
| 183 | auth = authorizer() |
---|
[5ecb9a3] | 184 | self.get_access = self.legacy_get_access |
---|
[7206e5a] | 185 | elif self.auth_type == 'abac': |
---|
| 186 | self.auth = abac_authorizer(load=self.auth_dir) |
---|
| 187 | else: |
---|
| 188 | raise service_error(service_error.internal, |
---|
| 189 | "Unknown auth_type: %s" % self.auth_type) |
---|
[866c983] | 190 | |
---|
| 191 | if mapdb_file: |
---|
| 192 | self.read_mapdb(mapdb_file) |
---|
| 193 | else: |
---|
| 194 | self.log.warn("[experiment_control] No testbed map, using defaults") |
---|
| 195 | self.tbmap = { |
---|
| 196 | 'deter':'https://users.isi.deterlab.net:23235', |
---|
| 197 | 'emulab':'https://users.isi.deterlab.net:23236', |
---|
| 198 | 'ucb':'https://users.isi.deterlab.net:23237', |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | if accessdb_file: |
---|
| 202 | self.read_accessdb(accessdb_file) |
---|
| 203 | else: |
---|
| 204 | raise service_error(service_error.internal, |
---|
| 205 | "No accessdb specified in config") |
---|
| 206 | |
---|
| 207 | # Grab saved state. OK to do this w/o locking because it's read only |
---|
| 208 | # and only one thread should be in existence that can see self.state at |
---|
| 209 | # this point. |
---|
| 210 | if self.state_filename: |
---|
| 211 | self.read_state() |
---|
| 212 | |
---|
[2761484] | 213 | if self.store_filename: |
---|
| 214 | self.read_store() |
---|
| 215 | else: |
---|
| 216 | self.log.warning("No saved synch store") |
---|
| 217 | self.synch_store = synch_store |
---|
| 218 | |
---|
[866c983] | 219 | # Dispatch tables |
---|
| 220 | self.soap_services = {\ |
---|
[a3ad8bd] | 221 | 'New': soap_handler('New', self.new_experiment), |
---|
[e19b75c] | 222 | 'Create': soap_handler('Create', self.create_experiment), |
---|
[866c983] | 223 | 'Vtopo': soap_handler('Vtopo', self.get_vtopo), |
---|
| 224 | 'Vis': soap_handler('Vis', self.get_vis), |
---|
| 225 | 'Info': soap_handler('Info', self.get_info), |
---|
[65f3f29] | 226 | 'MultiInfo': soap_handler('MultiInfo', self.get_multi_info), |
---|
[22a1a77] | 227 | 'Operation': soap_handler('Operation', self.do_operation), |
---|
[866c983] | 228 | 'Terminate': soap_handler('Terminate', |
---|
[e19b75c] | 229 | self.terminate_experiment), |
---|
[2761484] | 230 | 'GetValue': soap_handler('GetValue', self.GetValue), |
---|
| 231 | 'SetValue': soap_handler('SetValue', self.SetValue), |
---|
[866c983] | 232 | } |
---|
| 233 | |
---|
| 234 | self.xmlrpc_services = {\ |
---|
[a3ad8bd] | 235 | 'New': xmlrpc_handler('New', self.new_experiment), |
---|
[e19b75c] | 236 | 'Create': xmlrpc_handler('Create', self.create_experiment), |
---|
[866c983] | 237 | 'Vtopo': xmlrpc_handler('Vtopo', self.get_vtopo), |
---|
| 238 | 'Vis': xmlrpc_handler('Vis', self.get_vis), |
---|
| 239 | 'Info': xmlrpc_handler('Info', self.get_info), |
---|
[65f3f29] | 240 | 'MultiInfo': xmlrpc_handler('MultiInfo', self.get_multi_info), |
---|
[866c983] | 241 | 'Terminate': xmlrpc_handler('Terminate', |
---|
[e19b75c] | 242 | self.terminate_experiment), |
---|
[22a1a77] | 243 | 'Operation': xmlrpc_handler('Operation', self.do_operation), |
---|
[2761484] | 244 | 'GetValue': xmlrpc_handler('GetValue', self.GetValue), |
---|
| 245 | 'SetValue': xmlrpc_handler('SetValue', self.SetValue), |
---|
[866c983] | 246 | } |
---|
[19cc408] | 247 | |
---|
[a97394b] | 248 | # Call while holding self.state_lock |
---|
[eee2b2e] | 249 | def write_state(self): |
---|
[866c983] | 250 | """ |
---|
| 251 | Write a new copy of experiment state after copying the existing state |
---|
| 252 | to a backup. |
---|
| 253 | |
---|
| 254 | State format is a simple pickling of the state dictionary. |
---|
| 255 | """ |
---|
| 256 | if os.access(self.state_filename, os.W_OK): |
---|
[40dd8c1] | 257 | copy_file(self.state_filename, \ |
---|
| 258 | "%s.bak" % self.state_filename) |
---|
[866c983] | 259 | try: |
---|
| 260 | f = open(self.state_filename, 'w') |
---|
| 261 | pickle.dump(self.state, f) |
---|
[d3c8759] | 262 | except EnvironmentError, e: |
---|
[866c983] | 263 | self.log.error("Can't write file %s: %s" % \ |
---|
| 264 | (self.state_filename, e)) |
---|
| 265 | except pickle.PicklingError, e: |
---|
| 266 | self.log.error("Pickling problem: %s" % e) |
---|
| 267 | except TypeError, e: |
---|
| 268 | self.log.error("Pickling problem (TypeError): %s" % e) |
---|
[eee2b2e] | 269 | |
---|
[2761484] | 270 | @staticmethod |
---|
[29d5f7c] | 271 | def get_alloc_ids(exp): |
---|
[2761484] | 272 | """ |
---|
[29d5f7c] | 273 | Used by read_store and read state. This used to be worse. |
---|
[2761484] | 274 | """ |
---|
| 275 | |
---|
[29d5f7c] | 276 | return [ a.allocID for a in exp.get_all_allocations() ] |
---|
| 277 | |
---|
[2761484] | 278 | |
---|
[a97394b] | 279 | # Call while holding self.state_lock |
---|
[eee2b2e] | 280 | def read_state(self): |
---|
[866c983] | 281 | """ |
---|
| 282 | Read a new copy of experiment state. Old state is overwritten. |
---|
| 283 | |
---|
| 284 | State format is a simple pickling of the state dictionary. |
---|
| 285 | """ |
---|
[cc8d8e9] | 286 | |
---|
[866c983] | 287 | try: |
---|
| 288 | f = open(self.state_filename, "r") |
---|
| 289 | self.state = pickle.load(f) |
---|
| 290 | self.log.debug("[read_state]: Read state from %s" % \ |
---|
| 291 | self.state_filename) |
---|
[d3c8759] | 292 | except EnvironmentError, e: |
---|
[866c983] | 293 | self.log.warning("[read_state]: No saved state: Can't open %s: %s"\ |
---|
| 294 | % (self.state_filename, e)) |
---|
| 295 | except pickle.UnpicklingError, e: |
---|
| 296 | self.log.warning(("[read_state]: No saved state: " + \ |
---|
| 297 | "Unpickling failed: %s") % e) |
---|
| 298 | |
---|
[cc8d8e9] | 299 | for s in self.state.values(): |
---|
[866c983] | 300 | try: |
---|
[cc8d8e9] | 301 | |
---|
[29d5f7c] | 302 | eid = s.fedid |
---|
[cc8d8e9] | 303 | if eid : |
---|
[7206e5a] | 304 | if self.auth_type == 'legacy': |
---|
| 305 | # XXX: legacy |
---|
| 306 | # Give the owner rights to the experiment |
---|
[29d5f7c] | 307 | #self.auth.set_attribute(s['owner'], eid) |
---|
[7206e5a] | 308 | # And holders of the eid as well |
---|
| 309 | self.auth.set_attribute(eid, eid) |
---|
| 310 | # allow overrides to control experiments as well |
---|
| 311 | for o in self.overrides: |
---|
| 312 | self.auth.set_attribute(o, eid) |
---|
| 313 | # Set permissions to allow reading of the software |
---|
| 314 | # repo, if any, as well. |
---|
| 315 | for a in self.get_alloc_ids(s): |
---|
| 316 | self.auth.set_attribute(a, 'repo/%s' % eid) |
---|
[cc8d8e9] | 317 | else: |
---|
| 318 | raise KeyError("No experiment id") |
---|
[866c983] | 319 | except KeyError, e: |
---|
| 320 | self.log.warning("[read_state]: State ownership or identity " +\ |
---|
| 321 | "misformatted in %s: %s" % (self.state_filename, e)) |
---|
[4064742] | 322 | |
---|
| 323 | |
---|
| 324 | def read_accessdb(self, accessdb_file): |
---|
[866c983] | 325 | """ |
---|
| 326 | Read the mapping from fedids that can create experiments to their name |
---|
| 327 | in the 3-level access namespace. All will be asserted from this |
---|
| 328 | testbed and can include the local username and porject that will be |
---|
| 329 | asserted on their behalf by this fedd. Each fedid is also added to the |
---|
| 330 | authorization system with the "create" attribute. |
---|
| 331 | """ |
---|
| 332 | self.accessdb = {} |
---|
| 333 | # These are the regexps for parsing the db |
---|
| 334 | name_expr = "[" + string.ascii_letters + string.digits + "\.\-]+" |
---|
| 335 | project_line = re.compile("^\s*fedid:([" + string.hexdigits + "]+)"+ \ |
---|
| 336 | "\s*->\(\s*("+name_expr+")\s*,\s*("+name_expr+")\s*\)\s*$") |
---|
| 337 | user_line = re.compile("^\s*fedid:([" + string.hexdigits + "]+)"+ \ |
---|
| 338 | "\s*->\s*(" + name_expr + ")\s*$") |
---|
| 339 | lineno = 0 |
---|
| 340 | |
---|
| 341 | # Parse the mappings and store in self.authdb, a dict of |
---|
| 342 | # fedid -> (proj, user) |
---|
| 343 | try: |
---|
| 344 | f = open(accessdb_file, "r") |
---|
| 345 | for line in f: |
---|
| 346 | lineno += 1 |
---|
| 347 | line = line.strip() |
---|
| 348 | if len(line) == 0 or line.startswith('#'): |
---|
| 349 | continue |
---|
| 350 | m = project_line.match(line) |
---|
| 351 | if m: |
---|
| 352 | fid = fedid(hexstr=m.group(1)) |
---|
| 353 | project, user = m.group(2,3) |
---|
| 354 | if not self.accessdb.has_key(fid): |
---|
| 355 | self.accessdb[fid] = [] |
---|
| 356 | self.accessdb[fid].append((project, user)) |
---|
| 357 | continue |
---|
| 358 | |
---|
| 359 | m = user_line.match(line) |
---|
| 360 | if m: |
---|
| 361 | fid = fedid(hexstr=m.group(1)) |
---|
| 362 | project = None |
---|
| 363 | user = m.group(2) |
---|
| 364 | if not self.accessdb.has_key(fid): |
---|
| 365 | self.accessdb[fid] = [] |
---|
| 366 | self.accessdb[fid].append((project, user)) |
---|
| 367 | continue |
---|
| 368 | self.log.warn("[experiment_control] Error parsing access " +\ |
---|
| 369 | "db %s at line %d" % (accessdb_file, lineno)) |
---|
[d3c8759] | 370 | except EnvironmentError: |
---|
[866c983] | 371 | raise service_error(service_error.internal, |
---|
[05fceef] | 372 | ("Error opening/reading %s as experiment " +\ |
---|
| 373 | "control accessdb") % accessdb_file) |
---|
[866c983] | 374 | f.close() |
---|
| 375 | |
---|
| 376 | # Initialize the authorization attributes |
---|
[7206e5a] | 377 | # XXX: legacy |
---|
| 378 | if self.auth_type == 'legacy': |
---|
| 379 | for fid in self.accessdb.keys(): |
---|
| 380 | self.auth.set_attribute(fid, 'create') |
---|
| 381 | self.auth.set_attribute(fid, 'new') |
---|
[34bc05c] | 382 | |
---|
| 383 | def read_mapdb(self, file): |
---|
[866c983] | 384 | """ |
---|
| 385 | Read a simple colon separated list of mappings for the |
---|
| 386 | label-to-testbed-URL mappings. Clears or creates self.tbmap. |
---|
[a11eda5] | 387 | also adds testbeds to active if they include , active after |
---|
| 388 | their name. |
---|
[866c983] | 389 | """ |
---|
| 390 | |
---|
| 391 | self.tbmap = { } |
---|
[a11eda5] | 392 | self.tbactive = set() |
---|
[866c983] | 393 | lineno =0 |
---|
| 394 | try: |
---|
| 395 | f = open(file, "r") |
---|
| 396 | for line in f: |
---|
| 397 | lineno += 1 |
---|
| 398 | line = line.strip() |
---|
| 399 | if line.startswith('#') or len(line) == 0: |
---|
| 400 | continue |
---|
| 401 | try: |
---|
| 402 | label, url = line.split(':', 1) |
---|
[a11eda5] | 403 | if ',' in label: |
---|
| 404 | label, act = label.split(',', 1) |
---|
| 405 | active = (act.strip() == 'active') |
---|
| 406 | else: |
---|
| 407 | active = False |
---|
[866c983] | 408 | self.tbmap[label] = url |
---|
[a11eda5] | 409 | if active: self.tbactive.add(label) |
---|
[866c983] | 410 | except ValueError, e: |
---|
| 411 | self.log.warn("[read_mapdb] Ignored bad line (%d) in " +\ |
---|
| 412 | "map db: %s %s" % (lineno, line, e)) |
---|
[d3c8759] | 413 | except EnvironmentError, e: |
---|
[866c983] | 414 | self.log.warning("[read_mapdb]: No saved map database: Can't " +\ |
---|
| 415 | "open %s: %s" % (file, e)) |
---|
[1ec5d4a] | 416 | else: |
---|
| 417 | f.close() |
---|
[2761484] | 418 | |
---|
| 419 | def read_store(self): |
---|
| 420 | try: |
---|
| 421 | self.synch_store = synch_store() |
---|
| 422 | self.synch_store.load(self.store_filename) |
---|
| 423 | self.log.debug("[read_store]: Read store from %s" % \ |
---|
| 424 | self.store_filename) |
---|
[d3c8759] | 425 | except EnvironmentError, e: |
---|
[2761484] | 426 | self.log.warning("[read_store]: No saved store: Can't open %s: %s"\ |
---|
| 427 | % (self.state_filename, e)) |
---|
| 428 | self.synch_store = synch_store() |
---|
| 429 | |
---|
| 430 | # Set the initial permissions on data in the store. XXX: This ad hoc |
---|
| 431 | # authorization attribute initialization is getting out of hand. |
---|
[7206e5a] | 432 | # XXX: legacy |
---|
| 433 | if self.auth_type == 'legacy': |
---|
| 434 | for k in self.synch_store.all_keys(): |
---|
| 435 | try: |
---|
| 436 | if k.startswith('fedid:'): |
---|
| 437 | fid = fedid(hexstr=k[6:46]) |
---|
| 438 | if self.state.has_key(fid): |
---|
| 439 | for a in self.get_alloc_ids(self.state[fid]): |
---|
| 440 | self.auth.set_attribute(a, k) |
---|
| 441 | except ValueError, e: |
---|
| 442 | self.log.warn("Cannot deduce permissions for %s" % k) |
---|
[2761484] | 443 | |
---|
| 444 | |
---|
| 445 | def write_store(self): |
---|
| 446 | """ |
---|
| 447 | Write a new copy of synch_store after writing current state |
---|
| 448 | to a backup. We use the internal synch_store pickle method to avoid |
---|
| 449 | incinsistent data. |
---|
| 450 | |
---|
| 451 | State format is a simple pickling of the store. |
---|
| 452 | """ |
---|
| 453 | if os.access(self.store_filename, os.W_OK): |
---|
| 454 | copy_file(self.store_filename, \ |
---|
| 455 | "%s.bak" % self.store_filename) |
---|
| 456 | try: |
---|
| 457 | self.synch_store.save(self.store_filename) |
---|
[d3c8759] | 458 | except EnvironmentError, e: |
---|
[2761484] | 459 | self.log.error("Can't write file %s: %s" % \ |
---|
| 460 | (self.store_filename, e)) |
---|
| 461 | except TypeError, e: |
---|
| 462 | self.log.error("Pickling problem (TypeError): %s" % e) |
---|
| 463 | |
---|
[cf0ff4f] | 464 | |
---|
| 465 | def remove_dirs(self, dir): |
---|
| 466 | """ |
---|
| 467 | Remove the directory tree and all files rooted at dir. Log any errors, |
---|
| 468 | but continue. |
---|
| 469 | """ |
---|
| 470 | self.log.debug("[removedirs]: removing %s" % dir) |
---|
| 471 | try: |
---|
| 472 | for path, dirs, files in os.walk(dir, topdown=False): |
---|
| 473 | for f in files: |
---|
| 474 | os.remove(os.path.join(path, f)) |
---|
| 475 | for d in dirs: |
---|
| 476 | os.rmdir(os.path.join(path, d)) |
---|
| 477 | os.rmdir(dir) |
---|
| 478 | except EnvironmentError, e: |
---|
| 479 | self.log.error("Error deleting directory tree in %s" % e); |
---|
| 480 | |
---|
| 481 | @staticmethod |
---|
| 482 | def make_temp_certfile(expcert, tmpdir): |
---|
| 483 | """ |
---|
| 484 | make a protected copy of the access certificate so the experiment |
---|
| 485 | controller can act as the experiment principal. mkstemp is the most |
---|
| 486 | secure way to do that. The directory should be created by |
---|
| 487 | mkdtemp. Return the filename. |
---|
| 488 | """ |
---|
| 489 | if expcert and tmpdir: |
---|
| 490 | try: |
---|
| 491 | certf, certfn = tempfile.mkstemp(suffix=".pem", dir=tmpdir) |
---|
| 492 | f = os.fdopen(certf, 'w') |
---|
| 493 | print >> f, expcert |
---|
| 494 | f.close() |
---|
| 495 | except EnvironmentError, e: |
---|
| 496 | raise service_error(service_error.internal, |
---|
| 497 | "Cannot create temp cert file?") |
---|
| 498 | return certfn |
---|
| 499 | else: |
---|
| 500 | return None |
---|
| 501 | |
---|
[866c983] | 502 | |
---|
[6679c122] | 503 | def generate_ssh_keys(self, dest, type="rsa" ): |
---|
[866c983] | 504 | """ |
---|
| 505 | Generate a set of keys for the gateways to use to talk. |
---|
| 506 | |
---|
| 507 | Keys are of type type and are stored in the required dest file. |
---|
| 508 | """ |
---|
| 509 | valid_types = ("rsa", "dsa") |
---|
| 510 | t = type.lower(); |
---|
| 511 | if t not in valid_types: raise ValueError |
---|
| 512 | cmd = [self.ssh_keygen, '-t', t, '-N', '', '-f', dest] |
---|
| 513 | |
---|
| 514 | try: |
---|
| 515 | trace = open("/dev/null", "w") |
---|
[d3c8759] | 516 | except EnvironmentError: |
---|
[866c983] | 517 | raise service_error(service_error.internal, |
---|
| 518 | "Cannot open /dev/null??"); |
---|
| 519 | |
---|
| 520 | # May raise CalledProcessError |
---|
| 521 | self.log.debug("[generate_ssh_keys]: %s" % " ".join(cmd)) |
---|
[4ea1e22] | 522 | rv = call(cmd, stdout=trace, stderr=trace, close_fds=True) |
---|
[866c983] | 523 | if rv != 0: |
---|
| 524 | raise service_error(service_error.internal, |
---|
| 525 | "Cannot generate nonce ssh keys. %s return code %d" \ |
---|
| 526 | % (self.ssh_keygen, rv)) |
---|
[6679c122] | 527 | |
---|
[3df9b33] | 528 | def generate_seer_certs(self, destdir): |
---|
| 529 | ''' |
---|
| 530 | Create a SEER ca cert and a node cert in destdir/ca.pem and |
---|
| 531 | destdir/node.pem respectively. These will be distributed throughout |
---|
| 532 | the federated experiment. This routine reports errors via |
---|
| 533 | service_errors. |
---|
| 534 | ''' |
---|
| 535 | openssl = '/usr/bin/openssl' |
---|
| 536 | # All the filenames and parameters we need for openssl calls below |
---|
| 537 | ca_key =os.path.join(destdir, 'ca.key') |
---|
| 538 | ca_pem = os.path.join(destdir, 'ca.pem') |
---|
| 539 | node_key =os.path.join(destdir, 'node.key') |
---|
| 540 | node_pem = os.path.join(destdir, 'node.pem') |
---|
| 541 | node_req = os.path.join(destdir, 'node.req') |
---|
| 542 | node_signed = os.path.join(destdir, 'node.signed') |
---|
[9bde415] | 543 | days = '%s' % (365 * 10) |
---|
[95be336] | 544 | serial = '%s' % random.randint(0, 1<<16) |
---|
[3df9b33] | 545 | |
---|
| 546 | try: |
---|
| 547 | # Sequence of calls to create a CA key, create a ca cert, create a |
---|
| 548 | # node key, node signing request, and finally a signed node |
---|
| 549 | # certificate. |
---|
| 550 | sequence = ( |
---|
| 551 | (openssl, 'genrsa', '-out', ca_key, '1024'), |
---|
| 552 | (openssl, 'req', '-new', '-x509', '-key', ca_key, '-out', |
---|
| 553 | ca_pem, '-days', days, '-subj', |
---|
| 554 | '/C=US/ST=CA/O=DETER/OU=fedd/CN=CA' ), |
---|
| 555 | (openssl, 'genrsa', '-out', node_key, '1024'), |
---|
| 556 | (openssl, 'req', '-new', '-key', node_key, '-out', |
---|
| 557 | node_req, '-days', days, '-subj', |
---|
| 558 | '/C=US/ST=CA/O=DETER/OU=fedd/CN=node' ), |
---|
| 559 | (openssl, 'x509', '-CA', ca_pem, '-CAkey', ca_key, |
---|
[95be336] | 560 | '-set_serial', serial, '-req', '-in', node_req, |
---|
[3df9b33] | 561 | '-out', node_signed, '-days', days), |
---|
| 562 | ) |
---|
| 563 | # Do all that stuff; bail if there's an error, and push all the |
---|
| 564 | # output to dev/null. |
---|
| 565 | for cmd in sequence: |
---|
| 566 | trace = open("/dev/null", "w") |
---|
| 567 | rv = call(cmd, stdout=trace, stderr=trace, close_fds=True) |
---|
| 568 | if rv != 0: |
---|
| 569 | raise service_error(service_error.internal, |
---|
| 570 | "Cannot generate SEER certs. %s return code %d" \ |
---|
| 571 | % (' '.join(cmd), rv)) |
---|
| 572 | # Concatinate the node key and signed certificate into node.pem |
---|
| 573 | f = open(node_pem, 'w') |
---|
| 574 | for comp in (node_signed, node_key): |
---|
| 575 | g = open(comp, 'r') |
---|
[9bde415] | 576 | f.write(g.read()) |
---|
[3df9b33] | 577 | g.close() |
---|
| 578 | f.close() |
---|
| 579 | |
---|
| 580 | # Throw out intermediaries. |
---|
[95be336] | 581 | for fn in (ca_key, node_key, node_req, node_signed): |
---|
[3df9b33] | 582 | os.unlink(fn) |
---|
| 583 | |
---|
| 584 | except EnvironmentError, e: |
---|
| 585 | # Any difficulties with the file system wind up here |
---|
| 586 | raise service_error(service_error.internal, |
---|
| 587 | "File error on %s while creating SEER certs: %s" % \ |
---|
| 588 | (e.filename, e.strerror)) |
---|
| 589 | |
---|
| 590 | |
---|
| 591 | |
---|
[0d830de] | 592 | def gentopo(self, str): |
---|
[866c983] | 593 | """ |
---|
[1d73342] | 594 | Generate the topology data structure from the splitter's XML |
---|
[866c983] | 595 | representation of it. |
---|
| 596 | |
---|
| 597 | The topology XML looks like: |
---|
| 598 | <experiment> |
---|
| 599 | <nodes> |
---|
| 600 | <node><vname></vname><ips>ip1:ip2</ips></node> |
---|
| 601 | </nodes> |
---|
| 602 | <lans> |
---|
| 603 | <lan> |
---|
| 604 | <vname></vname><vnode></vnode><ip></ip> |
---|
| 605 | <bandwidth></bandwidth><member>node:port</member> |
---|
| 606 | </lan> |
---|
| 607 | </lans> |
---|
| 608 | """ |
---|
| 609 | class topo_parse: |
---|
| 610 | """ |
---|
| 611 | Parse the topology XML and create the dats structure. |
---|
| 612 | """ |
---|
| 613 | def __init__(self): |
---|
| 614 | # Typing of the subelements for data conversion |
---|
| 615 | self.str_subelements = ('vname', 'vnode', 'ips', 'ip', 'member') |
---|
| 616 | self.int_subelements = ( 'bandwidth',) |
---|
| 617 | self.float_subelements = ( 'delay',) |
---|
| 618 | # The final data structure |
---|
| 619 | self.nodes = [ ] |
---|
| 620 | self.lans = [ ] |
---|
| 621 | self.topo = { \ |
---|
| 622 | 'node': self.nodes,\ |
---|
| 623 | 'lan' : self.lans,\ |
---|
| 624 | } |
---|
| 625 | self.element = { } # Current element being created |
---|
| 626 | self.chars = "" # Last text seen |
---|
| 627 | |
---|
| 628 | def end_element(self, name): |
---|
| 629 | # After each sub element the contents is added to the current |
---|
| 630 | # element or to the appropriate list. |
---|
| 631 | if name == 'node': |
---|
| 632 | self.nodes.append(self.element) |
---|
| 633 | self.element = { } |
---|
| 634 | elif name == 'lan': |
---|
| 635 | self.lans.append(self.element) |
---|
| 636 | self.element = { } |
---|
| 637 | elif name in self.str_subelements: |
---|
| 638 | self.element[name] = self.chars |
---|
| 639 | self.chars = "" |
---|
| 640 | elif name in self.int_subelements: |
---|
| 641 | self.element[name] = int(self.chars) |
---|
| 642 | self.chars = "" |
---|
| 643 | elif name in self.float_subelements: |
---|
| 644 | self.element[name] = float(self.chars) |
---|
| 645 | self.chars = "" |
---|
| 646 | |
---|
| 647 | def found_chars(self, data): |
---|
| 648 | self.chars += data.rstrip() |
---|
| 649 | |
---|
| 650 | |
---|
| 651 | tp = topo_parse(); |
---|
| 652 | parser = xml.parsers.expat.ParserCreate() |
---|
| 653 | parser.EndElementHandler = tp.end_element |
---|
| 654 | parser.CharacterDataHandler = tp.found_chars |
---|
| 655 | |
---|
| 656 | parser.Parse(str) |
---|
| 657 | |
---|
| 658 | return tp.topo |
---|
| 659 | |
---|
[0d830de] | 660 | |
---|
| 661 | def genviz(self, topo): |
---|
[866c983] | 662 | """ |
---|
| 663 | Generate the visualization the virtual topology |
---|
| 664 | """ |
---|
| 665 | |
---|
| 666 | neato = "/usr/local/bin/neato" |
---|
| 667 | # These are used to parse neato output and to create the visualization |
---|
| 668 | # file. |
---|
[0ac1934] | 669 | vis_re = re.compile('^\s*"?([\w\-]+)"?\s+\[.*pos="([\d\.]+),([\d\.]+)"') |
---|
[866c983] | 670 | vis_fmt = "<node><name>%s</name><x>%s</x><y>%s</y><type>" + \ |
---|
| 671 | "%s</type></node>" |
---|
| 672 | |
---|
| 673 | try: |
---|
| 674 | # Node names |
---|
| 675 | nodes = [ n['vname'] for n in topo['node'] ] |
---|
| 676 | topo_lans = topo['lan'] |
---|
[cc8d8e9] | 677 | except KeyError, e: |
---|
| 678 | raise service_error(service_error.internal, "Bad topology: %s" %e) |
---|
[866c983] | 679 | |
---|
| 680 | lans = { } |
---|
| 681 | links = { } |
---|
| 682 | |
---|
| 683 | # Walk through the virtual topology, organizing the connections into |
---|
| 684 | # 2-node connections (links) and more-than-2-node connections (lans). |
---|
| 685 | # When a lan is created, it's added to the list of nodes (there's a |
---|
| 686 | # node in the visualization for the lan). |
---|
| 687 | for l in topo_lans: |
---|
| 688 | if links.has_key(l['vname']): |
---|
| 689 | if len(links[l['vname']]) < 2: |
---|
| 690 | links[l['vname']].append(l['vnode']) |
---|
| 691 | else: |
---|
| 692 | nodes.append(l['vname']) |
---|
| 693 | lans[l['vname']] = links[l['vname']] |
---|
| 694 | del links[l['vname']] |
---|
| 695 | lans[l['vname']].append(l['vnode']) |
---|
| 696 | elif lans.has_key(l['vname']): |
---|
| 697 | lans[l['vname']].append(l['vnode']) |
---|
| 698 | else: |
---|
| 699 | links[l['vname']] = [ l['vnode'] ] |
---|
| 700 | |
---|
| 701 | |
---|
| 702 | # Open up a temporary file for dot to turn into a visualization |
---|
| 703 | try: |
---|
| 704 | df, dotname = tempfile.mkstemp() |
---|
| 705 | dotfile = os.fdopen(df, 'w') |
---|
[d3c8759] | 706 | except EnvironmentError: |
---|
[866c983] | 707 | raise service_error(service_error.internal, |
---|
| 708 | "Failed to open file in genviz") |
---|
| 709 | |
---|
[db6b092] | 710 | try: |
---|
| 711 | dnull = open('/dev/null', 'w') |
---|
[d3c8759] | 712 | except EnvironmentError: |
---|
[db6b092] | 713 | service_error(service_error.internal, |
---|
[886307f] | 714 | "Failed to open /dev/null in genviz") |
---|
| 715 | |
---|
[866c983] | 716 | # Generate a dot/neato input file from the links, nodes and lans |
---|
| 717 | try: |
---|
| 718 | print >>dotfile, "graph G {" |
---|
| 719 | for n in nodes: |
---|
| 720 | print >>dotfile, '\t"%s"' % n |
---|
| 721 | for l in links.keys(): |
---|
| 722 | print >>dotfile, '\t"%s" -- "%s"' % tuple(links[l]) |
---|
| 723 | for l in lans.keys(): |
---|
| 724 | for n in lans[l]: |
---|
| 725 | print >>dotfile, '\t "%s" -- "%s"' % (n,l) |
---|
| 726 | print >>dotfile, "}" |
---|
| 727 | dotfile.close() |
---|
| 728 | except TypeError: |
---|
| 729 | raise service_error(service_error.internal, |
---|
| 730 | "Single endpoint link in vtopo") |
---|
[d3c8759] | 731 | except EnvironmentError: |
---|
[866c983] | 732 | raise service_error(service_error.internal, "Cannot write dot file") |
---|
| 733 | |
---|
| 734 | # Use dot to create a visualization |
---|
[5954004] | 735 | try: |
---|
| 736 | dot = Popen([neato, '-Gstart=rand', '-Gepsilon=0.005', |
---|
| 737 | '-Gmaxiter=2000', '-Gpack=true', dotname], stdout=PIPE, |
---|
| 738 | stderr=dnull, close_fds=True) |
---|
| 739 | except EnvironmentError: |
---|
| 740 | raise service_error(service_error.internal, |
---|
| 741 | "Cannot generate visualization: is graphviz available?") |
---|
[db6b092] | 742 | dnull.close() |
---|
[866c983] | 743 | |
---|
| 744 | # Translate dot to vis format |
---|
| 745 | vis_nodes = [ ] |
---|
| 746 | vis = { 'node': vis_nodes } |
---|
| 747 | for line in dot.stdout: |
---|
| 748 | m = vis_re.match(line) |
---|
| 749 | if m: |
---|
| 750 | vn = m.group(1) |
---|
| 751 | vis_node = {'name': vn, \ |
---|
| 752 | 'x': float(m.group(2)),\ |
---|
| 753 | 'y' : float(m.group(3)),\ |
---|
| 754 | } |
---|
| 755 | if vn in links.keys() or vn in lans.keys(): |
---|
| 756 | vis_node['type'] = 'lan' |
---|
| 757 | else: |
---|
| 758 | vis_node['type'] = 'node' |
---|
| 759 | vis_nodes.append(vis_node) |
---|
| 760 | rv = dot.wait() |
---|
| 761 | |
---|
| 762 | os.remove(dotname) |
---|
[1fed67b] | 763 | # XXX: graphviz seems to use low return codes for warnings, like |
---|
| 764 | # "couldn't find font" |
---|
| 765 | if rv < 2 : return vis |
---|
[866c983] | 766 | else: return None |
---|
[d0ae12d] | 767 | |
---|
[db6b092] | 768 | |
---|
[725c55d] | 769 | def release_access(self, tb, aid, tbmap=None, uri=None, cert_file=None, |
---|
| 770 | cert_pwd=None): |
---|
[e19b75c] | 771 | """ |
---|
| 772 | Release access to testbed through fedd |
---|
| 773 | """ |
---|
[db6b092] | 774 | |
---|
[fd07c48] | 775 | if not uri and tbmap: |
---|
| 776 | uri = tbmap.get(tb, None) |
---|
[e19b75c] | 777 | if not uri: |
---|
[69692a9] | 778 | raise service_error(service_error.server_config, |
---|
[e19b75c] | 779 | "Unknown testbed: %s" % tb) |
---|
[db6b092] | 780 | |
---|
[e19b75c] | 781 | if self.local_access.has_key(uri): |
---|
| 782 | resp = self.local_access[uri].ReleaseAccess(\ |
---|
[29d5f7c] | 783 | { 'ReleaseAccessRequestBody' : |
---|
| 784 | {'allocID': {'fedid': aid}},}, |
---|
[725c55d] | 785 | fedid(file=cert_file)) |
---|
[e19b75c] | 786 | resp = { 'ReleaseAccessResponseBody': resp } |
---|
| 787 | else: |
---|
[29d5f7c] | 788 | resp = self.call_ReleaseAccess(uri, {'allocID': {'fedid': aid} }, |
---|
[725c55d] | 789 | cert_file, cert_pwd, self.trusted_certs) |
---|
[db6b092] | 790 | |
---|
[e19b75c] | 791 | # better error coding |
---|
[db6b092] | 792 | |
---|
[5f6929a] | 793 | def remote_ns2topdl(self, uri, desc): |
---|
[db6b092] | 794 | |
---|
[e19b75c] | 795 | req = { |
---|
| 796 | 'description' : { 'ns2description': desc }, |
---|
[db6b092] | 797 | } |
---|
| 798 | |
---|
[5f6929a] | 799 | r = self.call_Ns2Topdl(uri, req, self.cert_file, self.cert_pwd, |
---|
[e19b75c] | 800 | self.trusted_certs) |
---|
| 801 | |
---|
[5f6929a] | 802 | if r.has_key('Ns2TopdlResponseBody'): |
---|
| 803 | r = r['Ns2TopdlResponseBody'] |
---|
[1dcaff4] | 804 | ed = r.get('experimentdescription', None) |
---|
| 805 | if ed.has_key('topdldescription'): |
---|
| 806 | return topdl.Topology(**ed['topdldescription']) |
---|
[e19b75c] | 807 | else: |
---|
| 808 | raise service_error(service_error.protocol, |
---|
| 809 | "Bad splitter response (no output)") |
---|
| 810 | else: |
---|
| 811 | raise service_error(service_error.protocol, "Bad splitter response") |
---|
[cc8d8e9] | 812 | |
---|
[e19b75c] | 813 | class start_segment: |
---|
[fd556d1] | 814 | def __init__(self, debug=False, log=None, testbed="", cert_file=None, |
---|
[f07fa49] | 815 | cert_pwd=None, trusted_certs=None, caller=None, |
---|
| 816 | log_collector=None): |
---|
[cc8d8e9] | 817 | self.log = log |
---|
| 818 | self.debug = debug |
---|
| 819 | self.cert_file = cert_file |
---|
| 820 | self.cert_pwd = cert_pwd |
---|
| 821 | self.trusted_certs = None |
---|
| 822 | self.caller = caller |
---|
[fd556d1] | 823 | self.testbed = testbed |
---|
[f07fa49] | 824 | self.log_collector = log_collector |
---|
[69692a9] | 825 | self.response = None |
---|
[b4b19c7] | 826 | self.node = { } |
---|
[9e5e251] | 827 | self.subs = { } |
---|
[e83f2f2] | 828 | self.proof = None |
---|
[b4b19c7] | 829 | |
---|
| 830 | def make_map(self, resp): |
---|
[29d5f7c] | 831 | if 'segmentdescription' not in resp or \ |
---|
| 832 | 'topdldescription' not in resp['segmentdescription']: |
---|
| 833 | self.log.warn('No topology returned from startsegment') |
---|
| 834 | return |
---|
| 835 | |
---|
| 836 | top = topdl.Topology( |
---|
| 837 | **resp['segmentdescription']['topdldescription']) |
---|
| 838 | |
---|
| 839 | for e in top.elements: |
---|
| 840 | if isinstance(e, topdl.Computer): |
---|
[1ae1aa2] | 841 | self.node[e.name] = e |
---|
[9e5e251] | 842 | for s in top.substrates: |
---|
| 843 | self.subs[s.name] = s |
---|
[cc8d8e9] | 844 | |
---|
[43197eb] | 845 | def __call__(self, uri, aid, topo, masters, attrs=None, connInfo=None): |
---|
[cc8d8e9] | 846 | req = { |
---|
| 847 | 'allocID': { 'fedid' : aid }, |
---|
| 848 | 'segmentdescription': { |
---|
| 849 | 'topdldescription': topo.to_dict(), |
---|
| 850 | }, |
---|
| 851 | } |
---|
[e02cd14] | 852 | |
---|
| 853 | if connInfo: |
---|
| 854 | req['connection'] = connInfo |
---|
[43197eb] | 855 | |
---|
| 856 | import_svcs = [ s for m in masters.values() \ |
---|
| 857 | for s in m if self.testbed in s.importers] |
---|
| 858 | |
---|
| 859 | if import_svcs or self.testbed in masters: |
---|
| 860 | req['service'] = [] |
---|
| 861 | |
---|
| 862 | for s in import_svcs: |
---|
| 863 | for r in s.reqs: |
---|
| 864 | sr = copy.deepcopy(r) |
---|
| 865 | sr['visibility'] = 'import'; |
---|
| 866 | req['service'].append(sr) |
---|
| 867 | |
---|
| 868 | for s in masters.get(self.testbed, []): |
---|
| 869 | for r in s.reqs: |
---|
| 870 | sr = copy.deepcopy(r) |
---|
| 871 | sr['visibility'] = 'export'; |
---|
| 872 | req['service'].append(sr) |
---|
| 873 | |
---|
[6c57fe9] | 874 | if attrs: |
---|
| 875 | req['fedAttr'] = attrs |
---|
[cc8d8e9] | 876 | |
---|
[fd556d1] | 877 | try: |
---|
[13e3dd2] | 878 | self.log.debug("Calling StartSegment at %s " % uri) |
---|
[fd556d1] | 879 | r = self.caller(uri, req, self.cert_file, self.cert_pwd, |
---|
| 880 | self.trusted_certs) |
---|
[f07fa49] | 881 | if r.has_key('StartSegmentResponseBody'): |
---|
| 882 | lval = r['StartSegmentResponseBody'].get('allocationLog', |
---|
| 883 | None) |
---|
| 884 | if lval and self.log_collector: |
---|
| 885 | for line in lval.splitlines(True): |
---|
| 886 | self.log_collector.write(line) |
---|
[b4b19c7] | 887 | self.make_map(r['StartSegmentResponseBody']) |
---|
[e83f2f2] | 888 | if 'proof' in r: self.proof = r['proof'] |
---|
[69692a9] | 889 | self.response = r |
---|
[f07fa49] | 890 | else: |
---|
| 891 | raise service_error(service_error.internal, |
---|
| 892 | "Bad response!?: %s" %r) |
---|
[fd556d1] | 893 | return True |
---|
| 894 | except service_error, e: |
---|
| 895 | self.log.error("Start segment failed on %s: %s" % \ |
---|
| 896 | (self.testbed, e)) |
---|
| 897 | return False |
---|
[cc8d8e9] | 898 | |
---|
| 899 | |
---|
[5ae3857] | 900 | |
---|
[e19b75c] | 901 | class terminate_segment: |
---|
[fd556d1] | 902 | def __init__(self, debug=False, log=None, testbed="", cert_file=None, |
---|
[5ae3857] | 903 | cert_pwd=None, trusted_certs=None, caller=None): |
---|
| 904 | self.log = log |
---|
| 905 | self.debug = debug |
---|
| 906 | self.cert_file = cert_file |
---|
| 907 | self.cert_pwd = cert_pwd |
---|
| 908 | self.trusted_certs = None |
---|
| 909 | self.caller = caller |
---|
[fd556d1] | 910 | self.testbed = testbed |
---|
[5ae3857] | 911 | |
---|
| 912 | def __call__(self, uri, aid ): |
---|
| 913 | req = { |
---|
[29d5f7c] | 914 | 'allocID': {'fedid': aid }, |
---|
[5ae3857] | 915 | } |
---|
[a69de97] | 916 | self.log.info("Calling terminate segment") |
---|
[fd556d1] | 917 | try: |
---|
| 918 | r = self.caller(uri, req, self.cert_file, self.cert_pwd, |
---|
| 919 | self.trusted_certs) |
---|
[a69de97] | 920 | self.log.info("Terminate segment succeeded") |
---|
[fd556d1] | 921 | return True |
---|
| 922 | except service_error, e: |
---|
| 923 | self.log.error("Terminate segment failed on %s: %s" % \ |
---|
| 924 | (self.testbed, e)) |
---|
| 925 | return False |
---|
[db6b092] | 926 | |
---|
[6e33086] | 927 | class info_segment(start_segment): |
---|
| 928 | def __init__(self, debug=False, log=None, testbed="", cert_file=None, |
---|
| 929 | cert_pwd=None, trusted_certs=None, caller=None, |
---|
| 930 | log_collector=None): |
---|
| 931 | experiment_control_local.start_segment.__init__(self, debug, |
---|
| 932 | log, testbed, cert_file, cert_pwd, trusted_certs, |
---|
| 933 | caller, log_collector) |
---|
| 934 | |
---|
| 935 | def __call__(self, uri, aid): |
---|
| 936 | req = { 'allocID': { 'fedid' : aid } } |
---|
| 937 | |
---|
| 938 | try: |
---|
| 939 | self.log.debug("Calling InfoSegment at %s " % uri) |
---|
| 940 | r = self.caller(uri, req, self.cert_file, self.cert_pwd, |
---|
| 941 | self.trusted_certs) |
---|
| 942 | if r.has_key('InfoSegmentResponseBody'): |
---|
| 943 | self.make_map(r['InfoSegmentResponseBody']) |
---|
| 944 | if 'proof' in r: self.proof = r['proof'] |
---|
| 945 | self.response = r |
---|
| 946 | else: |
---|
| 947 | raise service_error(service_error.internal, |
---|
| 948 | "Bad response!?: %s" %r) |
---|
| 949 | return True |
---|
| 950 | except service_error, e: |
---|
| 951 | self.log.error("Info segment failed on %s: %s" % \ |
---|
| 952 | (self.testbed, e)) |
---|
| 953 | return False |
---|
| 954 | |
---|
[22a1a77] | 955 | class operation_segment: |
---|
| 956 | def __init__(self, debug=False, log=None, testbed="", cert_file=None, |
---|
| 957 | cert_pwd=None, trusted_certs=None, caller=None, |
---|
| 958 | log_collector=None): |
---|
| 959 | self.log = log |
---|
| 960 | self.debug = debug |
---|
| 961 | self.cert_file = cert_file |
---|
| 962 | self.cert_pwd = cert_pwd |
---|
| 963 | self.trusted_certs = None |
---|
| 964 | self.caller = caller |
---|
| 965 | self.testbed = testbed |
---|
| 966 | self.status = None |
---|
| 967 | |
---|
| 968 | def __call__(self, uri, aid, op, targets, params): |
---|
| 969 | req = { |
---|
| 970 | 'allocID': { 'fedid' : aid }, |
---|
| 971 | 'operation': op, |
---|
| 972 | 'target': targets, |
---|
| 973 | } |
---|
| 974 | if params: req['parameter'] = params |
---|
| 975 | |
---|
| 976 | |
---|
| 977 | try: |
---|
| 978 | self.log.debug("Calling OperationSegment at %s " % uri) |
---|
| 979 | r = self.caller(uri, req, self.cert_file, self.cert_pwd, |
---|
| 980 | self.trusted_certs) |
---|
| 981 | if 'OperationSegmentResponseBody' in r: |
---|
| 982 | r = r['OperationSegmentResponseBody'] |
---|
| 983 | if 'status' in r: |
---|
| 984 | self.status = r['status'] |
---|
| 985 | else: |
---|
| 986 | raise service_error(service_error.internal, |
---|
| 987 | "Bad response!?: %s" %r) |
---|
| 988 | return True |
---|
| 989 | except service_error, e: |
---|
| 990 | self.log.error("Operation segment failed on %s: %s" % \ |
---|
| 991 | (self.testbed, e)) |
---|
| 992 | return False |
---|
| 993 | |
---|
[6e33086] | 994 | def annotate_topology(self, top, data): |
---|
[f671ef7] | 995 | # These routines do various parts of the annotation |
---|
| 996 | def add_new_names(nl, l): |
---|
| 997 | """ add any names in nl to the list in l """ |
---|
| 998 | for n in nl: |
---|
| 999 | if n not in l: l.append(n) |
---|
| 1000 | |
---|
| 1001 | def merge_services(ne, e): |
---|
| 1002 | for ns in ne.service: |
---|
| 1003 | # NB: the else is on the for |
---|
| 1004 | for s in e.service: |
---|
| 1005 | if ns.name == s.name: |
---|
| 1006 | s.importer = ns.importer |
---|
| 1007 | s.param = ns.param |
---|
| 1008 | s.description = ns.description |
---|
| 1009 | s.status = ns.status |
---|
| 1010 | break |
---|
| 1011 | else: |
---|
| 1012 | e.service.append(ns) |
---|
| 1013 | |
---|
| 1014 | def merge_oses(ne, e): |
---|
| 1015 | """ |
---|
| 1016 | Merge the operating system entries of ne into e |
---|
| 1017 | """ |
---|
| 1018 | for nos in ne.os: |
---|
| 1019 | # NB: the else is on the for |
---|
| 1020 | for os in e.os: |
---|
[7aaa8dc] | 1021 | if nos.name == os.name: |
---|
| 1022 | os.version = nos.version |
---|
| 1023 | os.version = nos.distribution |
---|
| 1024 | os.version = nos.distributionversion |
---|
[f671ef7] | 1025 | for a in nos.attribute: |
---|
[db3da0b] | 1026 | if os.get_attribute(a.attribute): |
---|
| 1027 | os.remove_attribute(a.attribute) |
---|
[7aaa8dc] | 1028 | os.set_attribute(a.attribute, a.value) |
---|
[f671ef7] | 1029 | break |
---|
| 1030 | else: |
---|
[db3da0b] | 1031 | # If both nodes have one OS, this is a replacement |
---|
| 1032 | if len(ne.os) == 1 and len(e.os) == 1: e.os = ne.os |
---|
| 1033 | else: e.os.append(nos) |
---|
[6e33086] | 1034 | |
---|
| 1035 | # Annotate the topology with embedding info |
---|
| 1036 | for e in top.elements: |
---|
| 1037 | if isinstance(e, topdl.Computer): |
---|
| 1038 | for s in data: |
---|
[f671ef7] | 1039 | ne = s.node.get(e.name, None) |
---|
| 1040 | if ne is not None: |
---|
| 1041 | add_new_names(ne.localname, e.localname) |
---|
| 1042 | e.status = ne.status |
---|
| 1043 | merge_services(ne, e) |
---|
| 1044 | add_new_names(ne.operation, e.operation) |
---|
| 1045 | if ne.os: merge_oses(ne, e) |
---|
[6e33086] | 1046 | break |
---|
[9e5e251] | 1047 | # Annotate substrates |
---|
| 1048 | for s in top.substrates: |
---|
| 1049 | for d in data: |
---|
| 1050 | ss = d.subs.get(s.name, None) |
---|
| 1051 | if ss is not None: |
---|
| 1052 | if ss.capacity is not None: |
---|
| 1053 | s.capacity = ss.capacity |
---|
| 1054 | if s.latency is not None: |
---|
| 1055 | s.latency = ss.latency |
---|
[6e33086] | 1056 | |
---|
| 1057 | |
---|
| 1058 | |
---|
[43197eb] | 1059 | def allocate_resources(self, allocated, masters, eid, expid, |
---|
[b4b19c7] | 1060 | tbparams, top, topo, tmpdir, alloc_log=None, log_collector=None, |
---|
[c573278] | 1061 | attrs=None, connInfo={}, tbmap=None, expcert=None): |
---|
[69692a9] | 1062 | |
---|
[cc8d8e9] | 1063 | started = { } # Testbeds where a sub-experiment started |
---|
| 1064 | # successfully |
---|
| 1065 | |
---|
| 1066 | # XXX |
---|
| 1067 | fail_soft = False |
---|
| 1068 | |
---|
[fd07c48] | 1069 | if tbmap is None: tbmap = { } |
---|
| 1070 | |
---|
[cc8d8e9] | 1071 | log = alloc_log or self.log |
---|
| 1072 | |
---|
[faea607] | 1073 | tp = thread_pool(self.nthreads) |
---|
[cc8d8e9] | 1074 | threads = [ ] |
---|
[b4b19c7] | 1075 | starters = [ ] |
---|
[cc8d8e9] | 1076 | |
---|
[c573278] | 1077 | if expcert: |
---|
| 1078 | cert = expcert |
---|
| 1079 | pw = None |
---|
| 1080 | else: |
---|
| 1081 | cert = self.cert_file |
---|
[822d31b] | 1082 | pw = self.cert_pwd |
---|
[c573278] | 1083 | |
---|
[109a32a] | 1084 | for tb in allocated.keys(): |
---|
| 1085 | # Create and start a thread to start the segment, and save it |
---|
| 1086 | # to get the return value later |
---|
[ab847bc] | 1087 | tb_attrs = copy.copy(attrs) |
---|
[faea607] | 1088 | tp.wait_for_slot() |
---|
[9294673] | 1089 | uri = tbparams[tb].uri or tbmap.get(testbed_base(tb), None) |
---|
[ab847bc] | 1090 | base, suffix = split_testbed(tb) |
---|
| 1091 | if suffix: |
---|
| 1092 | tb_attrs.append({'attribute': 'experiment_name', |
---|
[175b444] | 1093 | 'value': "%s-%s" % (eid, suffix)}) |
---|
[ab847bc] | 1094 | else: |
---|
| 1095 | tb_attrs.append({'attribute': 'experiment_name', 'value': eid}) |
---|
[109a32a] | 1096 | if not uri: |
---|
| 1097 | raise service_error(service_error.internal, |
---|
| 1098 | "Unknown testbed %s !?" % tb) |
---|
| 1099 | |
---|
[9294673] | 1100 | aid = tbparams[tb].allocID |
---|
| 1101 | if not aid: |
---|
[cc8d8e9] | 1102 | raise service_error(service_error.internal, |
---|
| 1103 | "No alloc id for testbed %s !?" % tb) |
---|
| 1104 | |
---|
[b4b19c7] | 1105 | s = self.start_segment(log=log, debug=self.debug, |
---|
[c573278] | 1106 | testbed=tb, cert_file=cert, |
---|
| 1107 | cert_pwd=pw, trusted_certs=self.trusted_certs, |
---|
[b4b19c7] | 1108 | caller=self.call_StartSegment, |
---|
| 1109 | log_collector=log_collector) |
---|
| 1110 | starters.append(s) |
---|
[faea607] | 1111 | t = pooled_thread(\ |
---|
[b4b19c7] | 1112 | target=s, name=tb, |
---|
[ab847bc] | 1113 | args=(uri, aid, topo[tb], masters, tb_attrs, connInfo[tb]), |
---|
[faea607] | 1114 | pdata=tp, trace_file=self.trace_file) |
---|
[69692a9] | 1115 | threads.append(t) |
---|
| 1116 | t.start() |
---|
[cc8d8e9] | 1117 | |
---|
[109a32a] | 1118 | # Wait until all finish (keep pinging the log, though) |
---|
| 1119 | mins = 0 |
---|
[dadc4da] | 1120 | revoked = False |
---|
[faea607] | 1121 | while not tp.wait_for_all_done(60.0): |
---|
[109a32a] | 1122 | mins += 1 |
---|
| 1123 | alloc_log.info("Waiting for sub threads (it has been %d mins)" \ |
---|
| 1124 | % mins) |
---|
[dadc4da] | 1125 | if not revoked and \ |
---|
[f52f5df] | 1126 | len([ t.getName() for t in threads if t.rv == False]) > 0: |
---|
[dadc4da] | 1127 | # a testbed has failed. Revoke this experiment's |
---|
| 1128 | # synchronizarion values so that sub experiments will not |
---|
| 1129 | # deadlock waiting for synchronization that will never happen |
---|
| 1130 | self.log.info("A subexperiment has failed to swap in, " + \ |
---|
| 1131 | "revoking synch keys") |
---|
| 1132 | var_key = "fedid:%s" % expid |
---|
| 1133 | for k in self.synch_store.all_keys(): |
---|
| 1134 | if len(k) > 45 and k[0:46] == var_key: |
---|
| 1135 | self.synch_store.revoke_key(k) |
---|
| 1136 | revoked = True |
---|
[69692a9] | 1137 | |
---|
[cc8d8e9] | 1138 | failed = [ t.getName() for t in threads if not t.rv ] |
---|
| 1139 | succeeded = [tb for tb in allocated.keys() if tb not in failed] |
---|
[3132419] | 1140 | |
---|
[cc8d8e9] | 1141 | # If one failed clean up, unless fail_soft is set |
---|
[32e7d93] | 1142 | if failed: |
---|
[cc8d8e9] | 1143 | if not fail_soft: |
---|
[faea607] | 1144 | tp.clear() |
---|
[cc8d8e9] | 1145 | for tb in succeeded: |
---|
| 1146 | # Create and start a thread to stop the segment |
---|
[faea607] | 1147 | tp.wait_for_slot() |
---|
[9294673] | 1148 | uri = tbparams[tb].uri |
---|
[faea607] | 1149 | t = pooled_thread(\ |
---|
[32e7d93] | 1150 | target=self.terminate_segment(log=log, |
---|
[fd556d1] | 1151 | testbed=tb, |
---|
[696a689] | 1152 | cert_file=cert, |
---|
| 1153 | cert_pwd=pw, |
---|
[32e7d93] | 1154 | trusted_certs=self.trusted_certs, |
---|
| 1155 | caller=self.call_TerminateSegment), |
---|
[9294673] | 1156 | args=(uri, tbparams[tb].allocID), |
---|
[32e7d93] | 1157 | name=tb, |
---|
[faea607] | 1158 | pdata=tp, trace_file=self.trace_file) |
---|
[cc8d8e9] | 1159 | t.start() |
---|
[f52f5df] | 1160 | # Wait until all finish (if any are being stopped) |
---|
| 1161 | if succeeded: |
---|
[faea607] | 1162 | tp.wait_for_all_done() |
---|
[cc8d8e9] | 1163 | |
---|
| 1164 | # release the allocations |
---|
| 1165 | for tb in tbparams.keys(): |
---|
[725c55d] | 1166 | try: |
---|
[9294673] | 1167 | self.release_access(tb, tbparams[tb].allocID, |
---|
| 1168 | tbmap=tbmap, uri=tbparams[tb].uri, |
---|
[696a689] | 1169 | cert_file=cert, cert_pwd=pw) |
---|
[725c55d] | 1170 | except service_error, e: |
---|
| 1171 | self.log.warn("Error releasing access: %s" % e.desc) |
---|
[cc8d8e9] | 1172 | # Remove the placeholder |
---|
| 1173 | self.state_lock.acquire() |
---|
[29d5f7c] | 1174 | self.state[eid].status = 'failed' |
---|
[6e33086] | 1175 | self.state[eid].updated() |
---|
[cc8d8e9] | 1176 | if self.state_filename: self.write_state() |
---|
| 1177 | self.state_lock.release() |
---|
[05e8da8] | 1178 | # Remove the repo dir |
---|
| 1179 | self.remove_dirs("%s/%s" %(self.repodir, expid)) |
---|
| 1180 | # Walk up tmpdir, deleting as we go |
---|
| 1181 | if self.cleanup: |
---|
| 1182 | self.remove_dirs(tmpdir) |
---|
| 1183 | else: |
---|
| 1184 | log.debug("[start_experiment]: not removing %s" % tmpdir) |
---|
| 1185 | |
---|
[cc8d8e9] | 1186 | |
---|
| 1187 | log.error("Swap in failed on %s" % ",".join(failed)) |
---|
| 1188 | return |
---|
| 1189 | else: |
---|
[29d5f7c] | 1190 | # Walk through the successes and gather the proofs |
---|
[e83f2f2] | 1191 | proofs = { } |
---|
[b4b19c7] | 1192 | for s in starters: |
---|
[e83f2f2] | 1193 | if s.proof: |
---|
| 1194 | proofs[s.testbed] = s.proof |
---|
[6e33086] | 1195 | self.annotate_topology(top, starters) |
---|
[cc8d8e9] | 1196 | log.info("[start_segment]: Experiment %s active" % eid) |
---|
| 1197 | |
---|
| 1198 | |
---|
| 1199 | # Walk up tmpdir, deleting as we go |
---|
[69692a9] | 1200 | if self.cleanup: |
---|
[05e8da8] | 1201 | self.remove_dirs(tmpdir) |
---|
[69692a9] | 1202 | else: |
---|
| 1203 | log.debug("[start_experiment]: not removing %s" % tmpdir) |
---|
[cc8d8e9] | 1204 | |
---|
[b4b19c7] | 1205 | # Insert the experiment into our state and update the disk copy. |
---|
[cc8d8e9] | 1206 | self.state_lock.acquire() |
---|
[29d5f7c] | 1207 | self.state[expid].status = 'active' |
---|
[cc8d8e9] | 1208 | self.state[eid] = self.state[expid] |
---|
[29d5f7c] | 1209 | self.state[eid].top = top |
---|
[6e33086] | 1210 | self.state[eid].updated() |
---|
[e83f2f2] | 1211 | # Append startup proofs |
---|
[29d5f7c] | 1212 | for f in self.state[eid].get_all_allocations(): |
---|
| 1213 | if f.tb in proofs: |
---|
| 1214 | f.proof.append(proofs[f.tb]) |
---|
[e83f2f2] | 1215 | |
---|
[cc8d8e9] | 1216 | if self.state_filename: self.write_state() |
---|
| 1217 | self.state_lock.release() |
---|
| 1218 | return |
---|
| 1219 | |
---|
| 1220 | |
---|
[895a133] | 1221 | def add_kit(self, e, kit): |
---|
| 1222 | """ |
---|
| 1223 | Add a Software object created from the list of (install, location) |
---|
| 1224 | tuples passed as kit to the software attribute of an object e. We |
---|
| 1225 | do this enough to break out the code, but it's kind of a hack to |
---|
| 1226 | avoid changing the old tuple rep. |
---|
| 1227 | """ |
---|
| 1228 | |
---|
| 1229 | s = [ topdl.Software(install=i, location=l) for i, l in kit] |
---|
| 1230 | |
---|
| 1231 | if isinstance(e.software, list): e.software.extend(s) |
---|
| 1232 | else: e.software = s |
---|
| 1233 | |
---|
[913dc7a] | 1234 | def append_experiment_authorization(self, expid, attrs, |
---|
| 1235 | need_state_lock=True): |
---|
| 1236 | """ |
---|
| 1237 | Append the authorization information to system state |
---|
| 1238 | """ |
---|
| 1239 | |
---|
| 1240 | for p, a in attrs: |
---|
| 1241 | self.auth.set_attribute(p, a) |
---|
| 1242 | self.auth.save() |
---|
| 1243 | |
---|
| 1244 | if need_state_lock: self.state_lock.acquire() |
---|
[29d5f7c] | 1245 | # XXX: really a no op? |
---|
| 1246 | #self.state[expid]['auth'].update(attrs) |
---|
[913dc7a] | 1247 | if self.state_filename: self.write_state() |
---|
| 1248 | if need_state_lock: self.state_lock.release() |
---|
| 1249 | |
---|
[a96d946] | 1250 | def clear_experiment_authorization(self, expid, need_state_lock=True): |
---|
[913dc7a] | 1251 | """ |
---|
| 1252 | Attrs is a set of attribute principal pairs that need to be removed |
---|
| 1253 | from the authenticator. Remove them and save the authenticator. |
---|
| 1254 | """ |
---|
| 1255 | |
---|
| 1256 | if need_state_lock: self.state_lock.acquire() |
---|
[29d5f7c] | 1257 | # XXX: should be a no-op |
---|
| 1258 | #if expid in self.state and 'auth' in self.state[expid]: |
---|
| 1259 | #for p, a in self.state[expid]['auth']: |
---|
| 1260 | #self.auth.unset_attribute(p, a) |
---|
| 1261 | #self.state[expid]['auth'] = set() |
---|
[913dc7a] | 1262 | if self.state_filename: self.write_state() |
---|
| 1263 | if need_state_lock: self.state_lock.release() |
---|
[b67fd22] | 1264 | self.auth.save() |
---|
[913dc7a] | 1265 | |
---|
[895a133] | 1266 | |
---|
[b4b19c7] | 1267 | def create_experiment_state(self, fid, req, expid, expcert, |
---|
[a3ad8bd] | 1268 | state='starting'): |
---|
[895a133] | 1269 | """ |
---|
| 1270 | Create the initial entry in the experiment's state. The expid and |
---|
| 1271 | expcert are the experiment's fedid and certifacte that represents that |
---|
| 1272 | ID, which are installed in the experiment state. If the request |
---|
| 1273 | includes a suggested local name that is used if possible. If the local |
---|
| 1274 | name is already taken by an experiment owned by this user that has |
---|
[a3ad8bd] | 1275 | failed, it is overwritten. Otherwise new letters are added until a |
---|
[895a133] | 1276 | valid localname is found. The generated local name is returned. |
---|
| 1277 | """ |
---|
| 1278 | |
---|
| 1279 | if req.has_key('experimentID') and \ |
---|
| 1280 | req['experimentID'].has_key('localname'): |
---|
| 1281 | overwrite = False |
---|
| 1282 | eid = req['experimentID']['localname'] |
---|
| 1283 | # If there's an old failed experiment here with the same local name |
---|
| 1284 | # and accessible by this user, we'll overwrite it, otherwise we'll |
---|
| 1285 | # fall through and do the collision avoidance. |
---|
| 1286 | old_expid = self.get_experiment_fedid(eid) |
---|
[74572ba] | 1287 | if old_expid: |
---|
| 1288 | users_experiment = True |
---|
| 1289 | try: |
---|
| 1290 | self.check_experiment_access(fid, old_expid) |
---|
| 1291 | except service_error, e: |
---|
| 1292 | if e.code == service_error.access: users_experiment = False |
---|
| 1293 | else: raise e |
---|
| 1294 | if users_experiment: |
---|
| 1295 | self.state_lock.acquire() |
---|
[29d5f7c] | 1296 | status = self.state[eid].status |
---|
[74572ba] | 1297 | if status and status == 'failed': |
---|
| 1298 | # remove the old access attributes |
---|
| 1299 | self.clear_experiment_authorization(eid, |
---|
| 1300 | need_state_lock=False) |
---|
| 1301 | overwrite = True |
---|
| 1302 | del self.state[eid] |
---|
| 1303 | del self.state[old_expid] |
---|
| 1304 | self.state_lock.release() |
---|
[6031c9d] | 1305 | else: |
---|
| 1306 | self.log.info('Experiment %s exists, ' % eid + \ |
---|
| 1307 | 'but this user cannot access it') |
---|
[895a133] | 1308 | self.state_lock.acquire() |
---|
| 1309 | while (self.state.has_key(eid) and not overwrite): |
---|
| 1310 | eid += random.choice(string.ascii_letters) |
---|
| 1311 | # Initial state |
---|
[29d5f7c] | 1312 | self.state[eid] = experiment_info(fedid=expid, localname=eid, |
---|
| 1313 | identity=expcert) |
---|
[895a133] | 1314 | self.state[expid] = self.state[eid] |
---|
[913dc7a] | 1315 | if self.state_filename: self.write_state() |
---|
| 1316 | self.state_lock.release() |
---|
[895a133] | 1317 | else: |
---|
| 1318 | eid = self.exp_stem |
---|
| 1319 | for i in range(0,5): |
---|
| 1320 | eid += random.choice(string.ascii_letters) |
---|
| 1321 | self.state_lock.acquire() |
---|
| 1322 | while (self.state.has_key(eid)): |
---|
| 1323 | eid = self.exp_stem |
---|
| 1324 | for i in range(0,5): |
---|
| 1325 | eid += random.choice(string.ascii_letters) |
---|
| 1326 | # Initial state |
---|
[29d5f7c] | 1327 | self.state[eid] = experiment_info(fedid=expid, localname=eid, |
---|
| 1328 | identity=expcert) |
---|
[895a133] | 1329 | self.state[expid] = self.state[eid] |
---|
[913dc7a] | 1330 | if self.state_filename: self.write_state() |
---|
| 1331 | self.state_lock.release() |
---|
| 1332 | |
---|
| 1333 | # Let users touch the state. Authorize this fid and the expid itself |
---|
| 1334 | # to touch the experiment, as well as allowing th eoverrides. |
---|
| 1335 | self.append_experiment_authorization(eid, |
---|
| 1336 | set([(fid, expid), (expid,expid)] + \ |
---|
| 1337 | [ (o, expid) for o in self.overrides])) |
---|
[895a133] | 1338 | |
---|
| 1339 | return eid |
---|
| 1340 | |
---|
| 1341 | |
---|
| 1342 | def allocate_ips_to_topo(self, top): |
---|
| 1343 | """ |
---|
[69692a9] | 1344 | Add an ip4_address attribute to all the hosts in the topology, based on |
---|
[895a133] | 1345 | the shared substrates on which they sit. An /etc/hosts file is also |
---|
[69692a9] | 1346 | created and returned as a list of hostfiles entries. We also return |
---|
| 1347 | the allocator, because we may need to allocate IPs to portals |
---|
| 1348 | (specifically DRAGON portals). |
---|
[895a133] | 1349 | """ |
---|
| 1350 | subs = sorted(top.substrates, |
---|
| 1351 | cmp=lambda x,y: cmp(len(x.interfaces), len(y.interfaces)), |
---|
| 1352 | reverse=True) |
---|
| 1353 | ips = ip_allocator(int(ip_addr("10.0.0.0")), 2 **24) |
---|
| 1354 | ifs = { } |
---|
| 1355 | hosts = [ ] |
---|
| 1356 | |
---|
| 1357 | for idx, s in enumerate(subs): |
---|
[289ff7e] | 1358 | net_size = len(s.interfaces)+2 |
---|
| 1359 | |
---|
| 1360 | a = ips.allocate(net_size) |
---|
[895a133] | 1361 | if a : |
---|
| 1362 | base, num = a |
---|
[289ff7e] | 1363 | if num < net_size: |
---|
[895a133] | 1364 | raise service_error(service_error.internal, |
---|
| 1365 | "Allocator returned wrong number of IPs??") |
---|
| 1366 | else: |
---|
| 1367 | raise service_error(service_error.req, |
---|
| 1368 | "Cannot allocate IP addresses") |
---|
[062b991] | 1369 | mask = ips.min_alloc |
---|
| 1370 | while mask < net_size: |
---|
| 1371 | mask *= 2 |
---|
[289ff7e] | 1372 | |
---|
[062b991] | 1373 | netmask = ((2**32-1) ^ (mask-1)) |
---|
[895a133] | 1374 | |
---|
| 1375 | base += 1 |
---|
| 1376 | for i in s.interfaces: |
---|
| 1377 | i.attribute.append( |
---|
| 1378 | topdl.Attribute('ip4_address', |
---|
| 1379 | "%s" % ip_addr(base))) |
---|
[289ff7e] | 1380 | i.attribute.append( |
---|
| 1381 | topdl.Attribute('ip4_netmask', |
---|
| 1382 | "%s" % ip_addr(int(netmask)))) |
---|
| 1383 | |
---|
[1e7f268] | 1384 | hname = i.element.name |
---|
[895a133] | 1385 | if ifs.has_key(hname): |
---|
| 1386 | hosts.append("%s\t%s-%s %s-%d" % \ |
---|
| 1387 | (ip_addr(base), hname, s.name, hname, |
---|
| 1388 | ifs[hname])) |
---|
| 1389 | else: |
---|
| 1390 | ifs[hname] = 0 |
---|
| 1391 | hosts.append("%s\t%s-%s %s-%d %s" % \ |
---|
| 1392 | (ip_addr(base), hname, s.name, hname, |
---|
| 1393 | ifs[hname], hname)) |
---|
| 1394 | |
---|
| 1395 | ifs[hname] += 1 |
---|
| 1396 | base += 1 |
---|
[69692a9] | 1397 | return hosts, ips |
---|
[895a133] | 1398 | |
---|
[1d73342] | 1399 | def get_access_to_testbeds(self, testbeds, fid, allocated, |
---|
[725c55d] | 1400 | tbparam, masters, tbmap, expid=None, expcert=None): |
---|
[6e63513] | 1401 | for tb in testbeds: |
---|
[1d73342] | 1402 | self.get_access(tb, tbparam, fid, masters, tbmap, expid, |
---|
[c573278] | 1403 | expcert) |
---|
[6e63513] | 1404 | allocated[tb] = 1 |
---|
| 1405 | |
---|
[1d73342] | 1406 | def get_access(self, tb, tbparam,fid, masters, tbmap, expid=None, |
---|
| 1407 | expcert=None): |
---|
[6e63513] | 1408 | """ |
---|
| 1409 | Get access to testbed through fedd and set the parameters for that tb |
---|
| 1410 | """ |
---|
| 1411 | def get_export_project(svcs): |
---|
| 1412 | """ |
---|
| 1413 | Look through for the list of federated_service for this testbed |
---|
| 1414 | objects for a project_export service, and extract the project |
---|
| 1415 | parameter. |
---|
| 1416 | """ |
---|
| 1417 | |
---|
| 1418 | pe = [s for s in svcs if s.name=='project_export'] |
---|
| 1419 | if len(pe) == 1: |
---|
| 1420 | return pe[0].params.get('project', None) |
---|
| 1421 | elif len(pe) == 0: |
---|
| 1422 | return None |
---|
| 1423 | else: |
---|
| 1424 | raise service_error(service_error.req, |
---|
| 1425 | "More than one project export is not supported") |
---|
| 1426 | |
---|
[d0912be] | 1427 | def add_services(svcs, type, slist, keys): |
---|
[63c6664] | 1428 | """ |
---|
[d0912be] | 1429 | Add the given services to slist. type is import or export. Also |
---|
| 1430 | add a mapping entry from the assigned id to the original service |
---|
| 1431 | record. |
---|
[63c6664] | 1432 | """ |
---|
| 1433 | for i, s in enumerate(svcs): |
---|
| 1434 | idx = '%s%d' % (type, i) |
---|
[d0912be] | 1435 | keys[idx] = s |
---|
[63c6664] | 1436 | sr = {'id': idx, 'name': s.name, 'visibility': type } |
---|
| 1437 | if s.params: |
---|
| 1438 | sr['fedAttr'] = [ { 'attribute': k, 'value': v } \ |
---|
| 1439 | for k, v in s.params.items()] |
---|
| 1440 | slist.append(sr) |
---|
| 1441 | |
---|
[6e63513] | 1442 | uri = tbmap.get(testbed_base(tb), None) |
---|
| 1443 | if not uri: |
---|
| 1444 | raise service_error(service_error.server_config, |
---|
| 1445 | "Unknown testbed: %s" % tb) |
---|
| 1446 | |
---|
| 1447 | export_svcs = masters.get(tb,[]) |
---|
| 1448 | import_svcs = [ s for m in masters.values() \ |
---|
| 1449 | for s in m \ |
---|
| 1450 | if tb in s.importers ] |
---|
| 1451 | |
---|
| 1452 | export_project = get_export_project(export_svcs) |
---|
| 1453 | # Compose the credential list so that IDs come before attributes |
---|
| 1454 | creds = set() |
---|
| 1455 | keys = set() |
---|
[c573278] | 1456 | certs = self.auth.get_creds_for_principal(fid) |
---|
| 1457 | if expid: |
---|
| 1458 | certs.update(self.auth.get_creds_for_principal(expid)) |
---|
| 1459 | for c in certs: |
---|
[6e63513] | 1460 | keys.add(c.issuer_cert()) |
---|
| 1461 | creds.add(c.attribute_cert()) |
---|
| 1462 | creds = list(keys) + list(creds) |
---|
| 1463 | |
---|
[c573278] | 1464 | if expcert: cert, pw = expcert, None |
---|
| 1465 | else: cert, pw = self.cert_file, self.cert_pw |
---|
| 1466 | |
---|
[6e63513] | 1467 | # Request credentials |
---|
| 1468 | req = { |
---|
| 1469 | 'abac_credential': creds, |
---|
| 1470 | } |
---|
| 1471 | # Make the service request from the services we're importing and |
---|
| 1472 | # exporting. Keep track of the export request ids so we can |
---|
| 1473 | # collect the resulting info from the access response. |
---|
| 1474 | e_keys = { } |
---|
| 1475 | if import_svcs or export_svcs: |
---|
[63c6664] | 1476 | slist = [] |
---|
[d0912be] | 1477 | add_services(import_svcs, 'import', slist, e_keys) |
---|
| 1478 | add_services(export_svcs, 'export', slist, e_keys) |
---|
[63c6664] | 1479 | req['service'] = slist |
---|
[6e63513] | 1480 | |
---|
| 1481 | if self.local_access.has_key(uri): |
---|
| 1482 | # Local access call |
---|
| 1483 | req = { 'RequestAccessRequestBody' : req } |
---|
| 1484 | r = self.local_access[uri].RequestAccess(req, |
---|
| 1485 | fedid(file=self.cert_file)) |
---|
| 1486 | r = { 'RequestAccessResponseBody' : r } |
---|
| 1487 | else: |
---|
[c573278] | 1488 | r = self.call_RequestAccess(uri, req, cert, pw, self.trusted_certs) |
---|
[6e63513] | 1489 | |
---|
[725c55d] | 1490 | if r.has_key('RequestAccessResponseBody'): |
---|
| 1491 | # Through to here we have a valid response, not a fault. |
---|
| 1492 | # Access denied is a fault, so something better or worse than |
---|
| 1493 | # access denied has happened. |
---|
| 1494 | r = r['RequestAccessResponseBody'] |
---|
| 1495 | self.log.debug("[get_access] Access granted") |
---|
| 1496 | else: |
---|
| 1497 | raise service_error(service_error.protocol, |
---|
| 1498 | "Bad proxy response") |
---|
[e83f2f2] | 1499 | if 'proof' not in r: |
---|
| 1500 | raise service_error(service_error.protocol, |
---|
| 1501 | "Bad access response (no access proof)") |
---|
[ab3d6c5] | 1502 | |
---|
[9294673] | 1503 | tbparam[tb] = allocation_info(allocID=r['allocID'].get('fedid', None), |
---|
[ab3d6c5] | 1504 | tb=tb, uri=uri, proof=[r['proof']], |
---|
| 1505 | services=masters.get(tb, None)) |
---|
[6e63513] | 1506 | |
---|
| 1507 | # Collect the responses corresponding to the services this testbed |
---|
| 1508 | # exports. These will be the service requests that we will include in |
---|
| 1509 | # the start segment requests (with appropriate visibility values) to |
---|
| 1510 | # import and export the segments. |
---|
| 1511 | for s in r.get('service', []): |
---|
| 1512 | id = s.get('id', None) |
---|
[ab3d6c5] | 1513 | # Note that this attaches the response to the object in the masters |
---|
| 1514 | # data structure. (The e_keys index disappears when this fcn |
---|
| 1515 | # returns) |
---|
[6e63513] | 1516 | if id and id in e_keys: |
---|
| 1517 | e_keys[id].reqs.append(s) |
---|
| 1518 | |
---|
| 1519 | # Add attributes to parameter space. We don't allow attributes to |
---|
| 1520 | # overlay any parameters already installed. |
---|
| 1521 | for a in r.get('fedAttr', []): |
---|
| 1522 | try: |
---|
[9294673] | 1523 | if a['attribute']: |
---|
| 1524 | tbparam[tb].set_attribute(a['attribute'], a['value']) |
---|
[6e63513] | 1525 | except KeyError: |
---|
| 1526 | self.log.error("Bad attribute in response: %s" % a) |
---|
| 1527 | |
---|
| 1528 | |
---|
[7fe81be] | 1529 | def split_topology(self, top, topo, testbeds): |
---|
[895a133] | 1530 | """ |
---|
[e02cd14] | 1531 | Create the sub-topologies that are needed for experiment instantiation. |
---|
[895a133] | 1532 | """ |
---|
| 1533 | for tb in testbeds: |
---|
| 1534 | topo[tb] = top.clone() |
---|
[7fe81be] | 1535 | # copy in for loop allows deletions from the original |
---|
| 1536 | for e in [ e for e in topo[tb].elements]: |
---|
[895a133] | 1537 | etb = e.get_attribute('testbed') |
---|
[7fe81be] | 1538 | # NB: elements without a testbed attribute won't appear in any |
---|
| 1539 | # sub topologies. |
---|
| 1540 | if not etb or etb != tb: |
---|
[895a133] | 1541 | for i in e.interface: |
---|
| 1542 | for s in i.subs: |
---|
| 1543 | try: |
---|
| 1544 | s.interfaces.remove(i) |
---|
| 1545 | except ValueError: |
---|
| 1546 | raise service_error(service_error.internal, |
---|
| 1547 | "Can't remove interface??") |
---|
[7fe81be] | 1548 | topo[tb].elements.remove(e) |
---|
[895a133] | 1549 | topo[tb].make_indices() |
---|
| 1550 | |
---|
[2627eb3] | 1551 | def confirm_software(self, top): |
---|
| 1552 | """ |
---|
| 1553 | Make sure that the software to be loaded in the topo is all available |
---|
| 1554 | before we begin making access requests, etc. This is a subset of |
---|
| 1555 | wrangle_software. |
---|
| 1556 | """ |
---|
| 1557 | pkgs = set([ d for i, d in self.fedkit + self.gatewaykit ]) |
---|
| 1558 | pkgs.update([x.location for e in top.elements for x in e.software]) |
---|
| 1559 | |
---|
| 1560 | for pkg in pkgs: |
---|
| 1561 | loc = pkg |
---|
| 1562 | |
---|
| 1563 | scheme, host, path = urlparse(loc)[0:3] |
---|
| 1564 | dest = os.path.basename(path) |
---|
| 1565 | if not scheme: |
---|
| 1566 | if not loc.startswith('/'): |
---|
| 1567 | loc = "/%s" % loc |
---|
| 1568 | loc = "file://%s" %loc |
---|
| 1569 | # NB: if scheme was found, loc == pkg |
---|
| 1570 | try: |
---|
| 1571 | u = urlopen(loc) |
---|
| 1572 | u.close() |
---|
| 1573 | except Exception, e: |
---|
| 1574 | raise service_error(service_error.req, |
---|
| 1575 | "Cannot open %s: %s" % (loc, e)) |
---|
| 1576 | return True |
---|
| 1577 | |
---|
[895a133] | 1578 | def wrangle_software(self, expid, top, topo, tbparams): |
---|
| 1579 | """ |
---|
| 1580 | Copy software out to the repository directory, allocate permissions and |
---|
| 1581 | rewrite the segment topologies to look for the software in local |
---|
| 1582 | places. |
---|
| 1583 | """ |
---|
| 1584 | |
---|
| 1585 | # Copy the rpms and tarfiles to a distribution directory from |
---|
| 1586 | # which the federants can retrieve them |
---|
| 1587 | linkpath = "%s/software" % expid |
---|
| 1588 | softdir ="%s/%s" % ( self.repodir, linkpath) |
---|
| 1589 | softmap = { } |
---|
[2627eb3] | 1590 | |
---|
| 1591 | # self.fedkit and self.gateway kit are lists of tuples of |
---|
| 1592 | # (install_location, download_location) this extracts the download |
---|
| 1593 | # locations. |
---|
| 1594 | pkgs = set([ d for i, d in self.fedkit + self.gatewaykit ]) |
---|
| 1595 | pkgs.update([x.location for e in top.elements for x in e.software]) |
---|
[895a133] | 1596 | try: |
---|
| 1597 | os.makedirs(softdir) |
---|
[d3c8759] | 1598 | except EnvironmentError, e: |
---|
[895a133] | 1599 | raise service_error( |
---|
| 1600 | "Cannot create software directory: %s" % e) |
---|
| 1601 | # The actual copying. Everything's converted into a url for copying. |
---|
[913dc7a] | 1602 | auth_attrs = set() |
---|
[895a133] | 1603 | for pkg in pkgs: |
---|
| 1604 | loc = pkg |
---|
| 1605 | |
---|
| 1606 | scheme, host, path = urlparse(loc)[0:3] |
---|
| 1607 | dest = os.path.basename(path) |
---|
| 1608 | if not scheme: |
---|
| 1609 | if not loc.startswith('/'): |
---|
| 1610 | loc = "/%s" % loc |
---|
| 1611 | loc = "file://%s" %loc |
---|
[2627eb3] | 1612 | # NB: if scheme was found, loc == pkg |
---|
[895a133] | 1613 | try: |
---|
| 1614 | u = urlopen(loc) |
---|
| 1615 | except Exception, e: |
---|
| 1616 | raise service_error(service_error.req, |
---|
| 1617 | "Cannot open %s: %s" % (loc, e)) |
---|
| 1618 | try: |
---|
| 1619 | f = open("%s/%s" % (softdir, dest) , "w") |
---|
| 1620 | self.log.debug("Writing %s/%s" % (softdir,dest) ) |
---|
| 1621 | data = u.read(4096) |
---|
| 1622 | while data: |
---|
| 1623 | f.write(data) |
---|
| 1624 | data = u.read(4096) |
---|
| 1625 | f.close() |
---|
| 1626 | u.close() |
---|
| 1627 | except Exception, e: |
---|
| 1628 | raise service_error(service_error.internal, |
---|
| 1629 | "Could not copy %s: %s" % (loc, e)) |
---|
| 1630 | path = re.sub("/tmp", "", linkpath) |
---|
| 1631 | # XXX |
---|
| 1632 | softmap[pkg] = \ |
---|
[7183b48] | 1633 | "%s/%s/%s" %\ |
---|
| 1634 | ( self.repo_url, path, dest) |
---|
[895a133] | 1635 | |
---|
[913dc7a] | 1636 | # Allow the individual segments to access the software by assigning |
---|
| 1637 | # an attribute to each testbed allocation that encodes the data to |
---|
| 1638 | # be released. This expression collects the data for each run of |
---|
| 1639 | # the loop. |
---|
| 1640 | auth_attrs.update([ |
---|
[9294673] | 1641 | (tbparams[tb].allocID, "/%s/%s" % ( path, dest)) \ |
---|
[913dc7a] | 1642 | for tb in tbparams.keys()]) |
---|
| 1643 | |
---|
| 1644 | self.append_experiment_authorization(expid, auth_attrs) |
---|
[895a133] | 1645 | |
---|
| 1646 | # Convert the software locations in the segments into the local |
---|
| 1647 | # copies on this host |
---|
| 1648 | for soft in [ s for tb in topo.values() \ |
---|
| 1649 | for e in tb.elements \ |
---|
| 1650 | if getattr(e, 'software', False) \ |
---|
| 1651 | for s in e.software ]: |
---|
| 1652 | if softmap.has_key(soft.location): |
---|
| 1653 | soft.location = softmap[soft.location] |
---|
| 1654 | |
---|
| 1655 | |
---|
[a3ad8bd] | 1656 | def new_experiment(self, req, fid): |
---|
| 1657 | """ |
---|
| 1658 | The external interface to empty initial experiment creation called from |
---|
| 1659 | the dispatcher. |
---|
| 1660 | |
---|
| 1661 | Creates a working directory, splits the incoming description using the |
---|
| 1662 | splitter script and parses out the avrious subsections using the |
---|
| 1663 | lcasses above. Once each sub-experiment is created, use pooled threads |
---|
| 1664 | to instantiate them and start it all up. |
---|
| 1665 | """ |
---|
[2bb8b35] | 1666 | self.log.info("New experiment call started for %s" % fid) |
---|
[7206e5a] | 1667 | req = req.get('NewRequestBody', None) |
---|
| 1668 | if not req: |
---|
| 1669 | raise service_error(service_error.req, |
---|
| 1670 | "Bad request format (no NewRequestBody)") |
---|
| 1671 | |
---|
| 1672 | if self.auth.import_credentials(data_list=req.get('credential', [])): |
---|
| 1673 | self.auth.save() |
---|
[c573278] | 1674 | |
---|
[8cab4c2] | 1675 | try: |
---|
| 1676 | access_ok, proof = self.auth.check_attribute(fid, 'new', |
---|
| 1677 | with_proof=True) |
---|
| 1678 | except service_error, e: |
---|
| 1679 | self.log.info("New experiment call for %s: access denied" % fid) |
---|
| 1680 | raise e |
---|
| 1681 | |
---|
[e83f2f2] | 1682 | |
---|
| 1683 | if not access_ok: |
---|
[2bb8b35] | 1684 | self.log.info("New experiment call for %s: Access denied" % fid) |
---|
[e83f2f2] | 1685 | raise service_error(service_error.access, "New access denied", |
---|
| 1686 | proof=[proof]) |
---|
[a3ad8bd] | 1687 | |
---|
| 1688 | try: |
---|
| 1689 | tmpdir = tempfile.mkdtemp(prefix="split-") |
---|
[d3c8759] | 1690 | except EnvironmentError: |
---|
[a3ad8bd] | 1691 | raise service_error(service_error.internal, "Cannot create tmp dir") |
---|
| 1692 | |
---|
| 1693 | try: |
---|
| 1694 | access_user = self.accessdb[fid] |
---|
| 1695 | except KeyError: |
---|
| 1696 | raise service_error(service_error.internal, |
---|
| 1697 | "Access map and authorizer out of sync in " + \ |
---|
[7183b48] | 1698 | "new_experiment for fedid %s" % fid) |
---|
[a3ad8bd] | 1699 | |
---|
| 1700 | # Generate an ID for the experiment (slice) and a certificate that the |
---|
| 1701 | # allocator can use to prove they own it. We'll ship it back through |
---|
[7206e5a] | 1702 | # the encrypted connection. If the requester supplied one, use it. |
---|
| 1703 | if 'experimentAccess' in req and 'X509' in req['experimentAccess']: |
---|
| 1704 | expcert = req['experimentAccess']['X509'] |
---|
[962ea25] | 1705 | expid = fedid(certstr=expcert) |
---|
[7206e5a] | 1706 | self.state_lock.acquire() |
---|
| 1707 | if expid in self.state: |
---|
| 1708 | self.state_lock.release() |
---|
| 1709 | raise service_error(service_error.req, |
---|
| 1710 | 'fedid %s identifies an existing experiment' % expid) |
---|
| 1711 | self.state_lock.release() |
---|
| 1712 | else: |
---|
| 1713 | (expid, expcert) = generate_fedid("test", dir=tmpdir, log=self.log) |
---|
[a3ad8bd] | 1714 | |
---|
| 1715 | #now we're done with the tmpdir, and it should be empty |
---|
| 1716 | if self.cleanup: |
---|
| 1717 | self.log.debug("[new_experiment]: removing %s" % tmpdir) |
---|
| 1718 | os.rmdir(tmpdir) |
---|
| 1719 | else: |
---|
| 1720 | self.log.debug("[new_experiment]: not removing %s" % tmpdir) |
---|
| 1721 | |
---|
| 1722 | eid = self.create_experiment_state(fid, req, expid, expcert, |
---|
| 1723 | state='empty') |
---|
| 1724 | |
---|
| 1725 | rv = { |
---|
| 1726 | 'experimentID': [ |
---|
| 1727 | {'localname' : eid }, { 'fedid': copy.copy(expid) } |
---|
| 1728 | ], |
---|
| 1729 | 'experimentStatus': 'empty', |
---|
[e83f2f2] | 1730 | 'experimentAccess': { 'X509' : expcert }, |
---|
| 1731 | 'proof': proof.to_dict(), |
---|
[a3ad8bd] | 1732 | } |
---|
| 1733 | |
---|
[2bb8b35] | 1734 | self.log.info("New experiment call succeeded for %s" % fid) |
---|
[a3ad8bd] | 1735 | return rv |
---|
| 1736 | |
---|
[cf0ff4f] | 1737 | # create_experiment sub-functions |
---|
| 1738 | |
---|
[5ecb9a3] | 1739 | @staticmethod |
---|
[cf0ff4f] | 1740 | def get_experiment_key(req, field='experimentID'): |
---|
[5ecb9a3] | 1741 | """ |
---|
| 1742 | Parse the experiment identifiers out of the request (the request body |
---|
| 1743 | tag has been removed). Specifically this pulls either the fedid or the |
---|
| 1744 | localname out of the experimentID field. A fedid is preferred. If |
---|
| 1745 | neither is present or the request does not contain the fields, |
---|
| 1746 | service_errors are raised. |
---|
| 1747 | """ |
---|
| 1748 | # Get the experiment access |
---|
[cf0ff4f] | 1749 | exp = req.get(field, None) |
---|
[5ecb9a3] | 1750 | if exp: |
---|
| 1751 | if exp.has_key('fedid'): |
---|
| 1752 | key = exp['fedid'] |
---|
| 1753 | elif exp.has_key('localname'): |
---|
| 1754 | key = exp['localname'] |
---|
| 1755 | else: |
---|
| 1756 | raise service_error(service_error.req, "Unknown lookup type") |
---|
| 1757 | else: |
---|
| 1758 | raise service_error(service_error.req, "No request?") |
---|
| 1759 | |
---|
| 1760 | return key |
---|
| 1761 | |
---|
| 1762 | def get_experiment_ids_and_start(self, key, tmpdir): |
---|
| 1763 | """ |
---|
| 1764 | Get the experiment name, id and access certificate from the state, and |
---|
| 1765 | set the experiment state to 'starting'. returns a triple (fedid, |
---|
| 1766 | localname, access_cert_file). The access_cert_file is a copy of the |
---|
| 1767 | contents of the access certificate, created in the tempdir with |
---|
| 1768 | restricted permissions. If things are confused, raise an exception. |
---|
| 1769 | """ |
---|
| 1770 | |
---|
| 1771 | expid = eid = None |
---|
| 1772 | self.state_lock.acquire() |
---|
[29d5f7c] | 1773 | if key in self.state: |
---|
| 1774 | exp = self.state[key] |
---|
| 1775 | exp.status = "starting" |
---|
[6e33086] | 1776 | exp.updated() |
---|
[29d5f7c] | 1777 | expid = exp.fedid |
---|
| 1778 | eid = exp.localname |
---|
| 1779 | expcert = exp.identity |
---|
[5ecb9a3] | 1780 | self.state_lock.release() |
---|
| 1781 | |
---|
| 1782 | # make a protected copy of the access certificate so the experiment |
---|
| 1783 | # controller can act as the experiment principal. |
---|
| 1784 | if expcert: |
---|
| 1785 | expcert_file = self.make_temp_certfile(expcert, tmpdir) |
---|
| 1786 | if not expcert_file: |
---|
| 1787 | raise service_error(service_error.internal, |
---|
| 1788 | "Cannot create temp cert file?") |
---|
| 1789 | else: |
---|
| 1790 | expcert_file = None |
---|
| 1791 | |
---|
| 1792 | return (eid, expid, expcert_file) |
---|
| 1793 | |
---|
| 1794 | def get_topology(self, req, tmpdir): |
---|
| 1795 | """ |
---|
| 1796 | Get the ns2 content and put it into a file for parsing. Call the local |
---|
| 1797 | or remote parser and return the topdl.Topology. Errors result in |
---|
| 1798 | exceptions. req is the request and tmpdir is a work directory. |
---|
| 1799 | """ |
---|
| 1800 | |
---|
| 1801 | # The tcl parser needs to read a file so put the content into that file |
---|
| 1802 | descr=req.get('experimentdescription', None) |
---|
| 1803 | if descr: |
---|
[a7c0bcb] | 1804 | if 'ns2description' in descr: |
---|
| 1805 | file_content=descr['ns2description'] |
---|
| 1806 | elif 'topdldescription' in descr: |
---|
| 1807 | return topdl.Topology(**descr['topdldescription']) |
---|
| 1808 | else: |
---|
| 1809 | raise service_error(service_error.req, |
---|
| 1810 | 'Unknown experiment description type') |
---|
[5ecb9a3] | 1811 | else: |
---|
| 1812 | raise service_error(service_error.req, "No experiment description") |
---|
| 1813 | |
---|
| 1814 | |
---|
| 1815 | if self.splitter_url: |
---|
| 1816 | self.log.debug("Calling remote topdl translator at %s" % \ |
---|
| 1817 | self.splitter_url) |
---|
| 1818 | top = self.remote_ns2topdl(self.splitter_url, file_content) |
---|
| 1819 | else: |
---|
| 1820 | tclfile = os.path.join(tmpdir, "experiment.tcl") |
---|
| 1821 | if file_content: |
---|
| 1822 | try: |
---|
| 1823 | f = open(tclfile, 'w') |
---|
| 1824 | f.write(file_content) |
---|
| 1825 | f.close() |
---|
| 1826 | except EnvironmentError: |
---|
| 1827 | raise service_error(service_error.internal, |
---|
| 1828 | "Cannot write temp experiment description") |
---|
| 1829 | else: |
---|
| 1830 | raise service_error(service_error.req, |
---|
| 1831 | "Only ns2descriptions supported") |
---|
| 1832 | pid = "dummy" |
---|
| 1833 | gid = "dummy" |
---|
| 1834 | eid = "dummy" |
---|
| 1835 | |
---|
| 1836 | tclcmd = [self.tclsh, self.tcl_splitter, '-t', '-x', |
---|
| 1837 | str(self.muxmax), '-m', 'dummy'] |
---|
| 1838 | |
---|
| 1839 | tclcmd.extend([pid, gid, eid, tclfile]) |
---|
| 1840 | |
---|
| 1841 | self.log.debug("running local splitter %s", " ".join(tclcmd)) |
---|
| 1842 | # This is just fantastic. As a side effect the parser copies |
---|
| 1843 | # tb_compat.tcl into the current directory, so that directory |
---|
| 1844 | # must be writable by the fedd user. Doing this in the |
---|
| 1845 | # temporary subdir ensures this is the case. |
---|
| 1846 | tclparser = Popen(tclcmd, stdout=PIPE, close_fds=True, |
---|
| 1847 | cwd=tmpdir) |
---|
| 1848 | split_data = tclparser.stdout |
---|
| 1849 | |
---|
| 1850 | top = topdl.topology_from_xml(file=split_data, top="experiment") |
---|
| 1851 | os.remove(tclfile) |
---|
| 1852 | |
---|
| 1853 | return top |
---|
| 1854 | |
---|
[1660f7c] | 1855 | def get_testbed_services(self, req, testbeds): |
---|
[5ecb9a3] | 1856 | """ |
---|
[57facae] | 1857 | Parse the services section of the request into two dicts mapping |
---|
| 1858 | testbed to lists of federated_service objects. The first dict maps all |
---|
| 1859 | exporters of services to those service objects, the second maps |
---|
| 1860 | testbeds to service objects only for services requiring portals. |
---|
[5ecb9a3] | 1861 | """ |
---|
[57facae] | 1862 | # We construct both dicts here because deriving the second is more |
---|
| 1863 | # comples than it looks - both the keys and lists can differ, and it's |
---|
| 1864 | # much easier to generate both in one pass. |
---|
[5ecb9a3] | 1865 | masters = { } |
---|
[57facae] | 1866 | pmasters = { } |
---|
[5ecb9a3] | 1867 | for s in req.get('service', []): |
---|
| 1868 | # If this is a service request with the importall field |
---|
| 1869 | # set, fill it out. |
---|
| 1870 | |
---|
| 1871 | if s.get('importall', False): |
---|
| 1872 | s['import'] = [ tb for tb in testbeds \ |
---|
| 1873 | if tb not in s.get('export',[])] |
---|
| 1874 | del s['importall'] |
---|
| 1875 | |
---|
| 1876 | # Add the service to masters |
---|
| 1877 | for tb in s.get('export', []): |
---|
| 1878 | if s.get('name', None): |
---|
| 1879 | |
---|
| 1880 | params = { } |
---|
| 1881 | for a in s.get('fedAttr', []): |
---|
| 1882 | params[a.get('attribute', '')] = a.get('value','') |
---|
| 1883 | |
---|
| 1884 | fser = federated_service(name=s['name'], |
---|
| 1885 | exporter=tb, importers=s.get('import',[]), |
---|
| 1886 | params=params) |
---|
| 1887 | if fser.name == 'hide_hosts' \ |
---|
| 1888 | and 'hosts' not in fser.params: |
---|
| 1889 | fser.params['hosts'] = \ |
---|
| 1890 | ",".join(tb_hosts.get(fser.exporter, [])) |
---|
| 1891 | if tb in masters: masters[tb].append(fser) |
---|
| 1892 | else: masters[tb] = [fser] |
---|
[57facae] | 1893 | |
---|
| 1894 | if fser.portal: |
---|
| 1895 | if tb in pmasters: pmasters[tb].append(fser) |
---|
| 1896 | else: pmasters[tb] = [fser] |
---|
[5ecb9a3] | 1897 | else: |
---|
| 1898 | self.log.error('Testbed service does not have name " + \ |
---|
| 1899 | "and importers') |
---|
[57facae] | 1900 | return masters, pmasters |
---|
[5ecb9a3] | 1901 | |
---|
[cf0ff4f] | 1902 | def generate_keys_and_hosts(self, tmpdir, expid, hosts, tbparams): |
---|
| 1903 | """ |
---|
[3df9b33] | 1904 | Create the ssh keys necessary for interconnecting the portal nodes and |
---|
[cf0ff4f] | 1905 | the global hosts file for letting each segment know about the IP |
---|
| 1906 | addresses in play. Save these into the repo. Add attributes to the |
---|
| 1907 | autorizer allowing access controllers to download them and return a set |
---|
[3df9b33] | 1908 | of attributes that inform the segments where to find this stuff. May |
---|
[cf0ff4f] | 1909 | raise service_errors in if there are problems. |
---|
| 1910 | """ |
---|
| 1911 | gw_pubkey_base = "fed.%s.pub" % self.ssh_type |
---|
| 1912 | gw_secretkey_base = "fed.%s" % self.ssh_type |
---|
[3df9b33] | 1913 | keydir = os.path.join(tmpdir, 'keys') |
---|
| 1914 | gw_pubkey = os.path.join(keydir, gw_pubkey_base) |
---|
| 1915 | gw_secretkey = os.path.join(keydir, gw_secretkey_base) |
---|
[cf0ff4f] | 1916 | |
---|
| 1917 | try: |
---|
| 1918 | self.generate_ssh_keys(gw_secretkey, self.ssh_type) |
---|
| 1919 | except ValueError: |
---|
| 1920 | raise service_error(service_error.server_config, |
---|
| 1921 | "Bad key type (%s)" % self.ssh_type) |
---|
| 1922 | |
---|
[3df9b33] | 1923 | self.generate_seer_certs(keydir) |
---|
[cf0ff4f] | 1924 | |
---|
| 1925 | # Copy configuration files into the remote file store |
---|
| 1926 | # The config urlpath |
---|
| 1927 | configpath = "/%s/config" % expid |
---|
| 1928 | # The config file system location |
---|
| 1929 | configdir ="%s%s" % ( self.repodir, configpath) |
---|
| 1930 | try: |
---|
| 1931 | os.makedirs(configdir) |
---|
| 1932 | except EnvironmentError, e: |
---|
| 1933 | raise service_error(service_error.internal, |
---|
| 1934 | "Cannot create config directory: %s" % e) |
---|
| 1935 | try: |
---|
| 1936 | f = open("%s/hosts" % configdir, "w") |
---|
| 1937 | print >> f, string.join(hosts, '\n') |
---|
| 1938 | f.close() |
---|
| 1939 | except EnvironmentError, e: |
---|
| 1940 | raise service_error(service_error.internal, |
---|
| 1941 | "Cannot write hosts file: %s" % e) |
---|
| 1942 | try: |
---|
[3df9b33] | 1943 | copy_file(gw_pubkey, os.path.join(configdir, gw_pubkey_base)) |
---|
| 1944 | copy_file(gw_secretkey, os.path.join(configdir, gw_secretkey_base)) |
---|
| 1945 | copy_file(os.path.join(keydir, 'ca.pem'), |
---|
| 1946 | os.path.join(configdir, 'ca.pem')) |
---|
| 1947 | copy_file(os.path.join(keydir, 'node.pem'), |
---|
| 1948 | os.path.join(configdir, 'node.pem')) |
---|
[cf0ff4f] | 1949 | except EnvironmentError, e: |
---|
| 1950 | raise service_error(service_error.internal, |
---|
| 1951 | "Cannot copy keyfiles: %s" % e) |
---|
| 1952 | |
---|
[913dc7a] | 1953 | # Allow the individual testbeds to access the configuration files, |
---|
| 1954 | # again by setting an attribute for the relevant pathnames on each |
---|
| 1955 | # allocation principal. Yeah, that's a long list comprehension. |
---|
| 1956 | self.append_experiment_authorization(expid, set([ |
---|
[9294673] | 1957 | (tbparams[tb].allocID, "%s/%s" % (configpath, f)) \ |
---|
[913dc7a] | 1958 | for tb in tbparams.keys() \ |
---|
[3df9b33] | 1959 | for f in ("hosts", 'ca.pem', 'node.pem', |
---|
| 1960 | gw_secretkey_base, gw_pubkey_base)])) |
---|
[cf0ff4f] | 1961 | |
---|
| 1962 | attrs = [ |
---|
| 1963 | { |
---|
| 1964 | 'attribute': 'ssh_pubkey', |
---|
| 1965 | 'value': '%s/%s/config/%s' % \ |
---|
| 1966 | (self.repo_url, expid, gw_pubkey_base) |
---|
| 1967 | }, |
---|
| 1968 | { |
---|
| 1969 | 'attribute': 'ssh_secretkey', |
---|
| 1970 | 'value': '%s/%s/config/%s' % \ |
---|
| 1971 | (self.repo_url, expid, gw_secretkey_base) |
---|
| 1972 | }, |
---|
| 1973 | { |
---|
| 1974 | 'attribute': 'hosts', |
---|
| 1975 | 'value': '%s/%s/config/hosts' % \ |
---|
| 1976 | (self.repo_url, expid) |
---|
| 1977 | }, |
---|
[3df9b33] | 1978 | { |
---|
| 1979 | 'attribute': 'seer_ca_pem', |
---|
| 1980 | 'value': '%s/%s/config/%s' % \ |
---|
| 1981 | (self.repo_url, expid, 'ca.pem') |
---|
| 1982 | }, |
---|
| 1983 | { |
---|
| 1984 | 'attribute': 'seer_node_pem', |
---|
| 1985 | 'value': '%s/%s/config/%s' % \ |
---|
| 1986 | (self.repo_url, expid, 'node.pem') |
---|
| 1987 | }, |
---|
[cf0ff4f] | 1988 | ] |
---|
| 1989 | return attrs |
---|
| 1990 | |
---|
| 1991 | |
---|
| 1992 | def get_vtopo(self, req, fid): |
---|
| 1993 | """ |
---|
| 1994 | Return the stored virtual topology for this experiment |
---|
| 1995 | """ |
---|
| 1996 | rv = None |
---|
| 1997 | state = None |
---|
[2bb8b35] | 1998 | self.log.info("vtopo call started for %s" % fid) |
---|
[cf0ff4f] | 1999 | |
---|
| 2000 | req = req.get('VtopoRequestBody', None) |
---|
| 2001 | if not req: |
---|
| 2002 | raise service_error(service_error.req, |
---|
| 2003 | "Bad request format (no VtopoRequestBody)") |
---|
| 2004 | exp = req.get('experiment', None) |
---|
| 2005 | if exp: |
---|
| 2006 | if exp.has_key('fedid'): |
---|
| 2007 | key = exp['fedid'] |
---|
| 2008 | keytype = "fedid" |
---|
| 2009 | elif exp.has_key('localname'): |
---|
| 2010 | key = exp['localname'] |
---|
| 2011 | keytype = "localname" |
---|
| 2012 | else: |
---|
| 2013 | raise service_error(service_error.req, "Unknown lookup type") |
---|
| 2014 | else: |
---|
| 2015 | raise service_error(service_error.req, "No request?") |
---|
| 2016 | |
---|
[8cab4c2] | 2017 | try: |
---|
| 2018 | proof = self.check_experiment_access(fid, key) |
---|
| 2019 | except service_error, e: |
---|
| 2020 | self.log.info("vtopo call failed for %s: access denied" % fid) |
---|
| 2021 | raise e |
---|
[cf0ff4f] | 2022 | |
---|
| 2023 | self.state_lock.acquire() |
---|
[29d5f7c] | 2024 | # XXX: this needs to be recalculated |
---|
[80b1e82] | 2025 | if key in self.state: |
---|
| 2026 | if self.state[key].top is not None: |
---|
| 2027 | vtopo = topdl.topology_to_vtopo(self.state[key].top) |
---|
[e83f2f2] | 2028 | rv = { 'experiment' : {keytype: key }, |
---|
[80b1e82] | 2029 | 'vtopo': vtopo, |
---|
[e83f2f2] | 2030 | 'proof': proof.to_dict(), |
---|
[cf0ff4f] | 2031 | } |
---|
| 2032 | else: |
---|
[80b1e82] | 2033 | state = self.state[key].status |
---|
[cf0ff4f] | 2034 | self.state_lock.release() |
---|
| 2035 | |
---|
[2bb8b35] | 2036 | if rv: |
---|
| 2037 | self.log.info("vtopo call completed for %s %s " % \ |
---|
| 2038 | (key, fid)) |
---|
| 2039 | return rv |
---|
[cf0ff4f] | 2040 | else: |
---|
| 2041 | if state: |
---|
[2bb8b35] | 2042 | self.log.info("vtopo call completed for %s %s (Not ready)" % \ |
---|
| 2043 | (key, fid)) |
---|
[cf0ff4f] | 2044 | raise service_error(service_error.partial, |
---|
| 2045 | "Not ready: %s" % state) |
---|
| 2046 | else: |
---|
[2bb8b35] | 2047 | self.log.info("vtopo call completed for %s %s (No experiment)"\ |
---|
| 2048 | % (key, fid)) |
---|
[cf0ff4f] | 2049 | raise service_error(service_error.req, "No such experiment") |
---|
| 2050 | |
---|
| 2051 | def get_vis(self, req, fid): |
---|
| 2052 | """ |
---|
| 2053 | Return the stored visualization for this experiment |
---|
| 2054 | """ |
---|
| 2055 | rv = None |
---|
| 2056 | state = None |
---|
| 2057 | |
---|
[2bb8b35] | 2058 | self.log.info("vis call started for %s" % fid) |
---|
[cf0ff4f] | 2059 | req = req.get('VisRequestBody', None) |
---|
| 2060 | if not req: |
---|
| 2061 | raise service_error(service_error.req, |
---|
| 2062 | "Bad request format (no VisRequestBody)") |
---|
| 2063 | exp = req.get('experiment', None) |
---|
| 2064 | if exp: |
---|
| 2065 | if exp.has_key('fedid'): |
---|
| 2066 | key = exp['fedid'] |
---|
| 2067 | keytype = "fedid" |
---|
| 2068 | elif exp.has_key('localname'): |
---|
| 2069 | key = exp['localname'] |
---|
| 2070 | keytype = "localname" |
---|
| 2071 | else: |
---|
| 2072 | raise service_error(service_error.req, "Unknown lookup type") |
---|
| 2073 | else: |
---|
| 2074 | raise service_error(service_error.req, "No request?") |
---|
| 2075 | |
---|
[8cab4c2] | 2076 | try: |
---|
| 2077 | proof = self.check_experiment_access(fid, key) |
---|
| 2078 | except service_error, e: |
---|
| 2079 | self.log.info("vis call failed for %s: access denied" % fid) |
---|
| 2080 | raise e |
---|
[cf0ff4f] | 2081 | |
---|
| 2082 | self.state_lock.acquire() |
---|
[80b1e82] | 2083 | # Generate the visualization |
---|
| 2084 | if key in self.state: |
---|
| 2085 | if self.state[key].top is not None: |
---|
| 2086 | try: |
---|
| 2087 | vis = self.genviz( |
---|
[6a50b78] | 2088 | topdl.topology_to_vtopo(self.state[key].top)) |
---|
[80b1e82] | 2089 | except service_error, e: |
---|
| 2090 | self.state_lock.release() |
---|
| 2091 | raise e |
---|
| 2092 | rv = { 'experiment' : {keytype: key }, |
---|
| 2093 | 'vis': vis, |
---|
[e83f2f2] | 2094 | 'proof': proof.to_dict(), |
---|
[80b1e82] | 2095 | } |
---|
[cf0ff4f] | 2096 | else: |
---|
[80b1e82] | 2097 | state = self.state[key].status |
---|
[cf0ff4f] | 2098 | self.state_lock.release() |
---|
| 2099 | |
---|
[2bb8b35] | 2100 | if rv: |
---|
| 2101 | self.log.info("vis call completed for %s %s " % \ |
---|
| 2102 | (key, fid)) |
---|
| 2103 | return rv |
---|
[cf0ff4f] | 2104 | else: |
---|
| 2105 | if state: |
---|
[2bb8b35] | 2106 | self.log.info("vis call completed for %s %s (not ready)" % \ |
---|
| 2107 | (key, fid)) |
---|
[cf0ff4f] | 2108 | raise service_error(service_error.partial, |
---|
| 2109 | "Not ready: %s" % state) |
---|
| 2110 | else: |
---|
[2bb8b35] | 2111 | self.log.info("vis call completed for %s %s (no experiment)" % \ |
---|
| 2112 | (key, fid)) |
---|
[cf0ff4f] | 2113 | raise service_error(service_error.req, "No such experiment") |
---|
| 2114 | |
---|
| 2115 | |
---|
[ec3aa4d] | 2116 | def save_federant_information(self, allocated, tbparams, eid, top): |
---|
[cf0ff4f] | 2117 | """ |
---|
| 2118 | Store the various data that have changed in the experiment state |
---|
| 2119 | between when it was started and the beginning of resource allocation. |
---|
| 2120 | This is basically the information about each local allocation. This |
---|
[e83f2f2] | 2121 | fills in the values of the placeholder allocation in the state. It |
---|
| 2122 | also collects the access proofs and returns them as dicts for a |
---|
| 2123 | response message. |
---|
[cf0ff4f] | 2124 | """ |
---|
[29d5f7c] | 2125 | self.state_lock.acquire() |
---|
| 2126 | exp = self.state[eid] |
---|
[ec3aa4d] | 2127 | exp.top = top.clone() |
---|
[cf0ff4f] | 2128 | # save federant information |
---|
| 2129 | for k in allocated.keys(): |
---|
[9294673] | 2130 | exp.add_allocation(tbparams[k]) |
---|
[ec3aa4d] | 2131 | top.elements.append(topdl.Testbed(uri=tbparams[k].uri, |
---|
[ab3d6c5] | 2132 | type="testbed", localname=[k], |
---|
| 2133 | service=[ s.to_topdl() for s in tbparams[k].services])) |
---|
[cf0ff4f] | 2134 | |
---|
[e83f2f2] | 2135 | # Access proofs for the response message |
---|
| 2136 | proofs = [copy.deepcopy(p) for k in tbparams.keys()\ |
---|
[9294673] | 2137 | for p in tbparams[k].proof] |
---|
[6e33086] | 2138 | exp.updated() |
---|
[cf0ff4f] | 2139 | if self.state_filename: |
---|
| 2140 | self.write_state() |
---|
| 2141 | self.state_lock.release() |
---|
[e83f2f2] | 2142 | return proofs |
---|
[cf0ff4f] | 2143 | |
---|
| 2144 | def clear_placeholder(self, eid, expid, tmpdir): |
---|
| 2145 | """ |
---|
| 2146 | Clear the placeholder and remove any allocated temporary dir. |
---|
| 2147 | """ |
---|
| 2148 | |
---|
| 2149 | self.state_lock.acquire() |
---|
| 2150 | del self.state[eid] |
---|
| 2151 | del self.state[expid] |
---|
| 2152 | if self.state_filename: self.write_state() |
---|
| 2153 | self.state_lock.release() |
---|
| 2154 | if tmpdir and self.cleanup: |
---|
| 2155 | self.remove_dirs(tmpdir) |
---|
| 2156 | |
---|
| 2157 | # end of create_experiment sub-functions |
---|
[5ecb9a3] | 2158 | |
---|
[e19b75c] | 2159 | def create_experiment(self, req, fid): |
---|
[db6b092] | 2160 | """ |
---|
| 2161 | The external interface to experiment creation called from the |
---|
| 2162 | dispatcher. |
---|
| 2163 | |
---|
| 2164 | Creates a working directory, splits the incoming description using the |
---|
[43197eb] | 2165 | splitter script and parses out the various subsections using the |
---|
[1a4ee0f] | 2166 | classes above. Once each sub-experiment is created, use pooled threads |
---|
| 2167 | to instantiate them and start it all up. |
---|
[db6b092] | 2168 | """ |
---|
[7183b48] | 2169 | |
---|
[2bb8b35] | 2170 | self.log.info("Create experiment call started for %s" % fid) |
---|
[7183b48] | 2171 | req = req.get('CreateRequestBody', None) |
---|
[5ecb9a3] | 2172 | if req: |
---|
[cf0ff4f] | 2173 | key = self.get_experiment_key(req) |
---|
[5ecb9a3] | 2174 | else: |
---|
[7183b48] | 2175 | raise service_error(service_error.req, |
---|
| 2176 | "Bad request format (no CreateRequestBody)") |
---|
| 2177 | |
---|
[6e63513] | 2178 | # Import information from the requester |
---|
| 2179 | if self.auth.import_credentials(data_list=req.get('credential', [])): |
---|
| 2180 | self.auth.save() |
---|
| 2181 | |
---|
[8cab4c2] | 2182 | try: |
---|
| 2183 | # Make sure that the caller can talk to us |
---|
| 2184 | proof = self.check_experiment_access(fid, key) |
---|
| 2185 | except service_error, e: |
---|
| 2186 | self.log.info("Create experiment call failed for %s: access denied"\ |
---|
| 2187 | % fid) |
---|
| 2188 | raise e |
---|
| 2189 | |
---|
[db6b092] | 2190 | |
---|
[fd07c48] | 2191 | # Install the testbed map entries supplied with the request into a copy |
---|
| 2192 | # of the testbed map. |
---|
| 2193 | tbmap = dict(self.tbmap) |
---|
[a11eda5] | 2194 | tbactive = set(self.tbactive) |
---|
[fd07c48] | 2195 | for m in req.get('testbedmap', []): |
---|
| 2196 | if 'testbed' in m and 'uri' in m: |
---|
| 2197 | tbmap[m['testbed']] = m['uri'] |
---|
[a11eda5] | 2198 | if 'active' in m and m['active']: tbactive.add(m['testbed']) |
---|
[fd07c48] | 2199 | |
---|
[5ecb9a3] | 2200 | # a place to work |
---|
[db6b092] | 2201 | try: |
---|
| 2202 | tmpdir = tempfile.mkdtemp(prefix="split-") |
---|
[895a133] | 2203 | os.mkdir(tmpdir+"/keys") |
---|
[d3c8759] | 2204 | except EnvironmentError: |
---|
[db6b092] | 2205 | raise service_error(service_error.internal, "Cannot create tmp dir") |
---|
| 2206 | |
---|
| 2207 | tbparams = { } |
---|
| 2208 | |
---|
[5ecb9a3] | 2209 | eid, expid, expcert_file = \ |
---|
| 2210 | self.get_experiment_ids_and_start(key, tmpdir) |
---|
[c573278] | 2211 | |
---|
[5ecb9a3] | 2212 | # This catches exceptions to clear the placeholder if necessary |
---|
[db6b092] | 2213 | try: |
---|
[5ecb9a3] | 2214 | if not (eid and expid): |
---|
| 2215 | raise service_error(service_error.internal, |
---|
| 2216 | "Cannot find local experiment info!?") |
---|
[5f6929a] | 2217 | |
---|
[5ecb9a3] | 2218 | top = self.get_topology(req, tmpdir) |
---|
[2627eb3] | 2219 | self.confirm_software(top) |
---|
[5ecb9a3] | 2220 | # Assign the IPs |
---|
[69692a9] | 2221 | hosts, ip_allocator = self.allocate_ips_to_topo(top) |
---|
[1a4ee0f] | 2222 | # Find the testbeds to look up |
---|
[5334044] | 2223 | tb_hosts = { } |
---|
[5ecb9a3] | 2224 | testbeds = [ ] |
---|
| 2225 | for e in top.elements: |
---|
| 2226 | if isinstance(e, topdl.Computer): |
---|
| 2227 | tb = e.get_attribute('testbed') or 'default' |
---|
| 2228 | if tb in tb_hosts: tb_hosts[tb].append(e.name) |
---|
| 2229 | else: |
---|
| 2230 | tb_hosts[tb] = [ e.name ] |
---|
| 2231 | testbeds.append(tb) |
---|
| 2232 | |
---|
[57facae] | 2233 | masters, pmasters = self.get_testbed_services(req, testbeds) |
---|
[895a133] | 2234 | allocated = { } # Testbeds we can access |
---|
| 2235 | topo ={ } # Sub topologies |
---|
[e02cd14] | 2236 | connInfo = { } # Connection information |
---|
[5334044] | 2237 | |
---|
[2627eb3] | 2238 | self.split_topology(top, topo, testbeds) |
---|
| 2239 | |
---|
[5ecb9a3] | 2240 | self.get_access_to_testbeds(testbeds, fid, allocated, |
---|
| 2241 | tbparams, masters, tbmap, expid, expcert_file) |
---|
[5f96438] | 2242 | |
---|
[cf0ff4f] | 2243 | attrs = self.generate_keys_and_hosts(tmpdir, expid, hosts, tbparams) |
---|
[cc8d8e9] | 2244 | |
---|
[fd07c48] | 2245 | part = experiment_partition(self.auth, self.store_url, tbmap, |
---|
[a11eda5] | 2246 | self.muxmax, self.direct_transit, tbactive) |
---|
[5334044] | 2247 | part.add_portals(top, topo, eid, pmasters, tbparams, ip_allocator, |
---|
[2761484] | 2248 | connInfo, expid) |
---|
[913dc7a] | 2249 | |
---|
| 2250 | auth_attrs = set() |
---|
[ab847bc] | 2251 | # Now get access to the dynamic testbeds (those added above) |
---|
| 2252 | for tb in [ t for t in topo if t not in allocated]: |
---|
[cf0ff4f] | 2253 | self.get_access(tb, tbparams, fid, masters, tbmap, |
---|
| 2254 | expid, expcert_file) |
---|
[ab847bc] | 2255 | allocated[tb] = 1 |
---|
| 2256 | store_keys = topo[tb].get_attribute('store_keys') |
---|
| 2257 | # Give the testbed access to keys it exports or imports |
---|
| 2258 | if store_keys: |
---|
[b16cfc0] | 2259 | auth_attrs.update(set([ |
---|
[9294673] | 2260 | (tbparams[tb].allocID, sk) \ |
---|
[913dc7a] | 2261 | for sk in store_keys.split(" ")])) |
---|
| 2262 | |
---|
| 2263 | if auth_attrs: |
---|
| 2264 | self.append_experiment_authorization(expid, auth_attrs) |
---|
[69692a9] | 2265 | |
---|
[cf0ff4f] | 2266 | # transit and disconnected testbeds may not have a connInfo entry. |
---|
| 2267 | # Fill in the blanks. |
---|
| 2268 | for t in allocated.keys(): |
---|
| 2269 | if not connInfo.has_key(t): |
---|
| 2270 | connInfo[t] = { } |
---|
| 2271 | |
---|
[895a133] | 2272 | self.wrangle_software(expid, top, topo, tbparams) |
---|
[cc8d8e9] | 2273 | |
---|
[e83f2f2] | 2274 | proofs = self.save_federant_information(allocated, tbparams, |
---|
[ec3aa4d] | 2275 | eid, top) |
---|
[866c983] | 2276 | except service_error, e: |
---|
| 2277 | # If something goes wrong in the parse (usually an access error) |
---|
| 2278 | # clear the placeholder state. From here on out the code delays |
---|
[db6b092] | 2279 | # exceptions. Failing at this point returns a fault to the remote |
---|
| 2280 | # caller. |
---|
[2bb8b35] | 2281 | |
---|
| 2282 | self.log.info("Create experiment call failed for %s %s: %s" % |
---|
| 2283 | (eid, fid, e)) |
---|
[cf0ff4f] | 2284 | self.clear_placeholder(eid, expid, tmpdir) |
---|
[866c983] | 2285 | raise e |
---|
| 2286 | |
---|
[db6b092] | 2287 | # Start the background swapper and return the starting state. From |
---|
| 2288 | # here on out, the state will stick around a while. |
---|
[866c983] | 2289 | |
---|
[db6b092] | 2290 | # Create a logger that logs to the experiment's state object as well as |
---|
| 2291 | # to the main log file. |
---|
| 2292 | alloc_log = logging.getLogger('fedd.experiment_control.%s' % eid) |
---|
[29d5f7c] | 2293 | alloc_collector = self.list_log(self.state[eid].log) |
---|
[f07fa49] | 2294 | h = logging.StreamHandler(alloc_collector) |
---|
[db6b092] | 2295 | # XXX: there should be a global one of these rather than repeating the |
---|
| 2296 | # code. |
---|
| 2297 | h.setFormatter(logging.Formatter("%(asctime)s %(name)s %(message)s", |
---|
| 2298 | '%d %b %y %H:%M:%S')) |
---|
| 2299 | alloc_log.addHandler(h) |
---|
[617592b] | 2300 | |
---|
[db6b092] | 2301 | # Start a thread to do the resource allocation |
---|
[e19b75c] | 2302 | t = Thread(target=self.allocate_resources, |
---|
[43197eb] | 2303 | args=(allocated, masters, eid, expid, tbparams, |
---|
[b4b19c7] | 2304 | top, topo, tmpdir, alloc_log, alloc_collector, attrs, |
---|
[725c55d] | 2305 | connInfo, tbmap, expcert_file), |
---|
[db6b092] | 2306 | name=eid) |
---|
| 2307 | t.start() |
---|
| 2308 | |
---|
| 2309 | rv = { |
---|
| 2310 | 'experimentID': [ |
---|
| 2311 | {'localname' : eid }, { 'fedid': copy.copy(expid) } |
---|
| 2312 | ], |
---|
| 2313 | 'experimentStatus': 'starting', |
---|
[e83f2f2] | 2314 | 'proof': [ proof.to_dict() ] + proofs, |
---|
[db6b092] | 2315 | } |
---|
[2bb8b35] | 2316 | self.log.info("Create experiment call succeeded for %s %s" % \ |
---|
| 2317 | (eid, fid)) |
---|
[db6b092] | 2318 | |
---|
| 2319 | return rv |
---|
[9479343] | 2320 | |
---|
| 2321 | def get_experiment_fedid(self, key): |
---|
| 2322 | """ |
---|
[db6b092] | 2323 | find the fedid associated with the localname key in the state database. |
---|
[9479343] | 2324 | """ |
---|
| 2325 | |
---|
[db6b092] | 2326 | rv = None |
---|
| 2327 | self.state_lock.acquire() |
---|
[29d5f7c] | 2328 | if key in self.state: |
---|
| 2329 | rv = self.state[key].fedid |
---|
[db6b092] | 2330 | self.state_lock.release() |
---|
| 2331 | return rv |
---|
[a97394b] | 2332 | |
---|
[4064742] | 2333 | def check_experiment_access(self, fid, key): |
---|
[866c983] | 2334 | """ |
---|
| 2335 | Confirm that the fid has access to the experiment. Though a request |
---|
| 2336 | may be made in terms of a local name, the access attribute is always |
---|
| 2337 | the experiment's fedid. |
---|
| 2338 | """ |
---|
| 2339 | if not isinstance(key, fedid): |
---|
[db6b092] | 2340 | key = self.get_experiment_fedid(key) |
---|
[866c983] | 2341 | |
---|
[e83f2f2] | 2342 | access_ok, proof = self.auth.check_attribute(fid, key, with_proof=True) |
---|
| 2343 | |
---|
| 2344 | if access_ok: |
---|
| 2345 | return proof |
---|
[866c983] | 2346 | else: |
---|
[e83f2f2] | 2347 | raise service_error(service_error.access, "Access Denied", |
---|
| 2348 | proof) |
---|
[4064742] | 2349 | |
---|
| 2350 | |
---|
[db6b092] | 2351 | def get_handler(self, path, fid): |
---|
[cf0ff4f] | 2352 | """ |
---|
| 2353 | Perhaps surprisingly named, this function handles HTTP GET requests to |
---|
| 2354 | this server (SOAP requests are POSTs). |
---|
| 2355 | """ |
---|
[7183b48] | 2356 | self.log.info("Get handler %s %s" % (path, fid)) |
---|
[e83f2f2] | 2357 | # XXX: log proofs? |
---|
[6c57fe9] | 2358 | if self.auth.check_attribute(fid, path): |
---|
| 2359 | return ("%s/%s" % (self.repodir, path), "application/binary") |
---|
| 2360 | else: |
---|
| 2361 | return (None, None) |
---|
[987aaa1] | 2362 | |
---|
[6e33086] | 2363 | def update_info(self, key, force=False): |
---|
| 2364 | top = None |
---|
| 2365 | self.state_lock.acquire() |
---|
| 2366 | if key in self.state: |
---|
| 2367 | if force or self.state[key].older_than(self.info_cache_limit): |
---|
[6a50b78] | 2368 | top = self.state[key].top |
---|
[6e33086] | 2369 | if top is not None: top = top.clone() |
---|
| 2370 | d1, info_params, cert, d2 = \ |
---|
| 2371 | self.get_segment_info(self.state[key], need_lock=False) |
---|
| 2372 | self.state_lock.release() |
---|
| 2373 | |
---|
| 2374 | if top is None: return |
---|
| 2375 | |
---|
| 2376 | try: |
---|
| 2377 | tmpdir = tempfile.mkdtemp(prefix="info-") |
---|
| 2378 | except EnvironmentError: |
---|
| 2379 | raise service_error(service_error.internal, |
---|
| 2380 | "Cannot create tmp dir") |
---|
| 2381 | cert_file = self.make_temp_certfile(cert, tmpdir) |
---|
| 2382 | |
---|
| 2383 | data = [] |
---|
| 2384 | try: |
---|
| 2385 | for k, (uri, aid) in info_params.items(): |
---|
| 2386 | info=self.info_segment(log=self.log, testbed=uri, |
---|
| 2387 | cert_file=cert_file, cert_pwd=None, |
---|
| 2388 | trusted_certs=self.trusted_certs, |
---|
| 2389 | caller=self.call_InfoSegment) |
---|
| 2390 | info(uri, aid) |
---|
| 2391 | data.append(info) |
---|
| 2392 | # Clean up the tmpdir no matter what |
---|
| 2393 | finally: |
---|
| 2394 | if tmpdir: self.remove_dirs(tmpdir) |
---|
| 2395 | |
---|
| 2396 | self.annotate_topology(top, data) |
---|
| 2397 | self.state_lock.acquire() |
---|
| 2398 | if key in self.state: |
---|
| 2399 | self.state[key].top = top |
---|
| 2400 | self.state[key].updated() |
---|
| 2401 | if self.state_filename: self.write_state() |
---|
| 2402 | self.state_lock.release() |
---|
| 2403 | |
---|
[29d5f7c] | 2404 | |
---|
[c52c48d] | 2405 | def get_info(self, req, fid): |
---|
[866c983] | 2406 | """ |
---|
| 2407 | Return all the stored info about this experiment |
---|
| 2408 | """ |
---|
| 2409 | rv = None |
---|
| 2410 | |
---|
[2bb8b35] | 2411 | self.log.info("Info call started for %s" % fid) |
---|
[866c983] | 2412 | req = req.get('InfoRequestBody', None) |
---|
| 2413 | if not req: |
---|
| 2414 | raise service_error(service_error.req, |
---|
[65f3f29] | 2415 | "Bad request format (no InfoRequestBody)") |
---|
[866c983] | 2416 | exp = req.get('experiment', None) |
---|
[80b1e82] | 2417 | legacy = req.get('legacy', False) |
---|
[6e33086] | 2418 | fresh = req.get('fresh', False) |
---|
[866c983] | 2419 | if exp: |
---|
| 2420 | if exp.has_key('fedid'): |
---|
| 2421 | key = exp['fedid'] |
---|
| 2422 | keytype = "fedid" |
---|
| 2423 | elif exp.has_key('localname'): |
---|
| 2424 | key = exp['localname'] |
---|
| 2425 | keytype = "localname" |
---|
| 2426 | else: |
---|
| 2427 | raise service_error(service_error.req, "Unknown lookup type") |
---|
| 2428 | else: |
---|
| 2429 | raise service_error(service_error.req, "No request?") |
---|
| 2430 | |
---|
[8cab4c2] | 2431 | try: |
---|
| 2432 | proof = self.check_experiment_access(fid, key) |
---|
| 2433 | except service_error, e: |
---|
| 2434 | self.log.info("Info call failed for %s: access denied" % fid) |
---|
| 2435 | |
---|
[866c983] | 2436 | |
---|
[6e33086] | 2437 | self.update_info(key, fresh) |
---|
| 2438 | |
---|
[866c983] | 2439 | self.state_lock.acquire() |
---|
| 2440 | if self.state.has_key(key): |
---|
[29d5f7c] | 2441 | rv = self.state[key].get_info() |
---|
[6e33086] | 2442 | # Copy the topo if we need legacy annotations |
---|
[80b1e82] | 2443 | if legacy: |
---|
[6a50b78] | 2444 | top = self.state[key].top |
---|
[80b1e82] | 2445 | if top is not None: top = top.clone() |
---|
[866c983] | 2446 | self.state_lock.release() |
---|
[2bb8b35] | 2447 | self.log.info("Gathered Info for %s %s" % (key, fid)) |
---|
[866c983] | 2448 | |
---|
[80b1e82] | 2449 | # If the legacy visualization and topology representations are |
---|
[6e33086] | 2450 | # requested, calculate them and add them to the return. |
---|
[80b1e82] | 2451 | if legacy and rv is not None: |
---|
[2bb8b35] | 2452 | self.log.info("Generating legacy Info for %s %s" % (key, fid)) |
---|
[80b1e82] | 2453 | if top is not None: |
---|
| 2454 | vtopo = topdl.topology_to_vtopo(top) |
---|
| 2455 | if vtopo is not None: |
---|
| 2456 | rv['vtopo'] = vtopo |
---|
| 2457 | try: |
---|
| 2458 | vis = self.genviz(vtopo) |
---|
| 2459 | except service_error, e: |
---|
| 2460 | self.log.debug('Problem generating visualization: %s' \ |
---|
| 2461 | % e) |
---|
| 2462 | vis = None |
---|
| 2463 | if vis is not None: |
---|
| 2464 | rv['vis'] = vis |
---|
[db6b092] | 2465 | if rv: |
---|
[2bb8b35] | 2466 | self.log.info("Info succeded for %s %s" % (key, fid)) |
---|
[29d5f7c] | 2467 | rv['proof'] = proof.to_dict() |
---|
| 2468 | return rv |
---|
[2bb8b35] | 2469 | else: |
---|
| 2470 | self.log.info("Info failed for %s %s: no experiment" % (key, fid)) |
---|
[db6b092] | 2471 | raise service_error(service_error.req, "No such experiment") |
---|
[7a8d667] | 2472 | |
---|
[22a1a77] | 2473 | def operate_on_segments(self, op_params, cert, op, testbeds, params, |
---|
| 2474 | results): |
---|
| 2475 | """ |
---|
| 2476 | Call OperateSegment on multiple testbeds and gather the results. |
---|
| 2477 | op_params contains the parameters needed to contact that testbed, cert |
---|
| 2478 | is a certificate containing the fedid to use, op is the operation, |
---|
| 2479 | testbeds is a dict mapping testbed name to targets in that testbed, |
---|
| 2480 | params are the parameters to include a,d results is a growing list of |
---|
| 2481 | the results of the calls. |
---|
| 2482 | """ |
---|
| 2483 | try: |
---|
| 2484 | tmpdir = tempfile.mkdtemp(prefix="info-") |
---|
| 2485 | except EnvironmentError: |
---|
| 2486 | raise service_error(service_error.internal, |
---|
| 2487 | "Cannot create tmp dir") |
---|
| 2488 | cert_file = self.make_temp_certfile(cert, tmpdir) |
---|
| 2489 | |
---|
| 2490 | try: |
---|
| 2491 | for tb, targets in testbeds.items(): |
---|
| 2492 | if tb in op_params: |
---|
| 2493 | uri, aid = op_params[tb] |
---|
| 2494 | operate=self.operation_segment(log=self.log, testbed=uri, |
---|
| 2495 | cert_file=cert_file, cert_pwd=None, |
---|
| 2496 | trusted_certs=self.trusted_certs, |
---|
| 2497 | caller=self.call_OperationSegment) |
---|
| 2498 | if operate(uri, aid, op, targets, params): |
---|
| 2499 | if operate.status is not None: |
---|
| 2500 | results.extend(operate.status) |
---|
| 2501 | continue |
---|
| 2502 | # Something went wrong in a weird way. Add statuses |
---|
| 2503 | # that reflect that to results |
---|
| 2504 | for t in targets: |
---|
| 2505 | results.append(operation_status(t, |
---|
| 2506 | operation_status.federant, |
---|
[b709861] | 2507 | 'Unexpected error on %s' % tb)) |
---|
[22a1a77] | 2508 | # Clean up the tmpdir no matter what |
---|
| 2509 | finally: |
---|
| 2510 | if tmpdir: self.remove_dirs(tmpdir) |
---|
| 2511 | |
---|
| 2512 | def do_operation(self, req, fid): |
---|
| 2513 | """ |
---|
| 2514 | Find the testbeds holding each target and ask them to carry out the |
---|
| 2515 | operation. Return the statuses. |
---|
| 2516 | """ |
---|
| 2517 | # Map an element to the testbed containing it |
---|
| 2518 | def element_to_tb(e): |
---|
| 2519 | if isinstance(e, topdl.Computer): return e.get_attribute("testbed") |
---|
| 2520 | elif isinstance(e, topdl.Testbed): return e.name |
---|
| 2521 | else: return None |
---|
| 2522 | # If d is an operation_status object, make it a dict |
---|
| 2523 | def make_dict(d): |
---|
| 2524 | if isinstance(d, dict): return d |
---|
| 2525 | elif isinstance(d, operation_status): return d.to_dict() |
---|
| 2526 | else: return { } |
---|
| 2527 | |
---|
[b709861] | 2528 | def element_name(e): |
---|
| 2529 | if isinstance(e, topdl.Computer): return e.name |
---|
| 2530 | elif isinstance(e, topdl.Testbed): |
---|
| 2531 | if e.localname: return e.localname[0] |
---|
| 2532 | else: return None |
---|
| 2533 | else: return None |
---|
| 2534 | |
---|
[8cab4c2] | 2535 | self.log.info("Operation call started for %s" % fid) |
---|
[22a1a77] | 2536 | req = req.get('OperationRequestBody', None) |
---|
| 2537 | if not req: |
---|
| 2538 | raise service_error(service_error.req, |
---|
| 2539 | "Bad request format (no OperationRequestBody)") |
---|
| 2540 | exp = req.get('experiment', None) |
---|
| 2541 | op = req.get('operation', None) |
---|
[b709861] | 2542 | targets = set(req.get('target', [])) |
---|
[22a1a77] | 2543 | params = req.get('parameter', None) |
---|
| 2544 | |
---|
| 2545 | if exp: |
---|
| 2546 | if 'fedid' in exp: |
---|
| 2547 | key = exp['fedid'] |
---|
| 2548 | keytype = "fedid" |
---|
| 2549 | elif 'localname' in exp: |
---|
| 2550 | key = exp['localname'] |
---|
| 2551 | keytype = "localname" |
---|
| 2552 | else: |
---|
| 2553 | raise service_error(service_error.req, "Unknown lookup type") |
---|
| 2554 | else: |
---|
| 2555 | raise service_error(service_error.req, "No request?") |
---|
| 2556 | |
---|
[b709861] | 2557 | if op is None or not targets: |
---|
[22a1a77] | 2558 | raise service_error(service_error.req, "No request?") |
---|
| 2559 | |
---|
[8cab4c2] | 2560 | try: |
---|
| 2561 | proof = self.check_experiment_access(fid, key) |
---|
| 2562 | except service_error, e: |
---|
| 2563 | self.log.info("Operation call failed for %s: access denied" % fid) |
---|
| 2564 | raise e |
---|
| 2565 | |
---|
[22a1a77] | 2566 | self.state_lock.acquire() |
---|
| 2567 | if key in self.state: |
---|
| 2568 | d1, op_params, cert, d2 = \ |
---|
[b709861] | 2569 | self.get_segment_info(self.state[key], need_lock=False, |
---|
| 2570 | key='tb') |
---|
[22a1a77] | 2571 | top = self.state[key].top |
---|
| 2572 | if top is not None: |
---|
| 2573 | top = top.clone() |
---|
| 2574 | self.state_lock.release() |
---|
| 2575 | |
---|
| 2576 | if top is None: |
---|
[8cab4c2] | 2577 | self.log.info("Operation call failed for %s: not active" % fid) |
---|
[22a1a77] | 2578 | raise service_error(service_error.partial, "No topology yet", |
---|
| 2579 | proof=proof) |
---|
| 2580 | |
---|
| 2581 | testbeds = { } |
---|
| 2582 | results = [] |
---|
| 2583 | for e in top.elements: |
---|
[b709861] | 2584 | ename = element_name(e) |
---|
| 2585 | if ename in targets: |
---|
[22a1a77] | 2586 | tb = element_to_tb(e) |
---|
[b709861] | 2587 | targets.remove(ename) |
---|
[22a1a77] | 2588 | if tb is not None: |
---|
[b709861] | 2589 | if tb in testbeds: testbeds[tb].append(ename) |
---|
| 2590 | else: testbeds[tb] = [ ename ] |
---|
[22a1a77] | 2591 | else: |
---|
| 2592 | results.append(operation_status(e.name, |
---|
| 2593 | code=operation_status.no_target, |
---|
| 2594 | description='Cannot map target to testbed')) |
---|
| 2595 | |
---|
[b709861] | 2596 | for t in targets: |
---|
| 2597 | results.append(operation_status(t, operation_status.no_target)) |
---|
| 2598 | |
---|
[22a1a77] | 2599 | self.operate_on_segments(op_params, cert, op, testbeds, params, |
---|
| 2600 | results) |
---|
| 2601 | |
---|
[8cab4c2] | 2602 | self.log.info("Operation call succeeded for %s" % fid) |
---|
[22a1a77] | 2603 | return { |
---|
| 2604 | 'experiment': exp, |
---|
[b709861] | 2605 | 'status': [make_dict(r) for r in results], |
---|
[22a1a77] | 2606 | 'proof': proof.to_dict() |
---|
| 2607 | } |
---|
| 2608 | |
---|
| 2609 | |
---|
[65f3f29] | 2610 | def get_multi_info(self, req, fid): |
---|
| 2611 | """ |
---|
| 2612 | Return all the stored info that this fedid can access |
---|
| 2613 | """ |
---|
[e83f2f2] | 2614 | rv = { 'info': [ ], 'proof': [ ] } |
---|
[65f3f29] | 2615 | |
---|
[2bb8b35] | 2616 | self.log.info("Multi Info call started for %s" % fid) |
---|
[db6b092] | 2617 | self.state_lock.acquire() |
---|
| 2618 | for key in [ k for k in self.state.keys() if isinstance(k, fedid)]: |
---|
[829246e] | 2619 | try: |
---|
[e83f2f2] | 2620 | proof = self.check_experiment_access(fid, key) |
---|
[829246e] | 2621 | except service_error, e: |
---|
| 2622 | if e.code == service_error.access: |
---|
| 2623 | continue |
---|
| 2624 | else: |
---|
[2bb8b35] | 2625 | self.log.info("Multi Info call failed for %s: %s" % \ |
---|
| 2626 | (e,fid)) |
---|
[829246e] | 2627 | self.state_lock.release() |
---|
| 2628 | raise e |
---|
[65f3f29] | 2629 | |
---|
[db6b092] | 2630 | if self.state.has_key(key): |
---|
[29d5f7c] | 2631 | e = self.state[key].get_info() |
---|
| 2632 | e['proof'] = proof.to_dict() |
---|
[db6b092] | 2633 | rv['info'].append(e) |
---|
[e83f2f2] | 2634 | rv['proof'].append(proof.to_dict()) |
---|
[65f3f29] | 2635 | self.state_lock.release() |
---|
[2bb8b35] | 2636 | self.log.info("Multi Info call succeeded for %s" % fid) |
---|
[db6b092] | 2637 | return rv |
---|
[65f3f29] | 2638 | |
---|
[cf0ff4f] | 2639 | def check_termination_status(self, fed_exp, force): |
---|
[e07c8f3] | 2640 | """ |
---|
[cf0ff4f] | 2641 | Confirm that the experiment is sin a valid state to stop (or force it) |
---|
| 2642 | return the state - invalid states for deletion and force settings cause |
---|
| 2643 | exceptions. |
---|
[e07c8f3] | 2644 | """ |
---|
[cf0ff4f] | 2645 | self.state_lock.acquire() |
---|
[29d5f7c] | 2646 | status = fed_exp.status |
---|
[e07c8f3] | 2647 | |
---|
[cf0ff4f] | 2648 | if status: |
---|
| 2649 | if status in ('starting', 'terminating'): |
---|
| 2650 | if not force: |
---|
| 2651 | self.state_lock.release() |
---|
| 2652 | raise service_error(service_error.partial, |
---|
| 2653 | 'Experiment still being created or destroyed') |
---|
| 2654 | else: |
---|
| 2655 | self.log.warning('Experiment in %s state ' % status + \ |
---|
| 2656 | 'being terminated by force.') |
---|
| 2657 | self.state_lock.release() |
---|
| 2658 | return status |
---|
[725c55d] | 2659 | else: |
---|
[cf0ff4f] | 2660 | # No status??? trouble |
---|
| 2661 | self.state_lock.release() |
---|
| 2662 | raise service_error(service_error.internal, |
---|
| 2663 | "Experiment has no status!?") |
---|
| 2664 | |
---|
[b709861] | 2665 | def get_segment_info(self, fed_exp, need_lock=True, key='aid'): |
---|
[cf0ff4f] | 2666 | ids = [] |
---|
| 2667 | term_params = { } |
---|
[6e33086] | 2668 | if need_lock: self.state_lock.acquire() |
---|
[29d5f7c] | 2669 | ids = [ x for x in (fed_exp.localname, fed_exp.fedid) if x is not None ] |
---|
| 2670 | expcert = fed_exp.identity |
---|
| 2671 | repo = "%s" % fed_exp.fedid |
---|
[cf0ff4f] | 2672 | |
---|
| 2673 | # Collect the allocation/segment ids into a dict keyed by the fedid |
---|
[29d5f7c] | 2674 | # of the allocation that contains a tuple of uri, aid |
---|
| 2675 | for i, fed in enumerate(fed_exp.get_all_allocations()): |
---|
| 2676 | uri = fed.uri |
---|
| 2677 | aid = fed.allocID |
---|
[b709861] | 2678 | if key == 'aid': term_params[aid] = (uri, aid) |
---|
| 2679 | elif key == 'tb': term_params[fed.tb] = (uri, aid) |
---|
| 2680 | |
---|
[6e33086] | 2681 | if need_lock: self.state_lock.release() |
---|
| 2682 | return ids, term_params, expcert, repo |
---|
| 2683 | |
---|
| 2684 | |
---|
| 2685 | def get_termination_info(self, fed_exp): |
---|
| 2686 | self.state_lock.acquire() |
---|
| 2687 | ids, term_params, expcert, repo = self.get_segment_info(fed_exp, False) |
---|
[cf0ff4f] | 2688 | # Change the experiment state |
---|
[29d5f7c] | 2689 | fed_exp.status = 'terminating' |
---|
[6e33086] | 2690 | fed_exp.updated() |
---|
[cf0ff4f] | 2691 | if self.state_filename: self.write_state() |
---|
| 2692 | self.state_lock.release() |
---|
| 2693 | |
---|
| 2694 | return ids, term_params, expcert, repo |
---|
| 2695 | |
---|
| 2696 | |
---|
| 2697 | def deallocate_resources(self, term_params, expcert, status, force, |
---|
| 2698 | dealloc_log): |
---|
| 2699 | tmpdir = None |
---|
| 2700 | # This try block makes sure the tempdir is cleared |
---|
| 2701 | try: |
---|
| 2702 | # If no expcert, try the deallocation as the experiment |
---|
| 2703 | # controller instance. |
---|
| 2704 | if expcert and self.auth_type != 'legacy': |
---|
| 2705 | try: |
---|
| 2706 | tmpdir = tempfile.mkdtemp(prefix="term-") |
---|
| 2707 | except EnvironmentError: |
---|
| 2708 | raise service_error(service_error.internal, |
---|
| 2709 | "Cannot create tmp dir") |
---|
| 2710 | cert_file = self.make_temp_certfile(expcert, tmpdir) |
---|
| 2711 | pw = None |
---|
| 2712 | else: |
---|
| 2713 | cert_file = self.cert_file |
---|
| 2714 | pw = self.cert_pwd |
---|
| 2715 | |
---|
| 2716 | # Stop everyone. NB, wait_for_all waits until a thread starts |
---|
| 2717 | # and then completes, so we can't wait if nothing starts. So, |
---|
| 2718 | # no tbparams, no start. |
---|
| 2719 | if len(term_params) > 0: |
---|
| 2720 | tp = thread_pool(self.nthreads) |
---|
| 2721 | for k, (uri, aid) in term_params.items(): |
---|
| 2722 | # Create and start a thread to stop the segment |
---|
| 2723 | tp.wait_for_slot() |
---|
| 2724 | t = pooled_thread(\ |
---|
| 2725 | target=self.terminate_segment(log=dealloc_log, |
---|
| 2726 | testbed=uri, |
---|
| 2727 | cert_file=cert_file, |
---|
| 2728 | cert_pwd=pw, |
---|
| 2729 | trusted_certs=self.trusted_certs, |
---|
| 2730 | caller=self.call_TerminateSegment), |
---|
| 2731 | args=(uri, aid), name=k, |
---|
| 2732 | pdata=tp, trace_file=self.trace_file) |
---|
| 2733 | t.start() |
---|
| 2734 | # Wait for completions |
---|
| 2735 | tp.wait_for_all_done() |
---|
| 2736 | |
---|
| 2737 | # release the allocations (failed experiments have done this |
---|
| 2738 | # already, and starting experiments may be in odd states, so we |
---|
| 2739 | # ignore errors releasing those allocations |
---|
| 2740 | try: |
---|
| 2741 | for k, (uri, aid) in term_params.items(): |
---|
| 2742 | self.release_access(None, aid, uri=uri, |
---|
| 2743 | cert_file=cert_file, cert_pwd=pw) |
---|
| 2744 | except service_error, e: |
---|
| 2745 | if status != 'failed' and not force: |
---|
| 2746 | raise e |
---|
| 2747 | |
---|
| 2748 | # Clean up the tmpdir no matter what |
---|
| 2749 | finally: |
---|
| 2750 | if tmpdir: self.remove_dirs(tmpdir) |
---|
| 2751 | |
---|
[7a8d667] | 2752 | def terminate_experiment(self, req, fid): |
---|
[866c983] | 2753 | """ |
---|
| 2754 | Swap this experiment out on the federants and delete the shared |
---|
| 2755 | information |
---|
| 2756 | """ |
---|
[2bb8b35] | 2757 | self.log.info("Terminate experiment call started for %s" % fid) |
---|
[866c983] | 2758 | tbparams = { } |
---|
| 2759 | req = req.get('TerminateRequestBody', None) |
---|
| 2760 | if not req: |
---|
| 2761 | raise service_error(service_error.req, |
---|
| 2762 | "Bad request format (no TerminateRequestBody)") |
---|
| 2763 | |
---|
[cf0ff4f] | 2764 | key = self.get_experiment_key(req, 'experiment') |
---|
[8cab4c2] | 2765 | try: |
---|
| 2766 | proof = self.check_experiment_access(fid, key) |
---|
| 2767 | except service_error, e: |
---|
| 2768 | self.log.info( |
---|
| 2769 | "Terminate experiment call failed for %s: access denied" \ |
---|
| 2770 | % fid) |
---|
| 2771 | raise e |
---|
[cf0ff4f] | 2772 | exp = req.get('experiment', False) |
---|
| 2773 | force = req.get('force', False) |
---|
[866c983] | 2774 | |
---|
[db6b092] | 2775 | dealloc_list = [ ] |
---|
[46e4682] | 2776 | |
---|
| 2777 | |
---|
[5ae3857] | 2778 | # Create a logger that logs to the dealloc_list as well as to the main |
---|
| 2779 | # log file. |
---|
| 2780 | dealloc_log = logging.getLogger('fedd.experiment_control.%s' % key) |
---|
[a69de97] | 2781 | dealloc_log.info("Terminating %s " %key) |
---|
[5ae3857] | 2782 | h = logging.StreamHandler(self.list_log(dealloc_list)) |
---|
| 2783 | # XXX: there should be a global one of these rather than repeating the |
---|
| 2784 | # code. |
---|
| 2785 | h.setFormatter(logging.Formatter("%(asctime)s %(name)s %(message)s", |
---|
| 2786 | '%d %b %y %H:%M:%S')) |
---|
| 2787 | dealloc_log.addHandler(h) |
---|
| 2788 | |
---|
| 2789 | self.state_lock.acquire() |
---|
| 2790 | fed_exp = self.state.get(key, None) |
---|
[cf0ff4f] | 2791 | self.state_lock.release() |
---|
[e07c8f3] | 2792 | repo = None |
---|
[5ae3857] | 2793 | |
---|
| 2794 | if fed_exp: |
---|
[cf0ff4f] | 2795 | status = self.check_termination_status(fed_exp, force) |
---|
[6e33086] | 2796 | # get_termination_info updates the experiment state |
---|
[cf0ff4f] | 2797 | ids, term_params, expcert, repo = self.get_termination_info(fed_exp) |
---|
| 2798 | self.deallocate_resources(term_params, expcert, status, force, |
---|
| 2799 | dealloc_log) |
---|
[5ae3857] | 2800 | |
---|
| 2801 | # Remove the terminated experiment |
---|
| 2802 | self.state_lock.acquire() |
---|
| 2803 | for id in ids: |
---|
[a96d946] | 2804 | self.clear_experiment_authorization(id, need_state_lock=False) |
---|
[cf0ff4f] | 2805 | if id in self.state: del self.state[id] |
---|
[5ae3857] | 2806 | |
---|
| 2807 | if self.state_filename: self.write_state() |
---|
| 2808 | self.state_lock.release() |
---|
| 2809 | |
---|
[2761484] | 2810 | # Delete any synch points associated with this experiment. All |
---|
| 2811 | # synch points begin with the fedid of the experiment. |
---|
| 2812 | fedid_keys = set(["fedid:%s" % f for f in ids \ |
---|
| 2813 | if isinstance(f, fedid)]) |
---|
| 2814 | for k in self.synch_store.all_keys(): |
---|
| 2815 | try: |
---|
| 2816 | if len(k) > 45 and k[0:46] in fedid_keys: |
---|
| 2817 | self.synch_store.del_value(k) |
---|
[dadc4da] | 2818 | except synch_store.BadDeletionError: |
---|
[2761484] | 2819 | pass |
---|
| 2820 | self.write_store() |
---|
[e07c8f3] | 2821 | |
---|
| 2822 | # Remove software and other cached stuff from the filesystem. |
---|
| 2823 | if repo: |
---|
| 2824 | self.remove_dirs("%s/%s" % (self.repodir, repo)) |
---|
[e83f2f2] | 2825 | |
---|
[2bb8b35] | 2826 | self.log.info("Terminate experiment succeeded for %s %s" % \ |
---|
| 2827 | (key, fid)) |
---|
[5ae3857] | 2828 | return { |
---|
| 2829 | 'experiment': exp , |
---|
[cf0ff4f] | 2830 | 'deallocationLog': string.join(dealloc_list, ''), |
---|
[e83f2f2] | 2831 | 'proof': [proof.to_dict()], |
---|
[5ae3857] | 2832 | } |
---|
| 2833 | else: |
---|
[2bb8b35] | 2834 | self.log.info("Terminate experiment failed for %s %s: no state" % \ |
---|
| 2835 | (key, fid)) |
---|
[5ae3857] | 2836 | raise service_error(service_error.req, "No saved state") |
---|
[2761484] | 2837 | |
---|
| 2838 | |
---|
| 2839 | def GetValue(self, req, fid): |
---|
| 2840 | """ |
---|
| 2841 | Get a value from the synchronized store |
---|
| 2842 | """ |
---|
| 2843 | req = req.get('GetValueRequestBody', None) |
---|
| 2844 | if not req: |
---|
| 2845 | raise service_error(service_error.req, |
---|
| 2846 | "Bad request format (no GetValueRequestBody)") |
---|
| 2847 | |
---|
[cf0ff4f] | 2848 | name = req.get('name', None) |
---|
| 2849 | wait = req.get('wait', False) |
---|
[2761484] | 2850 | rv = { 'name': name } |
---|
| 2851 | |
---|
[e83f2f2] | 2852 | if not name: |
---|
| 2853 | raise service_error(service_error.req, "No name?") |
---|
| 2854 | |
---|
| 2855 | access_ok, proof = self.auth.check_attribute(fid, name, with_proof=True) |
---|
| 2856 | |
---|
| 2857 | if access_ok: |
---|
[d8442da] | 2858 | self.log.debug("[GetValue] asking for %s " % name) |
---|
[dadc4da] | 2859 | try: |
---|
| 2860 | v = self.synch_store.get_value(name, wait) |
---|
| 2861 | except synch_store.RevokedKeyError: |
---|
| 2862 | # No more synch on this key |
---|
| 2863 | raise service_error(service_error.federant, |
---|
| 2864 | "Synch key %s revoked" % name) |
---|
[2761484] | 2865 | if v is not None: |
---|
| 2866 | rv['value'] = v |
---|
[e83f2f2] | 2867 | rv['proof'] = proof.to_dict() |
---|
[109a32a] | 2868 | self.log.debug("[GetValue] got %s from %s" % (v, name)) |
---|
[2761484] | 2869 | return rv |
---|
| 2870 | else: |
---|
[e83f2f2] | 2871 | raise service_error(service_error.access, "Access Denied", |
---|
| 2872 | proof=proof) |
---|
[2761484] | 2873 | |
---|
| 2874 | |
---|
| 2875 | def SetValue(self, req, fid): |
---|
| 2876 | """ |
---|
| 2877 | Set a value in the synchronized store |
---|
| 2878 | """ |
---|
| 2879 | req = req.get('SetValueRequestBody', None) |
---|
| 2880 | if not req: |
---|
| 2881 | raise service_error(service_error.req, |
---|
| 2882 | "Bad request format (no SetValueRequestBody)") |
---|
| 2883 | |
---|
[cf0ff4f] | 2884 | name = req.get('name', None) |
---|
| 2885 | v = req.get('value', '') |
---|
[2761484] | 2886 | |
---|
[e83f2f2] | 2887 | if not name: |
---|
| 2888 | raise service_error(service_error.req, "No name?") |
---|
| 2889 | |
---|
| 2890 | access_ok, proof = self.auth.check_attribute(fid, name, with_proof=True) |
---|
| 2891 | |
---|
| 2892 | if access_ok: |
---|
[2761484] | 2893 | try: |
---|
| 2894 | self.synch_store.set_value(name, v) |
---|
| 2895 | self.write_store() |
---|
[109a32a] | 2896 | self.log.debug("[SetValue] set %s to %s" % (name, v)) |
---|
[2761484] | 2897 | except synch_store.CollisionError: |
---|
| 2898 | # Translate into a service_error |
---|
| 2899 | raise service_error(service_error.req, |
---|
| 2900 | "Value already set: %s" %name) |
---|
[dadc4da] | 2901 | except synch_store.RevokedKeyError: |
---|
| 2902 | # No more synch on this key |
---|
| 2903 | raise service_error(service_error.federant, |
---|
| 2904 | "Synch key %s revoked" % name) |
---|
[e83f2f2] | 2905 | return { 'name': name, 'value': v, 'proof': proof.to_dict() } |
---|
[2761484] | 2906 | else: |
---|
[e83f2f2] | 2907 | raise service_error(service_error.access, "Access Denied", |
---|
| 2908 | proof=proof) |
---|