[11860f52] | 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 | |
---|
[5bf359d] | 14 | from local_segment import local_segment |
---|
[181aeb4] | 15 | from federation.emulab_segment import emulab_segment |
---|
[11860f52] | 16 | |
---|
| 17 | |
---|
[181aeb4] | 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) |
---|
[11860f52] | 23 | |
---|
[5bf359d] | 24 | def set_up_experiment_filespace(self, pid, eid, tmpdir): |
---|
[11860f52] | 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)) |
---|
[d3c8759] | 53 | except EnvironmentError, e: |
---|
[11860f52] | 54 | self.log.error("Error copying file: %s" %e) |
---|
| 55 | return False |
---|
| 56 | |
---|
[5bf359d] | 57 | return True |
---|
| 58 | |
---|
[f3898f7] | 59 | def __call__(self, parent, eid, pid, user, tclfile, tmpdir, timeout=0, |
---|
| 60 | gid=None): |
---|
[5bf359d] | 61 | """ |
---|
| 62 | Start a sub-experiment on a federant. |
---|
| 63 | |
---|
[c167378] | 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 |
---|
[5bf359d] | 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 | |
---|
[c167378] | 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 |
---|
[5bf359d] | 78 | |
---|
| 79 | if not self.set_up_experiment_filespace(pid, eid, tmpdir): |
---|
| 80 | return False |
---|
[181aeb4] | 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 |
---|
[5bf359d] | 88 | |
---|
[c167378] | 89 | # Stage the new configuration |
---|
[181aeb4] | 90 | if not self.modify_exp(pid, eid, tcl): |
---|
| 91 | self.log.error("modify failed") |
---|
[11860f52] | 92 | return False |
---|
[c167378] | 93 | |
---|
| 94 | if not self.swap_exp(pid, eid, 'in'): |
---|
| 95 | self.log.error("swap in failed") |
---|
| 96 | return False |
---|
[11860f52] | 97 | # Everything has gone OK. |
---|
[b4b19c7] | 98 | self.get_mapping(pid,eid) |
---|
[11860f52] | 99 | return True |
---|
| 100 | |
---|
[181aeb4] | 101 | class stop_segment(local_segment, emulab_segment): |
---|
| 102 | def __init__(self, log=None, keyfile=None, debug=False, boss=None, |
---|
| 103 | cert=None): |
---|
[5bf359d] | 104 | local_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
[181aeb4] | 105 | emulab_segment.__init__(self, boss=boss, cert=cert) |
---|
[11860f52] | 106 | |
---|
[05c41f5] | 107 | def __call__(self, parent, user, pid, eid, gid=None, terminate=False): |
---|
[11860f52] | 108 | """ |
---|
| 109 | Stop a sub experiment by calling swapexp on the federant |
---|
| 110 | """ |
---|
[f3898f7] | 111 | |
---|
[11860f52] | 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)) |
---|
[181aeb4] | 117 | rv = self.swap_exp(pid, eid, 'out') |
---|
[05c41f5] | 118 | if terminate: |
---|
| 119 | rv = self.terminate_exp(pid, eid) |
---|
[11860f52] | 120 | except self.cmd_timeout: |
---|
| 121 | rv = False |
---|
| 122 | return rv |
---|
| 123 | |
---|
[6e33086] | 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 |
---|
[b709861] | 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 | |
---|
[1ae1aa2] | 141 | def __call__(self, parent, op, targets, param, top): |
---|
[b709861] | 142 | for l, p in targets.items(): |
---|
| 143 | self.log.info("[operation_segment]: Calling op on %s(%s)" % (l,p)) |
---|
[1ae1aa2] | 144 | self.do_operation(op, l, p, param, top) |
---|
[b709861] | 145 | return True |
---|