newwanode.in 8.62 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2000-2008 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 24 25 26 27 28 29 30 31
#
use English;
use Getopt::Std;

#
# Add a new node to the DB. Strictly for widearea nodes right now.
# 
sub usage()
{
Leigh Stoller's avatar
Leigh Stoller committed
32 33
    print "Usage: ".
	"newwanode [-w] [-n nickname] -t <nodetype> -i <ip address>\n";
34 35
    exit(1);
}
36
sub fatal($);
37
my  $optlist = "wt:i:a:n:";
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

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

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

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

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libdb;
use libtestbed;
60
use NodeType;
61 62 63 64 65 66 67 68 69 70 71 72 73

#
# This is run from the web page, but tbadmins can run it from the command
# line. 
#
if ($UID != getpwnam("nobody") && !TBAdmin($UID)) {
    die("*** $0:\n".
	"    You do not have permission to run this script!\n");
}

my $nodetype;
my $nodeip;
my $fromweb = 0;
74
my $reuse   = 0;
Leigh Stoller's avatar
Leigh Stoller committed
75
my $nickname;
76 77
my $nodename;
my $nodevtype;
78
my $nodeauxtype;
79
my $nodevname;
80 81 82 83
my $pid;
my $eid;
my $nextid;
my $nextpri;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"w"})) {
    $fromweb = 1;
}
if (defined($options{"i"})) {
    $nodeip = $options{"i"};
}
if (defined($options{"t"})) {
    $nodetype = $options{"t"};
}
102 103
if (defined($options{"a"})) {
    $nodeauxtype = $options{"a"};
Leigh Stoller's avatar
Leigh Stoller committed
104 105 106 107
}
if (defined($options{"n"})) {
    $nickname = $options{"n"};
}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
if (!defined($nodeip) || !defined($nodetype)) {
    usage();
}

#
# Taint check.
#
if ($nodetype =~ /^([\w]+)$/) {
    $nodetype = $1;
}
else {
    die("*** Bad data in $nodetype\n");
}
if ($nodeip =~ /^([\d\.]+)$/) {
    $nodeip = $1;
}
else {
    die("*** Bad data in $nodeip\n");
}
127 128 129
if (defined($nodeauxtype)) {
    if ($nodeauxtype =~ /^([\w]+)$/) {
	$nodeauxtype = $1;
Leigh Stoller's avatar
Leigh Stoller committed
130 131
    }
    else {
132
	die("*** Bad data in $nodeauxtype\n");
Leigh Stoller's avatar
Leigh Stoller committed
133 134 135 136 137 138 139 140 141 142
    }
}
if (defined($nickname)) {
    if ($nickname =~ /^([-\w]+)$/) {
	$nickname = $1;
    }
    else {
	die("*** Bad data in $nickname\n");
    }
}
143 144 145 146

#
# Must be a valid type.
#
147 148
my $typeinfo = NodeType->Lookup($nodetype);
if (!defined($typeinfo)) {
149 150 151
    fatal("*** $0:\n".
	  "    No such nodetype $nodetype is defined in the DB!\n");
}
152 153 154 155 156 157 158 159 160 161
my ($defosid,$control_iface);

if ($typeinfo->default_osid(\$defosid) || !$defosid) {
    fatal("*** $0:\n".
	  "    No default OSID for $nodetype is defined in the DB!\n");
}
if ($typeinfo->control_interface(\$control_iface) || !$control_iface) {
    fatal("*** $0:\n".
	  "    No control interface for $nodetype is defined in the DB!\n");
}
162 163 164 165 166 167 168 169 170

#
# For now, just pcwa/pcron is allowed until we have more DB state that says
# how to build the virtual nodes, although that is temporary as well.
#
if ($nodetype ne "pcwa" && $nodetype ne "pcron") {
    fatal("Can only handle pcwa/pcron nodetype right now!");
}

Leigh Stoller's avatar
Leigh Stoller committed
171
#
172
# Create the virtual type name. Again, this is bogus.
Leigh Stoller's avatar
Leigh Stoller committed
173
#
174 175 176 177 178
if ($nodetype eq "pcwa") {
    $nodevtype = "pcvwa";
    $nodeauxtype = "pcvwainet"
	if (!defined($nodeauxtype));
}    
179
else {
180 181 182
    $nodevtype = "pcvron";
    $nodeauxtype = "pcvroninet"
	if (!defined($nodeauxtype));
183
}
184

185
#
186 187 188 189 190 191 192 193 194
# Again, this is bogus.
#
if ($nodetype eq "pcwa") {
    $pid = "emulab-ops";
    $eid = "widearea-nodes";
}    
else {
    $pid = "ron";
    $eid = "all";
195 196
}

197 198 199 200 201 202
my $exptidx;
if (!TBExptIDX($pid, $eid, \$exptidx)) {
    fatal("*** $0:\n".
	  "    No such experiment $pid/$eid!\n");
}

203
#
204
# Lets see if a known IP. If so we want to reuse the existing record.
205
#
206 207 208 209 210 211 212 213 214 215
if (TBIPtoNodeID($nodeip, \$nodename)) {
    #
    # We need the existing priority for creating the vnodes below.
    #
    $query_result =
	DBQueryFatal("select priority from nodes where node_id='$nodename'");

    my %row  = $query_result->fetchhash();
    $nextpri = $row{'priority'};
    $reuse   = 1;
216 217
}
else {
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
    #
    # We need the next id and priority.
    # 
    DBQueryFatal("lock tables nextfreenode write");

    $query_result =
	DBQueryFatal("select nextid,nextpri from nextfreenode ".
		     "where nodetype='$nodetype'");

    if (!$query_result->numrows) {
	fatal("nodetype $nodetype is not in the nextfreenode table!");
    }
    my %row  = $query_result->fetchhash();
    $nextid  = $row{'nextid'};
    $nextpri = $row{'nextpri'};

    DBQueryFatal("update nextfreenode ".
		 "set nextid=nextid+1,nextpri=nextpri+1 ".
		 "where nodetype='$nodetype'");

    DBQueryFatal("unlock tables");

    #
    # Form a new node name! Bogus. need more DB state.
    #
    if ($nodetype =~ /^pc(\w+)$/) {
	$nodename = "${1}${nextid}";
    }
    else {
	fatal("Could not determine a name from type for $nodetype!");
    }
249
}
250

Leigh Stoller's avatar
Leigh Stoller committed
251 252 253 254 255
if (!defined($nickname)) {
    $nickname = $nodename;
}

#
256
# Enter the records
Leigh Stoller's avatar
Leigh Stoller committed
257
#
258 259
if ($reuse) {
    print "Reusing exiting record for $nodename. IP=$nodeip\n";
Leigh Stoller's avatar
Leigh Stoller committed
260
}
261 262 263 264
else {
    print "Creating widearea node $nodename ...\n";
    DBQueryFatal("insert into nodes ".
		 "(node_id, type, phys_nodeid, role, priority, ".
265
		 " op_mode, def_boot_osid) ".
266
		 "values ('$nodename', '$nodetype', '$nodename', ".
267
		 "        'testnode', $nextpri, 'NORMAL', ".
268
		 "        '$defosid') ");
269

270 271 272
    DBQueryFatal("insert into node_hostkeys (node_id) ".
		 "values ('$nodename')");

273 274
    DBQueryFatal("insert into node_auxtypes ".
		 "(node_id, type, count) ".
275 276 277 278 279
		 "values ('$nodename', '$nodeauxtype', 20)");

    DBQueryFatal("insert into node_auxtypes ".
		 "(node_id, type, count) ".
		 "values ('$nodename', '$nodevtype', 20)");
280

281 282 283 284
    DBQueryFatal("replace into node_status ".
		 "(node_id, status, status_timestamp) ".
		 "values ('$nodename', 'down', now()) ");

285 286
    # Note that card/port are arbitrary for remote nodes since they are
    # not connected to local switches! 
287
    my $ifacerole = TBDB_IFACEROLE_CONTROL();
288
        
289
    DBQueryFatal("insert into interfaces ".
290
		 "(node_id, card, port, IP, interface_type, iface, role) ".
291 292
		 "values ('$nodename', 0, 1, '$nodeip', ".
		 "        'fxp', '$control_iface', '$ifacerole')");
293

294 295 296 297
    DBQueryFatal("insert into interface_state ".
		 "(node_id, card, port, iface) ".
		 "values ('$nodename', 0, 1, 'fxp')");

298
    DBQueryFatal("insert into reserved ".
299 300 301
		 "(node_id, exptidx, pid, eid, rsrv_time, vname) ".
		 "values ('$nodename', $exptidx, ".
		 "        '$pid', '$eid', now(), '$nickname')");
302 303 304 305 306
}

#
# Always create these. I'll kill off the old ones as needed for now.
# 
Leigh Stoller's avatar
Leigh Stoller committed
307
for ($i = 1; $i < 9; $i++) {
308
    my $priority  = ($nextpri * 100) + $i;
Leigh Stoller's avatar
Leigh Stoller committed
309
    $nodevname = "v${nodename}-${i}";
310

311
    print "Creating widearea virtual node $nodevname ...\n";
312 313
    DBQueryFatal("insert into nodes ".
		 "(node_id, type, phys_nodeid, role, priority, ".
314
		 " op_mode, def_boot_osid, update_accounts) ".
315
		 "values ('$nodevname', '$nodevtype', '$nodename', ".
316
		 "        'virtnode', $priority, 'NORMAL', ".
317
		 "        '$defosid', 1) ");
318 319 320

    DBQueryFatal("insert into node_hostkeys (node_id) ".
		 "values ('$nodevname')");
321 322
}

Leigh Stoller's avatar
Leigh Stoller committed
323
#
324
# Lets log it.
Leigh Stoller's avatar
Leigh Stoller committed
325
#
326 327 328 329 330 331
my $subject;
my $message;
if ($reuse) {
    $subject = "Existing WA node has reinstalled";
    $message = "Existing WA node has been reinstalled";
}
Leigh Stoller's avatar
Leigh Stoller committed
332
else {
333 334
    $subject = "New WA node created";
    $message = "New Wide Area node has been created";
Leigh Stoller's avatar
Leigh Stoller committed
335
}
336

337 338
SENDMAIL($TBLOGS, $subject,
	 "$message\n".
339 340 341 342 343
	 "\n".
	 "Type:      $nodetype\n".
	 "Name:      $nodename\n".
	 "IP:        $nodeip\n".
	 "Vtype:     $nodevtype\n".
344
	 "Auxtype:   $nodeauxtype\n".
345 346 347
	 "Vname:     $nodevname (actually the last vnode)\n".
	 "Nickname:  $nickname\n",
	 $TBOPS);
348 349 350 351 352

#
# update the named maps. Do it in the background since we do not
# want to hold up the caller.
#
353
print "Forcing a named map update ...\n";
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
my $logname = TBMakeLogname("newwanode");
if (TBBackGround($logname)) {
    #
    # Parent exits normally
    #
    exit(0);
}
system("$named_setup");
unlink($logname);
exit(0);

sub fatal($) {
    my ($msg) = @_;

    print STDERR "$msg\n";
    SENDMAIL($TBOPS, "Failure creating new WA node", $msg, $TBOPS);
    exit(1);
}