2
# $Id: slack-runscript,v 1.12 2006/09/25 18:35:17 alan Exp $
5
# Copyright (C) 2004-2006 Alan Sundell <alan@sundell.net>
6
# All Rights Reserved. This program comes with ABSOLUTELY NO WARRANTY.
7
# See the file COPYING for details.
9
# This script is in charge of running scripts out of the local stage
12
use warnings FATAL => qw(all);
14
use sigtrap qw(die untrapped normal-signals
15
stack-trace any error-signals);
20
use constant LIB_DIR => '/usr/lib/slack';
24
# Export these options to the environment of the script
25
my @export_options = qw(root stage hostname verbose);
27
(my $PROG = $0) =~ s#.*/##;
29
########################################
31
# Helpful prefix to die messages
32
$SIG{__DIE__} = sub { die "FATAL[$PROG]: @_"; };
33
# Set a reasonable umask
36
select((select(STDERR), $|=1)[0]);
37
# Get out of wherever (possibly NFS-mounted) we were
39
or die "Could not chdir '/': $!";
41
########################################
42
# Config and option parsing {{{
43
my $usage = Slack::default_usage("$PROG [options] <action> <role> [<role>...]");
49
required_options => \@export_options,
52
my $action = shift || die "No script to run!\n\n$usage";
53
# Arguments are required
54
die "No roles given!\n\n$usage" unless @ARGV;
58
# Start with a clean environment
60
PATH => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
62
# Export certain variables to the environment. These are guaranteed to
63
# be set because we require them in get_options above.
64
for my $option (@export_options) {
65
my $env_var = $option;
66
$env_var =~ tr/a-z-/A-Z_/;
67
$ENV{$env_var} = $opt{$option};
69
# We want to decrement the verbose value for the child if it's set.
70
$ENV{VERBOSE}-- if $ENV{VERBOSE};
72
# Run the script for each role given, if it exists and is executable
73
for my $role (@ARGV) {
74
my $script_to_run = "$opt{stage}/roles/$role/scripts/$action";
75
unless (-x $script_to_run) {
78
warn "WARNING[$PROG]: Skipping '$script_to_run' because it's not executable\n";
79
} elsif ($opt{verbose} > 0) {
80
print STDERR "$PROG: Skipping '$script_to_run' because it doesn't exist\n";
85
if ($action eq 'fixfiles') {
86
$dir = "$opt{stage}/roles/$role/files";
88
$dir = "$opt{stage}/roles/$role/scripts";
90
my @command = ($script_to_run, $role);
92
# It's OK to chdir even if we're not going to run the script.
93
# Might as well see if it works.
95
or die "Could not chdir '$dir': $!\n";
96
if ($opt{'dry-run'}) {
98
and print STDERR "$PROG: Not calling '@command' in '$dir' ".
99
"because --dry-run specified.\n";
102
and print STDERR "$PROG: Calling '@command' in '$dir'.\n";
103
unless (system(@command) == 0) {
104
Slack::check_system_exit(@command);
108
or die "Could not chdir '/': $!\n"