plabmsrenew.pl.in 7.45 KB
Newer Older
1 2 3 4 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 39 40 41 42 43 44 45 46 47 48 49
#!/usr/local/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#
#
# This is a simple little script that renews "meta" slices listed either in a 
# file  or on the command line.  The Emulab metaslices were all created through
# the PlanetLab webpage, so are "plc-instantiated," and thus cannot be renewed
# through the xmlrpc interface.  Therefore, this script logs into the web 
# interface and renews the requested slices.
#

use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use HTTP::Headers;
use HTTP::Cookies;
use Date::Parse;
use English;

use lib "@prefix@/lib";
use libtestbed;

$PLAB_CREDS_FILE = "@prefix@/etc/plab/plc.pw";
$PLAB_METASLICE_FILE = "@prefix@/etc/plab/metaslices";

$WEEK = (60*60*24*7);
$SLICE_EXP_WINDOW = $WEEK * 1;

my @slices;
my $sfile = $PLAB_METASLICE_FILE;

# grab args
while ($#ARGV > 1) {
    $arg = shift;
    if ($arg eq "-f" || $arg eq "--file") {
	$sfile = shift;
	if (!defined($sfile)) {
	    print "ERROR: -f,--file require a file with slices to renew\n";
	    exit(1);
	}
    }
    else {
	push @slices, $arg;
    }
}

# open the file and grab slices
50 51 52 53 54 55 56 57 58
if (!scalar(@slices)) {
    open(SFILE,"$sfile") 
	or die "could not open slice file '$sfile'!";
    while (my $line = <SFILE>) {
	chomp($line);
	
	if (!($line =~ /^\#/ || $line eq '')) {
	    push @slices, $line;
	}
59
    }
60
    close(SFILE);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
}

# read in our plc credentials
open(PFILE,"$PLAB_CREDS_FILE")
    or die "could not open plc uid/passwd file '$PLAB_CREDS_FILE'!";
my @creds = <PFILE>;
close(PFILE);
if (scalar(@creds) < 2) {
    print "ERROR: malformed credentials file '$PLAB_CREDS_FILE'!\n";
    exit(1);
}
chomp($creds[0]);
chomp($creds[1]);

my $uid = $creds[0];
my $passwd = $creds[1];

#print "DEBUG: uid=$uid,passwd=$passwd\n";


81 82 83 84 85
$PLC_LOGIN = 'https://www.planet-lab.org/db/login.php';
$PLC_SLICE_LIST = 'https://www.planet-lab.org/db/slices/index.php';
# this doesn't give us hh:mm:ss of the exp date; have to check renew page.
#$PLC_SLICE_DETAIL = 'https://www.planet-lab.org/db/slices/index.php?id=';
$PLC_RENEW = 'https://www.planet-lab.org/db/slices/renew_slice.php?id=';
86
$PLC_RENEW_NOARG = 'https://www.planet-lab.org/db/slices/renew_slice.php';
87 88
$PLC_SLICE_DETAIL = $PLC_RENEW;
$PLC_LOGOUT = 'https://www.planet-lab.org/db/logout.php';
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

print "Checking slices";
foreach my $slice (@slices) {
    print " $slice";
}
print ".\n";

my $ua = LWP::UserAgent->new();
# make a cookie jar to store the PHPSESSID cookie
$ua->cookie_jar({});

my ($req,$res);
my $phpsessid;

# log into planetlab website
#$req = HTTP::Request->new(POST => $PLC_LOGIN);
#$req->content_type('application/x-www-form-urlencoded');
#$req->content('email=$uid&password=$passwd');
$req = POST $PLC_LOGIN, [ email => $uid, password => $passwd ];

$res = $ua->request($req);
#print $res->as_string();

112 113 114 115 116 117 118 119 120 121 122 123
# ugh, now we have to dig out the slice_name->slice_id mapping so we can get
# to the details page for each slice.

my %slicemap = ();

$req = HTTP::Request->new(GET => "$PLC_SLICE_LIST");
$res = $ua->request($req);

my $page = $res->content();
my @plines = split('\n',$page);

foreach my $line (@plines) {
124
    if ($line =~ /href=\'\/db\/slices\/index\.php\?id\=(\d+)\'>([_a-zA-Z0-9]+)/) {
125
	$slicemap{$2} = $1;
126
	#print "DEBUG: slice id for $2 is $1\n";
127 128 129
    }
}

130 131 132 133 134 135 136 137 138
# now figure out, for each slice, what is the current exp date
# if the slice is due to expire in less than a week, renew it
#   by selecting one of the options in the drop-down box

my %ok;
my %errors;
my %sliceexp;

my $now = time();
139
my $line;
140
foreach my $slice (@slices) {
141 142 143 144 145

    # check if we actually got this slice in the map from PLC:
    if (!defined($slicemap{$slice})) {
	print "WARNING: cannot determine id for slice '$slice'!\n";
	$errors{$slice} = "Cannot determine id for slice '$slice'!\n";
146
	next;
147 148
    }

149 150 151 152
    # dig out the exp date
    # we have to look at the detail page, not the renew page, to get the date,
    # because the renew page will just tell us we can't renew if the slice has
    # has been recently renewed.
153
    $req = HTTP::Request->new(GET => "$PLC_SLICE_DETAIL" . $slicemap{$slice});
154 155 156 157 158 159 160 161 162 163 164 165
    $res = $ua->request($req);

    #print $res->as_string();

    my $page = $res->content();
    my @plines = split('\n',$page);
    my $i;
    my $expdate_str = '';

    my $surl = '';
    my $sdesc = '';

166 167
    foreach $line (@plines) {
	if ($line =~ /Expiration Date: .+ (\w+\-\d+\-\d+ \d+:\d+:\d+ \w+)/) {
168
	    $expdate_str = $1;
169 170
	    $expdate_str =~ tr/\-/ /;
	    $sliceexp{$slice} = $expdate_str;
171
	    #print "DEBUG: $slice expires at '$expdate_str'\n";
172 173 174 175 176
	    last;
	}
    }
    
    if ($expdate_str ne '') {
177 178
	# this should be the same exptime as in the 'expires' hidden field
        # in the renew form.
179
	my $exptime = str2time($expdate_str);
180
	#print "slice '$slice' expires in " . ($exptime - $now) . "s at $exptime\n";
181 182 183 184 185 186 187 188 189 190 191 192

	if ($exptime < $now) {
	    $errors{$slice} = "Slice '$slice' already expired!";
	    print "WARNING: slice '$slice' already expired!\n";
	}
	elsif (($exptime - $now) > $SLICE_EXP_WINDOW) {
	    $ok{$slice} = "Slice does not expire for " . (($exptime - $now)/(60*60*24)) . " days\n";
	}
	else {
	    # need a renew:
	    # first GET the renew page so we can parse out the description
	    # and url fields in the form (they're easiest to grab here)
193
	    $req = HTTP::Request->new(GET => "$PLC_RENEW" . $slicemap{$slice});
194 195 196 197
	    $res = $ua->request($req);

	    $page = $res->content();
	    @plines = split('\n',$page);
198 199
	    foreach $line (@plines) {
		if ($line =~ /\<input.*name=[\"\']{1}url[\"\']{1} value=[\"\']{1}(.*)[\"\']{1} \/\>/) {
200 201 202
		    # found the url
		    $surl = $1;
		}
203
		elsif ($line =~ /\<textarea name=[\"\']{1}description[\"\']{1}.*\>(.*)\<\/textarea/) {
204 205 206 207 208 209 210 211 212 213
		    # found the description
		    $sdesc = $1;
		}
	    }

	    if ($surl eq '' || $sdesc eq '') {
		print "WARNING: could not get url and description for slice '$slice'; cannot renew!\n";
		$errors{$slice} = "could not get url and description for slice '$slice'";
	    }
	    else {
214 215 216

		#print "DEBUG: got '$surl' and '$sdesc' for url and desc\n";

217 218 219
		# always push it out for a month -- the longest item in the 
		# page (at least it was when I looked in 12/2006)
		# the page should accept renews up to 8 weeks though...
220 221 222 223 224 225
		$req = POST $PLC_RENEW_NOARG, 
		           [ 'id' => $slicemap{$slice},
			     'expires' => $exptime,
			     'submitted' => 'Renew Slice', 
			     'url' => $surl,
			     'description' => $sdesc,
226
			     'expire_len' => 4 ];
227 228
		$res = $ua->request($req);

229
		#print $res->as_string();
230 231 232 233 234 235 236 237 238

		$ok{$slice} = "Renewed";
	    }
	}
    }
    else {
	print "WARNING: could not determine slice expiration time for '$slice'\n";
	$errors{$slice} = "couldn't get expiration time for slice '$slice'";
    }
239
    
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
}


# logout
$req = HTTP::Request->new(GET => $PLC_LOGOUT);
$res = $ua->request($req);

# spam time!
# XXX: this goes to my (dmj) account right now until I am sure that things
# are working good...
my $TBOPS = "johnsond\@flux.utah.edu";

my $mailcontent = "Meta Slice Renew Report:\n\n";
foreach my $slice (@slices) {
    $mailcontent .= 
	"Slice '$slice':\n" . 
        "  Expiration time: ".$sliceexp{$slice}."\n";
    if (defined($errors{$slice})) {
	$mailcontent .= "  ERROR: " . $errors{$slice} . "\n";
    }
    elsif (defined($ok{$slice})) {
	$mailcontent .= "  Success: " . $ok{$slice} . "\n";
    }
    else {
	$mailcontent .= "  Nothing identifiable happened!\n";
    }
    $mailcontent .= "\n";
}

print "Sending report.\n";

SENDMAIL("Testbed Operators <$TBOPS>",
	 "plabmsrenew did something",
	 "$mailcontent",
	 "plabmsrenew <$TBOPS>");

exit(0);