sitecheckin_client.in 3.95 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 50 51 52 53 54 55 56
#!/usr/bin/perl -w
#
# Copyright (c) 2000-2012 University of Utah and the Flux Group.
# 
# {{{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/>.
# 
# }}}
#
use English;
use strict;
use Getopt::Std;
use Data::Dumper;
use URI::Escape;
use File::Temp qw(tempfile);

#
# Phone home back to the mother ship and tell it about Emulab sites.
# Nothing sensitive is sent, just stuff like what version is running,
# commit hash of the code, URL, etc. You can opt out with a setting
# in your defs file.
#
# NOSITECHECKIN=1
#
sub usage()
{
    print("Usage: sitecheckin_client\n");
    exit(-1);
}
my $optlist = "dn";
my $debug   = 0;
my $impotent= 0;

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $MAINSITE    = @TBMAINSITE@;
my $ELABINELAB  = @ELABINELAB@;
my $NOCHECKIN   = @NOSITECHECKIN@;
my $TBBASE      = "@TBBASE@";
57
my $MOTHERSHIP  = "https://www.emulab.net/sitecheckin.php";
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
my $EMULAB_CERT = "$TB/etc/emulab.pem";
my $OPENSSL     = "/usr/bin/openssl";
my $WGET	= "/usr/local/bin/wget";

#
# Do not run this script as root please.
#
if ($EUID) {
    die("*** $0:\n".
	"    Only root can run this script!\n");
}

exit(0)
    if ($NOCHECKIN || $ELABINELAB);

#
# Untaint the path
#
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/usr/bin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1;

#
# Load the Testbed support stuff.
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use emutil;

# Protos
sub fatal($);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"n"})) {
    $impotent = 1;
}
usage()
    if (@ARGV);

my $osvers = `uname -s -r`;
chomp($osvers);

#
# These are the fields that we have to put into the xml file we send.
#
my %xmlfields = ("certificate"  => undef,
		 "url"		=> $TBBASE,
		 "buildinfo"	=> undef,
		 "commithash"	=> undef,
		 "dbrev"	=> undef,
		 "install"	=> undef,
		 "os_version"	=> $osvers,
		 "perl_version"	=> $],
		 "tbops"	=> $TBOPS);

my $query_result =
    DBQueryFatal("select * from version_info order by name");

while (my ($name, $value) = $query_result->fetchrow_array()) {
    $xmlfields{$name} = $value
	if (exists($xmlfields{$name}));
}

#
# Use openssl to cut any cruft.
#
my $certificate = emutil::ExecQuiet("$OPENSSL x509 -in $EMULAB_CERT");
if ($?) {
    fatal("Could not load CA certificate from $EMULAB_CERT");
}
$xmlfields{"certificate"} = $certificate;

#
# Create the XML file to send.
#
my ($fh, $xmlfilename) = tempfile(UNLINK => !$debug);
fatal("Could not create temporary file")
    if (!defined($fh));

my $xmlstuff = "<emulab_site>";
foreach my $key (keys(%xmlfields)) {
    my $val = $xmlfields{$key};

    $xmlstuff .= "<attribute name=\"$key\">";
    $xmlstuff .= "<value>$val</value>";
    $xmlstuff .= "</attribute>";
}
$xmlstuff .= "</emulab_site>";
print $fh "xmlstuff=" . uri_escape($xmlstuff) . "\n";
close($fh);

if ($debug) {
    system("/bin/cat $xmlfilename");
}
system("$WGET --post-file=$xmlfilename --no-check-certificate ".
       "      -O /dev/null -nv -q $MOTHERSHIP");

unlink($xmlfilename);
exit(0);

sub fatal($) {
    my ($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}