[4700b3b] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import os,sys |
---|
| 4 | import subprocess |
---|
| 5 | import tempfile |
---|
[c971895] | 6 | import logging |
---|
[4700b3b] | 7 | |
---|
| 8 | from fedd_services import * |
---|
| 9 | from fedd_internal_services import * |
---|
| 10 | from fedd_util import * |
---|
[51cc9df] | 11 | from fedid import fedid |
---|
[9460b1e] | 12 | from remote_service import xmlrpc_handler, soap_handler |
---|
[4700b3b] | 13 | import parse_detail |
---|
| 14 | from service_error import * |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | class nullHandler(logging.Handler): |
---|
| 18 | def emit(self, record): pass |
---|
| 19 | |
---|
[2c6128f] | 20 | fl = logging.getLogger("fedd.splitter") |
---|
[4700b3b] | 21 | fl.addHandler(nullHandler()) |
---|
| 22 | |
---|
| 23 | class fedd_split_local: |
---|
[3f6bc5f] | 24 | def __init__(self, config=None, auth=None): |
---|
[4700b3b] | 25 | """ |
---|
| 26 | Intialize the various attributes, most from the config object |
---|
| 27 | """ |
---|
[f4f4117] | 28 | if config.has_section("splitter"): |
---|
| 29 | self.debug = config.getboolean("splitter", "debug") |
---|
| 30 | self.muxmax = config.getint("splitter", "muxmax", 3) |
---|
[2c6128f] | 31 | self.tclsh = config.get("splitter", "tclsh", |
---|
| 32 | "/usr/local/bin/otclsh") |
---|
| 33 | self.tcl_splitter = config.get("splitter", "tcl_splitter", |
---|
| 34 | "/usr/testbed/lib/ns2ir/parse.tcl") |
---|
[f4f4117] | 35 | else: |
---|
| 36 | self.debug = False |
---|
| 37 | self.muxmax = 3 |
---|
[2c6128f] | 38 | self.tclsh = "/usr/local/bin/otclsh" |
---|
| 39 | self.tcl_splitter = "/usr/testbed/lib/ns2ir/parse.tcl" |
---|
[f4f4117] | 40 | |
---|
[4700b3b] | 41 | self.log = logging.getLogger("fedd.splitter") |
---|
[c971895] | 42 | set_log_level(config, "splitter", self.log) |
---|
[4700b3b] | 43 | self.trace_file = sys.stderr |
---|
| 44 | |
---|
[2c6128f] | 45 | set_log_level(config, "splitter", self.log) |
---|
[4700b3b] | 46 | # Dispatch tables |
---|
| 47 | self.soap_services = {\ |
---|
[058f58e] | 48 | 'Ns2Split': soap_handler(\ |
---|
[4700b3b] | 49 | Ns2SplitRequestMessage.typecode, |
---|
| 50 | getattr(self, "run_splitter"), |
---|
| 51 | Ns2SplitResponseMessage, |
---|
| 52 | "Ns2SplitResponseBody"), |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | self.xmlrpc_services = {\ |
---|
[058f58e] | 56 | 'Ns2Split': xmlrpc_handler(\ |
---|
[4700b3b] | 57 | getattr(self, "run_splitter"), |
---|
| 58 | "Ns2SplitResponseBody"), |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | def run_splitter(self, req, fid): |
---|
| 62 | """ |
---|
| 63 | The external interface to experiment creation called from the |
---|
| 64 | dispatcher. |
---|
| 65 | |
---|
| 66 | Creates a working directory, splits the incoming description using the |
---|
| 67 | splitter script and parses out the avrious subsections using the |
---|
| 68 | lcasses above. Once each sub-experiment is created, use pooled threads |
---|
| 69 | to instantiate them and start it all up. |
---|
| 70 | """ |
---|
| 71 | try: |
---|
| 72 | tmpdir = tempfile.mkdtemp(prefix="split-") |
---|
| 73 | except IOError: |
---|
| 74 | raise service_error(service_error.internal, "Cannot create tmp dir") |
---|
| 75 | |
---|
| 76 | tclfile = tmpdir + "/experiment.tcl" |
---|
| 77 | pid = "dummy" |
---|
| 78 | gid = "dummy" |
---|
[f4f4117] | 79 | eid = "dummy" |
---|
[4700b3b] | 80 | |
---|
| 81 | req = req.get('Ns2SplitRequestBody', None) |
---|
| 82 | if not req: |
---|
| 83 | raise service_error(service_error.req, |
---|
| 84 | "Bad request format (no Ns2SplitRequestBody)") |
---|
| 85 | # The tcl parser needs to read a file so put the content into that file |
---|
[f4f4117] | 86 | descr=req.get('description', None) |
---|
[4700b3b] | 87 | if descr: |
---|
| 88 | file_content=descr.get('ns2description', None) |
---|
| 89 | if file_content: |
---|
| 90 | try: |
---|
| 91 | f = open(tclfile, 'w') |
---|
| 92 | f.write(file_content) |
---|
| 93 | f.close() |
---|
| 94 | except IOError: |
---|
| 95 | raise service_error(service_error.internal, |
---|
| 96 | "Cannot write temp experiment description") |
---|
| 97 | else: |
---|
| 98 | raise service_error(service_error.req, |
---|
| 99 | "Only ns2descriptions supported") |
---|
| 100 | else: |
---|
[f4f4117] | 101 | raise service_error(service_error.req, "No description") |
---|
[4700b3b] | 102 | |
---|
| 103 | master = req.get('master', None) |
---|
| 104 | if master == None: |
---|
| 105 | raise service_error(service_error.req, "No master testbed label") |
---|
| 106 | |
---|
[2c6128f] | 107 | include_fedkit = req.get('include_fedkit', None) |
---|
| 108 | if include_fedkit == None: |
---|
| 109 | raise service_error(service_error.req, |
---|
| 110 | "No federation kit inclusion directive") |
---|
| 111 | |
---|
[4700b3b] | 112 | |
---|
| 113 | tclcmd = [self.tclsh, self.tcl_splitter, '-s', '-x', |
---|
[2c6128f] | 114 | str(self.muxmax), '-m', master] |
---|
| 115 | if include_fedkit: tclcmd.append('-k') |
---|
| 116 | tclcmd.extend([pid, gid, eid, tclfile]) |
---|
| 117 | self.log.debug("Calling splitter %s" % " ".join(tclcmd)) |
---|
[c971895] | 118 | tclparser = subprocess.Popen(tclcmd, stdout=subprocess.PIPE) |
---|
[4700b3b] | 119 | |
---|
[f4f4117] | 120 | out = "" |
---|
[4700b3b] | 121 | # Package up the split data |
---|
| 122 | for line in tclparser.stdout: |
---|
| 123 | out += line |
---|
| 124 | |
---|
| 125 | # Walk up tmpdir, deleting as we go |
---|
| 126 | for path, dirs, files in os.walk(tmpdir, topdown=False): |
---|
| 127 | for f in files: |
---|
| 128 | os.remove(os.path.join(path, f)) |
---|
| 129 | for d in dirs: |
---|
| 130 | os.rmdir(os.path.join(path, d)) |
---|
| 131 | os.rmdir(tmpdir) |
---|
| 132 | |
---|
| 133 | resp = { 'output' : out } |
---|
| 134 | |
---|
| 135 | return resp |
---|
| 136 | |
---|
| 137 | |
---|