~ubuntu-branches/ubuntu/hardy/slack/hardy-proposed

« back to all changes in this revision

Viewing changes to src/slack-runscript

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Pollock
  • Date: 2007-10-27 16:14:42 UTC
  • Revision ID: james.westby@ubuntu.com-20071027161442-z3wjuy3juutuxu7m
Tags: upstream-0.14.1
ImportĀ upstreamĀ versionĀ 0.14.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
# $Id: slack-runscript,v 1.12 2006/09/25 18:35:17 alan Exp $
 
3
# vim:sw=2
 
4
# vim600:fdm=marker
 
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.
 
8
#
 
9
# This script is in charge of running scripts out of the local stage
 
10
 
 
11
require 5.006;
 
12
use warnings FATAL => qw(all);
 
13
use strict;
 
14
use sigtrap qw(die untrapped normal-signals
 
15
               stack-trace any error-signals);
 
16
 
 
17
use File::Path;
 
18
use File::Find;
 
19
 
 
20
use constant LIB_DIR => '/usr/lib/slack';
 
21
use lib LIB_DIR;
 
22
use Slack;
 
23
 
 
24
# Export these options to the environment of the script
 
25
my @export_options = qw(root stage hostname verbose);
 
26
 
 
27
(my $PROG = $0) =~ s#.*/##;
 
28
 
 
29
########################################
 
30
# Environment
 
31
# Helpful prefix to die messages
 
32
$SIG{__DIE__} = sub { die "FATAL[$PROG]: @_"; };
 
33
# Set a reasonable umask
 
34
umask 077;
 
35
# Autoflush on STDERR
 
36
select((select(STDERR), $|=1)[0]);
 
37
# Get out of wherever (possibly NFS-mounted) we were
 
38
chdir('/')
 
39
  or die "Could not chdir '/': $!";
 
40
 
 
41
########################################
 
42
# Config and option parsing {{{
 
43
my $usage = Slack::default_usage("$PROG [options] <action> <role> [<role>...]");
 
44
# Option defaults
 
45
my %opt = ();
 
46
Slack::get_options(
 
47
  opthash => \%opt,
 
48
  usage => $usage,
 
49
  required_options => \@export_options,
 
50
);
 
51
 
 
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;
 
55
 
 
56
# }}}
 
57
 
 
58
# Start with a clean environment
 
59
%ENV = (
 
60
  PATH => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
 
61
);
 
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};
 
68
}
 
69
# We want to decrement the verbose value for the child if it's set.
 
70
$ENV{VERBOSE}-- if $ENV{VERBOSE};
 
71
 
 
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) {
 
76
    if (-e _) {
 
77
      # A helpful warning
 
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";
 
81
    }
 
82
    next;
 
83
  }
 
84
  my $dir;
 
85
  if ($action eq 'fixfiles') {
 
86
    $dir = "$opt{stage}/roles/$role/files";
 
87
  } else {
 
88
    $dir = "$opt{stage}/roles/$role/scripts";
 
89
  }
 
90
  my @command = ($script_to_run, $role);
 
91
 
 
92
  # It's OK to chdir even if we're not going to run the script.
 
93
  # Might as well see if it works.
 
94
  chdir($dir)
 
95
    or die "Could not chdir '$dir': $!\n";
 
96
  if ($opt{'dry-run'}) {
 
97
    ($opt{verbose} > 0)
 
98
      and print STDERR "$PROG: Not calling '@command' in '$dir' ".
 
99
        "because --dry-run specified.\n";
 
100
  } else {
 
101
    ($opt{verbose} > 0)
 
102
      and print STDERR "$PROG: Calling '@command' in '$dir'.\n";
 
103
    unless (system(@command) == 0) {
 
104
      Slack::check_system_exit(@command);
 
105
    }
 
106
  }
 
107
  chdir('/')
 
108
    or die "Could not chdir '/': $!\n"
 
109
}
 
110
exit 0;
 
111