[181aeb4] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
[06c1dba] | 3 | import sys, os |
---|
| 4 | import re |
---|
[181aeb4] | 5 | |
---|
[06c1dba] | 6 | import tempfile |
---|
| 7 | import subprocess |
---|
| 8 | import logging |
---|
| 9 | import time |
---|
| 10 | import signal |
---|
[1ae1aa2] | 11 | |
---|
[06c1dba] | 12 | import util |
---|
[181aeb4] | 13 | |
---|
[06c1dba] | 14 | from ssh_emulab_segment import ssh_emulab_segment |
---|
| 15 | from xmlrpc_emulab_segment import xmlrpc_emulab_segment |
---|
[181aeb4] | 16 | |
---|
| 17 | |
---|
[06c1dba] | 18 | class start_segment(ssh_emulab_segment, xmlrpc_emulab_segment): |
---|
| 19 | def __init__(self, log=None, keyfile=None, debug=False, boss=None, |
---|
[c7141dc] | 20 | ops=None, cert=None): |
---|
[06c1dba] | 21 | ssh_emulab_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
[c7141dc] | 22 | xmlrpc_emulab_segment.__init__(self, boss=boss, ops=ops, cert=cert) |
---|
[181aeb4] | 23 | |
---|
[06c1dba] | 24 | def set_up_experiment_filespace(self, user, host, pid, eid, tmpdir): |
---|
[181aeb4] | 25 | """ |
---|
[06c1dba] | 26 | Send all the software and configuration files into the experiment's |
---|
| 27 | file space. To reduce the number of ssh connections, we script many |
---|
| 28 | changes and execute the script. |
---|
[181aeb4] | 29 | """ |
---|
[06c1dba] | 30 | # Configuration directories on the remote machine |
---|
| 31 | proj_dir = "/proj/%s/exp/%s/tmp" % (pid, eid) |
---|
| 32 | softdir = "/proj/%s/software/%s" % (pid, eid) |
---|
| 33 | # Local software dir |
---|
| 34 | lsoftdir = "%s/software" % tmpdir |
---|
| 35 | |
---|
| 36 | # Open up a temporary file to contain a script for setting up the |
---|
| 37 | # filespace for the new experiment. |
---|
| 38 | self.log.info("[start_segment]: creating script file") |
---|
| 39 | try: |
---|
| 40 | sf, scriptname = tempfile.mkstemp() |
---|
| 41 | scriptfile = os.fdopen(sf, 'w') |
---|
| 42 | except EnvironmentError: |
---|
| 43 | return False |
---|
[181aeb4] | 44 | |
---|
[06c1dba] | 45 | scriptbase = os.path.basename(scriptname) |
---|
| 46 | |
---|
| 47 | # Script the filesystem changes |
---|
[d4946da] | 48 | |
---|
| 49 | # Set umask to 007. Since we are operating |
---|
| 50 | # within /proj, we want to permit all proj users |
---|
| 51 | # read and write. This is especially necessary |
---|
| 52 | # since creating directory hierarchy (mkdir -p). |
---|
| 53 | print >>scriptfile, "umask 007" |
---|
[06c1dba] | 54 | print >>scriptfile, "/bin/rm -rf %s" % proj_dir |
---|
| 55 | # Clear and create the software directory |
---|
| 56 | print >>scriptfile, "/bin/rm -rf %s/*" % softdir |
---|
| 57 | print >>scriptfile, 'mkdir -p %s' % proj_dir |
---|
| 58 | if os.path.isdir(lsoftdir): |
---|
| 59 | print >>scriptfile, 'mkdir -p %s' % softdir |
---|
[9a256f7] | 60 | print >>scriptfile, 'chmod 0770 %s' % softdir |
---|
[06c1dba] | 61 | print >>scriptfile, "rm -f %s" % scriptbase |
---|
| 62 | scriptfile.close() |
---|
| 63 | |
---|
| 64 | # Move the script to the remote machine |
---|
| 65 | # XXX: could collide tempfile names on the remote host |
---|
| 66 | if self.scp_file(scriptname, user, host, scriptbase): |
---|
| 67 | os.remove(scriptname) |
---|
[181aeb4] | 68 | else: |
---|
[06c1dba] | 69 | return False |
---|
[181aeb4] | 70 | |
---|
[06c1dba] | 71 | # Execute the script (and the script's last line deletes it) |
---|
| 72 | if not self.ssh_cmd(user, host, "sh -x %s" % scriptbase): |
---|
| 73 | return False |
---|
[181aeb4] | 74 | |
---|
[06c1dba] | 75 | for f in os.listdir(tmpdir): |
---|
| 76 | if not os.path.isdir("%s/%s" % (tmpdir, f)): |
---|
| 77 | if not self.scp_file("%s/%s" % (tmpdir, f), user, host, |
---|
| 78 | "%s/%s" % (proj_dir, f)): |
---|
| 79 | return False |
---|
| 80 | if os.path.isdir(lsoftdir): |
---|
| 81 | for f in os.listdir(lsoftdir): |
---|
| 82 | if not os.path.isdir("%s/%s" % (lsoftdir, f)): |
---|
| 83 | if not self.scp_file("%s/%s" % (lsoftdir, f), |
---|
| 84 | user, host, "%s/%s" % (softdir, f)): |
---|
| 85 | return False |
---|
| 86 | return True |
---|
[181aeb4] | 87 | |
---|
| 88 | |
---|
[06c1dba] | 89 | def __call__(self, parent, eid, pid, user, tclfile, tmpdir, timeout=0, |
---|
| 90 | gid=None): |
---|
[181aeb4] | 91 | """ |
---|
[06c1dba] | 92 | Start a sub-experiment on a federant. |
---|
[181aeb4] | 93 | |
---|
[06c1dba] | 94 | Get the current state, and terminate the experiment if it exists. The |
---|
| 95 | group membership of the experiment is difficult to determine or change, |
---|
| 96 | so start with a clean slate. Create a new one and ship data |
---|
| 97 | and configs and start the experiment. There are small ordering |
---|
| 98 | differences based on the initial state of the sub-experiment. |
---|
[05c41f5] | 99 | """ |
---|
| 100 | |
---|
[06c1dba] | 101 | state = self.get_state(pid, eid) |
---|
[05c41f5] | 102 | |
---|
[06c1dba] | 103 | if state != 'none': |
---|
| 104 | self.terminate_exp(pid, eid) |
---|
[05c41f5] | 105 | |
---|
[06c1dba] | 106 | if not self.make_null_experiment(pid, eid, tmpdir, gid): |
---|
| 107 | return False |
---|
[6e33086] | 108 | |
---|
[c7141dc] | 109 | if not self.set_up_experiment_filespace(user, self.ops, |
---|
| 110 | pid, eid, tmpdir): |
---|
[06c1dba] | 111 | return False |
---|
[1ae1aa2] | 112 | |
---|
[06c1dba] | 113 | # Put the file into a string to pass to emulab. |
---|
| 114 | try: |
---|
| 115 | tcl = "".join([ l for l in open(tclfile,"r")]) |
---|
| 116 | except EnvironmentError, e: |
---|
| 117 | self.log.error("Can't read %s: %s" % (tclfile, e)) |
---|
| 118 | return False |
---|
| 119 | |
---|
| 120 | # Stage the new configuration |
---|
| 121 | if not self.modify_exp(pid, eid, tcl): |
---|
| 122 | self.log.error("modify failed") |
---|
| 123 | return False |
---|
[1ae1aa2] | 124 | |
---|
[06c1dba] | 125 | if not self.swap_exp(pid, eid, 'in'): |
---|
| 126 | self.log.error("swap in failed") |
---|
| 127 | return False |
---|
| 128 | # Everything has gone OK. |
---|
| 129 | self.get_mapping(pid,eid) |
---|
| 130 | return True |
---|
[1ae1aa2] | 131 | |
---|
[c7141dc] | 132 | class stop_segment(ssh_emulab_segment, xmlrpc_emulab_segment): |
---|
[06c1dba] | 133 | def __init__(self, log=None, keyfile=None, debug=False, boss=None, |
---|
[c7141dc] | 134 | ops=None, cert=None): |
---|
| 135 | ssh_emulab_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
| 136 | xmlrpc_emulab_segment.__init__(self, boss=boss, ops=ops, cert=cert) |
---|
[181aeb4] | 137 | |
---|
[06c1dba] | 138 | def __call__(self, parent, user, pid, eid, gid=None, terminate=False): |
---|
[b709861] | 139 | """ |
---|
[06c1dba] | 140 | Stop a sub experiment by calling swapexp on the federant |
---|
[b709861] | 141 | """ |
---|
| 142 | |
---|
[06c1dba] | 143 | self.log.info("[stop_segment]: Stopping %s" % eid) |
---|
| 144 | rv = False |
---|
| 145 | try: |
---|
| 146 | # Clean out tar files: we've gone over quota in the past |
---|
[c7141dc] | 147 | self.ssh_cmd(user, self.ops, |
---|
| 148 | "rm -rf /proj/%s/software/%s" % (pid, eid)) |
---|
[06c1dba] | 149 | rv = self.swap_exp(pid, eid, 'out') |
---|
| 150 | if terminate: |
---|
| 151 | rv = self.terminate_exp(pid, eid) |
---|
| 152 | except self.cmd_timeout: |
---|
| 153 | rv = False |
---|
| 154 | return rv |
---|
| 155 | |
---|
[c7141dc] | 156 | class info_segment(ssh_emulab_segment, xmlrpc_emulab_segment): |
---|
[06c1dba] | 157 | def __init__(self, log=None, keyfile=None, debug=False, boss=None, |
---|
[c7141dc] | 158 | ops=None, cert=None): |
---|
| 159 | ssh_emulab_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
| 160 | xmlrpc_emulab_segment.__init__(self, boss=boss, ops=ops, cert=cert) |
---|
[06c1dba] | 161 | |
---|
| 162 | def __call__(self, parent, user, pid, eid): |
---|
| 163 | self.log.info("[info_segment]: Getting info from %s" % eid) |
---|
| 164 | self.get_mapping(pid,eid) |
---|
| 165 | return True |
---|
| 166 | |
---|
[c7141dc] | 167 | class operation_segment(ssh_emulab_segment, xmlrpc_emulab_segment): |
---|
[06c1dba] | 168 | def __init__(self, log=None, keyfile=None, debug=False, boss=None, |
---|
[c7141dc] | 169 | ops=None, cert=None): |
---|
| 170 | ssh_emulab_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
| 171 | xmlrpc_emulab_segment.__init__(self, boss=boss, ops=ops, cert=cert) |
---|
[06c1dba] | 172 | |
---|
| 173 | def __call__(self, parent, op, targets, param, top): |
---|
| 174 | for l, p in targets.items(): |
---|
| 175 | self.log.info("[operation_segment]: Calling op %s on %s(%s)" % \ |
---|
| 176 | (op, l,p)) |
---|
| 177 | self.do_operation(op, l, p, param, top) |
---|
| 178 | return True |
---|