aptevent_daemon.in 7.08 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2008-2017 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 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
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use Data::Dumper;
use JSON;

#
# Look for APT things that need to be dealt with.
#
sub usage()
{
    print "Usage: aptevent_daemon [-d] [-s] [-n]\n";
    exit(1);
}
my $optlist   = "dns";
my $debug     = 0;
my $impotent  = 0;

#
# Configure variables
#
my $TB		     = "@prefix@";
my $TBOPS            = "@TBOPSEMAIL@";
my $TBLOGS           = "@TBLOGSEMAIL@";
my $MAINSITE         = @TBMAINSITE@;
my $LOGFILE          = "$TB/log/aptevent_daemon.log";
# Portal pubsubd running on this port.
my $PSDPORT          = 16507;
	  
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Protos
sub HandleSliverStatus($$$);
sub HandleImageStatus($$$);
66
sub HandleFrisbeeStatus($$$);
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
sub fatal($);
	  
#
# Turn off line buffering on output
#
$| = 1; 

if ($UID != 0) {
    fatal("Must be root to run this script\n");
}
if (! $MAINSITE) {
    exit(0);
}

#
# Check args early so we get the right DB.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"n"})) {
    $impotent = 1;
}

# Do this early so that we talk to the right DB.
use vars qw($GENI_DBNAME);
$GENI_DBNAME = "geni-cm";

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use emdb;
require GeniDB;
require GeniSlice;
require GeniSliver;
use libtestbed;
use emutil;
use libEmulab;
use APT_Instance;
use event;

if (!$impotent) {
    if (CheckDaemonRunning("aptevent_daemon")) {
	fatal("Not starting another aptevent daemon!");
    }
    # Go to ground.
    if (! $debug) {
	if (TBBackGround($LOGFILE)) {
	    exit(0);
	}
    }
    if (MarkDaemonRunning("aptevent_daemon")) {
	fatal("Could not mark daemon as running!");
    }
}

#
# Capture all events from the local pubsubd.
#
my $localhandle = event_register("elvin://localhost:$PSDPORT", 0);
if (!$localhandle) {
    fatal("Unable to register with event system");
}

#
# Subscribe to all events.
#
my $tuple = address_tuple_alloc();
if (!$tuple) {
    fatal("Could not allocate an address tuple");
}
if (!event_subscribe($localhandle, \&callback, $tuple)) {
    fatal("Could not subscribe to all events");
}

#
# Flag to know when there are no more events to process. 
#
my $gotone;

sub callback($$$)
{
    my ($handle, $note, $data) = @_;
    $gotone++;

    my $time      = time();
    my $site      = event_notification_get_site($handle, $note);
    my $urn       = event_notification_get_string($handle, $note, "urn");
    my $slice     = event_notification_get_string($handle, $note, "slice");
    my $type      = event_notification_get_string($handle, $note, "type");
    my $details   = event_notification_get_string($handle, $note, "details");

162 163 164 165 166 167 168 169
    #
    # Not sure why this is happening, but sometime the slice urn has
    # extra double quotes around it. Kill them so the instance lookup
    # does not fail. Someday I will figure out why this is happening.
    #
    if ($slice =~ /^\"(.*)\"$/) {
	$slice = $1;
    }
170 171 172 173
    # Debugging.
    return
	if (0 && $slice !~ /stoller/);
    
Leigh Stoller's avatar
Leigh Stoller committed
174 175 176
    if ($debug) {
	print "Event: $time $site $type $urn $slice $details\n";
    }
177 178 179 180 181 182 183 184 185 186 187 188
    my $instance = APT_Instance->LookupBySlice($slice);
    return
	if (!defined($instance));

    if ($type eq "SLIVERSTATUS") {
	HandleSliverStatus($site, $instance, $details);
	goto done;
    }
    elsif ($type eq "IMAGESTATUS") {
	HandleImageStatus($site, $instance, $details);
	goto done;
    }
189 190 191 192
    elsif ($type eq "FRISBEESTATUS") {
	HandleFrisbeeStatus($site, $instance, $details);
	goto done;
    }
193
done:
194
    $instance->Purge();
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
}

#
# Handle an Sliverstatus event.
#
sub HandleSliverStatus($$$)
{
    my ($site, $instance, $details) = @_;

    if (exists($instance->AggregateHash()->{$site})) {
	my $sliver = $instance->AggregateHash()->{$site};
	if ($impotent) {
	    print "Would update sliver status for $sliver from details\n";
	}
	else {
	    if ($debug) {
		print "Updating sliver status for sliver from $details\n";
	    }
	    $details = eval { decode_json($details) };
	    if ($@) {
		print STDERR "Could not decode json data: $details\n";
		return;
	    }
	    $sliver->UpdateWebStatus({$site => $details});
	}
    }
}

#
# Handle an IMAGESTATUS event.
#
sub HandleImageStatus($$$)
{
    my ($site, $instance, $details) = @_;

    if (exists($instance->AggregateHash()->{$site})) {
	if ($impotent) {
	    print "Would update image status for $instance from details\n";
	}
	else {
	    if ($debug) {
		print "Updating image status for instance from $details\n";
	    }
	    $details = eval { decode_json($details) };
	    if ($@) {
		print STDERR "Could not decode json data: $details\n";
		return;
	    }
	    $instance->UpdateImageStatus($details);
	}
    }
}

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
#
# Handle an FRISBEESTATUS event.
#
sub HandleFrisbeeStatus($$$)
{
    my ($site, $instance, $details) = @_;

    if (exists($instance->AggregateHash()->{$site})) {
	my $sliver = $instance->AggregateHash()->{$site};
	if (0 && $impotent) {
	    print "Would update frisbee status for $sliver from details\n";
	}
	else {
	    if ($debug) {
		print "Updating frisbee status for sliver from $details\n";
	    }
	    $details = eval { decode_json($details) };
	    if ($@) {
		print STDERR "Could not decode json data: $details\n";
		return;
	    }
	    $sliver->UpdateFrisbeeStatus({$site => $details});
	}
    }
}

274 275 276 277 278 279 280 281 282 283 284 285 286 287
#
# Setup a signal handler for newsyslog.
#
sub handler()
{
    my $SAVEEUID = $EUID;
    
    $EUID = 0;
    ReOpenLog($LOGFILE);
    $EUID = $SAVEEUID;
}
$SIG{HUP} = \&handler
    if (! ($debug || $impotent));

288 289 290 291 292 293 294 295 296 297 298 299 300
#
# Loop processing events.
#
while (1)
{
    $gotone = 1;
    while ($gotone) {
	$gotone = 0;
	event_poll($localhandle);
    }
    event_poll_blocking($localhandle, 1000);
}

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
exit(0);

sub fatal($)
{
    my ($msg) = @_;

    if (! ($debug || $impotent)) {
	#
	# Send a message to the testbed list. 
	#
	SENDMAIL($TBOPS,
		 "APT Event daemon died",
		 $msg,
		 $TBOPS);
    }
    MarkDaemonStopped("aptevent_daemon")
	if (!$impotent);

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