/usr/share/perl5/Xen/Tools/Common.pm is in xen-tools 4.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 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 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 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 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 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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | # -*- perl -*
package Xen::Tools::Common;
=encoding utf8
=head1 NAME
Xen::Tools::Common - Common funtions used in xen-tools' Perl scripts
=head1 SYNOPSIS
use Xen::Tools::Common;
=cut
use warnings;
use strict;
use Exporter 'import';
use vars qw(@EXPORT_OK @EXPORT);
use English;
use File::Which;
@EXPORT = qw(readConfigurationFile xenRunning runCommand setupAdminUsers
findXenToolstack
logprint_with_config logonly_with_config fail_with_config);
=head1 FUNCTIONS
=head2 readConfigurationFile
=begin doc
Read the specified configuration file, and update our global configuration
hash with the values found in it.
=end doc
=cut
sub readConfigurationFile ($$)
{
my ($file, $CONFIG) = (@_);
# Don't read the file if it doesn't exist.
return if ( !-e $file );
my $line = "";
open( FILE, "<", $file ) or
fail_with_config("Cannot read file '$file' - $!", $CONFIG);
while ( defined( $line = <FILE> ) )
{
chomp $line;
if ( $line =~ s/\\$// )
{
$line .= <FILE>;
redo unless eof(FILE);
}
# Skip lines beginning with comments
next if ( $line =~ /^([ \t]*)\#/ );
# Skip blank lines
next if ( length($line) < 1 );
# Strip trailing comments.
if ( $line =~ /(.*)\#(.*)/ )
{
$line = $1;
}
# Find variable settings
if ( $line =~ /([^=]+)=([^\n]+)/ )
{
my $key = $1;
my $val = $2;
# Strip leading and trailing whitespace.
$key =~ s/^\s+//;
$key =~ s/\s+$//;
$val =~ s/^\s+//;
$val =~ s/\s+$//;
# command expansion?
if ( $val =~ /(.*)`([^`]+)`(.*)/ )
{
# store
my $pre = $1;
my $cmd = $2;
my $post = $3;
# get output
my $output = `$cmd`;
chomp($output);
# build up replacement.
$val = $pre . $output . $post;
}
# Store value.
$CONFIG->{ $key } = $val;
}
}
close(FILE);
}
=head2 xenRunning
=begin doc
Test to see if the given instance is running.
=end doc
=cut
sub xenRunning ($$)
{
my ($hostname, $CONFIG) = (@_);
my $running = 0;
die "Couldn't determine Xen toolstack" unless $CONFIG->{'xm'};
open( CMD, $CONFIG->{'xm'}." list $hostname 2>/dev/null |" ) or
fail_with_config("Failed to run '".$CONFIG->{'xm'}." list $hostname'", $CONFIG);
while (<CMD>)
{
my $line = $_;
$running = 1 if ( $line =~ /\Q$hostname\E/ );
}
close(CMD);
return ($running);
}
=head2 findXenToolstack
=begin doc
Find the right Xen toolstack. On Debian and derivatives there's a
script which tells you about the current toolstack.
=end doc
=cut
sub findXenToolstack
{
my $helper = '/usr/lib/xen-common/bin/xen-toolstack';
if (-x $helper) {
my $toolstack = `$helper`;
chomp($toolstack);
return $toolstack if $toolstack;
}
my $xm = which('xm');
if ($xm and system("$xm list >/dev/null 2>/dev/null") == 0) {
return $xm;
}
my $xl = which('xl');
if ($xl and system("$xl list >/dev/null 2>/dev/null") == 0) {
return $xl;
}
return undef;
}
=head2 runCommand
=begin doc
A utility method to run a system command. We will capture the return
value and exit if the command files.
When running verbosely we will also display any command output once
it has finished.
=end doc
=cut
sub runCommand ($$;$)
{
my ($cmd, $CONFIG, $fail_ok) = (@_);
#
# Set a local if we don't have one.
#
$ENV{ 'LC_ALL' } = "C" unless ( $ENV{ 'LC_ALL' } );
#
# Header.
#
if ($CONFIG->{ 'verbose' }) {
logprint_with_config("Executing : $cmd\n", $CONFIG);
}
#
# Copy stderr to stdout, so we can see it, and make sure we log it.
#
$cmd .= " 2>&1";
#
# Run it.
#
my $rcopen = open(CMD, '-|', $cmd);
if (!defined($rcopen)) {
logprint_with_config("Starting command '$cmd' failed: $!\n", $CONFIG);
unless ($fail_ok) {
logprint_with_config("Aborting\n", $CONFIG);
print "See /var/log/xen-tools/".$CONFIG->{'hostname'}.".log for details\n";
$CONFIG->{'FAIL'} = 1;
exit 127;
}
}
while (my $line = <CMD>) {
if ($CONFIG->{ 'verbose' }) {
logprint_with_config($line, $CONFIG);
} else {
logonly_with_config($line, $CONFIG);
}
}
my $rcclose = close(CMD);
if ($CONFIG->{ 'verbose' }) {
logprint_with_config("Finished : $cmd\n", $CONFIG);
}
if (!$rcclose)
{
logprint_with_config("Running command '$cmd' failed with exit code $?.\n", $CONFIG);
logprint_with_config("Aborting\n", $CONFIG);
print "See /var/log/xen-tools/".$CONFIG->{'hostname'}.".log for details\n";
unless ($fail_ok) {
$CONFIG->{'FAIL'} = 1;
exit 127;
}
}
}
=head2 setupAdminUsers (xen-shell helper)
=begin doc
This routine is designed to ensure that any users specified with
the --admins flag are setup as administrators of the new instance.
=end doc
=cut
sub setupAdminUsers ($)
{
my $CONFIG = (@_);
#
# If we're not root we can't modify users.
#
return if ( $EFFECTIVE_USER_ID != 0 );
#
# If we don't have a sudoers file then we'll also ignore this.
#
return if ( !-e "/etc/sudoers" );
#
# Find the path to the xen-login-shell
#
my $shell = undef;
$shell = "/usr/bin/xen-login-shell" if ( -x "/usr/bin/xen-login-shell" );
$shell = "/usr/local/bin/xen-login-shell"
if ( -x "/usr/bin/local/xen-login-shell" );
return if ( !defined($shell) );
#
# For each user make sure they exist, and setup the
# login shell for them.
#
foreach my $user ( split( /,/, $ENV{ 'admins' } ) )
{
# Strip leading and trailing whitespace.
$user =~ s/^\s+//;
$user =~ s/\s+$//;
# Ignore root
next if ( $user =~ /^root$/i );
# Does the user exist?
if ( getpwnam($user) )
{
# Change shell.
if ($CONFIG->{ 'verbose' }) {
logprint_with_config("Changing shell for $user: $shell\n", $CONFIG);
}
system( "chsh", "-s", $shell, $user );
}
else
{
# Add a new user.
if ($CONFIG->{ 'verbose' }) {
logprint_with_config("Adding new user: $user\n", $CONFIG);
}
system( "useradd", "-s", $shell, $user );
}
#
# Add the entry to /etc/sudoers.
#
open( SUDOERS, ">>", "/etc/sudoers" ) or
warn "Failed to add user to sudoers file : $user - $!";
print SUDOERS
"$user ALL = NOPASSWD: /usr/sbin/xm, /usr/sbin/xl, /usr/bin/xen-create-image\n";
close(SUDOERS);
}
}
=head2 fail_with_config
=begin doc
Properly set $CONFIG{FAIL} on die
=end doc
=cut
sub fail_with_config ($$)
{
my ($text, $CONFIG) = (@_);
logprint_with_config($text, $CONFIG);
$CONFIG->{'FAIL'} = 1;
exit 127;
}
=head2 logonly_with_config
=begin doc
Print the given string to the logfile.
=end doc
=cut
sub logonly_with_config ($$)
{
my ($text, $CONFIG) = (@_);
if ( $CONFIG->{ 'hostname' } )
{
open( LOGFILE, '>>', '/var/log/xen-tools/'.$CONFIG->{'hostname'}.'.log' ) or
return;
print LOGFILE $text;
close(LOGFILE);
}
}
=head2 logprint_with_config
=begin doc
Print the given string both to our screen, and to the logfile.
=end doc
=cut
sub logprint_with_config ($$)
{
my ($text, $CONFIG) = (@_);
print $text;
logonly_with_config($text, $CONFIG);
}
=head1 AUTHORS
Steve Kemp, http://www.steve.org.uk/
Axel Beckert, http://noone.org/abe/
Dmitry Nedospasov, http://nedos.net/
Stéphane Jourdois
Merged from several scripts by Axel Beckert.
=cut
return 1;
|