frisbeehelper.in 7.79 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2011-2016 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 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
#
use strict;
use Getopt::Std;
use Sys::Syslog;
use English;
use Socket;
use Errno;

#
# This is a compatibility shim for the pre-mfrisbeed frisbee world.
#
# It can be used to start or kill a frisbeed via the master server.
#

#
# This also kills a running frisbee.
#
sub usage()
{
    print "Usage: $0 [-d] -n nodeid <imageid>\n";
    print "  or\n";
    print "Usage: $0 [-d] -k <imageid>\n";
    print "-n nodeid   Node used for authentication of image access\n";
    print "-k:	       Kill (rather than start) a frisbeed.\n";
    print "-d:	       Print debugging output.\n";
    exit(1);
}
my $optlist  = "dkn:";
my $debug    = 0;
my $killmode = 0;
my $nodeid;

# Configure variables
my $TB		= "@prefix@";

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

use lib "@prefix@/lib";
use libdb;
use libtestbed;
use libtblog;
use Node;
use User;
75
use OSImage;
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

# Protos.
sub Fatal($);
sub debug($);

# Defines
my $FRISBEE	= "$TB/sbin/frisbee";
my $PIDDIR	= "/var/run";

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"k"})) {
    $killmode = 1;
}
if (defined($options{"n"})) {
    $nodeid = $options{"n"};
    if ($nodeid =~ /^([-\w]+)$/) {
	$nodeid = $1;
    } else {
	Fatal("invalid nodeid");
    }
}
usage()
    if (!@ARGV || (!$killmode && !$nodeid));

my $imagename = $ARGV[0];
if ($imagename =~ /^([-\w\.+\/]+)$/) {
    $imagename = $1;
} else {
    Fatal("invalid imagename");
}

#
# Authentication.
#
# Note that an imageid can be a traditional disk image or the pathname
# of a file, the master frisbee server worries about that distinction.
#
# For starting an image server, we provide a node_id which the master
# server can authenticate with. We would also like to make sure the calling
# user has permission to manipulate the node given, but unfortunately we
# are called from tmcd which runs as "nobody". So since this is a temporary
# shim, and we only run on boss where access is limited to authorized users
# or through constrained interfaces, we allow any user to start up a
# daemon for an image.
#
# For killing a server, we don't always have a node context.  In the
# case where we don't, it will be a true image and we can perform an access
# check with respect to the image.
#
# So, the strategy is:
#
#  * If we have a node context, and it is a regular Emulab image,
#    we let the master server worry about whether the node can access
#    the image.  This is the "relaxed" case for tmcd.
#
#  * If we have a node context, but it is not an Emulab image,
#    we check that the user can modify the node, and let the master server
#    worry about the node/user's ability to access the file.
#    We do the user check here because tmcd will never call us to check
#    on a non-image file.
#
#  * If there is no node context, it can only be a "kill" and we check
#    the user's ability to modify the image (if it isn't an image, this
#    check will fail).
#
my ($this_user, $isadmin, $image);

if ($UID) {
    $this_user = User->ThisUser();
    if (!defined($this_user)) {
	# XXX allow unknown user to start a server for a specified node
	if (!$nodeid || $killmode) {
	    Fatal("$UID: not a valid Emulab user");
	}
	$isadmin = 1;
    } else {
	$isadmin = $this_user->IsAdmin();
    }
} else {
    $isadmin = 1;
}

168
$image = OSImage->Lookup($imagename);
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 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
if (!$image) {
    # If not an Emulab image, we must have a node context to check
    if (!$nodeid) {
	Fatal("$imagename: not a valid Emulab image");
    }
    # We must also have a valid user
    if (!$this_user) {
	Fatal("$UID: not a valid Emulab user");
    }
}

if ($nodeid) {
    my $node = Node->Lookup($nodeid);
    if (!$node) {
	Fatal("$nodeid: no such node");
    }
    if (!$isadmin &&
	!$node->AccessCheck($this_user, TB_NODEACCESS_LOADIMAGE)) {
	Fatal("$nodeid: cannot access node");
    }
} else {
    # must be a kill operation; we checked this above
    # must be a real image; ditto
    # only called this way when deleting an image, so check access accordingly
    if (!$isadmin &&
	!$image->AccessCheck($this_user, TB_IMAGEID_DESTROY)) {
	Fatal("$imagename: no permission to destroy");
    }
}

#
# If it is a real image, we have to construct the proper master server
# imageid for it (pid/imagename), otherwise the given name is treated
# as a filename path.
#
my $imageid;
if ($image) {
    $imageid = $image->pid() . "/" . $image->imagename();
} else {
    $imageid = $imagename;
}

#
# Now we use the frisbee client to contact the master server
# and either request starting of a server or to get back info that
# we can use to kill the server instance.
#
my $cmd = "$FRISBEE -S localhost";
if ($nodeid) {
    $cmd .= " -P $nodeid";
}
if ($killmode) {
    $cmd .= " -Q $imageid";
} else {
    $cmd .= " -F $imageid /dev/null";
}
debug("Invoking: $cmd\n");

#
# Parse the query/get output to get the address/port to either report
# to the user on startup or to use to form a pid file name that we can
# use to kill a running server.
#
my ($addr,$port,$running);
if (!open(FRIS, "$cmd 2>&1 |")) {
    Fatal("frisbee invocation failed!");
}
while (<FRIS>) {
    if (/^error=(.*)$/) {
	if ($1 != 0) {
239 240 241 242 243
	    # For killmode we take an error to mean no daemon is running
	    if ($killmode) {
		$running = 0;
		last;
	    }
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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	    Fatal("frisbee returned error=$1");
	}
	next;
    }
    if (/^running=(\d)$/) {
	$running = $1;
	last
	    if ($running == 0);
	next;
    }
    if (/^addr=([\d.]+)$/) {
	$addr = $1;
	next;
    }
    if (/^port=(\d+)$/) {
	$port = $1;
	next;
    }
    if (/: address: ([\d.]+):(\d+)/) {
	$running = 1;
	$addr = $1;
	$port = $2;
	last;
    }
}
close(FRIS);
if ($running && (!$addr || !$port)) {
    Fatal("$imageid: could not determine address/port for server");
}

#
# Starting: just report information
#
if (!$killmode) {
    if (!$running) {
	Fatal("$imageid: could not start server");
    }
    print "Address is $addr:$port\n";
    exit(0);
}

#
# Killing: kill daemon if running
#
if ($running) {
    my $pidfile = "$PIDDIR/frisbeed-$addr-$port.pid";
    if (! -r "$pidfile") {
	debug("no pidfile '$pidfile' found\n");
    } else {
	my $pid = `cat $pidfile`;
	if ($?) {
	    debug("could not open '$pidfile'\n");
	} else {
	    if ($pid =~ /^(\d+)/) {
		$pid = $1;
		if (!kill('TERM', $pid)) {
		    debug("could not kill $imageid process $pid\n");
		} else {
		    print "Killed process $pid\n";
		}
	    } else {
		debug("bogus pid in $pidfile\n");
	    }
	}
    }
}
exit(0);


######################################################################
# Subroutines
######################################################################

# Only print if -d option was given. Also add $$ on the beginning of the
# string, to ease debugging
sub debug($)
{
    my ($msg) = @_;

    print STDERR "$$: $msg"
	if ($debug);
}

sub Fatal($)
{
    my ($msg) = @_;
    
    tbdie("*** $0:\n".
	  "    $msg\n");
}