tbrsync.in 5.75 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
Timothy Stack's avatar
Timothy Stack committed
4
# Copyright (c) 2004, 2005 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
# All rights reserved.
#

#
# tbrsync - An emulab frontend to rsync.
#

use lib '@prefix@/lib';
my $TB = '@prefix@';
 
use libdb;
use English;
use Getopt::Long;
use File::Basename;

#
# We have to be setuid root so that we can ssh into stargates as root
#
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
}

# un-taint path
$ENV{'PATH'} = "/bin:/usr/bin:/usr/local/bin:$TB/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
 
use strict;

#
# Constants
#
my $RSYNC  = "/usr/local/bin/rsync";
my $SSHTB  = "$TB/bin/sshtb";
Timothy Stack's avatar
Timothy Stack committed
39
my $POWER  = "$TB/bin/power";
40 41 42 43 44 45 46 47 48 49 50 51 52
my $DEBUG  = 1;

#
# Handle command-line arguments
# TODO: Allow a user to specify some of their own arguments to uisp
#
sub usage() {
    warn "Usage: $0 [-p <pid> -e <eid>] [--dry] [--nodelete] <operation> [dirname] <nodes...>\n";
    warn "Supported operations: upload\n";
    warn "[dirname] is required with the 'upload' operation\n";
    return 1;
}

53 54
sub dprint(@);

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
my %opt = ();
GetOptions(\%opt, 'p=s','e=s','dry','nodelete');

if (($opt{e} && ! $opt{p}) || (!$opt{e} && $opt{p})) {
    warn "-e and -p must be used togehter\n";
    die usage;
}

my ($eid, $pid);
if ($opt{e}) {
    $eid = $opt{e};
    $pid = $opt{p};
}

my ($dryrun, $delete) = (0, 1);
if ($opt{dry}) {
    $dryrun = 1;
}
if ($opt{nodelete}) {
    $delete = 0;
}

my $operation = shift @ARGV;
my $dirname;
if (!$operation) {
    exit usage();
}
# Check the operation type
# XXX - support the other operations uisp supports, like downloading code
SWITCH: for ($operation) {
    /^upload$/ && do {
	$dirname = shift @ARGV;
	if (!$dirname) {
	    exit usage();
	}
	last SWITCH;
    };
    
    # Default
    warn "Uknown operation $operation\n";
    exit usage();
}

# They have to give us at least one node, unless they gave a pid or eid, in
# which case we take that to mean all nodes in the experiment
my @nodes = @ARGV;
if (!@nodes && !$eid) {
    exit usage();
}

# Perm check on the eid and pid
if ($eid) {
    if (!TBExptAccessCheck($UID,$pid,$eid,TB_EXPT_READINFO)) {
	die "*** You do not have permission to access nodes in\n" .
	    "     $pid/$eid\n";
    }
}

# If given an eid and a node list, translate the node names to physical ones
if ($eid && @nodes) {
    my @tmp;
    foreach my $node (@nodes) {
	my $physnode;
	if (!VnameToNodeid($pid,$eid,$node,\$physnode)) {
	    die "*** No such node $node in $pid/$eid\n";
	}
	push @tmp, $physnode;
    }
    @nodes = @tmp;
}

# If given an eid and no node list, grab all nodes in the experiment
if (!@nodes && $eid) {
    @nodes = ExpNodes($pid, $eid);
    if (!@nodes) {
	die "*** Unable to get nodes in experiment $pid/$eid\n";
    }
}

#
# Taint check the dirname
#
if ($dirname =~ /^([-\w\/.]+)$/) {
    $dirname = $1;
} else {
    die("*** Tainted directory name: $dirname\n");
}

# Strip trailing slash, if present
$dirname =~ s/(^.*)\/$/$1/;

#
# Taint check the node names
#
@nodes = map {
    if (/^([-\w]+)$/) {
	$1;
    } else {
	die("*** Tainted node name: $_\n");
    }
} @nodes;

#
# Permission check
#
if ($UID && !TBNodeAccessCheck($UID,TB_NODEACCESS_LOADIMAGE,@nodes)) {
    die "You do not have permission to modify one or more nodes\n";
}

#
# Check the file to make sure it's readable - note, we want to make sure it's
# readable by the real uid, since w'ere setuid root
#
if ($dirname) {
    if (!-R $dirname) {
	die "$dirname not readable\n";
    }
}

#
# Rsync each node.
#
my $errors = 0;
NODE: foreach my $node (@nodes) {

    #
    # XXX: Need to check that the node is actually imageable via rsync!
    #

    #
    # Figure out the parameters we need to pass to rsync for this node/op.
    # global/common stuff added here..
    #
    my @rsync_args = "-ar";

David Johnson's avatar
David Johnson committed
190 191
    push @rsync_args, "--rsh=\"$TB/bin/sshtb -host \"";

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    # Are we deleting files that only exist at the destination?
    if ($delete) {
        push @rsync_args, "--delete";
    }

    # Are we doing this for real or testing?
    if ($dryrun) {
        push @rsync_args, "--dry-run";
    }

    #
    # Check for the existence of rsync exclude/include lists
    #
    my $nameprefix = basename($dirname);
    my $exclfile   = $nameprefix . ".exclude";
    my $inclfile   = $nameprefix . ".include";

    if (-R "$dirname/$exclfile") {
        push @rsync_args, "--exclude-from=$dirname/$exclfile";
    }

    if (-R "$dirname/$inclfile") {
        push @rsync_args, "--include-from=$dirname/$inclfile";
    }

    push @rsync_args, ("--exclude=$exclfile","--exclude=$inclfile");

    #
    # The operation we want to perform
    #
    my ($source, $dest);
    OSWITCH: for ($operation) {
	/^upload$/ && do {
            $source = $dirname . "/";
            $dest = "$node:/";
	    last OSWITCH;
	};

	# No default, we've checked for a valid operation above
    }

Timothy Stack's avatar
Timothy Stack committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    my $nodestate;
    if (! TBGetNodeEventState($node, \$nodestate) ||
	$nodestate eq TBDB_NODESTATE_POWEROFF) {
	warn "$node: power cycling";
	
	system("$POWER cycle $node");
	if ($?) {
	    $errors++;
	    warn "$node failed to power up.";
	    next NODE;
	}
    }
    
    my $actual_state;
    if (TBNodeStateWait($node,
			time,
			(60*6),
			\$actual_state,
251
			(TBDB_NODESTATE_ISUP,TBDB_NODESTATE_TBFAILED))) {
Timothy Stack's avatar
Timothy Stack committed
252 253 254 255 256
	$errors++;
	warn "$node is not up.";
	next NODE;
    }
    
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    #
    # Actually run rsync
    # TODO - Squelch output
    # TODO - Allow for some parallelism
    #
    print "Synching $node with $dirname\n";
    my $commandstr = "$RSYNC " . join(" ",@rsync_args) . " $source $dest";

    # Need to be root for the next part
    $UID = $EUID;

    dprint("$commandstr\n");
    if (system($commandstr)) {
	$errors++;
	warn "Failed to sync node $node";
    }
}

if ($errors) {
    exit 1;
} else {
    exit 0;
}

sub dprint(@) {
    if ($DEBUG) {
	print @_;
    }
}