[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) |
---|
[b4b19c7] | 23 | self.node = { } |
---|
[11860f52] | 24 | |
---|
[5bf359d] | 25 | def set_up_experiment_filespace(self, pid, eid, tmpdir): |
---|
[11860f52] | 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)) |
---|
[d3c8759] | 54 | except EnvironmentError, e: |
---|
[11860f52] | 55 | self.log.error("Error copying file: %s" %e) |
---|
| 56 | return False |
---|
| 57 | |
---|
[5bf359d] | 58 | return True |
---|
| 59 | |
---|
[f3898f7] | 60 | def __call__(self, parent, eid, pid, user, tclfile, tmpdir, timeout=0, |
---|
| 61 | gid=None): |
---|
[5bf359d] | 62 | """ |
---|
| 63 | Start a sub-experiment on a federant. |
---|
| 64 | |
---|
[c167378] | 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 |
---|
[5bf359d] | 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 | |
---|
[c167378] | 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 |
---|
[5bf359d] | 79 | |
---|
| 80 | if not self.set_up_experiment_filespace(pid, eid, tmpdir): |
---|
| 81 | return False |
---|
[181aeb4] | 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 |
---|
[5bf359d] | 89 | |
---|
[c167378] | 90 | # Stage the new configuration |
---|
[181aeb4] | 91 | if not self.modify_exp(pid, eid, tcl): |
---|
| 92 | self.log.error("modify failed") |
---|
[11860f52] | 93 | return False |
---|
[c167378] | 94 | |
---|
| 95 | if not self.swap_exp(pid, eid, 'in'): |
---|
| 96 | self.log.error("swap in failed") |
---|
| 97 | return False |
---|
[11860f52] | 98 | # Everything has gone OK. |
---|
[b4b19c7] | 99 | self.get_mapping(pid,eid) |
---|
[11860f52] | 100 | return True |
---|
| 101 | |
---|
[181aeb4] | 102 | class stop_segment(local_segment, emulab_segment): |
---|
| 103 | def __init__(self, log=None, keyfile=None, debug=False, boss=None, |
---|
| 104 | cert=None): |
---|
[5bf359d] | 105 | local_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) |
---|
[181aeb4] | 106 | emulab_segment.__init__(self, boss=boss, cert=cert) |
---|
[11860f52] | 107 | |
---|
[05c41f5] | 108 | def __call__(self, parent, user, pid, eid, gid=None, terminate=False): |
---|
[11860f52] | 109 | """ |
---|
| 110 | Stop a sub experiment by calling swapexp on the federant |
---|
| 111 | """ |
---|
[f3898f7] | 112 | |
---|
[11860f52] | 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)) |
---|
[181aeb4] | 118 | rv = self.swap_exp(pid, eid, 'out') |
---|
[05c41f5] | 119 | if terminate: |
---|
| 120 | rv = self.terminate_exp(pid, eid) |
---|
[11860f52] | 121 | except self.cmd_timeout: |
---|
| 122 | rv = False |
---|
| 123 | return rv |
---|
| 124 | |
---|