runsonxen.in 4.96 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
Leigh B Stoller's avatar
Leigh B Stoller committed
3
# Copyright (c) 2010-2015 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-BIGFS", "XEN44-64-THIN", "XEN44-64-STD");
Leigh B Stoller's avatar
Leigh B 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 70 71

# Protos
sub fatal($);

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

#
# Load the Testbed support stuff.
#
use lib "/usr/testbed/lib";
use emdb;
72
use libEmulab;
73 74 75
use Image;
use OSinfo;

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

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
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);
}
96 97 98
else {
    $parent = $DEFAULT_PARENT;
}
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
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()) {
	my $image = Image->Lookup($osid);
	push(@images, $image)
	    if (defined($image));
    }
}
else {
    my $image = Image->Lookup($ARGV[0]);
    if (!defined($image)) {
	fatal("No such image");
    }
    @images = ($image);
}

if ($clear) {
    foreach my $image (@images) {
	my $osinfo = OSinfo->Lookup($image->imageid());
	if (!defined($osinfo)) {
	    fatal("Could not find osinfo for $image");
	}
	my $imageid = $image->imageid();
	my $osid    = $osinfo->osid();
	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.
#
154
my $parent_image = Image->Lookup($parent);
155
if (!defined($parent_image)) {
156 157 158 159
    $parent_image = Image->LookupByName($parent);
    if (!defined($parent_image)) {
	fatal("No such parent $parent");
    }
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 206 207 208 209 210 211 212
}
my $parent_osinfo = OSinfo->Lookup($parent_image->imageid());
if (!defined($parent_osinfo)) {
    fatal("No osinfo for $parent");
}
    
my @other_parents = ();
foreach my $imagename (@OTHER_PARENTS) {
    my $osinfo = OSinfo->LookupByName($imagename);
    push(@other_parents, $osinfo)
	if (defined($osinfo));
}

#
# Loop through all images. 
#
foreach my $image (@images) {
    my $osinfo = OSinfo->Lookup($image->imageid());
    if (!defined($osinfo)) {
	fatal("Could not find osinfo for $image");
    }
    if ($impotent) {
	print "Would set $image to run on $parent_osinfo (default)\n";
    }
    else {
	$osinfo->SetParentOS($parent_osinfo);
	$osinfo->SetRunsOnParent($parent_osinfo);
    }
    # 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 {
	    $osinfo->SetRunsOnParent($other_parent);
	}
    }
    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);
}