template_graph.in 8.4 KB
Newer Older
1 2 3 4 5 6 7
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
8
use strict;
9
use Getopt::Std;
10
use HTML::Entities;
11 12 13 14 15 16 17

#
# Quickie graph layout.
#
sub usage()
{
    print(STDERR
18
	  "Usage: template_graph [-a] <guid>\n".
19
	  "switches and arguments:\n".
20
	  "-a          - Show all templates (ignore hidden bit)\n".
21 22 23 24
	  "-p <prefix> - prefix for output files.\n".
	  "<guid>      - GUID to graph\n");
    exit(-1);
}
25
my $optlist	 = "p:z:d";
26
my %options      = ();
27
my $debug	 = 0;
28
my $prefix       = "/tmp/dot$$";
29 30
my $scale        = 1.0;
my $zoom;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
my $guid;

#
# Configure variables
#
my $TB		= "@prefix@";
my $EVENTSYS	= @EVENTSYS@;
my $TBOPS	= "@TBOPSEMAIL@";
my $TBLOGS	= "@TBLOGSEMAIL@";
my $TBDOCBASE	= "@TBDOCBASE@";
my $TBBASE	= "@TBBASE@";
my $CONTROL	= "@USERNODE@";

# Protos
sub ParseArgs();
46
sub SetParent($@);
47 48 49 50 51 52 53 54 55 56 57 58

# Locals
my $DOT         = "/usr/local/bin/dot";
my %versions    = ();

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use libtblog;
59
use Template;
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

# Be careful not to exit on transient error
$libdb::DBQUERY_MAXTRIES = 0;

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

#
# Set umask for start/swap. We want other members in the project to be
# able to swap/end experiments, so the log and intermediate files need
# to be 664 since some are opened for append.
#
umask(0002);

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

# Now parse arguments.
ParseArgs();

# After parsing args.
my $dotfile	= "$prefix.dot";
my $imapfile	= "$prefix.imap";
my $giffile	= "$prefix.gif";

#
# Grab all the parent pointers.
#
my $query_result =
95
    DBQueryFatal("select vers,parent_vers,hidden,tid,description ".
96
		 "  from experiment_templates ".
97 98 99
		 "where guid='$guid' ".
		 "order by vers");

100 101 102 103 104 105 106 107 108 109 110 111
#
# If nothing left, remove the graph entry.
#
if (! $query_result->num_rows) {
    print "Clearing graph entry for template $guid ...\n";
    
    DBQueryFatal("delete from experiment_template_graphs ".
		 "where parent_guid='$guid'");
    exit(0);
}

#
112
# Build up lists of children (and associated stuff like hidden,description).
113 114 115 116
#
my %hidden	= ();
my %tids        = ();
my %children    = ();
117
my %parents     = ();
118
my %descriptions= ();
119 120
my $rootnode    = 1;	# XXX

121
while (my ($vers,$parent_vers,$hidden,$tid,$description) =
122
       $query_result->fetchrow_array()) {
123 124 125
    $tids{$vers}         = $tid;
    $hidden{$vers}       = $hidden;
    $descriptions{$vers} = $description;
126 127 128 129 130 131 132 133 134 135

    next
	if (!defined($parent_vers));

    $children{$parent_vers} = []
	if (!exists($children{$parent_vers}));
    push(@{ $children{$parent_vers} }, $vers);
}

#
136 137
# We overload the hidden bit on the root template. If its set, that means
# to hide hidden templates, otherwise it means to show hidden templates.
138
#
139 140
my $showall = ($hidden{$rootnode} == 0);
$hidden{$rootnode} = 0;
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
# Defaults;
my $size     = 8;
my $fontsize = 10;
my $bwidth   = 0.75;
my $bheight  = 0.25;
my $arrowsize= 0.7;
my $nodesep  = 0.25;
my $ranksep  = 0.50;

#
# Grab the current scale so we know what to do.
#
$query_result =
    DBQueryFatal("select scale from experiment_template_graphs ".
		 "where parent_guid='$guid'");

158 159
my $curscale = ($query_result->num_rows ?
		($query_result->fetchrow_array())[0] : 1);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

#
# Change scale if zooming and then recalc the defaults.
#
if (defined($zoom)) {
    if ($zoom eq "in") {
	$scale = $curscale * 1.25;
    }
    else {
	$scale = $curscale / 1.25;
    }
}
else {
    $scale = $curscale;
}
$bwidth    = $bwidth  * $scale;
$bheight   = $bheight * $scale;
$arrowsize = $arrowsize * $scale;
$nodesep   = $nodesep * $scale;
$ranksep   = $ranksep * $scale;

181
open(DOT, "> $dotfile") or
182
    tbdie("Could not open $dotfile!");
183 184

print DOT "digraph TemplateGraph {\n";
185 186 187
print DOT "  rankdir=\"LR\"\n";
print DOT "  nodesep=$nodesep\n";
print DOT "  ranksep=$ranksep\n";
188
print DOT "  fontname=\"Courier\"\n";
189 190 191
print DOT "  node [shape=rectangle,fontsize=$fontsize,".
            "height=$bheight,width=$bwidth,fixedsize=true]\n";
print DOT "  edge [arrowsize=$arrowsize]\n";
192 193 194
print DOT "  subgraph guid {\n";
print DOT "    label = \"$guid\";\n";

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
#
# Do a recursive decent to figure out the parent for each template.
# We do this cause we want to skip hidden templates, but pick it up
# again down the line when hidden=0.
#
sub SetParent($@)
{
    my ($parent, @kids) = @_;

    foreach my $kid (@kids) {
	if (!$showall && $hidden{$kid}) {
	    # pass our parent to children, thereby skipping a level.
	    SetParent($parent, @{ $children{$kid} })
		if (exists($children{$kid}));
	}
	else {
	    $parents{$kid} = $parent;
	    SetParent($kid, @{ $children{$kid} })
		if (exists($children{$kid}));
	}
    }
}
$parents{$rootnode} = undef;
SetParent($rootnode, @{ $children{$rootnode} });
219

220 221 222 223 224 225 226
#
# The parents hash now has all the templates that are visible. 
#
foreach my $kid (sort {$a <=> $b} keys(%parents)) {
    my $vers   = $kid;
    my $parent = $parents{$vers};
    my $tid    = $tids{$vers};
227 228
    my $url    = "$TBBASE/template_show.php?guid=$guid&version=$vers".
	"&show=graph";
229
    my $color  = "black";
230

231 232
    print DOT "    $parent -> $kid;\n"
	if (defined($parent));
233

234 235 236 237 238 239 240
    # Shorten the description
    my $description = $descriptions{$vers};
    if (length($description) > 50) {
	$description = substr($description, 0, 50) . " <b>...</b>";
    }

    # Encode to avoid confusing things.
241
    $description = encode_entities($description, '\'\r\n');
242 243
    $description = encode_entities($description);

244 245 246 247 248 249
    my $query_result =
	DBQueryFatal("select name,value from experiment_template_parameters ".
		     "where parent_guid='$guid' and parent_vers='$vers'");

    my $table = "<table cellpadding=0 cellspacing=0 border=0> ".
	"<tr><td>Version:</td><td>$vers</td></tr>".
250 251
	"<tr><td>TID:</td><td>$tid</td></tr>".
        "<tr><td>Description:</td><td>$description</td></tr>";
252 253 254

    if ($query_result->num_rows) {
	$table .= "<tr><td>Parameters:</td>".
255
	    "<td><table cellpadding=0 cellspacing=0 border=1>";
256 257

	while (my ($name, $value) = $query_result->fetchrow_array()) {
258
	    $table .= "<tr><td>${name}:</td><td>$value</td></tr>";
259 260 261 262 263 264
	}
	$table .= "</table></td>";
    }

    $table .= "</table>";
	
265
    my $tooltip = "return escape(\'$table\');";
266 267 268 269 270 271 272 273 274 275 276 277

    #
    # Figure out a point size that makes the label fit inside. We do not
    # want to go too big of course.
    #
    # Target labelwidth is 80% of the box, converted to points.
    my $labelwidth = ($bwidth * 0.80) / 0.0139;

    # And the fontsize ... I made this up.
    my $fontsize   = int((($labelwidth / length($tid)) * 1.8) + 0.5);

    # But we do not want the font arbitrarily large.
278 279
    $fontsize = 12
	if ($fontsize > 12);
280 281
    $color = "blue"
	if ($hidden{$vers});
282 283
	
    if (!exists($versions{"$vers"})) {
284
	print DOT "    $vers [fontsize=$fontsize,color=$color,".
285
	    "label=\"$tid\",href=\"$url\",tooltip=\"$tooltip\"];\n";
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	$versions{"$vers"} = $vers;
    }
}
print DOT "  }\n";
print DOT "}\n";
close(DOT);

#
# Now run dot and generate both a gif and an image map file.
#
system("$DOT -Tgif -o $giffile  $dotfile");
system("$DOT -Tcmapx -o $imapfile $dotfile");

#
# Grab the input data. 
#
my $gifdata = `cat $giffile`;
my $imapdata = `cat $imapfile`;

305 306 307 308
#
# Massage the mapfile; tooltips --> onmouseovers
#
$imapdata =~ s/title=/onmouseover=/g;
309
$imapdata =~ s/version=(\d*)\"/version=$1\" id=Tarea${1}/g;
310

311
#print "$imapdata\n";
312

313 314 315 316 317
$gifdata = DBQuoteSpecial($gifdata);
$imapdata = DBQuoteSpecial($imapdata);

DBQueryFatal("replace into experiment_template_graphs set ".
	     "    parent_guid='$guid', ".
318
	     "    scale='$scale', ".
319 320 321
	     "    image=$gifdata, ".
	     "    imap=$imapdata");

322 323
unlink($dotfile, $giffile, $imapfile)
    if (!$debug);
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
exit(0);

#
# Parse command arguments. Once we return from getopts, all that are
# left are the required arguments.
#
sub ParseArgs()
{
    if (! getopts($optlist, \%options)) {
	usage();
    }

    if (@ARGV != 1) {
	usage();
    }
    #
    # Pick up guid and untaint.
    #
    my $tmp = shift(@ARGV);

    if ($tmp =~ /^([\w]*)$/) {
	$guid = $1;
    }
    else {
	tbdie("Bad data in argument: $tmp");
    }

    if (defined($options{"p"})) {
	$prefix = $options{"p"};

	if ($prefix =~ /^([-\w\.\/]*)$/) {
	    $prefix = $1;
	}
	else {
	    tbdie("Bad data in argument: $prefix");
	}
    }
361 362
    if (defined($options{"d"})) {
	$debug = 1;
363
    }
364 365 366 367 368 369 370
    if (defined($options{"z"})) {
	$zoom = $options{"z"};

	if ($zoom ne "in" && $zoom ne "out") {
	    die("Improper zoom request: $zoom!\n");
	}
    }
371
}