1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | import os,sys |
---|
4 | import subprocess |
---|
5 | import tempfile |
---|
6 | import logging |
---|
7 | import topdl |
---|
8 | |
---|
9 | from util import * |
---|
10 | from fedid import fedid |
---|
11 | from remote_service import xmlrpc_handler, soap_handler |
---|
12 | from service_error import * |
---|
13 | from authorizer import authorizer |
---|
14 | |
---|
15 | |
---|
16 | class nullHandler(logging.Handler): |
---|
17 | def emit(self, record): pass |
---|
18 | |
---|
19 | fl = logging.getLogger("fedd.splitter") |
---|
20 | fl.addHandler(nullHandler()) |
---|
21 | |
---|
22 | class ns2topdl_local: |
---|
23 | def __init__(self, config=None, auth=None): |
---|
24 | """ |
---|
25 | Intialize the various attributes, most from the config object |
---|
26 | """ |
---|
27 | self.debug = config.getboolean("ns2topdl", "debug") |
---|
28 | self.muxmax = config.getint("ns2topdl", "muxmax", 3) |
---|
29 | self.tclsh = config.get("ns2topdl", "tclsh", |
---|
30 | "/usr/local/bin/otclsh") |
---|
31 | self.tcl_splitter = config.get("ns2topdl", "tcl_splitter", |
---|
32 | "/usr/testbed/lib/ns2ir/parse.tcl") |
---|
33 | access_db = config.get("ns2topdl", "accessdb", None) |
---|
34 | allow_any = config.getboolean("ns2topdl", "allow_any", False) |
---|
35 | |
---|
36 | self.log = logging.getLogger("fedd.ns2topdl") |
---|
37 | set_log_level(config, "ns2topdl", self.log) |
---|
38 | self.trace_file = sys.stderr |
---|
39 | |
---|
40 | set_log_level(config, "ns2topdl", self.log) |
---|
41 | |
---|
42 | if auth: |
---|
43 | self.auth= auth |
---|
44 | else: |
---|
45 | self.auth = authorizer() |
---|
46 | self.log.warning("[ns2topdl] No authorizer passed in, " +\ |
---|
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 " +\ |
---|
52 | "for ns2topdl") |
---|
53 | |
---|
54 | if access_db: |
---|
55 | try: |
---|
56 | read_simple_accessdb(access_db, self.auth, 'ns2topdl') |
---|
57 | except IOError, e: |
---|
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: |
---|
63 | auth.set_global_attribute("ns2topdl") |
---|
64 | |
---|
65 | |
---|
66 | # Dispatch tables |
---|
67 | self.soap_services = {\ |
---|
68 | 'Ns2Topdl': soap_handler("Ns2Topdl", self.run_ns2topdl), |
---|
69 | } |
---|
70 | |
---|
71 | self.xmlrpc_services = {\ |
---|
72 | 'Ns2Topdl': xmlrpc_handler('Ns2Topdl', self.run_ns2topdl), |
---|
73 | } |
---|
74 | |
---|
75 | def run_ns2topdl(self, req, fid): |
---|
76 | """ |
---|
77 | The external interface to tcl to topdl translation. |
---|
78 | |
---|
79 | Creates a working directory, translates the incoming description using |
---|
80 | the tcl script. |
---|
81 | """ |
---|
82 | |
---|
83 | if not self.auth.check_attribute(fid, 'ns2topdl'): |
---|
84 | raise service_error(service_error.access, "Access Denied") |
---|
85 | |
---|
86 | try: |
---|
87 | tmpdir = tempfile.mkdtemp(prefix="ns2topdl-") |
---|
88 | except IOError: |
---|
89 | raise service_error(service_error.internal, "Cannot create tmp dir") |
---|
90 | |
---|
91 | tclfile = tmpdir + "/experiment.tcl" |
---|
92 | pid = "dummy" |
---|
93 | gid = "dummy" |
---|
94 | eid = "dummy" |
---|
95 | master = "dummy" |
---|
96 | |
---|
97 | req = req.get('Ns2TopdlRequestBody', None) |
---|
98 | if not req: |
---|
99 | raise service_error(service_error.req, |
---|
100 | "Bad request format (no Ns2TopdlRequestBody)") |
---|
101 | # The tcl parser needs to read a file so put the content into that file |
---|
102 | descr=req.get('description', None) |
---|
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() |
---|
110 | except IOError: |
---|
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: |
---|
117 | raise service_error(service_error.req, "No description") |
---|
118 | |
---|
119 | |
---|
120 | tclcmd = [self.tclsh, self.tcl_splitter, '-t', '-x', |
---|
121 | str(self.muxmax), '-m', master] |
---|
122 | tclcmd.extend([pid, gid, eid, tclfile]) |
---|
123 | self.log.debug("Calling splitter %s" % " ".join(tclcmd)) |
---|
124 | tclparser = subprocess.Popen(tclcmd, stdout=subprocess.PIPE) |
---|
125 | |
---|
126 | top = topdl.topology_from_xml(file=tclparser.stdout, top="experiment") |
---|
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 | |
---|
136 | resp = { |
---|
137 | 'experimentdescription': { |
---|
138 | 'topdldescription': top.to_dict(), |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | return resp |
---|
143 | |
---|
144 | |
---|