[4700b3b] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import os,sys |
---|
| 4 | import subprocess |
---|
| 5 | import tempfile |
---|
[c971895] | 6 | import logging |
---|
[3159f5d] | 7 | import topdl |
---|
[4700b3b] | 8 | |
---|
[ec4fb42] | 9 | from util import * |
---|
[51cc9df] | 10 | from fedid import fedid |
---|
[9460b1e] | 11 | from remote_service import xmlrpc_handler, soap_handler |
---|
[4700b3b] | 12 | from service_error import * |
---|
[05191a6] | 13 | from authorizer import authorizer |
---|
[4700b3b] | 14 | |
---|
| 15 | |
---|
| 16 | class nullHandler(logging.Handler): |
---|
| 17 | def emit(self, record): pass |
---|
| 18 | |
---|
[2c6128f] | 19 | fl = logging.getLogger("fedd.splitter") |
---|
[4700b3b] | 20 | fl.addHandler(nullHandler()) |
---|
| 21 | |
---|
[5f6929a] | 22 | class ns2topdl_local: |
---|
[3f6bc5f] | 23 | def __init__(self, config=None, auth=None): |
---|
[4700b3b] | 24 | """ |
---|
| 25 | Intialize the various attributes, most from the config object |
---|
| 26 | """ |
---|
[5f6929a] | 27 | self.debug = config.getboolean("ns2topdl", "debug") |
---|
| 28 | self.muxmax = config.getint("ns2topdl", "muxmax", 3) |
---|
| 29 | self.tclsh = config.get("ns2topdl", "tclsh", |
---|
[05191a6] | 30 | "/usr/local/bin/otclsh") |
---|
[5f6929a] | 31 | self.tcl_splitter = config.get("ns2topdl", "tcl_splitter", |
---|
[05191a6] | 32 | "/usr/testbed/lib/ns2ir/parse.tcl") |
---|
[5f6929a] | 33 | access_db = config.get("ns2topdl", "accessdb", None) |
---|
| 34 | allow_any = config.getboolean("ns2topdl", "allow_any", False) |
---|
[f4f4117] | 35 | |
---|
[5f6929a] | 36 | self.log = logging.getLogger("fedd.ns2topdl") |
---|
| 37 | set_log_level(config, "ns2topdl", self.log) |
---|
[4700b3b] | 38 | self.trace_file = sys.stderr |
---|
| 39 | |
---|
[5f6929a] | 40 | set_log_level(config, "ns2topdl", self.log) |
---|
[05191a6] | 41 | |
---|
| 42 | if auth: |
---|
| 43 | self.auth= auth |
---|
| 44 | else: |
---|
| 45 | self.auth = authorizer() |
---|
[5f6929a] | 46 | self.log.warning("[ns2topdl] No authorizer passed in, " +\ |
---|
[05191a6] | 47 | "using local one") |
---|
| 48 | |
---|
| 49 | if access_db and allow_any: |
---|
| 50 | raise service_error(service_error.internal, |
---|
| 51 | "Cannot specify both an access database and allow_any " +\ |
---|
[5f6929a] | 52 | "for ns2topdl") |
---|
[05191a6] | 53 | |
---|
| 54 | if access_db: |
---|
| 55 | try: |
---|
[5f6929a] | 56 | read_simple_accessdb(access_db, self.auth, 'ns2topdl') |
---|
[d3c8759] | 57 | except EnvironmentError, e: |
---|
[05191a6] | 58 | raise service_error(service_error.internal, |
---|
| 59 | "Error reading accessDB %s: %s" % (access_db, e)) |
---|
| 60 | except ValueError: |
---|
| 61 | raise service_error(service_error.internal, "%s" % e) |
---|
| 62 | elif allow_any: |
---|
[5f6929a] | 63 | auth.set_global_attribute("ns2topdl") |
---|
[05191a6] | 64 | |
---|
| 65 | |
---|
[4700b3b] | 66 | # Dispatch tables |
---|
| 67 | self.soap_services = {\ |
---|
[5f6929a] | 68 | 'Ns2Topdl': soap_handler("Ns2Topdl", self.run_ns2topdl), |
---|
[4700b3b] | 69 | } |
---|
| 70 | |
---|
| 71 | self.xmlrpc_services = {\ |
---|
[5f6929a] | 72 | 'Ns2Topdl': xmlrpc_handler('Ns2Topdl', self.run_ns2topdl), |
---|
[4700b3b] | 73 | } |
---|
| 74 | |
---|
[5f6929a] | 75 | def run_ns2topdl(self, req, fid): |
---|
[4700b3b] | 76 | """ |
---|
[5f6929a] | 77 | The external interface to tcl to topdl translation. |
---|
[4700b3b] | 78 | |
---|
[5f6929a] | 79 | Creates a working directory, translates the incoming description using |
---|
| 80 | the tcl script. |
---|
[4700b3b] | 81 | """ |
---|
[05191a6] | 82 | |
---|
[5f6929a] | 83 | if not self.auth.check_attribute(fid, 'ns2topdl'): |
---|
[05191a6] | 84 | raise service_error(service_error.access, "Access Denied") |
---|
| 85 | |
---|
[4700b3b] | 86 | try: |
---|
[5f6929a] | 87 | tmpdir = tempfile.mkdtemp(prefix="ns2topdl-") |
---|
[d3c8759] | 88 | except EnvironmentError: |
---|
[4700b3b] | 89 | raise service_error(service_error.internal, "Cannot create tmp dir") |
---|
| 90 | |
---|
| 91 | tclfile = tmpdir + "/experiment.tcl" |
---|
| 92 | pid = "dummy" |
---|
| 93 | gid = "dummy" |
---|
[f4f4117] | 94 | eid = "dummy" |
---|
[5f6929a] | 95 | master = "dummy" |
---|
[4700b3b] | 96 | |
---|
[5f6929a] | 97 | req = req.get('Ns2TopdlRequestBody', None) |
---|
[4700b3b] | 98 | if not req: |
---|
| 99 | raise service_error(service_error.req, |
---|
[5f6929a] | 100 | "Bad request format (no Ns2TopdlRequestBody)") |
---|
[4700b3b] | 101 | # The tcl parser needs to read a file so put the content into that file |
---|
[f4f4117] | 102 | descr=req.get('description', None) |
---|
[4700b3b] | 103 | if descr: |
---|
| 104 | file_content=descr.get('ns2description', None) |
---|
| 105 | if file_content: |
---|
| 106 | try: |
---|
| 107 | f = open(tclfile, 'w') |
---|
| 108 | f.write(file_content) |
---|
| 109 | f.close() |
---|
[d3c8759] | 110 | except EnvironmentError: |
---|
[4700b3b] | 111 | raise service_error(service_error.internal, |
---|
| 112 | "Cannot write temp experiment description") |
---|
| 113 | else: |
---|
| 114 | raise service_error(service_error.req, |
---|
| 115 | "Only ns2descriptions supported") |
---|
| 116 | else: |
---|
[f4f4117] | 117 | raise service_error(service_error.req, "No description") |
---|
[4700b3b] | 118 | |
---|
| 119 | |
---|
[3159f5d] | 120 | tclcmd = [self.tclsh, self.tcl_splitter, '-t', '-x', |
---|
[2c6128f] | 121 | str(self.muxmax), '-m', master] |
---|
| 122 | tclcmd.extend([pid, gid, eid, tclfile]) |
---|
| 123 | self.log.debug("Calling splitter %s" % " ".join(tclcmd)) |
---|
[c971895] | 124 | tclparser = subprocess.Popen(tclcmd, stdout=subprocess.PIPE) |
---|
[4700b3b] | 125 | |
---|
[3159f5d] | 126 | top = topdl.topology_from_xml(file=tclparser.stdout, top="experiment") |
---|
[4700b3b] | 127 | |
---|
| 128 | # Walk up tmpdir, deleting as we go |
---|
| 129 | for path, dirs, files in os.walk(tmpdir, topdown=False): |
---|
| 130 | for f in files: |
---|
| 131 | os.remove(os.path.join(path, f)) |
---|
| 132 | for d in dirs: |
---|
| 133 | os.rmdir(os.path.join(path, d)) |
---|
| 134 | os.rmdir(tmpdir) |
---|
| 135 | |
---|
[3159f5d] | 136 | resp = { |
---|
| 137 | 'experimentdescription': { |
---|
| 138 | 'topdldescription': top.to_dict(), |
---|
| 139 | } |
---|
| 140 | } |
---|
[4700b3b] | 141 | |
---|
| 142 | return resp |
---|
| 143 | |
---|
| 144 | |
---|