announce.in 11.7 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:";
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 $info_mode = 0;
64
my $portal = undef;
65 66 67 68 69
my $style = "alert-info";
my $button = undef;
my $url = undef;
my $max_seen = 20;
my $retire_idx = undef;
70
my $info_idx = undef;
71
my $text = undef;
Leigh B Stoller's avatar
Leigh B Stoller committed
72 73 74 75 76
my $target_user = undef;
my $uuid = undef;
my $display_start = undef;
my $display_end = undef;
my $priority = undef;
77
my $list_type = "active";
78
my $compatibility = 0;
79 80

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

# 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
96
use emutil;
97
use libtestbed;
98
use libdb;
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
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
125 126 127 128
if (defined($options{"A"})) {
    $update_mode = 1;
    $update_target = $options{"A"};
}
129 130 131 132 133 134 135
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
136 137 138 139
if (defined($options{"R"})) {
    $purge_mode = 1;
    $retire_idx = $options{"R"};
}
140 141 142 143
if (defined($options{"i"})) {
    $info_mode = 1;
    $info_idx = $options{"i"};
}
144 145
if (defined($options{"p"})) {
    $portal = $options{"p"};
146 147 148 149 150 151 152 153 154 155
}
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
156 157 158 159 160 161 162
if (defined($options{"t"})) {
    $uuid = $options{"t"};
    if ($uuid !~ /^[-\w]*$/) {
	print STDERR "Invalid uuid\n";
	exit(1);
    }
}
163 164 165
if (defined($options{"m"})) {
    $max_seen = $options{"m"};
}
Leigh B Stoller's avatar
Leigh B Stoller committed
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
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.");
    }
}

198 199 200
if (defined($options{"c"})) {
    $compatibility = 1;
}
201

202 203 204 205 206 207 208 209 210 211 212
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();
    }
213 214
}

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

Leigh B Stoller's avatar
Leigh B Stoller committed
221
if ($add_mode || $update_mode) {
222 223
    $text = join(' ', @ARGV);
}
224

225 226
if (! $list_mode && ! $add_mode && ! $update_mode &&
    ! $purge_mode && scalar(@ARGV) > 0) {
227 228 229
    usage();
}

230 231 232 233
if ($list_mode && scalar(@ARGV) > 0) {
    $list_type = $ARGV[0];
}

234
if ($add_mode) {
235 236 237
    #
    # Add a new announcement
    #
238 239
    my $query = "insert into apt_announcements set ";
    $query .= "created=NOW()";
240
    $query .= ", portal=" . DBQuoteSpecial($portal);
241 242 243 244 245 246 247
    $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
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    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'";
    }
    
267
    DBQueryFatal($query);
268 269
    if ($compatibility)
    {
270
	TBSetSiteVar("web/banner", $text);
271
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
272 273 274 275 276
} elsif ($update_mode) {
    #
    # Update existing announcement.
    #
    my $query = "update apt_announcements set ";
277
    $query .= "text=" . DBQuoteSpecial($text);
Leigh B Stoller's avatar
Leigh B Stoller committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    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);
299
} elsif ($list_mode) {
300 301 302 303 304 305 306 307 308 309
    #
    # List announcements
    #
    my $condition = "retired=0 and ";
    if ($list_type eq "all") {
	$condition = "";
    } elsif ($list_type eq "retired") {
	$condition = "retired=1 and ";
    }
    $query_result = 
310 311
	DBQueryFatal("select idx, portal, text from apt_announcements where ".$condition."uid_idx is NULL");
    print "idx\tPortal    Text\n";
312
    print "---\t-------    ----\n";
313
    while (my ($idx, $portal, $text) = $query_result->fetchrow_array()) {
314 315 316 317
	my $textbit = substr($text, 0, 55);
	if (length($text) > 55) {
	    $textbit = $textbit . "...";
	}
318 319
	my $portalpad = sprintf("%-10s", $portal);
	print "$idx\t$portalpad $textbit\n"
320
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
321 322 323 324 325 326 327 328 329 330
} 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");
331
} elsif ($retire_mode) {
332 333 334 335 336 337 338
    #
    # Retire an announcement
    #
    $query_result =
	DBQueryFatal("update apt_announcements ".
		     "set retired=1 where idx=".
		     DBQuoteSpecial($retire_idx));
339 340 341 342
    if ($compatibility)
    {
	TBSetSiteVar("web/banner", "");
    }
343 344 345 346 347 348 349 350 351 352 353
} 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();
354

355 356 357 358
    $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 =
359 360
	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,
361 362 363 364 365 366 367
	    $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";
368
	print "Portal:\t$portal\n\n";
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
	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";
    }
}
396 397 398 399 400 401 402 403 404

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

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