aptevent_daemon.in 8.79 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2008-2018 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
# 
# {{{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);
}
44
my $optlist   = "dnsv";
45 46
my $debug     = 0;
my $impotent  = 0;
47
my $verbose   = 0;
48
my $count     = 0;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

#
# 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
66 67
sub HandleSliverStatus($$$$);
sub HandleSliceStatus($$$);
68
sub HandleImageStatus($$$);
69
sub HandleFrisbeeStatus($$$$);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
sub fatal($);
	  
#
# Turn off line buffering on output
#
$| = 1; 

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

#
# 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;
}
94 95 96
if (defined($options{"v"})) {
    $verbose = 1;
}
97 98 99 100 101 102 103 104

# 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;
105
require GeniUtil;
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
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!");
    }
}

#
131 132 133 134 135 136 137
# At the Mothership (Cloudlab Portal) we get events from the SSL
# enabled version of pubsubd, which is getting events from all of the
# clusters including the local cluster. 
#
# Otherwise, we listen on the regular event server since that is where
# the events first go anyway. It is only clusters that are part of
# Cloudlab that are forwarding to to the Mothership. 
138
#
139 140 141 142 143
my $url = "elvin://localhost";
if ($MAINSITE) {
    $url .= ":$PSDPORT";
}
my $localhandle = event_register($url, 0);
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
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 $slice     = event_notification_get_string($handle, $note, "slice");

173 174 175 176 177
    # Ignore extraneous events. They happen when listening to the local
    # cluster pubsubd.
    return
	if (! (defined($site) && defined($slice)));

178 179 180 181
    my $urn       = event_notification_get_string($handle, $note, "urn");
    my $type      = event_notification_get_string($handle, $note, "type");
    my $details   = event_notification_get_string($handle, $note, "details");

182 183 184 185 186 187 188 189
    #
    # 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;
    }
190 191 192
    # Debugging.
    return
	if (0 && $slice !~ /stoller/);
193
    $count++;
194
    
195
    if ($debug || $verbose) {
196
	print "Event: $count $time $site $type $urn $slice $details\n";
Leigh Stoller's avatar
Leigh Stoller committed
197
    }
198 199 200 201
    my $instance = APT_Instance->LookupBySlice($slice);
    return
	if (!defined($instance));

202 203 204 205
    if (0) {
	goto done;
    }

206
    if ($type eq "SLIVERSTATUS") {
207 208 209 210 211
	HandleSliverStatus($site, $urn, $instance, $details);
	goto done;
    }
    elsif ($type eq "SLICESTATUS") {
	HandleSliceStatus($site, $instance, $details);
212 213 214 215 216 217
	goto done;
    }
    elsif ($type eq "IMAGESTATUS") {
	HandleImageStatus($site, $instance, $details);
	goto done;
    }
218
    elsif ($type eq "FRISBEESTATUS") {
219
	HandleFrisbeeStatus($site, $urn, $instance, $details);
220 221
	goto done;
    }
222
  done:
223 224 225
    # This HAS TO BE DONE, to break a circular dependency that causes
    # the daemon to grow and grow till it consumes boss.
    $instance->Purge();
226 227
    emutil::FlushCaches();
    GeniUtil::FlushCaches();
228 229 230 231 232
}

#
# Handle an Sliverstatus event.
#
233
sub HandleSliverStatus($$$$)
234
{
235 236
    my ($site, $sliver_urn, $instance, $json) = @_;

237
    #print "HandleSliverStatus: $site, $sliver_urn, $instance\n";
238 239 240 241 242 243 244

    if (exists($instance->AggregateHash()->{$site})) {
	my $sliver = $instance->AggregateHash()->{$site};
	if ($impotent) {
	    print "Would update sliver status for $sliver from details\n";
	}
	else {
245
	    if ($debug || $verbose) {
246
		print "Updating sliver status for sliver from $json\n";
247
	    }
248
	    my $hash = eval { decode_json($json) };
249
	    if ($@) {
250
		print STDERR "Could not decode json data: $json\n";
251 252
		return;
	    }
253 254 255 256 257 258 259 260 261 262 263 264
	    $sliver->UpdateSliverStatus($sliver_urn, $hash, $json);
	}
    }
}

#
# Handle an Slice status event.
#
sub HandleSliceStatus($$$)
{
    my ($site, $instance, $json) = @_;

265
    #print "HandleSliceStatus: $site, $instance\n";
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

    if (exists($instance->AggregateHash()->{$site})) {
	my $sliver = $instance->AggregateHash()->{$site};
	if ($impotent) {
	    print "Would update slice status for $sliver from details\n";
	}
	else {
	    if ($debug || $verbose) {
		print "Updating slice status for sliver from $json\n";
	    }
	    my $hash = eval { decode_json($json) };
	    if ($@) {
		print STDERR "Could not decode json data: $json\n";
		return;
	    }
	    $sliver->UpdateSliceStatus($hash, $json);
282 283 284 285 286 287 288 289 290
	}
    }
}

#
# Handle an IMAGESTATUS event.
#
sub HandleImageStatus($$$)
{
291
    my ($site, $instance, $json) = @_;
292 293 294 295 296 297

    if (exists($instance->AggregateHash()->{$site})) {
	if ($impotent) {
	    print "Would update image status for $instance from details\n";
	}
	else {
298
	    if ($debug || $verbose) {
299
		print "Updating image status for instance from $json\n";
300
	    }
301
	    my $hash = eval { decode_json($json) };
302
	    if ($@) {
303
		print STDERR "Could not decode json data: $json\n";
304 305
		return;
	    }
306
	    $instance->UpdateImageStatusNew($hash, $json);
307 308 309 310
	}
    }
}

311 312 313
#
# Handle an FRISBEESTATUS event.
#
314
sub HandleFrisbeeStatus($$$$)
315
{
316
    my ($site, $sliver_urn, $instance, $json) = @_;
317 318 319

    if (exists($instance->AggregateHash()->{$site})) {
	my $sliver = $instance->AggregateHash()->{$site};
320
	if ($impotent) {
321 322 323
	    print "Would update frisbee status for $sliver from details\n";
	}
	else {
324
	    if ($debug || $verbose) {
325
		print "Updating frisbee status for sliver from $json\n";
326
	    }
327
	    my $hash = eval { decode_json($json) };
328
	    if ($@) {
329
		print STDERR "Could not decode json data: $json\n";
330 331
		return;
	    }
332
	    $sliver->UpdateFrisbeeStatusNew($sliver_urn, $hash, $json);
333 334 335 336
	}
    }
}

337 338 339 340 341 342 343 344 345 346 347 348 349 350
#
# Setup a signal handler for newsyslog.
#
sub handler()
{
    my $SAVEEUID = $EUID;
    
    $EUID = 0;
    ReOpenLog($LOGFILE);
    $EUID = $SAVEEUID;
}
$SIG{HUP} = \&handler
    if (! ($debug || $impotent));

351 352 353 354 355 356 357 358 359 360
#
# Loop processing events.
#
while (1)
{
    $gotone = 1;
    while ($gotone) {
	$gotone = 0;
	event_poll($localhandle);
    }
361
    event_poll_blocking($localhandle, 100);
362 363
}

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
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");
}