igevent_daemon.in 7.41 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
57
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/perl -w
#
# Copyright (c) 2008-2016 University of Utah and the Flux Group.
# 
# {{{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 Date::Parse;
use POSIX qw(strftime);

#
#
# 
sub usage()
{
    print "Usage: igevent_daemon [-d] [-i]\n";
    exit(1);
}
my $optlist   = "di";
my $debug     = 0;
my $impotent  = 0;

#
# Configure variables
#
my $TB		  = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
my $TBLOGS        = "@TBLOGSEMAIL@";
my $PGENIDOMAIN   = "@PROTOGENI_DOMAIN@";
my $OURDOMAIN     = "@OURDOMAIN@";
my $PGENISUPPORT  = @PROTOGENI_SUPPORT@;
my $LOGFILE       = "$TB/log/igevent_daemon.log";
my $MYURN	  = "urn:publicid:IDN+{OURDOMAIN}+authority+cm";
my $SLEEP_INTERVAL= 60;

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Protos
sub fatal($);
	  
#
# Turn off line buffering on output
#
$| = 1; 

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

#
# Exit if not a protogeni site.
#
if (! $PGENISUPPORT) {
    exit(0);
}

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

# 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 GeniUtil;
require GeniSliver;
require GeniSlice;
require GeniCertificate;
require GeniCredential;
require GeniAggregate;
use GeniResponse;
use Experiment;
use EmulabConstants;
use Node;
use libtestbed;
use emutil;
use libEmulab;
use event;

if (!$impotent && CheckDaemonRunning("igevent_daemon")) {
    fatal("Not starting another igevent daemon!");
}
# Go to ground.
if (! $debug) {
    if (TBBackGround($LOGFILE)) {
	exit(0);
    }
}
if (!$impotent && MarkDaemonRunning("igevent_daemon")) {
    fatal("Could not mark daemon as running!");
}
#
# Setup a signal handler for newsyslog.
#
sub handler()
{
    my $SAVEEUID = $EUID;
    
    $EUID = 0;
    ReOpenLog($LOGFILE);
    $EUID = $SAVEEUID;
}
$SIG{HUP} = \&handler
    if (! $debug);

GeniUtil::FlipToGeniUser();

#
# Capture all events from the local pubsubd and transform them into
# events to send to the portal event server via the local clusterd.
#
my $localhandle = event_register("elvin://localhost", 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, $notification, $data) = @_;
    $gotone++;

    my $site = event_notification_get_site($handle, $notification);
    my $objtype = event_notification_get_objtype($handle, $notification);
    
    #
    # If there is a URN, then the event was generated by Geni code.
    # We ignore it, forwarded by pubsub_forwarder.
    #
    if ($site && $site =~ /^urn:/) {
	return;
    }
    #
    # We are looking for node state change events to pass along.
    #
    return
	if (!defined($objtype) ||
	    ($objtype ne TBDB_TBEVENT_NODESTATE() &&
	     $objtype ne TBDB_TBEVENT_NODESTARTSTATUS() &&
197
	     $objtype ne TBDB_TBEVENT_NODESTATUS() &&
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
	     $objtype ne TBDB_TBEVENT_NODEACCOUNTS()));

    my $node_id = event_notification_get_objname($handle, $notification);
    my $state   = event_notification_get_eventtype($handle, $notification);

    #
    # We do not care about nodes that are not allocated to Geni experiments.
    # So we have to look up the node, get the reservation and check it. This
    # seems like a lot of overhead, but these events are not coming in all
    # that fast.
    #
    my $node = Node->Lookup($node_id);
    if (!defined($node)) {
	print STDERR "Unknown node $node_id\n";
	goto done;
    }
    #
    # If this is a state change and nothing changed, then do nothing.
    #
    goto done
	if (0 && $objtype && TBDB_TBEVENT_NODESTATE() &&
	    $state eq $node->eventstate());

    # We want this so we can flush it from the cache.
    my $experiment = $node->Reservation();
    goto done
	if (!defined($experiment));
    goto done
	if (!$node->genisliver_idx());

    my $sliver = GeniSliver->Lookup($node->genisliver_idx());
    goto done
	if (!$sliver);

    #
    # This will generate a new event, which we will get here later,
    # and forward directly (above).
    #
    if ($objtype eq TBDB_TBEVENT_NODESTATE() ||
237
	$objtype eq TBDB_TBEVENT_NODESTATUS() ||
238
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
	$objtype eq TBDB_TBEVENT_NODEACCOUNTS()) {
	my $oldstatus = $sliver->status();
	my $newstatus;

	#
	# The point here, is that we do not want to send an event if
	# ComputeStatus sent one, but since the rawstate has probably
	# changed, we want to make sure an event goes out. 
	#
	if ($sliver->ComputeStatus(\$newstatus) == 0 &&
	    $oldstatus eq $newstatus) {
	    $sliver->SendStatusEvent();
	}
	if ($debug) {
	    print "State/Status sent for $node_id\n";
	}
    }
    else {
	#
	# A change in the start command status generates a new event.
	# This is generated by tmcd when it comes in.
	#
	if ($debug) {
	    print "Start Command event for $node_id\n";
	}
	$sliver->SendStatusEvent();
    }
    $sliver->Flush();
  done:
    $node->Flush()
	if (defined($node));
    $experiment->Flush()
	if (defined($experiment));
}

my $counter  = 0;

while (1)
{
    $gotone = 1;
    while ($gotone) {
	$gotone = 0;
	event_poll($localhandle);
    }
    event_poll_blocking($localhandle, 1000);
    
    #
    # Every 60 seconds compute new status for all slices. This might
    # generate new events for the loop above.
    #
    if ($counter++ >= 10) {
	$counter = 0;
	
	my @slices;
	if (GeniSlice->ListAll(\@slices) == 0) {
	    foreach my $slice (@slices) {
		if ($slice->Lock() == 0) {
		    my $aggregate = GeniAggregate->SliceAggregate($slice);
		    if ($aggregate) {
			$aggregate->ComputeState();
		    }
		    $slice->UnLock();
		}
	    }
	}
	GeniUtil::FlushCaches();
    }
}
exit(0);

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

    #
    # Send a message to the testbed list. 
    #
    SENDMAIL($TBOPS,
	     "ProtoGENI igevent daemon died",
	     $msg,
	     $TBOPS);
    MarkDaemonStopped("igevent_daemon");
    die("*** $0:\n".
	"    $msg\n");
}