Skip to content

Instantly share code, notes, and snippets.

@themouette
Created October 23, 2013 15:48
Show Gist options
  • Save themouette/7121212 to your computer and use it in GitHub Desktop.
Save themouette/7121212 to your computer and use it in GitHub Desktop.
Install and test scripts for puppet module.

Install

$ puppet module install my-module

or

$ git clone path-to-repo.git && ./bin/bootstrap.sh

Testing

Tests for this module are written using rspec-puppet.

Launch tests as follow

$ ./bin/test.sh

It installs dependencies locally and launches the tests scripts.

#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
function usage {
echo "$0 [--module-path /path/to/modules]"
echo ""
echo " Options:"
echo " -h, --help this help"
echo " -m, --modulepath <modulepath> specify path to module directory"
echo ""
}
# default options
MODULEPATH=""
function parse_options {
SHORT_OPT="m:h"
LONG_OPT="modulepath:,help"
# parse arguments
OPTS=$( getopt -o "$SHORT_OPT" -l "$LONG_OPT" -- "$@" )
if [ $? != 0 ]; then
usage;
exit 1;
fi
eval set -- "$OPTS"
# read options
while true ; do
case "$1" in
-m|--modulepath) MODULEPATH="$2"
shift 2;;
-h|--help) usage
exit 0;;
--) shift
break;;
esac
done
}
function log {
echo -e " \033[32m*\033[0m ${1}"
}
function error {
echo -e "\033[31m${1}\033[0m"
}
function check_deps {
if [ -z "$(which bundle)" ] ;
then
if [ -z "$(which gem)" ] ;
then
error "Ruby and gem are required (install with ruby executable in your path)"
exit 1;
fi
log "Install bundler."
sudo gem install bundler
fi
}
function install_gem {
log "Install gem files";
bundle install
}
function install_modules {
log "Install puppet modules";
# default puppet module install arguments
ARGUMENTS=""
# compute the default module location
LOCALPATH=$(puppet config print modulepath | cut -d":" -f1)
if [ -n "$MODULEPATH" ]
then
# append the modulepath to arguments
ARGUMENTS="$ARGUMENTS --modulepath $MODULEPATH"
# replace module location with provided one
LOCALPATH="$MODULEPATH"
fi
# parse Modulefile to install dependencies localy or widely
#
# 1. extract dependencies from Modulefile.
# 2. split dependencies around ' and / to extract:
# * $2 module namespace
# * $3 module name
# * $5 version (eventualy)
# 3. generate a "`if` module is not available" wrapped `puppet module
# install` command
# 4. execute each line through `bash -c`. `xargs -t` means echo command first.
grep ^dependency Modulefile \
| sed -e "s:\(\"\|'\)\(>\|<\)\?=\?\s*:\1:g" \
| awk -v args="$ARGUMENTS" -v modules="$LOCALPATH" -F "[/']" \
'{ if ($5 != "") {$5 = " --version \\\""$5"\\\""}; \
{ print \
"if [ ! -d \\\""modules"/"$3"\\\" ] ; then \
puppet module install "args" "$2"/"$3$5"; \
fi;\
"}}' \
| xargs -I'{}' -t bash -c '{}'
}
function install {
check_deps
install_gem
install_modules
}
parse_options "$@"
install
echo "Done."
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
$DIR/bin/bootstrap.sh --modulepath $DIR/spec/fixtures/modules
bundle exec rake spec SPEC_OPTS='--color --format documentation'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment