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 | gid=None): |
---|
62 | """ |
---|
63 | Start a sub-experiment on a federant. |
---|
64 | |
---|
65 | Get the current state, and terminate the experiment if it exists. The |
---|
66 | group membership of the experiment is difficult to determine or change, |
---|
67 | so start with a clean slate. Create a new one and ship data |
---|
68 | and configs and start the experiment. There are small ordering |
---|
69 | differences based on the initial state of the sub-experiment. |
---|
70 | """ |
---|
71 | |
---|
72 | state = self.get_state(pid, eid) |
---|
73 | |
---|
74 | if state != 'none': |
---|
75 | self.terminate_exp(pid, eid) |
---|
76 | |
---|
77 | if not self.make_null_experiment(pid, eid, tmpdir, gid): |
---|
78 | return False |
---|
79 | |
---|
80 | if not self.set_up_experiment_filespace(pid, eid, tmpdir): |
---|
81 | return False |
---|
82 | |
---|
83 | # Put the file into a string to pass to emulab. |
---|
84 | try: |
---|
85 | tcl = "".join([ l for l in open(tclfile,"r")]) |
---|
86 | except EnvironmentError, e: |
---|
87 | self.log.error("Can't read %s: %s" % (tclfile, e)) |
---|
88 | return False |
---|
89 | |
---|
90 | # Stage the new configuration |
---|
91 | if not self.modify_exp(pid, eid, tcl): |
---|
92 | self.log.error("modify failed") |
---|
93 | return False |
---|
94 | |
---|
95 | if not self.swap_exp(pid, eid, 'in'): |
---|
96 | self.log.error("swap in failed") |
---|
97 | return False |
---|
98 | # Everything has gone OK. |
---|
99 | self.get_mapping(pid,eid) |
---|
100 | return True |
---|
101 | |
---|
102 | class stop_segment(local_segment, emulab_segment): |
---|
103 | def __init__(self, log=None, keyfile=None, debug=False, boss=None, |
---|
104 | cert=None): |
---|
105 | local_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
106 | emulab_segment.__init__(self, boss=boss, cert=cert) |
---|
107 | |
---|
108 | def __call__(self, parent, user, pid, eid, gid=None, terminate=False): |
---|
109 | """ |
---|
110 | Stop a sub experiment by calling swapexp on the federant |
---|
111 | """ |
---|
112 | |
---|
113 | self.log.info("[stop_segment]: Stopping %s" % eid) |
---|
114 | rv = False |
---|
115 | try: |
---|
116 | # Clean out tar files: we've gone over quota in the past |
---|
117 | self.cmd_with_timeout("rm -rf /proj/%s/software/%s" % (pid, eid)) |
---|
118 | rv = self.swap_exp(pid, eid, 'out') |
---|
119 | if terminate: |
---|
120 | rv = self.terminate_exp(pid, eid) |
---|
121 | except self.cmd_timeout: |
---|
122 | rv = False |
---|
123 | return rv |
---|
124 | |
---|