announce.in 12.2 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
Leigh B Stoller's avatar
Leigh B Stoller committed
3
# Copyright (c) 2016-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
# 
# {{{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 strict;
use English;
use Getopt::Std;
use Date::Parse;

#
# Set up and clear node pre-reservations.
#
sub usage()
{
34
    print STDERR "Usage: announce -a <-p portal> [-c] [-s alert_style] [-b button_label] [-u action_url] [-m max_seen_count] <announcement_text>\n";
35
    print STDERR "       announce -l [active|retired|all]\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
36 37
    print STDERR "       announce -r idx|uuid [-c]\n";
    print STDERR "       announce -R idx|uuid\n";
38
    print STDERR "       announce -i idx\n";
39 40 41
    print STDERR "       announce -h\n";
    print STDERR "   -h   This message\n";
    print STDERR "   -a   Create a new announcement with the given announcement text.\n";
42
    print STDERR "   -p   Set the portal to be the one given. Should be one of 'cloudlab', 'emulab', 'aptlab', 'phantomnet'.\n";
43
    print STDERR "   -s   Set the style of the overall announcement box. Should normally be one of bootstraps alert-* classes. Defaults to 'alert-info'.\n";
44
    print STDERR "        Common styles: 'alert-success' -> green, 'alert-info' -> blue, 'alert-warning' -> yellow, 'alert-danger' -> red\n\n";
45 46 47
    print STDERR "   -b   If there is an action associated with this announcement, this is the text which goes into the action button. Can include HTML. If it is unset, there is no action button.\n";
    print STDERR "   -u   URL of action associated with this announcement. {uid_idx} and {uid} can be used as URL templates to generate a different URL on a per-user basis. If it is unset, there is no action button.\n";
    print STDERR "   -m   The maximum number of times that this announcement will appear to a user. Every page view (even those in a single session) counts. A value of '0' indicates that the announcement should keep appearing indefinitely until dismissed by the user or an action is taken. Defaults to 20.\n";
48
    print STDERR "   -l   List global announcements. Defaults to listing active announcements.\n";
49
    print STDERR "   -r   Retire announcement with the given idx. A retired announcement will no longer be displayed to users.\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
50
    print STDERR "   -R   Purge announcement from DB with the given idx or uuid.\n";
51 52
    print STDERR "   -i   Info about a particular announcement.\n";
    print STDERR "   -c   Compatibility mode. When adding in compatibility mode, the sitevar is changed, thus setting the legacy Emulab announcement. When removing in compatibility mode, the sitevar is cleared.\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
53
    print STDERR "   -U   In add mode, specify a specific target user.\n";
54 55
    exit(-1);
}
56
my $optlist  = "haA:cp:s:b:u:m:lr:R:i:U:t:I:S:E:w:";
57
my $add_mode = 0;
Leigh B Stoller's avatar
Leigh B Stoller committed
58 59
my $update_mode = 0;
my $update_target;
60 61
my $list_mode = 0;
my $retire_mode = 0;
Leigh B Stoller's avatar
Leigh B Stoller committed
62
my $purge_mode = 0;
63
my $sees_mode = 0;
64
my $info_mode = 0;
65
my $portal = undef;
66 67 68 69 70
my $style = "alert-info";
my $button = undef;
my $url = undef;
my $max_seen = 20;
my $retire_idx = undef;
71
my $info_idx = undef;
72
my $text = undef;
Leigh B Stoller's avatar
Leigh B Stoller committed
73 74 75 76 77
my $target_user = undef;
my $uuid = undef;
my $display_start = undef;
my $display_end = undef;
my $priority = undef;
78
my $list_type = "active";
79
my $compatibility = 0;
80 81

my $query_result;
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

# Protos
sub fatal($);

#
# Configure variables
#
my $TB		 = "@prefix@";
my $TBOPS        = "@TBOPSEMAIL@";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use emdb;
Leigh B Stoller's avatar
Leigh B Stoller committed
97
use emutil;
98
use libtestbed;
99
use libdb;
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
use User;

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

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";

#
# 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{"h"})) {
    usage();
}
if (defined($options{"a"})) {
    $add_mode = 1;
}
Leigh B Stoller's avatar
Leigh B Stoller committed
126 127 128 129
if (defined($options{"A"})) {
    $update_mode = 1;
    $update_target = $options{"A"};
}
130 131 132 133 134 135 136
if (defined($options{"l"})) {
    $list_mode = 1;
}
if (defined($options{"r"})) {
    $retire_mode = 1;
    $retire_idx = $options{"r"};
}
Leigh B Stoller's avatar
Leigh B Stoller committed
137 138 139 140
if (defined($options{"R"})) {
    $purge_mode = 1;
    $retire_idx = $options{"R"};
}
141 142 143 144
if (defined($options{"w"})) {
    $sees_mode  = 1;
    $retire_idx = $options{"w"};
}
145 146 147 148
if (defined($options{"i"})) {
    $info_mode = 1;
    $info_idx = $options{"i"};
}
149 150
if (defined($options{"p"})) {
    $portal = $options{"p"};
151 152 153 154 155 156 157 158 159 160
}
if (defined($options{"s"})) {
    $style = $options{"s"};
}
if (defined($options{"b"})) {
    $button = $options{"b"};
}
if (defined($options{"u"})) {
    $url = $options{"u"};
}
Leigh B Stoller's avatar
Leigh B Stoller committed
161 162 163 164 165 166 167
if (defined($options{"t"})) {
    $uuid = $options{"t"};
    if ($uuid !~ /^[-\w]*$/) {
	print STDERR "Invalid uuid\n";
	exit(1);
    }
}
168 169 170
if (defined($options{"m"})) {
    $max_seen = $options{"m"};
}
Leigh B Stoller's avatar
Leigh B Stoller committed
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
if (defined($options{"U"})) {
    $target_user = User->Lookup($options{"U"});
    if (!defined($target_user)) {
	print STDERR "No such user!\n";
	exit(-1);
    }
}
if (defined($options{"S"})) {
    $display_start = $options{"S"};
    if ($display_start !~ /^\d+$/) {
	$display_start = str2time($display_start);
	if (!defined($display_start)) {
	    fatal("Could not parse -S option.");
	}
    }
}
if (defined($options{"E"})) {
    $display_end = $options{"E"};
    if ($display_end !~ /^\d+$/) {
	$display_end = str2time($display_end);
	if (!defined($display_end)) {
	    fatal("Could not parse -E option.");
	}
    }
}
if (defined($options{"I"})) {
    $priority = $options{"I"};
    if ($priority !~ /^\d+$/) {
	fatal("Could not parse -I option.");
    }
}

203 204 205
if (defined($options{"c"})) {
    $compatibility = 1;
}
206

207 208 209 210 211 212 213 214 215 216 217
if ($add_mode)
{
    if (! defined($portal)) {
	print STDERR "Portal must be defined when adding an announcement\n\n";
	usage();
    }
    elsif ($portal ne "cloudlab" && $portal ne "emulab" &&
	$portal ne "aptlab" && $portal ne "phantomnet") {
	print STDERR "Portal $portal is invalid. Must be cloudlab, emulab, aptlab, or phantomnet.\n\n";
	usage();
    }
218 219
}

Leigh B Stoller's avatar
Leigh B Stoller committed
220
if ($add_mode + $update_mode + $list_mode +
221
    $retire_mode + $sees_mode + $purge_mode + $info_mode != 1) {
Leigh B Stoller's avatar
Leigh B Stoller committed
222
    print STDERR "No mode selected. Must use one of -a, -A, -r, -R, -l, or -i\n\n";
223 224 225
    usage();
}

Leigh B Stoller's avatar
Leigh B Stoller committed
226
if ($add_mode || $update_mode) {
227 228
    $text = join(' ', @ARGV);
}
229

230 231
if (! $list_mode && ! $add_mode && ! $update_mode &&
    ! $purge_mode && scalar(@ARGV) > 0) {
232 233 234
    usage();
}

235 236 237 238
if ($list_mode && scalar(@ARGV) > 0) {
    $list_type = $ARGV[0];
}

239
if ($add_mode) {
240 241 242
    #
    # Add a new announcement
    #
243 244
    my $query = "insert into apt_announcements set ";
    $query .= "created=NOW()";
245
    $query .= ", portal=" . DBQuoteSpecial($portal);
246 247 248 249 250 251 252
    $query .= ", max_seen=" . DBQuoteSpecial($max_seen);
    $query .= ", text=" . DBQuoteSpecial($text);
    $query .= ", style=" . DBQuoteSpecial($style);
    if (defined($button) && defined($url)) {
	$query .= ", link_label=" . DBQuoteSpecial($button);
	$query .= ", link_url=" . DBQuoteSpecial($url);
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    if (defined($target_user)) {
	$query .= ", uid_idx='" . $target_user->uid_idx() . "'";
    }
    if (defined($uuid)) {
	$query .= ", uuid='$uuid'";
    }
    else {
	$query .= ", uuid=uuid()";
    }
    if (defined($display_start)) {
	$query .= ", display_start=FROM_UNIXTIME($display_start)";
    }
    if (defined($display_end)) {
	$query .= ", display_end=FROM_UNIXTIME($display_end)";
    }
    if (defined($priority)) {
	$query .= ", priority='$priority'";
    }
    
272
    DBQueryFatal($query);
273 274
    if ($compatibility)
    {
275
	TBSetSiteVar("web/banner", $text);
276
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
277 278 279 280 281
} elsif ($update_mode) {
    #
    # Update existing announcement.
    #
    my $query = "update apt_announcements set ";
282
    $query .= "text=" . DBQuoteSpecial($text);
Leigh B Stoller's avatar
Leigh B Stoller committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    if (defined($button) && defined($url)) {
	$query .= ", link_label=" . DBQuoteSpecial($button);
	$query .= ", link_url=" . DBQuoteSpecial($url);
    }
    if (defined($display_start)) {
	$query .= ", display_start=FROM_UNIXTIME($display_start)";
    }
    if (defined($display_end)) {
	$query .= ", display_end=FROM_UNIXTIME($display_end)";
    }
    $query .= " ";
    if (ValidUUID($update_target)) {
	$query .= "where uuid='$update_target'";
    }
    elsif ($update_target =~ /^\d+$/) {
	$query .= "where idx='$update_target'";
    }
    else {
	fatal("Invalid argument for -A mode, must be uuid or idx");
    }
    DBQueryFatal($query);
304
} elsif ($list_mode) {
305 306 307 308 309 310 311 312 313 314
    #
    # List announcements
    #
    my $condition = "retired=0 and ";
    if ($list_type eq "all") {
	$condition = "";
    } elsif ($list_type eq "retired") {
	$condition = "retired=1 and ";
    }
    $query_result = 
315 316
	DBQueryFatal("select idx, portal, text from apt_announcements where ".$condition."uid_idx is NULL");
    print "idx\tPortal    Text\n";
317
    print "---\t-------    ----\n";
318
    while (my ($idx, $portal, $text) = $query_result->fetchrow_array()) {
319 320 321 322
	my $textbit = substr($text, 0, 55);
	if (length($text) > 55) {
	    $textbit = $textbit . "...";
	}
323 324
	my $portalpad = sprintf("%-10s", $portal);
	print "$idx\t$portalpad $textbit\n"
325
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
326 327 328 329 330 331 332 333 334 335
} elsif ($purge_mode) {
    #
    # Purge an announcement
    #
    my $clause = (ValidUUID($retire_idx) ?
		  "a.uuid='$retire_idx'" : "a.idx='$retire_idx'");
    DBQueryFatal("delete a, i ".
		 "from apt_announcements as a ".
		 "join apt_announcement_info as i on a.idx = i.aid ".
		 "where $clause");
336
} elsif ($retire_mode) {
337 338 339
    #
    # Retire an announcement
    #
340 341
    my $clause = (ValidUUID($retire_idx) ?
		  "uuid='$retire_idx'" : "idx='$retire_idx'");
342 343
    $query_result =
	DBQueryFatal("update apt_announcements ".
344
		     "set retired=1 where $clause");
345 346 347 348
    if ($compatibility)
    {
	TBSetSiteVar("web/banner", "");
    }
349 350 351 352 353 354 355 356 357
} elsif ($sees_mode) {
    #
    # Force a retired announcement back to work.
    #
    my $clause = (ValidUUID($retire_idx) ?
		  "uuid='$retire_idx'" : "idx='$retire_idx'");
    $query_result =
	DBQueryFatal("update apt_announcements ".
		     "set retired=0 where $clause");
358 359 360 361 362 363 364 365 366 367 368
} elsif ($info_mode) {
    #
    # Details about an announcement
    #
    $query_result =
	DBQueryFatal("select count(*) from apt_announcement_info where aid=".DBQuoteSpecial($info_idx));
    my ($seen) = $query_result->fetchrow_array();

    $query_result =
	DBQueryFatal("select count(*) from apt_announcement_info where clicked=1 and aid=".DBQuoteSpecial($info_idx));
    my ($clicked) = $query_result->fetchrow_array();
369

370 371 372 373
    $query_result =
	DBQueryFatal("select count(*) from apt_announcement_info where dismissed=1 and aid=".DBQuoteSpecial($info_idx));
    my ($dismissed) = $query_result->fetchrow_array();
    $query_result =
374 375
	DBQueryFatal("select idx, created, uid_idx, portal, retired, max_seen, text, style, link_label, link_url from apt_announcements where idx=".DBQuoteSpecial($info_idx));
    if (my ($idx, $created, $uid_idx, $portal, $retired,
376 377 378 379 380 381 382
	    $max_seen, $text, $style, $link_label,
	    $link_url) = $query_result->fetchrow_array())
    {
	print "-----------------------------\n";
	print "Details for announcement $idx\n";
	print "-----------------------------\n\n";
	print "Created:\t$created\n";
383
	print "Portal:\t$portal\n\n";
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
	if (defined($link_label) && defined($link_url)) {
	    print "Button Label:\t$link_label\n";
	    print "Button URL:\t$link_url\n";
	}
	print "Style:\t\t$style\n";
	if ($max_seen == 0) {
	    print "Shown until dismissed or clicked\n";
	} else {
	    print "Shown a maximum of $max_seen times\n";
	}
	print "\n";

	if ($retired) {
	    print "* Announcement is retired\n";
	} else {
	    print "* Announcement is active\n";
	}
	print "* Seen by $seen users\n";
	if (defined($link_label) && defined($link_url)) {
	    print "* Clicked by $clicked users\n";
	}
	print "* Dismissed by $dismissed users\n";

	print "\nText of announcement:\n\n";
	print "$text\n\n";
    }
}
411 412 413 414 415 416 417 418 419

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

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