#!/bin/bash
#
# Written by Xueshan Feng <sfeng@stanford.edu>
#
# How to use this script:
#
# 1. Copy puppet repo to desitnation machine, e.g. /tmp/repo.tar.gz
# 2. cd /tmp/ && tar zxvf repo.tar.gz
# 3. puppet-dry-run -d /tmp/repo

# To copy repository:
#
# 1. Change directory to your clone, e.g., cd /srv/puppet-repo
#
# 2. Make a tar file 
#
#      git archive --format=tar -o /tmp/repo.tar master
##############################################
# Default values; can override on command line
##############################################
action="--noop"
debug=''
repo=''
me=`hostname --short`
environment='default'
if [ -f /etc/puppet/puppet.conf ]; then
    environment=$(grep '^environment' /etc/puppet/puppet.conf|sed -e 's/\s\+//g' | awk -F'=' '{print $2}')
fi

# Must be root
if [ x`whoami` != "xroot" ]; then
    echo "$0 must be run as user root"
    exit 1
fi

# Get command line options
while getopts "e:r:da" OPTION
do
    case $OPTION in
        r)
          repo=$OPTARG
          ;;
        e)
          environment=$OPTARG
          ;;
        d)
          debug='--debug'
          ;;
        a)
          # Confirm if you really want to apply the change
          answer='N'
          echo -n "Do you really want to apply the change on $me [Y/N]?"
          read answer
          echo ""
          if [[ "X$answer" = "XY" ]]; then
              echo "Will apply the change on $me."
              action=''
          else
              echo "Fine. I will do nothing."
              exit 0
          fi
          ;;
        *) perldoc -t $0; exit 0;;
    esac
done

if [[ ! -d $repo ]]; then
    echo "repo $repo does not exist. You need to setting up your repo sandbox."
    echo "Usage puppet-dry-run -r <repodir> [-e <puppet env>] [-a ] [-d] [-h]"
    exit 1
else
    # Change owner to root so puppet doesn't try to use your id to
    # check file mode and owner
    chown -R root:root $repo
fi

mkdir -p /tmp/$USER/stat

# puppet command runs in noop mode, and reports running environment, its
# configuration files and report changes

echo "############## Running Puppet ######"
echo "/usr/bin/puppet apply $debug --environment=$environment --use-nodes $action --statedir /tmp/$USER/stat --modulepath $repo/modules:$repo/services:$repo/clients --templatedir $repo/templates $repo/manifests/site.pp"

/usr/bin/puppet apply $debug --environment=$environment --use-nodes $action \
  --statedir /tmp/$USER/stat \
  --modulepath $repo/modules:$repo/services:$repo/clients \
  --templatedir $repo/templates $repo/manifests/site.pp

exit 0

DOCS=<<__END_OF_DOCS__

=head1 NAME

puppet-dry-run - Run puppet apply with a local repository copy

=head1 SYNOPSIS

B<puppet-dry-run> [B<-adh>] B<-r> I<repodir> [B<-e> I<puppet-env>]

=head1 DESCRIPTION

This script runs the puppet apply command in no-op mode (B<--noop>) to test
Puppet configurations.  It must be run as root against a local copy of the
Puppet repository specified with B<-r>.q

=head1 COMMANDS

=over 4

=item B<-a>

Apply the changes on the host rather than only showing what would be done.
Optional.  Default is no-op mode.

=item B<-d>

Run in debug mode.  Optional.  Default is no debug.

=item B<-e>

Puppet environment.  Optional.  By default, the environment configured in
F</etc/puppet/puppet.conf> will be used.  If that file does not exist,
C<default> will be used.

=item B<-h>

Print this help.

=item B<-r> I<repodir>

Puppet repository directory.  Required.

=back

=head1 AUTHOR

Xueshan Feng <sfeng@stanford.edu>

=cut
