runsonxen.in 4.55 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2010-2017 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 23 24 25 26 27
# 
# {{{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/>.
# 
# }}}
#
use English;
use strict;
use Getopt::Std;

28 29 30 31 32
#
# Mark an image as running on XEN. 
#
my $PROTOGENI_GENIRACK = @PROTOGENI_GENIRACK@;

33 34 35
#
# Mark an image as running on XEN. 
#
36
my @OTHER_PARENTS  = ("XEN44-64-STD", "XEN46-64-STD");
Leigh Stoller's avatar
Leigh Stoller committed
37 38 39
if ($PROTOGENI_GENIRACK) {
    push(@OTHER_PARENTS, "XEN44-64-GENIRACK");
}
40 41 42 43 44 45 46 47 48 49

sub usage()
{
    print STDERR "usage: runsonxen [-p <parent>] <imageid>\n";
    print STDERR "usage: runsonxen -a [-p <parent>]\n";
    print STDERR "usage: runsonxen -c <imageid>\n";
    print STDERR "Options:\n";
    print STDERR " -n      - Impotent mode\n";
    print STDERR " -c      - Clear XEN parent settings completely\n";
    print STDERR " -a      - Operate on all current XEN capable images\n";
50
    print STDERR " -p      - Set default parent\n";
51 52 53 54 55 56
    exit(1);
}
my $optlist    = "acp:n";
my $doall      = 0;
my $clear      = 0;
my $impotent   = 0;
57
my $parent;
58 59 60 61 62 63 64 65 66 67 68 69

# Protos
sub fatal($);

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

#
# Load the Testbed support stuff.
#
70
use lib "@prefix@/lib";
71
use emdb;
72
use libEmulab;
73
use OSImage;
74

75 76 77
# Default parent for this site is a sitevar.
my $DEFAULT_PARENT = GetSiteVar("general/default_xen_parentosid");

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
my %options    = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"c"})) {
    $clear = 1;
}
if (defined($options{"a"})) {
    $doall = 1;
}
if (defined($options{"n"})) {
    $impotent = 1;
}
if (defined($options{"p"})) {
    $parent = $options{"p"};
    @OTHER_PARENTS = ($DEFAULT_PARENT, @OTHER_PARENTS);
}
95 96 97
else {
    $parent = $DEFAULT_PARENT;
}
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
usage()
    if (!$doall && !@ARGV);

#
# List of images to operate on.
#
my @images = ();

if ($doall) {
    my $query_result =
	DBQueryFatal("select distinct v.osid from os_info_versions as v ".
		     "left join os_info_versions as v2 on ".
		     "     v2.osid=v.def_parentosid ".
		     "where v.deleted is null and ".
		     "      v.def_parentosid is not null and ".
		     "      FIND_IN_SET('xen-host', v2.osfeatures)");

    while (my ($osid) = $query_result->fetchrow_array()) {
116
	my $image = OSImage->Lookup($osid);
117 118 119 120 121
	push(@images, $image)
	    if (defined($image));
    }
}
else {
122
    my $image = OSImage->Lookup($ARGV[0]);
123 124 125 126 127 128 129 130 131
    if (!defined($image)) {
	fatal("No such image");
    }
    @images = ($image);
}

if ($clear) {
    foreach my $image (@images) {
	my $imageid = $image->imageid();
132
	my $osid    = $image->osid();
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	if ($impotent) {
	    print "Would clear parents for $image\n";
	}
	else {
	    DBQueryFatal("delete from osidtoimageid ".
			 "where osid='$imageid' and type='pcvm'");
	    DBQueryFatal("delete from os_submap ".
			 "where osid='$imageid'");
	}
    }
    exit(0);
}

#
# Find the "default" parent image and the list of others parents.
#
149
my $parent_image = OSImage->Lookup($parent);
150
if (!defined($parent_image)) {
151
    $parent_image = OSImage->LookupByName($parent);
152 153 154
    if (!defined($parent_image)) {
	fatal("No such parent $parent");
    }
155 156 157
}
my @other_parents = ();
foreach my $imagename (@OTHER_PARENTS) {
158 159 160
    my $osimage = OSImage->LookupByName($imagename);
    push(@other_parents, $osimage)
	if (defined($osimage));
161 162 163 164 165 166 167
}

#
# Loop through all images. 
#
foreach my $image (@images) {
    if ($impotent) {
168
	print "Would set $image to run on $parent_image (default)\n";
169 170
    }
    else {
171 172
	$image->SetParentOS($parent_image);
	$image->SetRunsOnParent($parent_image);
173 174 175 176 177 178 179
    }
    # And the rest of the parents.
    foreach my $other_parent (@other_parents) {
	if ($impotent) {
	    print "  Would set $image to run on $other_parent\n";
	}
	else {
180
	    $image->SetRunsOnParent($other_parent);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	}
    }
    if ($impotent) {
	print "  Setting $image to run on type pcvm\n";
    }
    else {
	$image->SetRunsOnNodeType("pcvm");
    }
}
exit(0);

sub fatal($) {
    my ($mesg) = $_[0];

    print STDERR "*** $0:\n".
	         "    $mesg\n";
    exit(-1);
}