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 | self.node = { } |
---|
24 | |
---|
25 | def set_up_experiment_filespace(self, pid, eid, tmpdir): |
---|
26 | # Configuration directories on this machine |
---|
27 | proj_dir = "/proj/%s/exp/%s/tmp" % (pid, eid) |
---|
28 | softdir = "/proj/%s/software/%s" % (pid, eid) |
---|
29 | # Softwrae staging directory software dir |
---|
30 | lsoftdir = "%s/software" % tmpdir |
---|
31 | |
---|
32 | # Set up the experiment's file space |
---|
33 | if not self.cmd_with_timeout("/bin/rm -rf %s" % proj_dir): |
---|
34 | return False |
---|
35 | # Clear and create the software and configuration directories |
---|
36 | if not self.cmd_with_timeout("/bin/rm -rf %s/*" % softdir): |
---|
37 | return False |
---|
38 | if not self.cmd_with_timeout('mkdir -p %s' % proj_dir): |
---|
39 | return False |
---|
40 | if os.path.isdir(lsoftdir): |
---|
41 | if not self.cmd_with_timeout('mkdir -p %s' % softdir): |
---|
42 | return False |
---|
43 | |
---|
44 | try: |
---|
45 | for f in os.listdir(tmpdir): |
---|
46 | if not os.path.isdir("%s/%s" % (tmpdir, f)): |
---|
47 | self.copy_file("%s/%s" % (tmpdir, f), |
---|
48 | "%s/%s" % (proj_dir, f)) |
---|
49 | if os.path.isdir(lsoftdir): |
---|
50 | for f in os.listdir(lsoftdir): |
---|
51 | if not os.path.isdir("%s/%s" % (lsoftdir, f)): |
---|
52 | self.copy_file("%s/%s" % (lsoftdir, f), |
---|
53 | "%s/%s" % (softdir, f)) |
---|
54 | except EnvironmentError, e: |
---|
55 | self.log.error("Error copying file: %s" %e) |
---|
56 | return False |
---|
57 | |
---|
58 | return True |
---|
59 | |
---|
60 | def __call__(self, parent, eid, pid, user, tclfile, tmpdir, timeout=0): |
---|
61 | """ |
---|
62 | Start a sub-experiment on a federant. |
---|
63 | |
---|
64 | Get the current state, modify or create as appropriate, ship data |
---|
65 | and configs and start the experiment. There are small ordering |
---|
66 | differences based on the initial state of the sub-experiment. |
---|
67 | """ |
---|
68 | |
---|
69 | state = self.get_state(pid, eid) |
---|
70 | |
---|
71 | if state == 'none': |
---|
72 | if not self.make_null_experiment(pid, eid, tmpdir): |
---|
73 | return False |
---|
74 | |
---|
75 | if not self.set_up_experiment_filespace(pid, eid, tmpdir): |
---|
76 | return False |
---|
77 | |
---|
78 | # Put the file into a string to pass to emulab. |
---|
79 | try: |
---|
80 | tcl = "".join([ l for l in open(tclfile,"r")]) |
---|
81 | except EnvironmentError, e: |
---|
82 | self.log.error("Can't read %s: %s" % (tclfile, e)) |
---|
83 | return False |
---|
84 | |
---|
85 | # Stage the new configuration (active experiments will stay swapped |
---|
86 | # in now) |
---|
87 | if not self.modify_exp(pid, eid, tcl): |
---|
88 | self.log.error("modify failed") |
---|
89 | return False |
---|
90 | # Active experiments are still swapped, this swaps the others in. |
---|
91 | if state != 'active': |
---|
92 | if not self.swap_exp(pid, eid, 'in'): |
---|
93 | return False |
---|
94 | # Everything has gone OK. |
---|
95 | self.get_mapping(pid,eid) |
---|
96 | return True |
---|
97 | |
---|
98 | class stop_segment(local_segment, emulab_segment): |
---|
99 | def __init__(self, log=None, keyfile=None, debug=False, boss=None, |
---|
100 | cert=None): |
---|
101 | local_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
102 | emulab_segment.__init__(self, boss=boss, cert=cert) |
---|
103 | |
---|
104 | def __call__(self, parent, user, pid, eid): |
---|
105 | """ |
---|
106 | Stop a sub experiment by calling swapexp on the federant |
---|
107 | """ |
---|
108 | self.log.info("[stop_segment]: Stopping %s" % eid) |
---|
109 | rv = False |
---|
110 | try: |
---|
111 | # Clean out tar files: we've gone over quota in the past |
---|
112 | self.cmd_with_timeout("rm -rf /proj/%s/software/%s" % (pid, eid)) |
---|
113 | rv = self.swap_exp(pid, eid, 'out') |
---|
114 | except self.cmd_timeout: |
---|
115 | rv = False |
---|
116 | return rv |
---|
117 | |
---|