node_attributes.in 5.82 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
Leigh Stoller's avatar
Leigh Stoller committed
4
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
5 6
# All rights reserved.
#
Leigh Stoller's avatar
Leigh Stoller committed
7
use strict;
8 9 10 11 12 13 14 15 16
use English;
use Getopt::Std;

#
# This script is invoked from ops and from the web interface. Must check
# all the args.
#
sub usage()
{
Leigh Stoller's avatar
Leigh Stoller committed
17 18 19
    print("Usage: node_attributes -m|a name=value [name=value ...] node ...\n".
          "       node_attributes -r attr [attr ...] node\n".
          "       node_attributes -r attr [attr ...] nodelist: node ...\n".
20 21 22 23 24 25 26 27
          "       node_attributes -l node [node ...]\n".
          "\n".
          "Must specify one of: ".
          "       -m modify attributes\n". 
          "       -a add attributes\n".
          "       -r remove attributes\n".
          "       -l list attributes\n".
          "\n".
Leigh Stoller's avatar
Leigh Stoller committed
28
          "For multi-node attribute removal, use the \"nodelist:\" syntax\n");
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
    exit(-1);
}
my  $optlist = "mardl";

#
# Define a few constants
#
my $NODEATTRS_TABLE = "node_attributes";
my $NODEATTRS_KEY   = "attrkey";
my $NODEATTRS_VAL   = "attrvalue";

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBLOGS      = "@TBLOGSEMAIL@";

my @nodes       = ();
my %attrs       = ();
my $debug       = 0;
my $errors	= 0;

my $modify_attrs = 0;
my $add_attrs    = 0;
my $remove_attrs = 0;
my $list_attrs   = 0;

#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
Leigh Stoller's avatar
Leigh Stoller committed
63 64 65
use Node;
use User;
use Experiment;
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

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

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

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
my $operation = 0;
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"m"})) {
    $modify_attrs = 1;
    $operation++;
}
if (defined($options{"a"})) {
    $add_attrs    = 1;
    $operation++;
}
if (defined($options{"r"})) {
    $remove_attrs = 1;
    $operation++;
}
if (defined($options{"l"})) {
    $list_attrs   = 1;
    $operation++;
}

#
# Sanity checks.
#
if ($operation > 1) {
    print "Error: Only one of -m, -a, -r, or -l may be specified!\n";
    usage();
}
elsif ($operation == 0) {
    print "Error: One of -m, -a, -r, or -l MUST be specified!\n";
    usage();
}

if (! @ARGV) {
    usage();
}

Leigh Stoller's avatar
Leigh Stoller committed
119 120 121 122 123 124 125 126 127 128 129 130 131
#
# Verify user and get his DB uid and other info for later.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    die("*** $0:\n".
	"    You ($UID) do not exist!\n");
}
if (!$this_user->IsAdmin()) {
    die("*** $0:\n".
	"    You must be an admin to use this command!\n");
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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
#
# Shift off the set strings (name=value). Verify that each one is in the
# proper format.
#
while (@ARGV) {
    my $string = $ARGV[0];

    #
    # Attributes are bare when in removal mode.
    # If the 'nodelist:' token is given, then the remainder
    # of the command line contains nodes.  Otherwise, the last
    # argument must be the node to operate on.
    #
    if ($remove_attrs) {
        if ($string =~ /nodelist:/) {
            shift @ARGV;
            last;
        }
        elsif (@ARGV == 1) {
            last;
        }
        else {
            $attrs{$string} = "remove";
        }
    }
    else {
        if (! ($string =~ /([-\w]*)=[\']?([^\']*)[\']?/)) {
            last;
        }
        $attrs{$1} = "$2";
    }
    shift @ARGV;
}

if ($debug) {
    foreach my $option (keys(%attrs)) {
	print "Will set $option to '$attrs{$option}'\n";
    }
}

# Be sure at least one node was specified.
if (! @ARGV) {
    print "You must specify one or more nodes!\n";
    usage();
}
    
# Untaint the nodes.
Leigh Stoller's avatar
Leigh Stoller committed
179 180 181 182 183
foreach my $n ( @ARGV ) {
    my $node = Node->Lookup($n);
    if (!defined($node)) {
	die("*** $0:\n".
	    "    Node $n does not exist!\n");
184
    }
Leigh Stoller's avatar
Leigh Stoller committed
185
    push(@nodes, $node->node_id());
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
}

if ($debug) {
    print "node list: @nodes\n";
}

# If this is a attribute listing command, then let's list 'em!
# XXX: this is done in a very lame way right now.
if ($list_attrs) {
    my $nodelist = join("','", @nodes);
    my $query_result =
        DBQueryFatal("select * from node_attributes where ". 
                     "node_id in ('$nodelist') order by node_id");
    print "node_id \t attribute \t value\n";
    while (my $row = join("\t",$query_result->fetchrow_array())) {
        print "$row\n";
    }
    exit(0);
}

#
# Process the attributes to add, mod, or remove
#
foreach my $attr (keys(%attrs)) {
    my $value = $attrs{$attr};

    #
    # Do a checkslot on the key and value to make sure they are 
    # valid.
    #
    if ($attr ne "" &&
	!TBcheck_dbslot($attr, $NODEATTRS_TABLE, $NODEATTRS_KEY,
			TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	die("*** $0:\n".
	    "    Illegal attribute name: '$attr'\n");
    }
    if ($value ne "" &&
	!TBcheck_dbslot($value, $NODEATTRS_TABLE, $NODEATTRS_VAL,
			TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	die("*** $0:\n".
	    "    Illegal value contents: '$value'\n");
    }

    #
    # If this is a modify operation, then just build up a single clause to
    # execute later.  Otherwise perform the insert/delete operation
    # for each node now.
    #
    if ($modify_attrs) {
        foreach my $node (@nodes) {
            DBQueryFatal("replace into $NODEATTRS_TABLE values ".
                         "('$node','$attr','$value')");
        }
    }
    # XXX: maybe just merge this with modify operation.
    elsif ($add_attrs) {
        foreach my $node (@nodes) {
            DBQueryFatal("insert into $NODEATTRS_TABLE values ".
                         "('$node','$attr','$value')");
        }
    }
    elsif ($remove_attrs) {
        foreach my $node (@nodes) {
            DBQueryFatal("delete from $NODEATTRS_TABLE where ".
                         "node_id='$node' and $NODEATTRS_KEY='$attr'");
        }
    }

}

exit(0);