announce.in 12.6 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2016-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
# 
# {{{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
    print STDERR "   -P   In add mode, add portal announcement for user\n";
55 56
    exit(-1);
}
57
my $optlist  = "haA:cp:s:b:u:m:lr:R:i:U:t:I:S:E:w:P";
58
my $add_mode = 0;
Leigh B Stoller's avatar
Leigh B Stoller committed
59 60
my $update_mode = 0;
my $update_target;
61 62
my $list_mode = 0;
my $retire_mode = 0;
Leigh B Stoller's avatar
Leigh B Stoller committed
63
my $purge_mode = 0;
64
my $sees_mode = 0;
65
my $info_mode = 0;
66
my $portal = undef;
67 68 69 70 71
my $style = "alert-info";
my $button = undef;
my $url = undef;
my $max_seen = 20;
my $retire_idx = undef;
72
my $info_idx = undef;
73
my $text = undef;
Leigh B Stoller's avatar
Leigh B Stoller committed
74 75 76 77 78
my $target_user = undef;
my $uuid = undef;
my $display_start = undef;
my $display_end = undef;
my $priority = undef;
79
my $list_type = "active";
80
my $compatibility = 0;
81
my $canned = 0;
82 83

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

# 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
99
use emutil;
100
use libtestbed;
101
use libdb;
102 103
use User;

104 105 106 107
my $CANNED = "Looking for the old Emulab interface? ".
    "See this <a target=_blank href='https://gitlab.flux.utah.edu/emulab/".
    "emulab-devel/wikis/faq/Using%20the%20Testbed/Emulab%20Classic'>".
    "FAQ entry</a>.";
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#
# 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
132 133 134 135
if (defined($options{"A"})) {
    $update_mode = 1;
    $update_target = $options{"A"};
}
136 137 138 139 140 141 142
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
143 144 145 146
if (defined($options{"R"})) {
    $purge_mode = 1;
    $retire_idx = $options{"R"};
}
147 148 149 150
if (defined($options{"w"})) {
    $sees_mode  = 1;
    $retire_idx = $options{"w"};
}
151 152 153 154
if (defined($options{"i"})) {
    $info_mode = 1;
    $info_idx = $options{"i"};
}
155 156
if (defined($options{"p"})) {
    $portal = $options{"p"};
157 158 159 160 161 162 163 164 165 166
}
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
167 168 169 170 171 172 173
if (defined($options{"t"})) {
    $uuid = $options{"t"};
    if ($uuid !~ /^[-\w]*$/) {
	print STDERR "Invalid uuid\n";
	exit(1);
    }
}
174 175 176
if (defined($options{"m"})) {
    $max_seen = $options{"m"};
}
Leigh B Stoller's avatar
Leigh B Stoller committed
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
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.");
    }
}
208 209 210
if (defined($options{"P"})) {
    $canned = 1;
}
211 212 213
if (defined($options{"c"})) {
    $compatibility = 1;
}
214

215 216 217 218 219 220 221 222 223 224 225
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();
    }
226 227
}

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

Leigh B Stoller's avatar
Leigh B Stoller committed
234
if ($add_mode || $update_mode) {
235 236 237 238 239 240 241 242 243
    if ($canned) {
	$text = $CANNED;
    }
    elsif (!@ARGV) {
	usage();
    }
    else {
	$text = join(' ', @ARGV);
    }
244
}
245

246 247
if (! $list_mode && ! $add_mode && ! $update_mode &&
    ! $purge_mode && scalar(@ARGV) > 0) {
248 249 250
    usage();
}

251 252 253 254
if ($list_mode && scalar(@ARGV) > 0) {
    $list_type = $ARGV[0];
}

255
if ($add_mode) {
256 257 258
    #
    # Add a new announcement
    #
259 260
    my $query = "insert into apt_announcements set ";
    $query .= "created=NOW()";
261
    $query .= ", portal=" . DBQuoteSpecial($portal);
262 263 264 265 266 267 268
    $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
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    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'";
    }
    
288
    DBQueryFatal($query);
289 290
    if ($compatibility)
    {
291
	TBSetSiteVar("web/banner", $text);
292
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
293 294 295 296 297
} elsif ($update_mode) {
    #
    # Update existing announcement.
    #
    my $query = "update apt_announcements set ";
298
    $query .= "text=" . DBQuoteSpecial($text);
Leigh B Stoller's avatar
Leigh B Stoller committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    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);
320
} elsif ($list_mode) {
321 322 323 324 325 326 327 328 329 330
    #
    # List announcements
    #
    my $condition = "retired=0 and ";
    if ($list_type eq "all") {
	$condition = "";
    } elsif ($list_type eq "retired") {
	$condition = "retired=1 and ";
    }
    $query_result = 
331 332
	DBQueryFatal("select idx, portal, text from apt_announcements where ".$condition."uid_idx is NULL");
    print "idx\tPortal    Text\n";
333
    print "---\t-------    ----\n";
334
    while (my ($idx, $portal, $text) = $query_result->fetchrow_array()) {
335 336 337 338
	my $textbit = substr($text, 0, 55);
	if (length($text) > 55) {
	    $textbit = $textbit . "...";
	}
339 340
	my $portalpad = sprintf("%-10s", $portal);
	print "$idx\t$portalpad $textbit\n"
341
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
342 343 344 345 346 347 348 349 350 351
} 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");
352
} elsif ($retire_mode) {
353 354 355
    #
    # Retire an announcement
    #
356 357
    my $clause = (ValidUUID($retire_idx) ?
		  "uuid='$retire_idx'" : "idx='$retire_idx'");
358 359
    $query_result =
	DBQueryFatal("update apt_announcements ".
360
		     "set retired=1 where $clause");
361 362 363 364
    if ($compatibility)
    {
	TBSetSiteVar("web/banner", "");
    }
365 366 367 368 369 370 371 372 373
} 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");
374 375 376 377 378 379 380 381 382 383 384
} 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();
385

386 387 388 389
    $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 =
390 391
	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,
392 393 394 395 396 397 398
	    $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";
399
	print "Portal:\t$portal\n\n";
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	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";
    }
}
427 428 429 430 431 432 433 434 435

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

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