1
0
mirror of synced 2026-01-13 15:17:30 +00:00

deduplicate runCommand()

Version taken from xen-create-image. Needed a small hack WRT $FAIL.
This commit is contained in:
Axel Beckert 2011-11-16 01:34:23 +01:00
parent 2de01d8cbc
commit 0f71f545df
4 changed files with 102 additions and 212 deletions

View File

@ -1101,7 +1101,7 @@ if ( $CONFIG{ 'boot' } )
logprint("Creating auto-start symlink to: $cfg\n");
my $link = "ln -s $cfg /etc/xen/auto/";
runCommand($link);
runCommand($link, \%CONFIG);
}
}
@ -2936,7 +2936,7 @@ sub createLoopbackImages
umask(0077);
# run the image creation command
runCommand($image_cmd);
runCommand($image_cmd, \%CONFIG);
logprint("Done\n");
# Reset the umask to the previous value
@ -3074,7 +3074,7 @@ sub createLVMBits
{
logprint(
"Removing $lvm_disk - since we're forcing the install\n");
runCommand("lvremove --force $lvm_disk");
runCommand("lvremove --force $lvm_disk", \%CONFIG);
}
else
{
@ -3106,7 +3106,7 @@ sub createLVMBits
#
# Create the volume
#
runCommand($disk_cmd);
runCommand($disk_cmd, \%CONFIG);
#
# Make sure that worked.
@ -3172,7 +3172,7 @@ sub createEVMSBits
logprint(
"Removing $evms_volume_disk - since we're forcing the install\n"
);
runCommand("echo Delete : $evms_volume_disk | evms");
runCommand("echo Delete : $evms_volume_disk | evms", \%CONFIG);
}
else
{
@ -3202,7 +3202,7 @@ sub createEVMSBits
logprint(
"Removing $evms_object_disk - since we're forcing the install\n"
);
runCommand("echo Delete : $evms_object_disk | evms");
runCommand("echo Delete : $evms_object_disk | evms", \%CONFIG);
}
else
{
@ -3262,8 +3262,8 @@ sub createEVMSBits
#
# Create the volumes
#
runCommand($disk_cmd_object);
runCommand($disk_cmd_volume);
runCommand($disk_cmd_object, \%CONFIG);
runCommand($disk_cmd_volume, \%CONFIG);
#
# Initialise the partition with the relevant filesystem.
@ -3333,7 +3333,7 @@ sub createFilesystem
$command .= " " . $image;
runCommand($command);
runCommand($command, \%CONFIG);
logprint("Done\n");
}
@ -3353,7 +3353,7 @@ sub createSwap
logprint("\nCreating swap on $path\n");
runCommand("mkswap $path");
runCommand("mkswap $path", \%CONFIG);
logprint("Done\n");
}
@ -3410,7 +3410,7 @@ sub mountImage
{
$mount_cmd = "mount $mount_type -o loop $image $mountpoint";
}
runCommand($mount_cmd);
runCommand($mount_cmd, \%CONFIG);
}
}
@ -3509,7 +3509,7 @@ sub installSystem
#
# Run the command.
#
runCommand($cmd);
runCommand($cmd, \%CONFIG);
logprint("Done\n");
}
@ -3577,7 +3577,7 @@ sub runCustomisationHooks
mkdir( $MOUNT_POINT . "/proc", 0755 ) if ( !-d $MOUNT_POINT . "/proc" );
# 2. Mount
runCommand("mount -o bind /proc $MOUNT_POINT/proc");
runCommand("mount -o bind /proc $MOUNT_POINT/proc", \%CONFIG);
#
# Before running any scripts we'll mount /dev/pts in the guest, too.
@ -3588,7 +3588,7 @@ sub runCustomisationHooks
if ( !-d $MOUNT_POINT . "/dev/pts" );
# 2. Mount
runCommand("mount -t devpts devpts $MOUNT_POINT/dev/pts");
runCommand("mount -t devpts devpts $MOUNT_POINT/dev/pts", \%CONFIG);
#
# Now update the environment for each defined IP address.
@ -3641,7 +3641,7 @@ sub runCustomisationHooks
$customize .= " --verbose";
}
logprint("\nRunning hooks\n");
runCommand($customize);
runCommand($customize, \%CONFIG);
logprint("Done\n");
#
@ -3837,7 +3837,7 @@ sub runRoleScript
#
# NOTE: Space added to $args as prefix ..
#
runCommand( $file . " " . $MOUNT_POINT . $args );
runCommand( $file . " " . $MOUNT_POINT . $args, \%CONFIG );
logprint("Role script completed.\n");
}
@ -3905,7 +3905,7 @@ sub runXenConfigCreation
}
logprint("\nCreating Xen configuration file\n");
runCommand($command);
runCommand($command, \%CONFIG);
logprint("Done\n");
}
@ -4097,74 +4097,6 @@ sub findBinary
=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) = (@_);
#
# Set a local if we don't have one.
#
$ENV{ 'LC_ALL' } = "C" unless ( $ENV{ 'LC_ALL' } );
#
# Header.
#
$CONFIG{ 'verbose' } && print "Executing : $cmd\n";
#
# 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("Starting command '$cmd' failed: $!\n");
logprint("Aborting\n");
print "See /var/log/xen-tools/$CONFIG{'hostname'}.log for details\n";
$FAIL = 1;
exit 127;
}
while (my $line = <CMD>) {
if ($CONFIG{ 'verbose' }) {
logprint $line;
} else {
logonly $line;
}
}
my $rcclose = close(CMD);
$CONFIG{ 'verbose' } && print "Finished : $cmd\n";
if (!$rcclose)
{
logprint("Running command '$cmd' failed with exit code $?.\n");
logprint("Aborting\n");
print "See /var/log/xen-tools/$CONFIG{'hostname'}.log for details\n";
$FAIL = 1;
exit 127;
}
}
=begin doc
Unmount any mount-points which are below the given path.
@ -4183,8 +4115,8 @@ sub unMountImage
#
# First we unmount /proc and /dev/pts in the guest install.
#
runCommand("umount $MOUNT_POINT/proc");
#runCommand("umount $MOUNT_POINT/dev/pts");
runCommand("umount $MOUNT_POINT/proc", \%CONFIG);
#runCommand("umount $MOUNT_POINT/dev/pts", \%CONFIG);
#
# Open /proc/mount and get a list of currently mounted paths
@ -4221,7 +4153,7 @@ sub unMountImage
foreach my $path (@points)
{
$CONFIG{ 'verbose' } && print "Unmounting : $path\n";
runCommand("umount $path");
runCommand("umount $path", \%CONFIG);
}
$MOUNT_POINT = undef;
@ -4282,7 +4214,7 @@ sub END
# $FAIL = 0 - Success
# $FAIL = 1 - Failed to install, delete the image
# $FAIL = 2 - Files exist, either .cfg or lvm... etc
if ( ($FAIL == 1) && ( !$CONFIG{ 'keep' } ) )
if ( ($FAIL == 1 or $CONFIG{'FAIL'} == 1) && ( !$CONFIG{ 'keep' } ) )
{
#

View File

@ -438,7 +438,7 @@ sub deleteXenImage
print "Would remove LVM swap volume /dev/$CONFIG{'lvm'}/$hostname-swap\n";
} else {
print "Removing swap volume\n";
runCommand("lvremove /dev/$CONFIG{'lvm'}/$hostname-swap --force");
runCommand("lvremove /dev/$CONFIG{'lvm'}/$hostname-swap --force", \%CONFIG);
}
}
@ -448,7 +448,7 @@ sub deleteXenImage
print "Would remove LVM disk volume /dev/$CONFIG{'lvm'}/$hostname-disk\n";
} else {
print "Removing LVM disk volume\n";
runCommand("lvremove /dev/$CONFIG{'lvm'}/$hostname-disk --force");
runCommand("lvremove /dev/$CONFIG{'lvm'}/$hostname-disk --force", \%CONFIG);
}
}
@ -470,8 +470,8 @@ sub deleteXenImage
print "Would remove EVMS swap volume: $CONFIG{'evms'}/$hostname-swap\n";
} else {
print "Removing EVMS swap volume\n";
runCommand("echo Delete : /dev/evms/$hostname-swap | evms");
runCommand("echo Delete : $CONFIG{'evms'}/$hostname-swap | evms");
runCommand("echo Delete : /dev/evms/$hostname-swap | evms", \%CONFIG);
runCommand("echo Delete : $CONFIG{'evms'}/$hostname-swap | evms", \%CONFIG);
}
}
@ -482,8 +482,8 @@ sub deleteXenImage
print "Would remove EVMS disk volume: $CONFIG{'evms'}/$hostname-swap\n";
} else {
print "Removing EVMS disk volume\n";
runCommand("echo Delete : /dev/evms/$hostname-disk | evms");
runCommand("echo Delete : $CONFIG{'evms'}/$hostname-disk | evms");
runCommand("echo Delete : /dev/evms/$hostname-disk | evms", \%CONFIG);
runCommand("echo Delete : $CONFIG{'evms'}/$hostname-disk | evms", \%CONFIG);
}
}
@ -496,66 +496,3 @@ sub deleteXenImage
exit 127;
}
}
=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.
=end doc
=cut
sub runCommand
{
my ($cmd) = (@_);
#
# Header.
#
$CONFIG{ 'verbose' } && print "Executing : $cmd\n";
#
# Hide output unless running with --debug.
#
if ( $CONFIG{ 'verbose' } )
{
#
# Copy stderr to stdout, so we can see it.
#
$cmd .= " 2>&1";
}
else
{
$cmd .= " >/dev/null 2>/dev/null";
}
#
# Run it.
#
my $output = `$cmd`;
if ( $? != 0 )
{
print "Running command '$cmd' failed.\n";
print "Aborting\n";
exit;
}
#
# All done.
#
$CONFIG{ 'verbose' } && print "Output\n";
$CONFIG{ 'verbose' } && print "======\n";
$CONFIG{ 'verbose' } && print $output . "\n";
$CONFIG{ 'verbose' } && print "Finished : $cmd\n";
return ($output);
}

View File

@ -471,53 +471,6 @@ EOT
=begin doc
A utility method to run a system command. We will capture the return
value and exit if the command fails.
When running verbosely we will also display any command output.
=end doc
=cut
sub runCommand
{
my ($cmd) = (@_);
#
# Command start.
#
$CONFIG{ 'verbose' } && print "Executing : $cmd\n";
#
# Copy stderr to stdout, so we can see it, and make sure we log it.
#
$cmd .= " 2>&1 | tee --append /var/log/xen-tools/$CONFIG{'hostname'}.log";
#
# Run it.
#
my $output = `$cmd`;
if ( $? != 0 )
{
print "Running command '$cmd' failed.\n";
print "Aborting\n";
exit 127;
}
#
# Command finished.
#
$CONFIG{ 'verbose' } && print "Finished : $cmd\n";
return ($output);
}
=begin doc
This function will copy all the .deb files from one directory
@ -603,7 +556,7 @@ sub do_copy
#
# Run the copy command.
#
runCommand($cmd);
runCommand($cmd, \%CONFIG);
}
@ -654,7 +607,7 @@ sub do_debootstrap
}
$cachedir = $xtcache;
}
runCommand("mkdir -p $CONFIG{'location'}/var/cache/apt/archives");
runCommand("mkdir -p $CONFIG{'location'}/var/cache/apt/archives", \%CONFIG);
copyDebFiles( "$cachedir",
"$CONFIG{'location'}/var/cache/apt/archives" );
print("Done\n");
@ -687,7 +640,7 @@ sub do_debootstrap
#
# Run the command.
#
runCommand($command);
runCommand($command, \%CONFIG);
#
@ -741,7 +694,7 @@ sub do_rinse
$command .= " --verbose";
}
runCommand($command);
runCommand($command, \%CONFIG);
}
@ -784,7 +737,7 @@ sub do_rpmstrap
# The command we're going to run.
#
my $command = "rpmstrap $EXTRA $CONFIG{'dist'} $CONFIG{'location'} $mirror";
runCommand($command);
runCommand($command, \%CONFIG);
}
@ -818,5 +771,5 @@ sub do_tar
#
# Run a command to copy an installed system into the new root.
#
runCommand("cd $CONFIG{'location'} && $cmd");
runCommand("cd $CONFIG{'location'} && $cmd", \%CONFIG);
}

View File

@ -18,7 +18,7 @@ use strict;
use Exporter 'import';
use vars qw(@EXPORT_OK @EXPORT);
@EXPORT = qw(readConfigurationFile xenRunning);
@EXPORT = qw(readConfigurationFile xenRunning runCommand);
=head1 FUNCTIONS
@ -131,6 +131,74 @@ sub xenRunning ($)
return ($running);
}
=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) = (@_);
#
# Set a local if we don't have one.
#
$ENV{ 'LC_ALL' } = "C" unless ( $ENV{ 'LC_ALL' } );
#
# Header.
#
$CONFIG->{ 'verbose' } && print "Executing : $cmd\n";
#
# 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("Starting command '$cmd' failed: $!\n");
logprint("Aborting\n");
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 $line;
} else {
logonly $line;
}
}
my $rcclose = close(CMD);
$CONFIG->{ 'verbose' } && print "Finished : $cmd\n";
if (!$rcclose)
{
logprint("Running command '$cmd' failed with exit code $?.\n");
logprint("Aborting\n");
print "See /var/log/xen-tools/".$CONFIG->{'hostname'}.".log for details\n";
$CONFIG->{'FAIL'} = 1;
exit 127;
}
}
=head1 AUTHORS
Steve Kemp, http://www.steve.org.uk/