1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | import os,sys |
---|
4 | |
---|
5 | from ZSI import * |
---|
6 | from M2Crypto import SSL |
---|
7 | from M2Crypto.SSL.SSLServer import SSLServer |
---|
8 | import M2Crypto.httpslib |
---|
9 | |
---|
10 | import xml.parsers.expat |
---|
11 | |
---|
12 | import re |
---|
13 | import random |
---|
14 | import string |
---|
15 | import subprocess |
---|
16 | import tempfile |
---|
17 | import copy |
---|
18 | import pickle |
---|
19 | |
---|
20 | import traceback |
---|
21 | |
---|
22 | from threading import * |
---|
23 | |
---|
24 | from subprocess import * |
---|
25 | |
---|
26 | from fedd_services import * |
---|
27 | from fedd_internal_services import * |
---|
28 | from fedd_util import * |
---|
29 | import parse_detail |
---|
30 | from service_error import * |
---|
31 | |
---|
32 | import logging |
---|
33 | |
---|
34 | class nullHandler(logging.Handler): |
---|
35 | def emit(self, record): pass |
---|
36 | |
---|
37 | fl = logging.getLogger("fedd.splitter") |
---|
38 | fl.addHandler(nullHandler()) |
---|
39 | |
---|
40 | class fedd_split_local: |
---|
41 | def __init__(self, config=None): |
---|
42 | """ |
---|
43 | Intialize the various attributes, most from the config object |
---|
44 | """ |
---|
45 | if config.has_section("splitter"): |
---|
46 | self.debug = config.getboolean("splitter", "debug") |
---|
47 | self.muxmax = config.getint("splitter", "muxmax", 3) |
---|
48 | self.tclsh = config.get("splitter", "tclsh", |
---|
49 | "/usr/local/bin/otclsh") |
---|
50 | self.tcl_splitter = config.get("splitter", "tcl_splitter", |
---|
51 | "/usr/testbed/lib/ns2ir/parse.tcl") |
---|
52 | else: |
---|
53 | self.debug = False |
---|
54 | self.muxmax = 3 |
---|
55 | self.tclsh = "/usr/local/bin/otclsh" |
---|
56 | self.tcl_splitter = "/usr/testbed/lib/ns2ir/parse.tcl" |
---|
57 | |
---|
58 | self.log = logging.getLogger("fedd.splitter") |
---|
59 | self.trace_file = sys.stderr |
---|
60 | |
---|
61 | set_log_level(config, "splitter", self.log) |
---|
62 | # Dispatch tables |
---|
63 | self.soap_services = {\ |
---|
64 | 'Ns2Split': soap_handler(\ |
---|
65 | Ns2SplitRequestMessage.typecode, |
---|
66 | getattr(self, "run_splitter"), |
---|
67 | Ns2SplitResponseMessage, |
---|
68 | "Ns2SplitResponseBody"), |
---|
69 | } |
---|
70 | |
---|
71 | self.xmlrpc_services = {\ |
---|
72 | 'Ns2Split': xmlrpc_handler(\ |
---|
73 | getattr(self, "run_splitter"), |
---|
74 | "Ns2SplitResponseBody"), |
---|
75 | } |
---|
76 | |
---|
77 | def run_splitter(self, req, fid): |
---|
78 | """ |
---|
79 | The external interface to experiment creation called from the |
---|
80 | dispatcher. |
---|
81 | |
---|
82 | Creates a working directory, splits the incoming description using the |
---|
83 | splitter script and parses out the avrious subsections using the |
---|
84 | lcasses above. Once each sub-experiment is created, use pooled threads |
---|
85 | to instantiate them and start it all up. |
---|
86 | """ |
---|
87 | try: |
---|
88 | tmpdir = tempfile.mkdtemp(prefix="split-") |
---|
89 | except IOError: |
---|
90 | raise service_error(service_error.internal, "Cannot create tmp dir") |
---|
91 | |
---|
92 | tclfile = tmpdir + "/experiment.tcl" |
---|
93 | pid = "dummy" |
---|
94 | gid = "dummy" |
---|
95 | eid = "dummy" |
---|
96 | |
---|
97 | req = req.get('Ns2SplitRequestBody', None) |
---|
98 | if not req: |
---|
99 | raise service_error(service_error.req, |
---|
100 | "Bad request format (no Ns2SplitRequestBody)") |
---|
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 | master = req.get('master', None) |
---|
120 | if master == None: |
---|
121 | raise service_error(service_error.req, "No master testbed label") |
---|
122 | |
---|
123 | include_fedkit = req.get('include_fedkit', None) |
---|
124 | if include_fedkit == None: |
---|
125 | raise service_error(service_error.req, |
---|
126 | "No federation kit inclusion directive") |
---|
127 | |
---|
128 | |
---|
129 | tclcmd = [self.tclsh, self.tcl_splitter, '-s', '-x', |
---|
130 | str(self.muxmax), '-m', master] |
---|
131 | if include_fedkit: tclcmd.append('-k') |
---|
132 | tclcmd.extend([pid, gid, eid, tclfile]) |
---|
133 | self.log.debug("Calling splitter %s" % " ".join(tclcmd)) |
---|
134 | tclparser = Popen(tclcmd, stdout=PIPE) |
---|
135 | |
---|
136 | out = "" |
---|
137 | # Package up the split data |
---|
138 | for line in tclparser.stdout: |
---|
139 | out += line |
---|
140 | |
---|
141 | # Walk up tmpdir, deleting as we go |
---|
142 | for path, dirs, files in os.walk(tmpdir, topdown=False): |
---|
143 | for f in files: |
---|
144 | os.remove(os.path.join(path, f)) |
---|
145 | for d in dirs: |
---|
146 | os.rmdir(os.path.join(path, d)) |
---|
147 | os.rmdir(tmpdir) |
---|
148 | |
---|
149 | resp = { 'output' : out } |
---|
150 | |
---|
151 | return resp |
---|
152 | |
---|
153 | |
---|