plabmsrenew.pl.in 8.13 KB
Newer Older
1 2 3
#!/usr/local/bin/perl -w
#
# Copyright (c) 2006 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#
#
# 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
67 68 69 70 71 72 73 74 75
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;
	}
76
    }
77
    close(SFILE);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
}

# 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";


98 99 100 101 102
$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=';
103
$PLC_RENEW_NOARG = 'https://www.planet-lab.org/db/slices/renew_slice.php';
104 105
$PLC_SLICE_DETAIL = $PLC_RENEW;
$PLC_LOGOUT = 'https://www.planet-lab.org/db/logout.php';
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

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();

129 130 131 132 133 134 135 136 137 138 139 140
# 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) {
141
    if ($line =~ /href=\'\/db\/slices\/index\.php\?id\=(\d+)\'>([_a-zA-Z0-9]+)/) {
142
	$slicemap{$2} = $1;
143
	#print "DEBUG: slice id for $2 is $1\n";
144 145 146
    }
}

147 148 149 150 151 152 153 154 155
# 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();
156
my $line;
157
foreach my $slice (@slices) {
158 159 160 161 162

    # 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";
163
	next;
164 165
    }

166 167 168 169
    # 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.
170
    $req = HTTP::Request->new(GET => "$PLC_SLICE_DETAIL" . $slicemap{$slice});
171 172 173 174 175 176 177 178 179 180 181 182
    $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 = '';

183 184
    foreach $line (@plines) {
	if ($line =~ /Expiration Date: .+ (\w+\-\d+\-\d+ \d+:\d+:\d+ \w+)/) {
185
	    $expdate_str = $1;
186 187
	    $expdate_str =~ tr/\-/ /;
	    $sliceexp{$slice} = $expdate_str;
188
	    #print "DEBUG: $slice expires at '$expdate_str'\n";
189 190 191 192 193
	    last;
	}
    }
    
    if ($expdate_str ne '') {
194 195
	# this should be the same exptime as in the 'expires' hidden field
        # in the renew form.
196
	my $exptime = str2time($expdate_str);
197
	#print "slice '$slice' expires in " . ($exptime - $now) . "s at $exptime\n";
198 199 200 201 202 203 204 205 206 207 208 209

	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)
210
	    $req = HTTP::Request->new(GET => "$PLC_RENEW" . $slicemap{$slice});
211 212 213 214
	    $res = $ua->request($req);

	    $page = $res->content();
	    @plines = split('\n',$page);
215 216
	    foreach $line (@plines) {
		if ($line =~ /\<input.*name=[\"\']{1}url[\"\']{1} value=[\"\']{1}(.*)[\"\']{1} \/\>/) {
217 218 219
		    # found the url
		    $surl = $1;
		}
220
		elsif ($line =~ /\<textarea name=[\"\']{1}description[\"\']{1}.*\>(.*)\<\/textarea/) {
221 222 223 224 225 226 227 228 229 230
		    # 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 {
231 232 233

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

234 235 236
		# 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...
237 238 239 240 241 242
		$req = POST $PLC_RENEW_NOARG, 
		           [ 'id' => $slicemap{$slice},
			     'expires' => $exptime,
			     'submitted' => 'Renew Slice', 
			     'url' => $surl,
			     'description' => $sdesc,
243
			     'expire_len' => 4 ];
244 245
		$res = $ua->request($req);

246
		#print $res->as_string();
247 248 249 250 251 252 253 254 255

		$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'";
    }
256
    
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 286 287 288 289 290 291 292 293
}


# 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);