#!/usr/local/bin/python import sys, os import re import tempfile import subprocess import logging import time import signal import util from local_segment import local_segment from federation.emulab_segment import emulab_segment class start_segment(local_segment, emulab_segment): def __init__(self, log=None, keyfile=None, debug=False, boss=None, cert=None): local_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) emulab_segment.__init__(self, boss=boss, cert=cert) self.node = { } def set_up_experiment_filespace(self, pid, eid, tmpdir): # Configuration directories on this machine proj_dir = "/proj/%s/exp/%s/tmp" % (pid, eid) softdir = "/proj/%s/software/%s" % (pid, eid) # Softwrae staging directory software dir lsoftdir = "%s/software" % tmpdir # Set up the experiment's file space if not self.cmd_with_timeout("/bin/rm -rf %s" % proj_dir): return False # Clear and create the software and configuration directories if not self.cmd_with_timeout("/bin/rm -rf %s/*" % softdir): return False if not self.cmd_with_timeout('mkdir -p %s' % proj_dir): return False if os.path.isdir(lsoftdir): if not self.cmd_with_timeout('mkdir -p %s' % softdir): return False try: for f in os.listdir(tmpdir): if not os.path.isdir("%s/%s" % (tmpdir, f)): self.copy_file("%s/%s" % (tmpdir, f), "%s/%s" % (proj_dir, f)) if os.path.isdir(lsoftdir): for f in os.listdir(lsoftdir): if not os.path.isdir("%s/%s" % (lsoftdir, f)): self.copy_file("%s/%s" % (lsoftdir, f), "%s/%s" % (softdir, f)) except EnvironmentError, e: self.log.error("Error copying file: %s" %e) return False return True def __call__(self, parent, eid, pid, user, tclfile, tmpdir, timeout=0, gid=None): """ Start a sub-experiment on a federant. Get the current state, modify or create as appropriate, ship data and configs and start the experiment. There are small ordering differences based on the initial state of the sub-experiment. """ state = self.get_state(pid, eid) if state == 'none': if not self.make_null_experiment(pid, eid, tmpdir, gid): return False if not self.set_up_experiment_filespace(pid, eid, tmpdir): return False # Put the file into a string to pass to emulab. try: tcl = "".join([ l for l in open(tclfile,"r")]) except EnvironmentError, e: self.log.error("Can't read %s: %s" % (tclfile, e)) return False # Stage the new configuration (active experiments will stay swapped # in now) if not self.modify_exp(pid, eid, tcl): self.log.error("modify failed") return False # Active experiments are still swapped, this swaps the others in. if state != 'active': if not self.swap_exp(pid, eid, 'in'): return False # Everything has gone OK. self.get_mapping(pid,eid) return True class stop_segment(local_segment, emulab_segment): def __init__(self, log=None, keyfile=None, debug=False, boss=None, cert=None): local_segment.__init__(self, log=log, keyfile=keyfile, debug=debug) emulab_segment.__init__(self, boss=boss, cert=cert) def __call__(self, parent, user, pid, eid, gid=None, terminate=False): """ Stop a sub experiment by calling swapexp on the federant """ self.log.info("[stop_segment]: Stopping %s" % eid) rv = False try: # Clean out tar files: we've gone over quota in the past self.cmd_with_timeout("rm -rf /proj/%s/software/%s" % (pid, eid)) rv = self.swap_exp(pid, eid, 'out') if terminate: rv = self.terminate_exp(pid, eid) except self.cmd_timeout: rv = False return rv