source: fedd/federation/local_emulab_segment.py @ 451fb96

compt_changesinfo-ops
Last change on this file since 451fb96 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
Line 
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
14from local_segment import local_segment
15from federation.emulab_segment import emulab_segment
16
17
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)
23        self.node = { }
24
25    def set_up_experiment_filespace(self, pid, eid, tmpdir):
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))
54        except EnvironmentError, e:
55            self.log.error("Error copying file: %s" %e)
56            return False
57
58        return True
59
60    def __call__(self, parent, eid, pid, user, tclfile, tmpdir, timeout=0, 
61            gid=None):
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':
73            if not self.make_null_experiment(pid, eid, tmpdir, gid):
74                return False
75
76        if not self.set_up_experiment_filespace(pid, eid, tmpdir):
77            return False
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
85       
86        # Stage the new configuration (active experiments will stay swapped
87        # in now)
88        if not self.modify_exp(pid, eid, tcl):
89            self.log.error("modify failed")
90            return False
91        # Active experiments are still swapped, this swaps the others in.
92        if state != 'active':
93            if not self.swap_exp(pid, eid, 'in'):
94                return False
95        # Everything has gone OK.
96        self.get_mapping(pid,eid)
97        return True
98
99class stop_segment(local_segment, emulab_segment):
100    def __init__(self, log=None, keyfile=None, debug=False, boss=None, 
101            cert=None):
102        local_segment.__init__(self, log=log, keyfile=keyfile, debug=debug)
103        emulab_segment.__init__(self, boss=boss, cert=cert)
104
105    def __call__(self, parent, user, pid, eid, gid=None, terminate=False):
106        """
107        Stop a sub experiment by calling swapexp on the federant
108        """
109
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))
115            rv = self.swap_exp(pid, eid, 'out')
116            if terminate:
117                rv = self.terminate_exp(pid, eid)
118        except self.cmd_timeout:
119            rv = False
120        return rv
121
Note: See TracBrowser for help on using the repository browser.