Skip to content

Commit

Permalink
Adds --pre-script option (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliendufresne committed Jul 10, 2016
1 parent 244babc commit 150b42f
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 42 deletions.
5 changes: 5 additions & 0 deletions Vagrantfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ Vagrant.configure(2) do |config|
vb.customize ["modifyvm", :id, "--ioapic", "on"]
end

config.vm.provision "pre_script", type: "shell" do |pre_script|
pre_script.privileged = true
pre_script.path = "${pre_script}"
end

config.vm.provision "ansible", type: "ansible_local" do |ansible|
ansible.galaxy_role_file = "requirements.yml"
ansible.install = true
Expand Down
31 changes: 15 additions & 16 deletions config/default.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
| Ansible role | Vagrant box | enable-vbguest |
| ----------------------- | ------------------- | -------------- |
| juliendufresne.influxdb | bento/debian-7.9 | no |
| juliendufresne.influxdb | bento/debian-7.10 | no |
| juliendufresne.influxdb | bento/debian-8.1 | no |
| juliendufresne.influxdb | bento/debian-8.2 | no |
| juliendufresne.influxdb | bento/debian-8.3 | no |
| juliendufresne.influxdb | bento/debian-8.4 | no |
| juliendufresne.influxdb | bento/ubuntu-12.04 | no |
| juliendufresne.influxdb | bento/ubuntu-14.04 | no |
| juliendufresne.influxdb | bento/ubuntu-15.04 | no |
| juliendufresne.influxdb | bento/ubuntu-15.10 | no |
| juliendufresne.influxdb | bento/ubuntu-16.04 | no |
| juliendufresne.influxdb | bento/centos-6.7 | yes |
| juliendufresne.influxdb | geerlingguy/centos6 | yes |
| juliendufresne.influxdb | geerlingguy/centos7 | no |
| Ansible role | Vagrant box | enable-vbguest | pre-script |
| ----------------------- | ------------------- | -------------- | -------------------------------- |
| juliendufresne.influxdb | geerlingguy/centos6 | yes | provision/install_ansible.sh |
| juliendufresne.influxdb | geerlingguy/centos7 | no | |
| juliendufresne.influxdb | bento/debian-7.9 | no | |
| juliendufresne.influxdb | bento/debian-7.10 | no | |
| juliendufresne.influxdb | bento/debian-8.1 | no | |
| juliendufresne.influxdb | bento/debian-8.2 | no | |
| juliendufresne.influxdb | bento/debian-8.3 | no | |
| juliendufresne.influxdb | bento/debian-8.4 | no | |
| juliendufresne.influxdb | bento/ubuntu-12.04 | no | |
| juliendufresne.influxdb | bento/ubuntu-14.04 | no | |
| juliendufresne.influxdb | bento/ubuntu-15.04 | no | |
| juliendufresne.influxdb | bento/ubuntu-15.10 | no | |
| juliendufresne.influxdb | bento/ubuntu-16.04 | no | |
3 changes: 3 additions & 0 deletions provision/empty.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo "empty provision used"
8 changes: 8 additions & 0 deletions provision/install_ansible.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

if which yum &>/dev/null
then
yum install -y epel-release python-crypto
yum reinstall -y python-crypto
yum install -y ansible
fi
49 changes: 45 additions & 4 deletions src/_option_resolver.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/usr/bin/env bash

CONFIG_FILE=
PRE_SCRIPT=
VAGRANT_BOXES=

resolve_config_file() {
local current_option_value="$1"
local previously_defined_value="$2"
local repository_directory="$3"
local repository_directory="${REPOSITORY_DIRECTORY}"

if [ -z "${current_option_value}" ]
then
Expand All @@ -12,7 +15,7 @@ resolve_config_file() {
exit 1
fi

if [ -n "${previously_defined_value}" ]
if [ -n "${CONFIG_FILE}" ]
then
error "You can not specify multiple configuration files"
usage
Expand All @@ -39,5 +42,43 @@ resolve_config_file() {
exit 1
fi

echo "${current_option_value}"
CONFIG_FILE="${current_option_value}"
}

resolve_pre_script() {
local value="$1"

if [ -n "${PRE_SCRIPT}" ]
then
error "You can not specify multiple values for option --pre-script"
usage
exit 1
fi

if [ -z "$value" ]
then
error "Argument is required for the --pre-script option"
usage
exit 1
fi

PRE_SCRIPT="$value"
}

resolve_vagrant_box() {
local value="$1"

if [ -z "${value}" ]
then
error "Argument is required for the --vagrant-box option"
usage
exit 1
fi

if [ -z "${VAGRANT_BOXES}" ]
then
VAGRANT_BOXES="${value}"
else
VAGRANT_BOXES="${VAGRANT_BOXES} ${value}"
fi
}
12 changes: 8 additions & 4 deletions src/_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ clean_previous_vagrant_box() {
create_vagrantfile() {
local vagrant_box="$1"
local is_vbguest_enabled="$2"
local pre_script="$3"
local line=

[ -z "${pre_script}" ] && pre_script="provision/empty.sh"

while read -r line
do
while [[ "$line" =~ (\$\{[a-zA-Z_][a-zA-Z_0-9]*\}) ]]
Expand Down Expand Up @@ -59,8 +62,9 @@ run() {
local ansible_role="$1"
local vagrant_box="$2"
local repository_directory="$3"
local is_verbose=$4
local is_vbguest_enabled="$5"
local pre_script="$4"
local is_verbose=$5
local is_vbguest_enabled="$6"
local testing_directory="$(mktemp -d)"

printf "# Testing ansible role \033[1;34m%s\033[0m in vagrant box \033[1;34m%s\033[0m\n" "${ansible_role}" "${vagrant_box}"
Expand All @@ -69,7 +73,7 @@ run() {

# Ensure we are in the repository root
cd "${repository_directory}"
cp -r inventory playbooks requirements.yml Vagrantfile.template "${testing_directory}"
cp -r inventory playbooks provision requirements.yml Vagrantfile.template "${testing_directory}"

# Checks ansible roles requirements
if [ ! -f "playbooks/${ansible_role}.yml" ]
Expand All @@ -85,7 +89,7 @@ run() {

cd "${testing_directory}"

create_vagrantfile "${vagrant_box}" "${is_vbguest_enabled}"
create_vagrantfile "${vagrant_box}" "${is_vbguest_enabled}" "${pre_script}"
local report_file=$(ensure_report_file_exists "${repository_directory}" "${ansible_role}")

boot_vagrant_box ${is_verbose}
Expand Down
3 changes: 2 additions & 1 deletion src/_usage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ usage() {
- this project config directory
To ease usage, you can omit file directory and extension.
e.g: '--config-file default' may be resolved with <repository_root>/config/default.md
--enable-vbguest enable the vagrant-vbguest plugin and install it if needed.
-h, --help show this help.
--pre-script PRE_SCRIPT path to a script to run before running ansible playbook.
-v, --verbose increase verbosity.
--enable-vbguest enable the vagrant-vbguest plugin and install it if needed.
--vagrant-box VAGRANT_BOX use specified vagrant box instead of default one. This option may be specified multiple times.
"
}
33 changes: 16 additions & 17 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ VAGRANT_BOXES=
ANSIBLE_ROLES=
CONFIG_FILE=
ENABLE_VBGUEST="false"
PRE_SCRIPT=
while [[ $# -ge 1 ]]
do
case "$1" in
Expand All @@ -29,30 +30,27 @@ do
fi
ANSIBLE_ROLES="${ANSIBLE_ROLES} $2"
shift
;;
;;
--config-file)
CONFIG_FILE="$(resolve_config_file "$2" "${CONFIG_FILE}" "${REPOSITORY_DIRECTORY}")"
[ $? -ne 0 ] && { printf "$CONFIG_FILE"; exit 1; }
resolve_config_file "$2"
shift
;;
;;
--enable-vbguest)
ENABLE_VBGUEST="true"
;;
;;
-h|--help)
usage
exit 0
;;
--pre-script)
resolve_pre_script "$2"
shift
;;
-v|--verbose)
IS_VERBOSE=true
;;
--vagrant-box)
if [ -z "$2" ]
then
error "Argument is required for the $1 option"
usage
exit 1
fi
VAGRANT_BOXES="${VAGRANT_BOXES} $2"
resolve_vagrant_box "$2"
shift
;;
*)
Expand All @@ -75,19 +73,20 @@ then
do
for VAGRANT_BOX in ${VAGRANT_BOXES}
do
run "${ANSIBLE_ROLE}" "${VAGRANT_BOX}" "${REPOSITORY_DIRECTORY}" ${IS_VERBOSE} ${ENABLE_VBGUEST}
run "${ANSIBLE_ROLE}" "${VAGRANT_BOX}" "${REPOSITORY_DIRECTORY}" "${PRE_SCRIPT}" ${IS_VERBOSE} ${ENABLE_VBGUEST}
done
done
exit 0;
exit 0
fi

tail -n+3 "${CONFIG_FILE}" | while IFS='' read -r line || [[ -n "$line" ]]
do
FOUND=true
# Remove trailing pipe (because we can use them or not)
line=$(echo "${line}" | sed 's/^\s*|//' | sed 's/|\s*$//')
CURRENT_ANSIBLE_ROLE=$(echo "${line}" | cut -d '|' -f 1 | sed 's/^\s*//' | sed 's/\s*$//')
CURRENT_VAGRANT_BOX=$(echo "${line}" | cut -d '|' -f 2 | sed 's/^\s*//' | sed 's/\s*$//')
CURRENT_ANSIBLE_ROLE="$(echo "${line}" | cut -d '|' -f 1 | sed 's/^\s*//' | sed 's/\s*$//')"
CURRENT_VAGRANT_BOX="$(echo "${line}" | cut -d '|' -f 2 | sed 's/^\s*//' | sed 's/\s*$//')"
PRE_SCRIPT="$(echo "${line}" | cut -d '|' -f 4 | sed 's/^\s*//' | sed 's/\s*$//')"
ENABLE_VBGUEST="false"
if [ "yes" == "$(echo "${line}" | cut -d '|' -f 3 | sed 's/^\s*//' | sed 's/\s*$//')" ]
then
Expand Down Expand Up @@ -122,5 +121,5 @@ do

${FOUND} || continue

run "${CURRENT_ANSIBLE_ROLE}" "${CURRENT_VAGRANT_BOX}" "${REPOSITORY_DIRECTORY}" ${IS_VERBOSE} ${ENABLE_VBGUEST}
run "${CURRENT_ANSIBLE_ROLE}" "${CURRENT_VAGRANT_BOX}" "${REPOSITORY_DIRECTORY}" "${PRE_SCRIPT}" ${IS_VERBOSE} ${ENABLE_VBGUEST}
done

0 comments on commit 150b42f

Please sign in to comment.