1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | import sys, os |
---|
4 | import re |
---|
5 | |
---|
6 | import tempfile |
---|
7 | import subprocess |
---|
8 | import logging |
---|
9 | import time |
---|
10 | import signal |
---|
11 | |
---|
12 | import util |
---|
13 | |
---|
14 | from local_segment import local_segment |
---|
15 | from federation.emulab_segment import emulab_segment |
---|
16 | |
---|
17 | |
---|
18 | class start_segment(local_segment, emulab_segment): |
---|
19 | def __init__(self, log=None, keyfile=None, debug=False, boss=None, |
---|
20 | cert=None): |
---|
21 | local_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
22 | emulab_segment.__init__(self, boss=boss, cert=cert) |
---|
23 | |
---|
24 | def set_up_experiment_filespace(self, pid, eid, tmpdir): |
---|
25 | # Configuration directories on this machine |
---|
26 | proj_dir = "/proj/%s/exp/%s/tmp" % (pid, eid) |
---|
27 | softdir = "/proj/%s/software/%s" % (pid, eid) |
---|
28 | # Softwrae staging directory software dir |
---|
29 | lsoftdir = "%s/software" % tmpdir |
---|
30 | |
---|
31 | # Set up the experiment's file space |
---|
32 | if not self.cmd_with_timeout("/bin/rm -rf %s" % proj_dir): |
---|
33 | return False |
---|
34 | # Clear and create the software and configuration directories |
---|
35 | if not self.cmd_with_timeout("/bin/rm -rf %s/*" % softdir): |
---|
36 | return False |
---|
37 | if not self.cmd_with_timeout('mkdir -p %s' % proj_dir): |
---|
38 | return False |
---|
39 | if os.path.isdir(lsoftdir): |
---|
40 | if not self.cmd_with_timeout('mkdir -p %s' % softdir): |
---|
41 | return False |
---|
42 | |
---|
43 | try: |
---|
44 | for f in os.listdir(tmpdir): |
---|
45 | if not os.path.isdir("%s/%s" % (tmpdir, f)): |
---|
46 | self.copy_file("%s/%s" % (tmpdir, f), |
---|
47 | "%s/%s" % (proj_dir, f)) |
---|
48 | if os.path.isdir(lsoftdir): |
---|
49 | for f in os.listdir(lsoftdir): |
---|
50 | if not os.path.isdir("%s/%s" % (lsoftdir, f)): |
---|
51 | self.copy_file("%s/%s" % (lsoftdir, f), |
---|
52 | "%s/%s" % (softdir, f)) |
---|
53 | except EnvironmentError, e: |
---|
54 | self.log.error("Error copying file: %s" %e) |
---|
55 | return False |
---|
56 | |
---|
57 | return True |
---|
58 | |
---|
59 | def __call__(self, parent, eid, pid, user, tclfile, tmpdir, timeout=0, |
---|
60 | gid=None): |
---|
61 | """ |
---|
62 | Start a sub-experiment on a federant. |
---|
63 | |
---|
64 | Get the current state, and terminate the experiment if it exists. The |
---|
65 | group membership of the experiment is difficult to determine or change, |
---|
66 | so start with a clean slate. Create a new one and ship data |
---|
67 | and configs and start the experiment. There are small ordering |
---|
68 | differences based on the initial state of the sub-experiment. |
---|
69 | """ |
---|
70 | |
---|
71 | state = self.get_state(pid, eid) |
---|
72 | |
---|
73 | if state != 'none': |
---|
74 | self.terminate_exp(pid, eid) |
---|
75 | |
---|
76 | if not self.make_null_experiment(pid, eid, tmpdir, gid): |
---|
77 | return False |
---|
78 | |
---|
79 | if not self.set_up_experiment_filespace(pid, eid, tmpdir): |
---|
80 | return False |
---|
81 | |
---|
82 | # Put the file into a string to pass to emulab. |
---|
83 | try: |
---|
84 | tcl = "".join([ l for l in open(tclfile,"r")]) |
---|
85 | except EnvironmentError, e: |
---|
86 | self.log.error("Can't read %s: %s" % (tclfile, e)) |
---|
87 | return False |
---|
88 | |
---|
89 | # Stage the new configuration |
---|
90 | if not self.modify_exp(pid, eid, tcl): |
---|
91 | self.log.error("modify failed") |
---|
92 | return False |
---|
93 | |
---|
94 | if not self.swap_exp(pid, eid, 'in'): |
---|
95 | self.log.error("swap in failed") |
---|
96 | return False |
---|
97 | # Everything has gone OK. |
---|
98 | self.get_mapping(pid,eid) |
---|
99 | return True |
---|
100 | |
---|
101 | class stop_segment(local_segment, emulab_segment): |
---|
102 | def __init__(self, log=None, keyfile=None, debug=False, boss=None, |
---|
103 | cert=None): |
---|
104 | local_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
105 | emulab_segment.__init__(self, boss=boss, cert=cert) |
---|
106 | |
---|
107 | def __call__(self, parent, user, pid, eid, gid=None, terminate=False): |
---|
108 | """ |
---|
109 | Stop a sub experiment by calling swapexp on the federant |
---|
110 | """ |
---|
111 | |
---|
112 | self.log.info("[stop_segment]: Stopping %s" % eid) |
---|
113 | rv = False |
---|
114 | try: |
---|
115 | # Clean out tar files: we've gone over quota in the past |
---|
116 | self.cmd_with_timeout("rm -rf /proj/%s/software/%s" % (pid, eid)) |
---|
117 | rv = self.swap_exp(pid, eid, 'out') |
---|
118 | if terminate: |
---|
119 | rv = self.terminate_exp(pid, eid) |
---|
120 | except self.cmd_timeout: |
---|
121 | rv = False |
---|
122 | return rv |
---|
123 | |
---|
124 | class info_segment(local_segment, emulab_segment): |
---|
125 | def __init__(self, log=None, keyfile=None, debug=False, boss=None, |
---|
126 | cert=None): |
---|
127 | local_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
128 | emulab_segment.__init__(self, boss=boss, cert=cert) |
---|
129 | |
---|
130 | def __call__(self, parent, user, pid, eid): |
---|
131 | self.log.info("[info_segment]: Getting info from %s" % eid) |
---|
132 | self.get_mapping(pid,eid) |
---|
133 | return True |
---|
134 | |
---|
135 | class operation_segment(local_segment, emulab_segment): |
---|
136 | def __init__(self, log=None, keyfile=None, debug=False, boss=None, |
---|
137 | cert=None): |
---|
138 | local_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
139 | emulab_segment.__init__(self, boss=boss, cert=cert) |
---|
140 | |
---|
141 | def __call__(self, parent, op, targets, param, top): |
---|
142 | for l, p in targets.items(): |
---|
143 | self.log.info("[operation_segment]: Calling op on %s(%s)" % (l,p)) |
---|
144 | self.do_operation(op, l, p, param, top) |
---|
145 | return True |
---|