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 split_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("splitter", "debug") |
---|
28 | self.muxmax = config.getint("splitter", "muxmax", 3) |
---|
29 | self.tclsh = config.get("splitter", "tclsh", |
---|
30 | "/usr/local/bin/otclsh") |
---|
31 | self.tcl_splitter = config.get("splitter", "tcl_splitter", |
---|
32 | "/usr/testbed/lib/ns2ir/parse.tcl") |
---|
33 | access_db = config.get("splitter", "accessdb", None) |
---|
34 | allow_any = config.getboolean("splitter", "allow_any", False) |
---|
35 | |
---|
36 | self.log = logging.getLogger("fedd.splitter") |
---|
37 | set_log_level(config, "splitter", self.log) |
---|
38 | self.trace_file = sys.stderr |
---|
39 | |
---|
40 | set_log_level(config, "splitter", self.log) |
---|
41 | |
---|
42 | if auth: |
---|
43 | self.auth= auth |
---|
44 | else: |
---|
45 | self.auth = authorizer() |
---|
46 | self.log.warning("[splitter] 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 splitter") |
---|
53 | |
---|
54 | if access_db: |
---|
55 | try: |
---|
56 | read_simple_accessdb(access_db, self.auth, 'split') |
---|
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("split") |
---|
64 | |
---|
65 | |
---|
66 | # Dispatch tables |
---|
67 | self.soap_services = {\ |
---|
68 | 'Ns2Split': soap_handler("Ns2Split", self.run_splitter), |
---|
69 | } |
---|
70 | |
---|
71 | self.xmlrpc_services = {\ |
---|
72 | 'Ns2Split': xmlrpc_handler('Ns2Split', self.run_splitter), |
---|
73 | } |
---|
74 | |
---|
75 | def run_splitter(self, req, fid): |
---|
76 | """ |
---|
77 | The external interface to experiment creation called from the |
---|
78 | dispatcher. |
---|
79 | |
---|
80 | Creates a working directory, splits the incoming description using the |
---|
81 | splitter script and parses out the avrious subsections using the |
---|
82 | lcasses above. Once each sub-experiment is created, use pooled threads |
---|
83 | to instantiate them and start it all up. |
---|
84 | """ |
---|
85 | |
---|
86 | if not self.auth.check_attribute(fid, 'split'): |
---|
87 | raise service_error(service_error.access, "Access Denied") |
---|
88 | |
---|
89 | try: |
---|
90 | tmpdir = tempfile.mkdtemp(prefix="split-") |
---|
91 | except IOError: |
---|
92 | raise service_error(service_error.internal, "Cannot create tmp dir") |
---|
93 | |
---|
94 | tclfile = tmpdir + "/experiment.tcl" |
---|
95 | pid = "dummy" |
---|
96 | gid = "dummy" |
---|
97 | eid = "dummy" |
---|
98 | |
---|
99 | req = req.get('Ns2SplitRequestBody', None) |
---|
100 | if not req: |
---|
101 | raise service_error(service_error.req, |
---|
102 | "Bad request format (no Ns2SplitRequestBody)") |
---|
103 | # The tcl parser needs to read a file so put the content into that file |
---|
104 | descr=req.get('description', None) |
---|
105 | if descr: |
---|
106 | file_content=descr.get('ns2description', None) |
---|
107 | if file_content: |
---|
108 | try: |
---|
109 | f = open(tclfile, 'w') |
---|
110 | f.write(file_content) |
---|
111 | f.close() |
---|
112 | except IOError: |
---|
113 | raise service_error(service_error.internal, |
---|
114 | "Cannot write temp experiment description") |
---|
115 | else: |
---|
116 | raise service_error(service_error.req, |
---|
117 | "Only ns2descriptions supported") |
---|
118 | else: |
---|
119 | raise service_error(service_error.req, "No description") |
---|
120 | |
---|
121 | master = req.get('master', None) |
---|
122 | if master == None: |
---|
123 | raise service_error(service_error.req, "No master testbed label") |
---|
124 | |
---|
125 | include_fedkit = req.get('include_fedkit', None) |
---|
126 | if include_fedkit == None: |
---|
127 | raise service_error(service_error.req, |
---|
128 | "No federation kit inclusion directive") |
---|
129 | |
---|
130 | |
---|
131 | tclcmd = [self.tclsh, self.tcl_splitter, '-t', '-x', |
---|
132 | str(self.muxmax), '-m', master] |
---|
133 | if include_fedkit: tclcmd.append('-k') |
---|
134 | tclcmd.extend([pid, gid, eid, tclfile]) |
---|
135 | self.log.debug("Calling splitter %s" % " ".join(tclcmd)) |
---|
136 | tclparser = subprocess.Popen(tclcmd, stdout=subprocess.PIPE) |
---|
137 | |
---|
138 | top = topdl.topology_from_xml(file=tclparser.stdout, top="experiment") |
---|
139 | |
---|
140 | # Walk up tmpdir, deleting as we go |
---|
141 | for path, dirs, files in os.walk(tmpdir, topdown=False): |
---|
142 | for f in files: |
---|
143 | os.remove(os.path.join(path, f)) |
---|
144 | for d in dirs: |
---|
145 | os.rmdir(os.path.join(path, d)) |
---|
146 | os.rmdir(tmpdir) |
---|
147 | |
---|
148 | resp = { |
---|
149 | 'experimentdescription': { |
---|
150 | 'topdldescription': top.to_dict(), |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | return resp |
---|
155 | |
---|
156 | |
---|