#!/bin/bash
if [ $# -lt 2 ]; then
echo 'Expects 2 arguments: ./populate.sh APPLICATION K8S_ENVIRONMENT'
exit 1
fi
extra_vars="--extra-vars @group_vars/secrets.yml"
application=$1
k8s_environment=$2
vault_password_file="${VAULT_PASSWORD_FILE:-./my-vault-stuff/vault-password}"
if [ ! -f "${application}.yml" ]; then
echo "Could not find expected playbook ${application}.yml"
exit 1
else
extra_vars="${extra_vars} --extra-vars application=${application}"
fi
# Make sure the k8s_environment is set
if [ "${k8s_environment}" = 'dev' ] || [ "${k8s_environment}" = 'prod' ] || \
[ "${k8s_environment}" = 'staging' ] || [ "${k8s_environment}" = 'test' ]; then
extra_vars="${extra_vars} --extra-vars k8s_environment=$k8s_environment"
else
echo 'You must specify $k8s_environment as dev|prod|staging|test'
exit 1
fi
if [ -f "group_vars/${application}/${k8s_environment}.yml" ]; then
extra_vars="${extra_vars} --extra-vars @group_vars/${application}/${k8s_environment}.yml"
else
echo "Did not find file: group_vars/${application}/${k8s_environment}.yml"
fi
if [ -f "group_vars/${k8s_environment}.yml" ]; then
extra_vars="${extra_vars} --extra-vars @group_vars/${k8s_environment}.yml"
else
echo "Did not find file: group_vars/${k8s_environment}.yml"
fi
# add extra secrets variables if the files exist
if [ -f "group_vars/${application}/secrets.yml" ]; then
extra_vars="${extra_vars} --extra-vars @group_vars/${application}/secrets.yml"
fi
if [ -f "group_vars/${application}/secrets/${k8s_environment}.yml" ]; then
extra_vars="${extra_vars} --extra-vars @group_vars/${application}/secrets/${k8s_environment}.yml"
fi
echo "Populating ${application} into environment ${K8S_ENVIRONMENT}"
ansible-playbook "${application}.yml" $extra_vars --vault-password-file=$vault_password_file