node_attributes.in 6.51 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
Leigh Stoller's avatar
Leigh Stoller committed
3
# Copyright (c) 2000-2007 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
# 
# {{{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/>.
# 
# }}}
23
#
Leigh Stoller's avatar
Leigh Stoller committed
24
use strict;
25 26 27 28 29 30 31 32 33
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
34 35 36
    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".
37 38 39 40 41 42 43 44
          "       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
45
          "For multi-node attribute removal, use the \"nodelist:\" syntax\n");
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
    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
80 81 82
use Node;
use User;
use Experiment;
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

# 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
136 137 138 139 140 141 142 143 144 145 146 147 148
#
# 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");
}

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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
#
# 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
196 197 198 199 200
foreach my $n ( @ARGV ) {
    my $node = Node->Lookup($n);
    if (!defined($node)) {
	die("*** $0:\n".
	    "    Node $n does not exist!\n");
201
    }
Leigh Stoller's avatar
Leigh Stoller committed
202
    push(@nodes, $node->node_id());
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
}

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