runsonxen.in 4.4 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2010-2018 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 28 29 30
# 
# {{{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;

#
# Mark an image as running on XEN. 
#
31
my @OTHER_PARENTS  = ("XEN44-64-STD", "XEN46-64-STD", "XEN49-64-STD");
32 33 34 35 36 37 38 39 40 41

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";
42
    print STDERR " -p      - Set default parent\n";
43 44 45 46 47 48
    exit(1);
}
my $optlist    = "acp:n";
my $doall      = 0;
my $clear      = 0;
my $impotent   = 0;
49
my $parent;
50 51 52 53 54 55 56 57 58 59 60 61

# Protos
sub fatal($);

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

#
# Load the Testbed support stuff.
#
62
use lib "@prefix@/lib";
63
use emdb;
64
use libEmulab;
65
use OSImage;
66

67 68 69
# Default parent for this site is a sitevar.
my $DEFAULT_PARENT = GetSiteVar("general/default_xen_parentosid");

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
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);
}
87 88 89
else {
    $parent = $DEFAULT_PARENT;
}
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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()) {
108
	my $image = OSImage->Lookup($osid);
109 110 111 112 113
	push(@images, $image)
	    if (defined($image));
    }
}
else {
114
    my $image = OSImage->Lookup($ARGV[0]);
115 116 117 118 119 120 121 122 123
    if (!defined($image)) {
	fatal("No such image");
    }
    @images = ($image);
}

if ($clear) {
    foreach my $image (@images) {
	my $imageid = $image->imageid();
124
	my $osid    = $image->osid();
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	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.
#
141
my $parent_image = OSImage->Lookup($parent);
142
if (!defined($parent_image)) {
143
    $parent_image = OSImage->LookupByName($parent);
144 145 146
    if (!defined($parent_image)) {
	fatal("No such parent $parent");
    }
147 148 149
}
my @other_parents = ();
foreach my $imagename (@OTHER_PARENTS) {
150 151 152
    my $osimage = OSImage->LookupByName($imagename);
    push(@other_parents, $osimage)
	if (defined($osimage));
153 154 155 156 157 158 159
}

#
# Loop through all images. 
#
foreach my $image (@images) {
    if ($impotent) {
160
	print "Would set $image to run on $parent_image (default)\n";
161 162
    }
    else {
163 164
	$image->SetParentOS($parent_image);
	$image->SetRunsOnParent($parent_image);
165 166 167 168 169 170 171
    }
    # 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 {
172
	    $image->SetRunsOnParent($other_parent);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	}
    }
    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);
}