source: fedd/federation/local_emulab_segment.py @ 05c41f5

compt_changesinfo-ops
Last change on this file since 05c41f5 was 05c41f5, checked in by Ted Faber <faber@…>, 13 years ago

Terminate nonce experiments #36

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[11860f52]1#!/usr/local/bin/python
2
3import sys, os
4import re
5
6import tempfile
7import subprocess
8import logging 
9import time
10import signal
11
12import util
13
[5bf359d]14from local_segment import local_segment
[181aeb4]15from federation.emulab_segment import emulab_segment
[11860f52]16
17
[181aeb4]18class 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
65        Get the current state, modify or create as appropriate, ship data
66        and configs and start the experiment.  There are small ordering
67        differences based on the initial state of the sub-experiment.
68        """
69
70        state = self.get_state(pid, eid)
71
72        if state == 'none':
[f3898f7]73            if not self.make_null_experiment(pid, eid, tmpdir, gid):
[5bf359d]74                return False
75
76        if not self.set_up_experiment_filespace(pid, eid, tmpdir):
77            return False
[181aeb4]78
79        # Put the file into a string to pass to emulab.
80        try:
81            tcl = "".join([ l for l in open(tclfile,"r")])
82        except EnvironmentError, e:
83            self.log.error("Can't read %s: %s" % (tclfile, e))
84            return False
[5bf359d]85       
[11860f52]86        # Stage the new configuration (active experiments will stay swapped
87        # in now)
[181aeb4]88        if not self.modify_exp(pid, eid, tcl):
89            self.log.error("modify failed")
[11860f52]90            return False
91        # Active experiments are still swapped, this swaps the others in.
92        if state != 'active':
[181aeb4]93            if not self.swap_exp(pid, eid, 'in'):
[5bf359d]94                return False
[11860f52]95        # Everything has gone OK.
[b4b19c7]96        self.get_mapping(pid,eid)
[11860f52]97        return True
98
[181aeb4]99class stop_segment(local_segment, emulab_segment):
100    def __init__(self, log=None, keyfile=None, debug=False, boss=None, 
101            cert=None):
[5bf359d]102        local_segment.__init__(self, log=log, keyfile=keyfile, debug=debug)
[181aeb4]103        emulab_segment.__init__(self, boss=boss, cert=cert)
[11860f52]104
[05c41f5]105    def __call__(self, parent, user, pid, eid, gid=None, terminate=False):
[11860f52]106        """
107        Stop a sub experiment by calling swapexp on the federant
108        """
[f3898f7]109
[11860f52]110        self.log.info("[stop_segment]: Stopping %s" % eid)
111        rv = False
112        try:
113            # Clean out tar files: we've gone over quota in the past
114            self.cmd_with_timeout("rm -rf /proj/%s/software/%s" % (pid, eid))
[181aeb4]115            rv = self.swap_exp(pid, eid, 'out')
[05c41f5]116            if terminate:
117                rv = self.terminate_exp(pid, eid)
[11860f52]118        except self.cmd_timeout:
119            rv = False
120        return rv
121
Note: See TracBrowser for help on using the repository browser.