sa_daemon.in 6.36 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
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
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
#!/usr/bin/perl -w
#
# GENIPUBLIC-COPYRIGHT
# Copyright (c) 2008-2009 University of Utah and the Flux Group.
# All rights reserved.
#
use strict;
use English;
use Getopt::Std;

#
# Look for things that need to be expired and resources released. This is
# incredibly primitive at the moment.
#
sub usage()
{
    print "Usage: sa_daemon [-d] [-n]\n";
    exit(1);
}
my $optlist   = "dn";
my $debug     = 0;
my $impotent  = 0;

#
# Configure variables
#
my $TB		  = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
my $TBLOGS        = "@TBLOGSEMAIL@";
my $PGENIDOMAIN   = "@PROTOGENI_DOMAIN@";
my $PGENISUPPORT  = @PROTOGENI_SUPPORT@;
my $PGENICLRHOUSE = @PROTOGENI_CLEARINGHOUSE@;
my $LOGFILE       = "$TB/log/sa_daemon.log";
my $EMULAB_PEMFILE = "$TB/etc/genisa.pem";
my $CLEANUPSLICE  = "$TB/sbin/cleanupslice";

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

# Load the Testbed support stuff.
use lib "@prefix@/lib";
require GeniDB;
require GeniUtil;
require GeniSlice;
require GeniCertificate;
require GeniUser;
use Experiment;
use Node;
use libtestbed;
use POSIX qw(strftime);

#
# So we know who/what we are acting as.
#
my $certificate = GeniCertificate->LoadFromFile($EMULAB_PEMFILE);
if (!defined($certificate)) {
    fatal("Could not get uuid/urn from $EMULAB_PEMFILE\n");
}
Genixmlrpc->SetContext(Genixmlrpc->Context($certificate));
Genixmlrpc->SetTimeout(10);
$ENV{'MYUUID'} = $certificate->uuid();
$ENV{'MYURN'} = "urn:publicid:IDN+@OURDOMAIN@+authority+sa";

# Go to ground.
if (! $debug) {
    if (TBBackGround($LOGFILE)) {
	exit(0);
    }
}
GeniUtil::FlipToGeniUser();

#
# Keep track of who has been warned. Do not expire until 24 hours after
# warning.
#
my %warned  = ();
my %expires = ();
my $hours   = 24;
my $grace   = $hours * 3600;

#
# Send out warnings about 24 hours ahead of time. Do not delete until
# at least 24 hours goes by, even if expired.
#
sub WarnSlices()
{
    my $query_result =
	GeniDB::DBQueryWarn("select idx,expires,UNIX_TIMESTAMP(expires) ".
			    "  from geni_slices ".
			    "where shutdown is null");

    while (my ($idx,$expires,$stamp) = $query_result->fetchrow_array()) {
	if (exists($expires{$idx})) {
	    if ($expires{$idx} == $stamp) {
		# Already warned, skip.
	    }
	    else {
		# Expiration changed, delete and catch in the next loop
		print STDERR "Expiration changed for slice $idx to $expires\n";
		delete($expires{$idx});
		delete($warned{$idx});
	    }
	    next;
	}
	#
	# If expiration is greater then 24 hours out, skip. 
	#
	if ($stamp > time() + $grace) {
	    delete($expires{$idx});
	    delete($warned{$idx});
	    next;
	}
	my $slice = GeniSlice->Lookup($idx);

	if (!defined($slice)) {
	    # Slice is gone, lets not worry.
	    next;
	}
	if ($slice->Lock() != 0) {
	    print STDERR "Could not lock slice $slice.\n";
	    goto skip;
	}
	if (! $slice->IsExpired()) {
	    delete($warned{$idx});
	    delete($expires{$idx});
	    $slice->UnLock();
	    goto skip;
	}
	my $geniuser  = GeniUser->Lookup($slice->creator_uuid(), 1);
	my $emailaddr = $geniuser->email();
	my $urn       = $slice->urn();
	my $hrn       = $slice->hrn();
	
	if ($impotent) {
	    print STDERR "Would send mail to $geniuser about $idx,$hrn\n";
	    $warned{$idx}  = time();
	    $expires{$idx} = $stamp;
	    $slice->UnLock();
	    goto skip;
	}
	print STDERR
	    "Sending mail to $geniuser about expiring slice $idx,$hrn\n";
	SENDMAIL($emailaddr, "Slice $urn expiring",
		 "Slice $urn\n".
		 "is going to expire in approximately 24 hours.\n".
		 "Please renew it or let it expire if you not using it.\n",
		 undef, "CC: $TBOPS");
	
	$warned{$idx}  = time();
	$expires{$idx} = $stamp;
	$slice->UnLock();
	
      skip:
	$slice->Flush()
	    if (defined($slice));
    }
}

#
# Look for slices that have expired.
#
sub ExpireSlices()
{
    my $query_result =
	GeniDB::DBQueryWarn("select idx from geni_slices ".
			    "where UNIX_TIMESTAMP(now()) > ".
			    "      UNIX_TIMESTAMP(expires) and ".
			    "      shutdown is null");

    while (my ($idx) = $query_result->fetchrow_array()) {
	my $slice = GeniSlice->Lookup($idx);

	if (!defined($slice)) {
	    # Slice is gone, lets not worry.
	    next;
	}
	if ($slice->Lock() != 0) {
	    print STDERR "Could not lock slice $slice.\n";
	    goto skip;
	}
	if (! $slice->IsExpired()) {
	    $slice->UnLock();
	    delete($warned{"$idx"});
	    delete($expires{"$idx"});
	    goto skip;
	}
	# Do not delete anything that was not warned at least 24 hours ago.
	if (!exists($warned{"$idx"}) ||
	    $warned{"$idx"} > time() - $grace) {
	    print STDERR "Skipping $slice cause it was not warned at least ".
		"$hours hours ago\n" if ($debug > 1);
	    $slice->UnLock();
	    goto skip;
	}
	if ($impotent) {
	    print STDERR "Would expire $slice\n";
	    $slice->UnLock();
	    goto skip;
	}
	my $geniuser  = GeniUser->Lookup($slice->creator_uuid(), 1);
	my $emailaddr = $geniuser->email();
	my $urn       = $slice->urn();
	my $hrn       = $slice->hrn();
	
	print STDERR "Expiring $slice\n";
	system("$CLEANUPSLICE -f -s $idx");
	if (! $?) {
	    $slice->UnLock();
	    goto skip;
	}
	SENDMAIL($emailaddr, "Slice $urn has expired",
		 "Slice $urn\n".
		 "has been expired. Sorry :-(\n",
		 undef, "CC: $TBOPS");
	
	delete($warned{"$idx"});
	delete($expires{"$idx"});
      skip:
	$slice->Flush()
	    if (defined($slice));
    }
}

#
# Setup a signal handler for newsyslog.
#
sub handler()
{
    ReOpenLog($LOGFILE);
}
$SIG{HUP} = \&handler
    if (!$debug);

while (1) {
    print "Running at ".
	POSIX::strftime("20%y-%m-%d %H:%M:%S", localtime()) . "\n";

    WarnSlices();
    ExpireSlices();

    # Be certain stale info is gone.
    GeniUtil::FlushCaches();

    sleep(15);
}
exit(0);

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

    #
    # Send a message to the testbed list. 
    #
    SENDMAIL($TBOPS,
	     "ProtoGENI SA daemon died",
	     $msg,
	     $TBOPS);

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