source: fedkit/splitter.pl @ c23025e

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

pre-testing commit

  • Property mode set to 100644
File size: 15.4 KB
Line 
1#!/usr/bin/perl
2
3use Getopt::Std;
4
5@scripts = ("federate.sh", "smbmount.pl");
6$local_script_dir = ".";
7
8# use scp to transfer a file, reporting true if successful and false otherwise.
9# Parameters are the local file name, the ssh host destination (either hostname
10# oe user@host), and an optional destination file name or directory.  If no
11# destination is given, the file is transferred to the given user's home
12# directory.  If only a machine is given in the ssh host destination, the
13# current user is used.
14sub scp_file {
15    my($file, $where, $dest) = @_;
16
17    # XXX system with a relative pathname is sort of gross
18    system("scp $file $where:$dest");
19    if ($?) {
20        warn "scp failed $?\n";
21        return 0;
22    }
23    else { return 1; }
24}
25
26# use ssh to execute the given command on the machine (and as the user) in
27# $where.  Parameters are the ssh destination directive ($where) and the
28# command to execute, and a prefix to be placed on a message generated if the
29# command fails.   On failure print a warning if a warning prefix was given and
30# return false.
31sub ssh_cmd {
32    my($user, $host, $cmd, $wname) = @_;
33
34    # XXX system with a relative pathname is sort of gross
35    # if ( $host ne "localhost") {
36        #system ("ssh $user\@$host $cmd");
37        #}
38    #else {
39        #system ("$cmd");
40        #}
41    system ("ssh $user\@$host $cmd");
42    if ($?) {
43        warn "$wname failed $?\n" if $wname;
44        return 0;
45    }
46    else { return 1; }
47}
48
49# Ship local copies of the federation scripts out to the given host.  If any of
50# the script transfers fails, return 0.  The scripts to transfer are from the
51# global @scripts and are found locally in $local_script_dir (another global).
52sub ship_scripts {
53    my($host, $user, $dest_dir) = @_;       # Where, who, where remotely
54    my($s);
55
56    for $s (@scripts) {
57        &scp_file("$local_script_dir/$s", "$user\@$host", $dest_dir) || 
58            return 0;
59    }
60    return 1;
61}
62
63
64
65# Start a sub section of the experiment on a given testbed.  The testbed and
66# the user to start the experiment as are pulled from the global per-testbed
67# hash, as is the project name on the remote testbed.  Parameters are the
68# testbed and the experiment id.  Configuration files are scp-ed over to the
69# target testbed from the global $tmpdir/$tb directory.  Then the current state
70# of the experiment determined using expinfo.  From that state, the experiment
71# is either created, modified or spapped in.  If everything succeeds, true is
72# returned.
73sub start_segment {
74    my($tb, $eid) = @_;                     # testbed and experiment ID
75    my($host) = "$host{$tb}$domain{$tb}";   # Host name of remote ops (FQDN)
76    my($user) = $user{$tb};                 # user to pass to ssh
77    my($pid) = $project{$tb};               # remote project to start the
78                                            # experiment under
79    my($tclfile) = "./$eid.$tb.tcl";        # Local tcl file with the
80                                            # sub-experiment
81    my($proj_dir) = "/proj/$pid/exp/$eid/tmp";  # Where to stash federation stuff
82    my($to_hostname) = "$proj_dir/hosts";   # remote hostnames file
83
84    # Determine the status of the remote experiment
85    #if ( $host ne "localhost") {
86#       open(STATUS, "ssh $user\@$host /usr/testbed/bin/expinfo $pid $eid|") ||
87#           die "Can't ssh to $user\@$host:$!\n";
88#    }
89#    else {
90#       open(STATUS, "/usr/testbed/bin/expinfo $pid $eid|") ||
91#           die "Can't call expinfo locally";
92#    }
93    open(STATUS, "ssh $user\@$host /usr/testbed/bin/expinfo $pid $eid|") || 
94        die "Can't ssh to $user\@$host:$!\n";
95    # XXX: this is simple now.  Parsing may become more complex
96    while (<STATUS>) {
97        /State: (\w+)/ && ($state = $1);
98        /No\s+such\s+experiment/ && ($state = "none");
99    }
100    close(STATUS);
101    print "$tb: $state\n";
102
103    # Copy the experiment definition data over (unless the host is local)
104    #if ( $host ne "localhost") {
105#       &scp_file("$tmpdir/$tb/$tclfile", "$user\@$host") || return 0;
106#    }
107#    else {
108#       # XXX this ain't quite right
109#       system("cp $tmpdir/$tb/$tclfile .");
110#    }
111
112    &scp_file("$tmpdir/$tb/$tclfile", "$user\@$host") || return 0;
113    # Remote experiment is active.  Modify it.
114    if ($state eq "active") {
115        # First copy new scripts and hostinfo into the remote /proj
116        &scp_file("$tmpdir/$tb/hostnames", "$user\@$host", $to_hostname) ||
117            return 0;
118        &ship_scripts($host, $user, $proj_dir) || return 0;
119        &ssh_cmd($user, $host, "/usr/testbed/bin/modexp -r -s -w $pid " . 
120            "$eid $tclfile", "modexp") || return 0;
121        return 1;
122    }
123
124    # Remote experiment is swapped out, modify it and swap it in.
125    if ($state eq "swapped") {
126        &ssh_cmd($user, $host, "/usr/testbed/bin/modexp -w $pid $eid $tclfile", 
127            "modexp") || return 0;
128        # First copy new scripts and hostinfo into the remote /proj
129        &scp_file("./hostnames", "$user\@$host", $to_hostname) || return 0;
130        &ship_scripts($host, $user, $proj_dir) || return 0;
131        # Now start up
132        &ssh_cmd($user, $host, "/usr/testbed/bin/swapexp -w $pid $eid in", 
133            "swapexp") || return 0;
134        return 1;
135    }
136
137    # No remote experiment.  Create one.  We do this in 2 steps so we can put
138    # the configuration files and scripts into the new experiment directories.
139    if ($state eq "none") {
140        &ssh_cmd($user, $host, "/usr/testbed/bin/startexp -f -w -p " . 
141            "$pid -e $eid $tclfile", "startexp") || return 0;
142        # First copy new scripts and hostinfo into the remote /proj
143        &scp_file("./hostnames", "$user\@$host", $to_hostname) || return 0;
144        &ship_scripts($host, $user, $proj_dir) || return 0;
145        # Now start up
146        &ssh_cmd($user, $host, "/usr/testbed/bin/swapexp -w $pid $eid in", 
147            "swapexp") || return 0;
148        return 1;
149    }
150
151    # Every branch for a known state returns.  If execution gets here, the
152    # state is unknown.
153    warn "unknown state: $state\n";
154    return 0;
155}
156
157# Swap out a sub-experiment - probably because another has failed.  Arguments
158# are testbed and experiment.  Most of the control flow is similar to
159# start_segment, though much simpler.
160sub stop_segment {
161    my($tb, $eid) = @_;
162    my($user) = "$user{$tb}";
163    my($host) = "$host{$tb}$domain{$tb}";
164    my($pid) = $project{$tb};
165
166    &ssh_cmd($user, $host, "/usr/testbed/bin/swapexp -w $pid $eid out", 
167        "swapexp (out)") || return 0;
168    return 1;
169}
170
171# tcl program to split experiments
172# $tcl_splitter = "/usr/testbed/lib/ns2ir/parse.tcl";
173$tcl_splitter = "/users/faber/testbed/tbsetup/ns2ir/parse.tcl";
174$tclsh = "/usr/local/bin/otclsh";   # tclsh to call directly
175
176$pid = $gid = "dummy";              # Default project and group to pass to
177                                    # $tcl_splitter above.  These are total
178                                    # dummy arguments;  the splitter doesn't
179                                    # use them at all, but we supply them to
180                                    # keep our changes to the parser minimal.
181
182# Argument processing.
183getopts('d:c:m:e:f:nt:', \%opts);
184
185$eid = $opts{'e'};                  # Experiment ID
186$tcl = $opts{'f'} || shift;         # The experiment description
187$master = $opts{'m'};               # Master testbed
188$startem = $opts{'n'} ? 0 : 1;      # If true, start the sub-experiments
189$tmpdir = $opts{'t'} || "/tmp";             # where to collect tmp files
190$config = $opts{'c'} || "./testbeds";
191$local_script_dir = $opts{'d'};     # Local scripts
192
193$tmpdir .= "/split$$";
194
195unless (-d "$tmpdir") {
196    mkdir("$tmpdir") || die "Can't create $tmpdir: $!";
197}
198
199
200for $s (@scripts) {
201    die "$local_script_dir/$s not in local script directory. Try -d\n"
202        unless -r "$local_script_dir/$s";
203}
204
205die "Must supply file, master and experiment" unless $master && $tcl && $eid;
206
207# Read a hash of per-testbed parameters from the local configurations.
208open(CONF, $config) || die "can't read testbed configutions from $config: $!\n";
209while (<CONF>) {
210    next if /^#/;
211    chomp;
212    ($tb, $h, $d, $u, $p, $es, $gs, $mes, $mgs, $t, $i) = split(":", $_);
213    $host{$tb} = $h;
214    $user{$tb} = $u;
215    $domain{$tb} = $d;
216    $project{$tb} = $p;
217    $gwtype{$tb} = $t;
218    $expstart{$tb} = $es;
219    $gwstart{$tb} = $gs;
220    $mexpstart{$tb} = $mes;
221    $mgwstart{$tb} = $mgs;
222    $gwimage{$tb} = $i;
223
224    # Make sure the domain starts with a period
225    $domain{$tb} = ".$domain{$tb}" unless $domain{$tb} =~ /^\./;
226}
227close(CONF);
228
229# Open a pipe to the splitter program and start it parsing the experiments
230open(PIPE, "$tclsh $tcl_splitter -s -m $master -p $pid $gid $eid $tcl|") || 
231    die "Cannot execute $tclsh $tcl_splitter -s -p $pid $gid $eid $tcl:$!\n";
232
233# Parse the splitter output.
234while (<PIPE>) {
235    # Start of a sub-experiment
236    /^#\s+Begin\s+Testbed\s+\((\w+)\)/ && do {
237        $ctb = $1;
238
239        # If we know the testbed, start collecting its sub experiment tcl
240        # description.  If not, warn the caller and ignore the configuration of
241        # this testbed.
242        if ($host{$ctb}) {
243            $allocated{$ctb}++; # Keep track of the testbeds allocated
244
245            unless (-d "$tmpdir/$ctb") {
246                mkdir("$tmpdir/$ctb") || die "Can't create $tmpdir/$ctb: $!";
247            }
248            $destfile = "$tmpdir/$ctb/$eid.$ctb.tcl";
249
250            open(FILE, ">$destfile") || die "Cannot open $destfile:$!\n";
251        }
252        else { 
253            warn "No such testbed $ctb\n";
254            $destfile = "";
255        }
256        next;
257    };
258
259    # End of that experiment
260    /^#\s+End\s+Testbed\s+\((\w+)\)/ && do {
261        # Simple syntax check and close out this experiment's tcl description
262        die "Mismatched testbed markers ($1, $ctb)\n" unless ($1 eq $ctb);
263        close(FILE);
264        $destfile = $ctb = "";
265        next;
266    };
267
268    # Beginning of a gateway set
269    /^#\s+Begin\s+gateways\s+\((\w+)\)/ && do {
270        $gateways = $1;
271        # If we've heard of this tb, create the config lines for it one at a
272        # time.
273        if ($allocated{$gateways}) {
274            # Just in case.  This directory should already have been created
275            # above.
276            unless (-d "$tmpdir/$gateways") {
277                mkdir("$tmpdir/$gateways") || 
278                    die "Can't create $tmpdir/$gateways: $!";
279            }
280        }
281        else {
282            warn "Gateways given (and ignored) for testbed not in use: " .
283                "$gateways\n";
284            $gateways = 0;
285        }
286        next;
287    };
288    /^#\s+End\s+gateways\s+\((\w+)\)/ && do {
289        die "Mismatched gateway markers ($1, $gateways)\n" 
290            unless !$gateways || $gateways == $1;
291        $gateways = 0;
292        next;
293    };
294    # Beginning of the hostnames list.  Collection is always in the hostnames
295    # file.
296    /^#\s+Begin\s+hostnames/ && do {
297        $destfile = "$tmpdir/hostnames";
298        open(FILE, ">$destfile") || die "Can't open $destfile:$!\n";
299        next;
300    };
301    # end of the hostnames list.
302    /^#\s+End\s+hostnames/ && do {
303        close(FILE);
304        $destfile = "";
305        next;
306    };
307
308    # Generate gateway configuration info, one file per line
309    $gateways && do {
310        chomp;
311        my($dtb, $myname, $desthost, $type) = split(" ", $_);
312        my($sdomain) = $domain{$gateways};      # domain for the source
313        my($ddomain) = $domain{$dtb};           # domain for the destination
314
315        # If either end of this link is in the master side of the testbed, that
316        # side is the active end. Otherwise the first testbed encountered in
317        # the file will be the active end.  The $active_end variable keeps
318        # track of those decisions
319        if ( $dtb eq $master ) { $active = "false"; }
320        elsif ($gateways eq $master ) { $active = "true"; }
321        elsif ( $active_end{"$dtb-$gateways"} ) { $active="false"; }
322        else { $active_end{"$gateways-$dtb"}++; $active = "true"; }
323
324        # Write out the file
325        open(GWCONFIG, ">$tmpdir/$gateways/$myname$sdomain.gw.conf") || 
326            die "can't open $tmpdir/%gateways/$myname$sdomain.gw.conf: $!\n";
327        print GWCONFIG "Active: $active\n";
328        print GWCONFIG "Type: $type\n";
329        print GWCONFIG "Peer: $desthost$ddomain\n";
330        print GWCONFIG "Pubkeys: /placeholder\n";
331        print GWCONFIG "Privkeys: /placeholder\n";
332        close(GWCONFIG);
333
334        #done processing gateway entry, ready for next line
335        next; 
336    };
337
338
339    next unless $destfile;  # Unidentified testbed, ignore config
340
341    # Substitute variables
342    s/GWTYPE/$gwtype{$ctb}/g;
343    s/GWIMAGE/$gwimage{$ctb}/g;
344    if ($ctb eq $master ) {
345        s/GWSTART/$mgwstart{$ctb}/g;
346        s/EXPSTART/$mexpstart{$ctb}/g;
347    }
348    else {
349        s/GWSTART/$gwstart{$ctb}/g;
350        s/EXPSTART/$expstart{$ctb}/g;
351    }
352    print FILE;
353}
354close(PIPE);
355die "No nodes in master testbed ($master)\n" unless $allocated{$master};
356
357exit(0) unless $startem;
358
359# Start up the slave sub-experiments first
360TESTBED:
361for $tb (keys %allocated) {
362    if ($tb ne $master) {
363        if (&start_segment($tb, $eid)) { $started{$tb}++; }
364        else { last TESTBED; }
365    }
366}
367
368# Now the master
369if (&start_segment($master, $eid)) { 
370    $started{$master}++;
371}
372
373# If any testbed failed, swap the rest out.
374if ( scalar(keys %started) != scalar(keys %allocated)) {
375    for $tb (keys %started) { &stop_segment($tb, $eid); }
376    print "Error starting experiment\n";
377    exit(1);
378}
379print "Experiment started\n";
380exit(0);    # set the exit value
381
382=pod
383
384=head1 NAME
385
386B<splitter.pl>
387
388=head1 SYNOPSIS
389
390B<splitter.pl> B<-e> I<experiment> B<-m> I<master_testbed> [B<-n>]
391    [B<-d> F<script_dir>] [B<-c> F<config_file>] [B<-f> F<experiment_tcl>]
392    [F<experiment_tcl>]
393
394=head1 DESCRIPTION
395
396B<splitter.pl> invokes the DETER experiment parser to split an annotated
397experiment into multiple sub-experments and instantiates the sub-experiments on
398their intended testbeds.  Annotation is accomplished using the
399tb-set-node-testbed command, added to the parser.
400
401The testbed labels are meaningful based on their presence in the testbeds file.
402that file can be specified with the B<-c> option, and defaults to
403F<./testbeds>.  The syntax is described below.
404
405The expreriment is split out into one experiment description per testbed in the
406current directory named as F<experiment.testbed.tcl> where the experiment is
407the argument to B<-e> and the testbed is the tb-set-node-testbed parameter for
408the nodes in the file.
409
410If the B<-n> option is absent the sub-experiments are then instantiated on
411their testbeds.  (Here B<-n> is analogous to its use in L<make(1)>).
412Per-testbed parameters are set in the configuration file.  Sub-experiments on
413slave testbeds are instantiated in a random order, but the master testbed is
414currently instantiated last.
415
416Scripts to start federation are copied into the local experiment's tmp file -
417e.g., F</proj/DETER/exp/simple-split/tmp>.  These are taken from the directory
418given by the B<-d> option.
419
420If any sub-experiment fails to instantiate, the other sub-exeriments are
421swapped out.
422
423=head2 Configuration file
424
425The configuration file (F<./testbeds> unless overridden by B<-c>) is a
426colon-separated set of parameters keyed by testbed name.  The fields, in order,
427are:
428
429=over 5
430
431=item name
432
433The testbed to which this line of parameters applies.
434
435=item user
436
437The user under which to make requests to this testbed.  The user running
438B<splitter.pl> must be able to authenicate as this user under L<ssh(1)> to this
439testbed.
440
441=item host
442
443The host name of the testbed's ops node.  The user calling B<splitter.pl> must
444be able to execute commands on this host via L<ssh(1)>.
445
446=item domain
447
448The domain of nodes in this testbed (including the ops host).
449
450=item project
451
452The project under which to instantiate sub-experiments on this testbed.
453
454=item gateway type
455
456The node type for inter-testbed gateway nodes on this testbed.
457
458=item experiment start (slave)
459
460The start command to run on experimental nodes when this testbed is used as a
461slave.
462
463=item gateway start (slave)
464
465The start command to run on gateway nodes when this testbed is used as a
466slave.
467
468=item experiment start (master)
469
470The start command to run on experimental nodes when this testbed is used as a
471master.
472
473=item gateway start (master)
474
475The start command to run on gateway nodes when this testbed is used as a
476master.
477
478=item gateway image
479
480The disk image to be loaded on a gateway node on this testbed.
481
482=back
483
484The parsing of the configuration is extremely simple.  Colons separate each
485field and there is n provision for escaping them at this time.
486
487=head1 ENVIRONMENT
488
489B<splitter.pl> does not directly make use of environment variables, but calls
490out to L<ssh(1)> and (indirectly) to L<sh(1)>, which may be influenced by the
491environment.
492
493=head1 SEE ALSO
494
495L<sh(1)>, L<ssh(1)>
496
497=cut
Note: See TracBrowser for help on using the repository browser.