rc.hostnames 6.19 KB
Newer Older
1 2 3 4 5 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
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2004 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use Getopt::Std;

#
# This takes a special argument so it can be called by ixpsetup to create
# a specific hosts file other than /etc/hosts.
# 
sub usage()
{
    print "Usage: " .
	scriptname() . " [-j vnodeid] [-f file] boot|shutdown|reconfig|reset\n";
    exit(1);
}
my $optlist = "j:f:";
my $action  = "boot";

# Only root.
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script!\n");
}

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

# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

# Script specific goo
my $HOSTSFILE  = "/etc/hosts";
my $DEFAULT    = "$ETCDIR/hosts";
my $pathname   = $HOSTSFILE;

#
# Load the OS independent support library. It will load the OS dependent
# library and initialize itself. 
# 
use libsetup;
use liblocsetup;
use libtmcc;
use librc;

#
# Not all clients support this.
#
exit(0)
    if (MFS() || (REMOTE() && !(PLAB() || JAILED())));

# Protos.
sub doboot();
sub doshutdown();
sub doreconfig();
sub docleanup();

# Parse command line.
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'f'})) {
    $pathname = $options{'f'};
}
if (defined($options{'j'})) {
    my $vnodeid = $options{'j'};
    libsetup_setvnodeid($vnodeid);
}
# Allow default above.
if (@ARGV) {
    $action = $ARGV[0];
}

# Execute the action.
SWITCH: for ($action) {
    /^boot$/i && do {
	doboot();
	last SWITCH;
    };
    /^shutdown$/i && do {
	doshutdown();
	last SWITCH;
    };
    /^reconfig$/i && do {
	doreconfig();
	last SWITCH;
    };
    /^reset$/i && do {
	docleanup();
	last SWITCH;
    };
    fatal("Invalid action: $action\n");
}
exit(0);

99 100 101
# More protos
sub fromtopo($);

102 103 104 105 106 107 108 109 110 111
#
# Boot Action.
#
sub doboot()
{
    my $HTEMP = "${pathname}.new";
    my @tmccresults;

    print STDOUT "Checking Testbed hostnames configuration ... \n";

112 113 114 115 116 117
    #
    # First see if we have a topo file; we can generate our own hosts
    # file if we do, saving a lot of load on tmcd in big experiments.
    #
    if (fromtopo(\@tmccresults) < 0 &&
	tmcc(TMCCCMD_HOSTS, undef, \@tmccresults) < 0) {
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 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 196 197 198 199 200 201 202 203 204 205
	fatal("Could not get hosts file from server!");
    }
    # Important; if no results then do nothing. Do not want to kill
    # the existing hosts file.
    return 0
	if (! @tmccresults);

    #
    # Note, we no longer start with the 'prototype' file here because we have
    # to make up a localhost line that's properly qualified.
    #
    if (!open(HOSTS, ">$HTEMP")) {
	fatal("Could not open $HTEMP: $!");
    }

    my $localaliases = "loghost";

    #
    # Find out our domain name, so that we can qualify the localhost entry
    #
    my $hostname = `hostname`;
    if ($hostname =~ /[^.]+\.(.+)/) {
	$localaliases .= " localhost.$1";
    }
    
    #
    # First, write a localhost line into the hosts file - we have to know the
    # domain to use here
    #
    print HOSTS os_etchosts_line("localhost", "127.0.0.1",
				 $localaliases), "\n";

    #
    # Now convert each hostname into hosts file representation and write
    # it to the hosts file. Note that ALIASES is for backwards compat.
    # Should go away at some point.
    #
    my $pat  = q(NAME=([-\w\.]+) IP=([0-9\.]*) ALIASES=\'([-\w\. ]*)\');

    foreach my $str (@tmccresults) {
	if ($str =~ /$pat/) {
	    my $name    = $1;
	    my $ip      = $2;
	    my $aliases = $3;
	    
	    my $hostline = os_etchosts_line($name, $ip, $aliases);
	    
	    print HOSTS "$hostline\n";
	}
	else {
	    warning("Bad hosts line: $str");
	}
    }
    close(HOSTS);
    system("mv -f $HTEMP $pathname");
    if ($?) {
	fatal("Could not move $HTEMP to $pathname!");
    }
    return 0;
}

#
# Shutdown Action.
#
sub doshutdown()
{
    # Nothing to do
}

#
# Node Reconfig Action (without rebooting).
#
sub doreconfig()
{
    doshutdown();
    return doboot();
}

#
# Node cleanup action (node is reset to completely clean state).
#
sub docleanup()
{
    system("$CP -f $DEFAULT $HOSTSFILE");
    if ($?) {
	fatal("Could not copy default $HOSTSFILE into place!");
    }
}
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

#
# Generate hosts file (as if it came from tmcd) locally if we have a topo file.
#
sub fromtopo($)
{
    my ($rptr)	= @_;
    my @results = ();
    my $topomap;
    my $mapfile = "$BOOTDIR/hostmap";
    my ($pid, $eid, $vname) = check_nickname();
    my %nodes = ();
    my %lans  = ();;

    if (gettopomap(\$topomap)) {
	return -1;
    }

224 225 226 227 228 229
    # Special case of experiment with no lans; no hostfile stuff needed.
    if (! scalar(@{ $topomap->{"lans"} })) {
	@$rptr = ();
	return 0;
    }

230 231 232 233 234 235
    # The nodes section tells us the name of each node, and all its links.
    foreach my $noderef (@{ $topomap->{"nodes"} }) {
	my $vname  = $noderef->{"vname"};
	my $links  = $noderef->{"links"};
	my $count  = 0;

236 237 238
	next
	    if (!defined($links));

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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	$nodes{$vname} = [];

	# Links is a string of "$lan1:$ip1 $lan2:$ip2 ..."
	foreach my $link (split(" ", $links)) {
	    my ($lan,$ip) = split(":", $link);

	    push(@{ $nodes{$vname} }, "$count:$ip");
	    $lans{"$vname:$count"} = $lan;
	    $count++;
	}
    }

    #
    # Construct input for external program. 
    #
    if (! open(MAP, ">$mapfile")) {
	warn("*** WARNING: Could not create $mapfile!\n");
	@$rptr  = ();
	return -1;
    }

    #
    # First spit out virt_nodes
    #
    print MAP scalar(keys(%nodes)) . "\n";

    foreach my $node (keys(%nodes)) {
	my @members = @{ $nodes{$node} };

	print MAP "$node,";
	print MAP join(" ", @members);
	print MAP "\n";
    }
    #
    # Then spit out virt_lans.
    # 
    print MAP scalar(keys(%lans)) . "\n";

    foreach my $member (keys(%lans)) {
	my $lan = $lans{$member};

	print MAP "$lan,$member\n";
    }
    close(MAP);

    #
    # Now run the dijkstra program on the input. 
    # 
    if (!open(GENH, "cat $mapfile | $BINDIR/genhostsfile $vname |")) {
	warn("*** WARNING: Could not invoke genhostsfile on mapfile!\n");
	@$rptr  = ();
	return -1;
    }
    while (<GENH>) {
	push(@results, $_);
    }
    if (! close(GENH)) {
	if ($?) {
	    warn("*** WARNING: genhostsfile exited with status $?!\n");
	}
	else {
	    warn("*** WARNING: Error closing genhostsfile pipe: $!\n");
	}
	@$rptr  = ();
	return -1;
    }
    @$rptr = @results;
    return 0;
}