setsitevar.in 2.91 KB
Newer Older
1 2 3 4
#!/usr/bin/perl -wT

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 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 144 145 146 147 148 149
# All rights reserved.
#
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".  
		"setsitevar <variablename>\n".
		"  - displays name, description, value, \n".
		"    and default value for <variablename>.\n".
		"\n".
		"setsitevar <variablename> -\n".
		"  - sets <variablename> to default value.\n".
		"\n".
		"setsitevar <variablename> <newvalue>\n".
		"  - sets <variablename> to <newvalue>.\n";

    exit(-1);
}

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

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

$name = shift;

if (defined $name && $name eq "-h") {
    usage();
}

$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) {
    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);