setsitevar.in 3.98 KB
Newer Older
1
#!/usr/bin/perl -w
Chad Barb's avatar
Chad Barb committed
2 3

#
4
# Copyright (c) 2000-2012 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{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/>.
# 
# }}}
Chad Barb's avatar
Chad Barb committed
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
#
use English;
use Getopt::Std;

#
# usage: setsitevar
#         - displays list of all sitevar names and values
#
#        setsitevar <variablename>
#         - displays name, description, value, and default for <variablename>
#
#        setsitevar <variablename> -
#         - sets <variablename> to default value
#
#        setsitevar <variablename> <newvalue>
#         - sets <variablename> to <newvalue>
#

sub usage()
{
   print STDOUT "Usage:\n".
                "setsitevar -h\n".
		"  - displays this usage information.\n".
		"\n".  
                "setsitevar\n".
		"  - displays list of all sitevar names and values.\n".
		"\n".  
51
		"setsitevar [-c] <variablename>\n".
Chad Barb's avatar
Chad Barb committed
52 53
		"  - displays name, description, value, \n".
		"    and default value for <variablename>.\n".
54
		"    With -c option, just display current value.\n".
Chad Barb's avatar
Chad Barb committed
55 56 57 58 59 60 61 62 63
		"\n".
		"setsitevar <variablename> -\n".
		"  - sets <variablename> to default value.\n".
		"\n".
		"setsitevar <variablename> <newvalue>\n".
		"  - sets <variablename> to <newvalue>.\n";

    exit(-1);
}
64 65
my $optlist    = "dc";
my $curvalue   = 0;
Chad Barb's avatar
Chad Barb committed
66 67 68 69 70 71 72 73 74 75 76 77

#
# Configure variables
#
use lib "@prefix@/lib";
use libdb;

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

78 79 80 81 82 83 84 85
#
# Check args.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"h"})) {
Chad Barb's avatar
Chad Barb committed
86 87
    usage();
}
88 89 90 91 92
if (defined($options{"c"})) {
    $curvalue = 1;
}
usage()
    if (!@ARGV);
Chad Barb's avatar
Chad Barb committed
93

94
$name  = shift;
Chad Barb's avatar
Chad Barb committed
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
$value = shift;

shift and usage(); # too many args

if (defined $name)  { $qname  = DBQuoteSpecial($name); }
if (defined $value) { $qvalue = DBQuoteSpecial($value); }

#
# Only root or admin types!
# 
if (($UID != 0) && (!TBAdmin($UID))) {
	die("Only root or TB administrators can set site variables.\n");
}

#
# Briefly list all sitevars, a la 'set'.
#
if (! defined $name) {
    $result = DBQueryFatal("SELECT name, value, defaultvalue, description ".
			   "FROM sitevariables ".
			   "ORDER BY name");

    while (($name, $value, $default, $desc) = 
	   $result->fetchrow_array()) {
	if (defined $value) {	    
	    print "$name\t\"$value\"\n";
	} else {
	    print "$name\t\"$default\"(default)\n";
	}
    }
    exit(0);
}


$result = DBQueryFatal("SELECT value, defaultvalue, description ".
		       "FROM sitevariables ".
		       "WHERE name=$qname");

if ($result->numrows == 0) {
    print STDERR "No such sitevar $qname!\n";
    exit(1);
}    

($currentvalue, $default, $desc) = $result->fetchrow_array();

#
# List a specific sitevar in detail
#
if (! defined $value) {
144 145 146 147 148 149 150 151
    if ($curvalue) {
	if (defined $currentvalue) {
	    print "$currentvalue\n";
	} else {
	    print "$default\n";
	}
	exit(0)
    }
Chad Barb's avatar
Chad Barb committed
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
    print "sitevar name:  $name\n".
	  "description:   $desc\n".
	  "value:         ";
    if (defined $currentvalue) {
	print "\"$currentvalue\"\n";
    } else {
	print "<default>\n";
    }
    print "default value: \"$default\"\n";
    exit(0);
}

#
# Set a sitevar
#

#
# Handle '-' argument
#
if ($qvalue eq "'-'") {
    $qvalue = "NULL";
}

$result = DBQueryFatal("UPDATE sitevariables ".
		       "SET value=$qvalue ".
		       "WHERE name=$qname");

#
# Warn user if operation was redundant.
#
#if ($result->rows == 0) {
#    print STDERR "Warning: row value same as before\n";
#}

exit(0);