source: fedd/federation/emulab_segment.py @ 68f03a6

compt_changes
Last change on this file since 68f03a6 was c7141dc, checked in by Ted Faber <faber@…>, 13 years ago

Single access works

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[181aeb4]1#!/usr/local/bin/python
2
[06c1dba]3import sys, os
4import re
[181aeb4]5
[06c1dba]6import tempfile
7import subprocess
8import logging 
9import time
10import signal
[1ae1aa2]11
[06c1dba]12import util
[181aeb4]13
[06c1dba]14from ssh_emulab_segment import ssh_emulab_segment
15from xmlrpc_emulab_segment import xmlrpc_emulab_segment
[181aeb4]16
17
[06c1dba]18class start_segment(ssh_emulab_segment, xmlrpc_emulab_segment):
19    def __init__(self, log=None, keyfile=None, debug=False, boss=None,
[c7141dc]20            ops=None, cert=None):
[06c1dba]21        ssh_emulab_segment.__init__(self, log=log, keyfile=keyfile, debug=debug)
[c7141dc]22        xmlrpc_emulab_segment.__init__(self, boss=boss, ops=ops, cert=cert)
[181aeb4]23
[06c1dba]24    def set_up_experiment_filespace(self, user, host, pid, eid, tmpdir):
[181aeb4]25        """
[06c1dba]26        Send all the software and configuration files into the experiment's
27        file space.  To reduce the number of ssh connections, we script many
28        changes and execute the script.
[181aeb4]29        """
[06c1dba]30        # Configuration directories on the remote machine
31        proj_dir = "/proj/%s/exp/%s/tmp" % (pid, eid)
32        softdir = "/proj/%s/software/%s" % (pid, eid)
33        # Local software dir
34        lsoftdir = "%s/software" % tmpdir
35
36        # Open up a temporary file to contain a script for setting up the
37        # filespace for the new experiment.
38        self.log.info("[start_segment]: creating script file")
39        try:
40            sf, scriptname = tempfile.mkstemp()
41            scriptfile = os.fdopen(sf, 'w')
42        except EnvironmentError:
43            return False
[181aeb4]44
[06c1dba]45        scriptbase = os.path.basename(scriptname)
46
47        # Script the filesystem changes
48        print >>scriptfile, "/bin/rm -rf %s" % proj_dir
49        # Clear and create the software directory
50        print >>scriptfile, "/bin/rm -rf %s/*" % softdir
51        print >>scriptfile, 'mkdir -p %s' % proj_dir
52        if os.path.isdir(lsoftdir):
53            print >>scriptfile, 'mkdir -p %s' % softdir
54        print >>scriptfile, "rm -f %s" % scriptbase
55        scriptfile.close()
56
57        # Move the script to the remote machine
58        # XXX: could collide tempfile names on the remote host
59        if self.scp_file(scriptname, user, host, scriptbase):
60            os.remove(scriptname)
[181aeb4]61        else:
[06c1dba]62            return False
[181aeb4]63
[06c1dba]64        # Execute the script (and the script's last line deletes it)
65        if not self.ssh_cmd(user, host, "sh -x %s" % scriptbase):
66            return False
[181aeb4]67
[06c1dba]68        for f in os.listdir(tmpdir):
69            if not os.path.isdir("%s/%s" % (tmpdir, f)):
70                if not self.scp_file("%s/%s" % (tmpdir, f), user, host,
71                        "%s/%s" % (proj_dir, f)):
72                    return False
73        if os.path.isdir(lsoftdir):
74            for f in os.listdir(lsoftdir):
75                if not os.path.isdir("%s/%s" % (lsoftdir, f)):
76                    if not self.scp_file("%s/%s" % (lsoftdir, f), 
77                            user, host, "%s/%s" % (softdir, f)):
78                        return False
79        return True
[181aeb4]80
81
[06c1dba]82    def __call__(self, parent, eid, pid, user, tclfile, tmpdir, timeout=0, 
83            gid=None):
[181aeb4]84        """
[06c1dba]85        Start a sub-experiment on a federant.
[181aeb4]86
[06c1dba]87        Get the current state, and terminate the experiment if it exists. The
88        group membership of the experiment is difficult to determine or change,
89        so start with a clean slate.  Create a new one and ship data
90        and configs and start the experiment.  There are small ordering
91        differences based on the initial state of the sub-experiment.
[05c41f5]92        """
93
[06c1dba]94        state = self.get_state(pid, eid)
[05c41f5]95
[06c1dba]96        if state != 'none':
97            self.terminate_exp(pid, eid)
[05c41f5]98
[06c1dba]99        if not self.make_null_experiment(pid, eid, tmpdir, gid):
100            return False
[6e33086]101
[c7141dc]102        if not self.set_up_experiment_filespace(user, self.ops, 
103                pid, eid, tmpdir):
[06c1dba]104            return False
[1ae1aa2]105
[06c1dba]106        # Put the file into a string to pass to emulab.
107        try:
108            tcl = "".join([ l for l in open(tclfile,"r")])
109        except EnvironmentError, e:
110            self.log.error("Can't read %s: %s" % (tclfile, e))
111            return False
112       
113        # Stage the new configuration
114        if not self.modify_exp(pid, eid, tcl):
115            self.log.error("modify failed")
116            return False
[1ae1aa2]117
[06c1dba]118        if not self.swap_exp(pid, eid, 'in'):
119            self.log.error("swap in failed")
120            return False
121        # Everything has gone OK.
122        self.get_mapping(pid,eid)
123        return True
[1ae1aa2]124
[c7141dc]125class stop_segment(ssh_emulab_segment, xmlrpc_emulab_segment):
[06c1dba]126    def __init__(self, log=None, keyfile=None, debug=False, boss=None, 
[c7141dc]127            ops=None, cert=None):
128        ssh_emulab_segment.__init__(self, log=log, keyfile=keyfile, debug=debug)
129        xmlrpc_emulab_segment.__init__(self, boss=boss, ops=ops, cert=cert)
[181aeb4]130
[06c1dba]131    def __call__(self, parent, user, pid, eid, gid=None, terminate=False):
[b709861]132        """
[06c1dba]133        Stop a sub experiment by calling swapexp on the federant
[b709861]134        """
135
[06c1dba]136        self.log.info("[stop_segment]: Stopping %s" % eid)
137        rv = False
138        try:
139            # Clean out tar files: we've gone over quota in the past
[c7141dc]140            self.ssh_cmd(user, self.ops, 
141                    "rm -rf /proj/%s/software/%s" % (pid, eid))
[06c1dba]142            rv = self.swap_exp(pid, eid, 'out')
143            if terminate:
144                rv = self.terminate_exp(pid, eid)
145        except self.cmd_timeout:
146            rv = False
147        return rv
148
[c7141dc]149class info_segment(ssh_emulab_segment, xmlrpc_emulab_segment):
[06c1dba]150    def __init__(self, log=None, keyfile=None, debug=False, boss=None, 
[c7141dc]151            ops=None, cert=None):
152        ssh_emulab_segment.__init__(self, log=log, keyfile=keyfile, debug=debug)
153        xmlrpc_emulab_segment.__init__(self, boss=boss, ops=ops, cert=cert)
[06c1dba]154
155    def __call__(self, parent, user, pid, eid):
156        self.log.info("[info_segment]: Getting info from %s" % eid)
157        self.get_mapping(pid,eid)
158        return True
159
[c7141dc]160class operation_segment(ssh_emulab_segment, xmlrpc_emulab_segment):
[06c1dba]161    def __init__(self, log=None, keyfile=None, debug=False, boss=None, 
[c7141dc]162            ops=None, cert=None):
163        ssh_emulab_segment.__init__(self, log=log, keyfile=keyfile, debug=debug)
164        xmlrpc_emulab_segment.__init__(self, boss=boss, ops=ops, cert=cert)
[06c1dba]165
166    def __call__(self, parent, op, targets, param, top):
167        for l, p in targets.items():
168            self.log.info("[operation_segment]: Calling op %s on %s(%s)" % \
169                    (op, l,p))
170            self.do_operation(op, l, p, param, top)
171        return True
Note: See TracBrowser for help on using the repository browser.