source: fedkit/splitter.pl @ 1a8a08a

axis_examplecompt_changesinfo-opsversion-1.30version-2.00version-3.01version-3.02
Last change on this file since 1a8a08a was 1a8a08a, checked in by Ted Faber <faber@…>, 17 years ago

ISI Federation scripts

  • Property mode set to 100644
File size: 10.7 KB
Line 
1#!/usr/bin/perl
2
3use Getopt::Std;
4
5# use scp to transfer a file, reporting true if successful and false otherwise.
6# Parameters are the local file name, the ssh host destination (either hostname
7# oe user@host), and an optional destination file name or directory.  If no
8# destination is given, the file is transferred to the given user's home
9# directory.  If only a machine is given in the ssh host destination, the
10# current user is used.
11sub scp_file {
12    my($file, $where, $dest) = @_;
13
14    # XXX system with a relative pathname is sort of gross
15    system("scp $file $where:$dest");
16    if ($?) {
17        warn "scp failed $?\n";
18        return 0;
19    }
20    else { return 1; }
21}
22
23# use ssh to execute the given command on the machine (and as the user) in
24# $where.  Parameters are the ssh destination directive ($where) and the
25# command to execute, and a prefix to be placed on a message generated if the
26# command fails.   On failure print a warning if a warning prefix was given and
27# return false.
28sub ssh_cmd {
29    my($user, $host, $cmd, $wname) = @_;
30
31    # XXX system with a relative pathname is sort of gross
32    if ( $host ne "localhost") {
33        system ("ssh $user\@$host $cmd");
34    }
35    else {
36        system ("$cmd");
37    }
38    if ($?) {
39        warn "$wname failed $?\n" if $wname;
40        return 0;
41    }
42    else { return 1; }
43}
44
45
46
47# Start a sub section of the experiment on a given testbed.  The testbed and
48# the user to start the experiment as are pulled from the global per-testbed
49# hash, as is the project name on the remote testbed.  Parameters are the
50# testbed and the experiment id.  Configuration files are scp-ed over to the
51# target testbed.  Then the current state of the experiment determined using
52# expinfo.  From that state, the experiment is either created, modified or
53# spapped in.  If everything succeeds, true is returned.
54sub start_segment {
55    my($tb, $eid) = @_;                     # testbed and experiment ID
56    my($host) = $host{$tb};                 # Host name of remote users
57    my($user) = $user{$tb};                 # user to pass to ssh
58    my($pid) = $project{$tb};               # remote project to start the
59                                            # experiment under
60    my($tclfile) = "./$eid.$tb.tcl";        # Local tcl file with the
61                                            # sub-experiment
62    my($to_hostname) = "/proj/$pid/hosts.$eid"; # remote hostnames file
63
64    # Determine the status of the remote experiment
65    if ( $host ne "localhost") {
66        open(STATUS, "ssh $user\@$host /usr/testbed/bin/expinfo $pid $eid|") || 
67            die "Can't ssh to $user\@$host:$!\n";
68    }
69    else {
70        open(STATUS, "/usr/testbed/bin/expinfo $pid $eid|") || 
71            die "Can't call expinfo locally";
72    }
73    # XXX: this is simple now.  Parsing may become more complex
74    while (<STATUS>) {
75        /State: (\w+)/ && ($state = $1);
76        /No\s+such\s+experiment/ && ($state = "none");
77    }
78    close(STATUS);
79    print "$tb: $state\n";
80
81    # Copy the configuration data over (unless the host is local)
82    if ( $host ne "localhost") {
83        &scp_file($tclfile, "$user\@$host") || return 0;
84        &scp_file("./hostnames", "$user\@$host", $to_hostname) || return 0;
85    }
86
87    # Remote experiment is active.  Modify it.
88    if ($state eq "active") {
89        &ssh_cmd($user, $host, "/usr/testbed/bin/modexp -r -s -w $pid " . 
90            "$eid $tclfile", "modexp") || return 0;
91        return 1;
92    }
93
94    # Remote experiment is swapped out, modify it and swap it in.
95    if ($state eq "swapped") {
96        &ssh_cmd($user, $host, "/usr/testbed/bin/modexp -w $pid $eid $tclfile", 
97            "modexp") || return 0;
98        &ssh_cmd($user, $host, "/usr/testbed/bin/swapexp -w $pid $eid in", 
99            "swapexp") || return 0;
100        return 1;
101    }
102
103    # No remote experiment.  Create one.
104    if ($state eq "none") {
105        &ssh_cmd($user, $host, "/usr/testbed/bin/startexp -i -w -p " . 
106            "$pid -e $eid $tclfile", "startexp") || return 0;
107        return 1;
108    }
109
110    # Every branch for a known state returns.  If execution gets here, the
111    # state is unknown.
112    warn "unknown state: $state\n";
113    return 0;
114}
115
116# Swap out a sub-experiment - probably because another has failed.  Arguments
117# are testbed and experiment.  Most of the control flow is similar to
118# start_segment, though much simpler.
119sub stop_segment {
120    my($tb, $eid) = @_;
121    my($user) = "$user{$tb}";
122    my($host) = "$host{$tb}";
123    my($pid) = $project{$tb};
124
125    &ssh_cmd($user, $host, "/usr/testbed/bin/swapexp -w $pid $eid out", 
126        "swapexp (out)") || return 0;
127    return 1;
128}
129
130$tcl_splitter = "/usr/testbed/lib/ns2ir/parse.tcl";         # tcl program to split experiments
131$tclsh = "/usr/local/bin/otclsh";   # tclsh to call directly
132
133$pid = $gid = "dummy";              # Default project and group to pass to
134                                    # $tcl_splitter above.  These are total
135                                    # dummy arguments;  the splitter doesn't
136                                    # use them at all, but we supply them to
137                                    # keep our changes to the parser minimal.
138
139# Argument processing.
140getopts('c:m:e:f:n', \%opts);
141
142$eid = $opts{'e'};                  # Experiment ID
143$tcl = $opts{'f'} || shift;         # The experiment description
144$master = $opts{'m'};               # Master testbed
145$startem = $opts{'n'} ? 0 : 1;      # If true, start the sub-experiments
146$config = $opts{'c'} || "./testbeds";
147
148die "Must supply file, master and experiment" unless $master && $tcl && $eid;
149
150# Read a hash of per-testbed parameters from the local configurations.
151open(CONF, $config) || die "can't read testbed configutions from $config: $!\n";
152while (<CONF>) {
153    next if /^#/;
154    chomp;
155    ($tb, $h, $u, $p, $es, $gs, $mes, $mgs, $t, $i) = split(":", $_);
156    $host{$tb} = $h;
157    $user{$tb} = $u;
158    $project{$tb} = $p;
159    $gwtype{$tb} = $t;
160    $expstart{$tb} = $es;
161    $gwstart{$tb} = $gs;
162    $mexpstart{$tb} = $mes;
163    $mgwstart{$tb} = $mgs;
164    $gwimage{$tb} = $i;
165}
166close(CONF);
167
168# Open a pipe to the splitter program and start it parsing the experiments
169open(PIPE, "$tclsh $tcl_splitter -s -p $pid $gid $eid $tcl|") || 
170    die "Cannot execute $tclsh $tcl_splitter -s -p $pid $gid $eid $tcl:$!\n";
171
172# Parse the splitter output.
173while (<PIPE>) {
174    # Start of a sub-experiment
175    /^#\s+Begin\s+Testbed\s+\((\w+)\)/ && do {
176        $ctb = $1;
177
178        # If we know the testbed, start collecting its sub experiment tcl
179        # description.  If not, warn the caller and ignore the configuration of
180        # this testbed.
181        if ($host{$ctb}) {
182            $allocated{$ctb}++; # Keep track of the testbeds allocated
183            $destfile = "./$eid.$ctb.tcl";
184
185            open(FILE, ">$destfile") || die "Cannot open $destfile:$!\n";
186        }
187        else { 
188            warn "No such testbed $ctb\n";
189            $destfile = "";
190        }
191        next;
192    };
193    # End of that experiment
194    /^#\s+End\s+Testbed\s+\((\w+)\)/ && do {
195        # Simple syntax check and close out this experiment's tcl description
196        die "Mismatched testbed markers ($1, $ctb)\n" unless ($1 eq $ctb);
197        close(FILE);
198        $destfile = $ctb = "";
199        next;
200    };
201    # Beginning of the hostnames list.  Collection is always in the hostnames
202    # file.
203    /^#\s+Begin\s+hostnames/ && do {
204        $destfile = "./hostnames";
205        open(FILE, ">$destfile") || die "Can't open $destfile:$!\n";
206        next;
207    };
208    # end of the hostnames list.
209    /^#\s+End\s+hostnames/ && do {
210        close(FILE);
211        $destfile = "";
212        next;
213    };
214
215    next unless $destfile;  # Unidentified testbed, ignore config
216
217    # Substitute variables
218    s/GWTYPE/$gwtype{$ctb}/g;
219    s/GWIMAGE/$gwimage{$ctb}/g;
220    if ($ctb eq $master ) {
221        s/GWSTART/$mgwstart{$ctb}/g;
222        s/EXPSTART/$mexpstart{$ctb}/g;
223    }
224    else {
225        s/GWSTART/$gwstart{$ctb}/g;
226        s/EXPSTART/$expstart{$ctb}/g;
227    }
228    print FILE;
229}
230close(PIPE);
231die "No nodes in master testbed ($master)\n" unless $allocated{$master};
232
233exit(0) unless $startem;
234
235# Start up the slave sub-experiments first
236TESTBED:
237for $tb (keys %allocated) {
238    if ($tb ne $master) {
239        if (&start_segment($tb, $eid)) { $started{$tb}++; }
240        else { last TESTBED; }
241    }
242}
243
244# Now the master
245if (&start_segment($master, $eid)) { 
246    $started{$master}++;
247}
248
249# If any testbed failed, swap the rest out.
250if ( scalar(keys %started) != scalar(keys %allocated)) {
251    for $tb (keys %started) { &stop_segment($tb, $eid); }
252    print "Error starting experiment\n";
253    exit(1);
254}
255print "Experiment started\n";
256exit(0);    # set the exit value
257
258=pod
259
260=head1 NAME
261
262B<splitter.pl>
263
264=head1 SYNOPSIS
265
266B<splitter.pl> B<-e> I<experiment> B<-m> I<master_testbed> [B<-n>]
267    [B<-c> I<config_file>] [B<-f> I<experiment_tcl>] [I<experiment_tcl>]
268
269=head1 DESCRIPTION
270
271B<splitter.pl> invokes the DETER experiment parser to split an annotated
272experiment into multiple sub-experments and instantiates the sub-experiments on
273their intended testbeds.  Annotation is accomplished using the
274tb-set-node-testbed command, added to the parser.
275
276The testbed labels are meaningful based on their presence in the testbeds file.
277that file can be specified with the B<-c> option, and defaults to
278F<./testbeds>.  The syntax is described below.
279
280The expreriment is split out into one experiment description per testbed in the
281current directory named as F<experiment.testbed.tcl> where the experiment is
282the argument to B<-e> and the testbed is the tb-set-node-testbed parameter for
283the nodes in the file.
284
285If the B<-n> option is absent the sub-experiments are then instantiated on
286their testbeds.  (Here B<-n> is analogous to its use in L<make(1)>).
287Per-testbed parameters are set in the configuration file.  Sub-experiments on
288slave testbeds are instantiated in a random order, but the master testbed is
289currently instantiated last.
290
291If any sub-experiment fails to instantiate, the other sub-exeriments are
292swapped out.
293
294=head2 Configuration file
295
296The configuration file (F<./testbeds> unless overridden by B<-c>) is a
297colon-separated set of parameters keyed by testbed name.  The fields, in order,
298are:
299
300=over 5
301
302=item name
303
304The testbed to which this line of parameters applies.
305
306=item user
307
308The user under which to make requests to this testbed.  The user running
309B<splitter.pl> must be able to authenicate as this user under L<ssh(1)> to this
310testbed.
311
312=item project
313
314The project under which to instantiate sub-experiments on this testbed.
315
316=item gateway type
317
318The node type for inter-testbed gateway nodes on this testbed.
319
320=item experiment start (slave)
321
322The start command to run on experimental nodes when this testbed is used as a
323slave.
324
325=item gateway start (slave)
326
327The start command to run on gateway nodes when this testbed is used as a
328slave.
329
330=item experiment start (master)
331
332The start command to run on experimental nodes when this testbed is used as a
333master.
334
335=item gateway start (master)
336
337The start command to run on gateway nodes when this testbed is used as a
338master.
339
340=item gateway image
341
342The disk image to be loaded on a gateway node on this testbed.
343
344=back
345
346The parsing of the configuration is extremely simple.  Colons separate each
347field and there is n provision for escaping them at this time.
348
349=head1 ENVIRONMENT
350
351B<splitter.pl> does not directly make use of environment variables, but calls
352out to L<ssh(1)> and (indirectly) to L<sh(1)>, which may be influenced by the
353environment.
354
355=head1 SEE ALSO
356
357L<sh(1)>, L<ssh(1)>
358
359=cut
Note: See TracBrowser for help on using the repository browser.