[1a8a08a] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
| 3 | use 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. |
---|
| 11 | sub 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. |
---|
| 28 | sub 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. |
---|
| 54 | sub 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. |
---|
| 119 | sub 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. |
---|
| 140 | getopts('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 | |
---|
| 148 | die "Must supply file, master and experiment" unless $master && $tcl && $eid; |
---|
| 149 | |
---|
| 150 | # Read a hash of per-testbed parameters from the local configurations. |
---|
| 151 | open(CONF, $config) || die "can't read testbed configutions from $config: $!\n"; |
---|
| 152 | while (<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 | } |
---|
| 166 | close(CONF); |
---|
| 167 | |
---|
| 168 | # Open a pipe to the splitter program and start it parsing the experiments |
---|
| 169 | open(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. |
---|
| 173 | while (<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 | } |
---|
| 230 | close(PIPE); |
---|
| 231 | die "No nodes in master testbed ($master)\n" unless $allocated{$master}; |
---|
| 232 | |
---|
| 233 | exit(0) unless $startem; |
---|
| 234 | |
---|
| 235 | # Start up the slave sub-experiments first |
---|
| 236 | TESTBED: |
---|
| 237 | for $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 |
---|
| 245 | if (&start_segment($master, $eid)) { |
---|
| 246 | $started{$master}++; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | # If any testbed failed, swap the rest out. |
---|
| 250 | if ( 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 | } |
---|
| 255 | print "Experiment started\n"; |
---|
| 256 | exit(0); # set the exit value |
---|
| 257 | |
---|
| 258 | =pod |
---|
| 259 | |
---|
| 260 | =head1 NAME |
---|
| 261 | |
---|
| 262 | B<splitter.pl> |
---|
| 263 | |
---|
| 264 | =head1 SYNOPSIS |
---|
| 265 | |
---|
| 266 | B<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 | |
---|
| 271 | B<splitter.pl> invokes the DETER experiment parser to split an annotated |
---|
| 272 | experiment into multiple sub-experments and instantiates the sub-experiments on |
---|
| 273 | their intended testbeds. Annotation is accomplished using the |
---|
| 274 | tb-set-node-testbed command, added to the parser. |
---|
| 275 | |
---|
| 276 | The testbed labels are meaningful based on their presence in the testbeds file. |
---|
| 277 | that file can be specified with the B<-c> option, and defaults to |
---|
| 278 | F<./testbeds>. The syntax is described below. |
---|
| 279 | |
---|
| 280 | The expreriment is split out into one experiment description per testbed in the |
---|
| 281 | current directory named as F<experiment.testbed.tcl> where the experiment is |
---|
| 282 | the argument to B<-e> and the testbed is the tb-set-node-testbed parameter for |
---|
| 283 | the nodes in the file. |
---|
| 284 | |
---|
| 285 | If the B<-n> option is absent the sub-experiments are then instantiated on |
---|
| 286 | their testbeds. (Here B<-n> is analogous to its use in L<make(1)>). |
---|
| 287 | Per-testbed parameters are set in the configuration file. Sub-experiments on |
---|
| 288 | slave testbeds are instantiated in a random order, but the master testbed is |
---|
| 289 | currently instantiated last. |
---|
| 290 | |
---|
| 291 | If any sub-experiment fails to instantiate, the other sub-exeriments are |
---|
| 292 | swapped out. |
---|
| 293 | |
---|
| 294 | =head2 Configuration file |
---|
| 295 | |
---|
| 296 | The configuration file (F<./testbeds> unless overridden by B<-c>) is a |
---|
| 297 | colon-separated set of parameters keyed by testbed name. The fields, in order, |
---|
| 298 | are: |
---|
| 299 | |
---|
| 300 | =over 5 |
---|
| 301 | |
---|
| 302 | =item name |
---|
| 303 | |
---|
| 304 | The testbed to which this line of parameters applies. |
---|
| 305 | |
---|
| 306 | =item user |
---|
| 307 | |
---|
| 308 | The user under which to make requests to this testbed. The user running |
---|
| 309 | B<splitter.pl> must be able to authenicate as this user under L<ssh(1)> to this |
---|
| 310 | testbed. |
---|
| 311 | |
---|
| 312 | =item project |
---|
| 313 | |
---|
| 314 | The project under which to instantiate sub-experiments on this testbed. |
---|
| 315 | |
---|
| 316 | =item gateway type |
---|
| 317 | |
---|
| 318 | The node type for inter-testbed gateway nodes on this testbed. |
---|
| 319 | |
---|
| 320 | =item experiment start (slave) |
---|
| 321 | |
---|
| 322 | The start command to run on experimental nodes when this testbed is used as a |
---|
| 323 | slave. |
---|
| 324 | |
---|
| 325 | =item gateway start (slave) |
---|
| 326 | |
---|
| 327 | The start command to run on gateway nodes when this testbed is used as a |
---|
| 328 | slave. |
---|
| 329 | |
---|
| 330 | =item experiment start (master) |
---|
| 331 | |
---|
| 332 | The start command to run on experimental nodes when this testbed is used as a |
---|
| 333 | master. |
---|
| 334 | |
---|
| 335 | =item gateway start (master) |
---|
| 336 | |
---|
| 337 | The start command to run on gateway nodes when this testbed is used as a |
---|
| 338 | master. |
---|
| 339 | |
---|
| 340 | =item gateway image |
---|
| 341 | |
---|
| 342 | The disk image to be loaded on a gateway node on this testbed. |
---|
| 343 | |
---|
| 344 | =back |
---|
| 345 | |
---|
| 346 | The parsing of the configuration is extremely simple. Colons separate each |
---|
| 347 | field and there is n provision for escaping them at this time. |
---|
| 348 | |
---|
| 349 | =head1 ENVIRONMENT |
---|
| 350 | |
---|
| 351 | B<splitter.pl> does not directly make use of environment variables, but calls |
---|
| 352 | out to L<ssh(1)> and (indirectly) to L<sh(1)>, which may be influenced by the |
---|
| 353 | environment. |
---|
| 354 | |
---|
| 355 | =head1 SEE ALSO |
---|
| 356 | |
---|
| 357 | L<sh(1)>, L<ssh(1)> |
---|
| 358 | |
---|
| 359 | =cut |
---|