bunkerized-nginx/tests/create.sh

51 lines
1.0 KiB
Bash
Raw Permalink Normal View History

2023-03-03 14:47:18 +01:00
#!/bin/bash
# drop and export secrets
echo "${CICD_SECRETS}" > /opt/.env
2023-08-31 08:17:56 +02:00
echo "export TF_VAR_k8s_ip=${K8S_IP}" >> /opt/.env
2023-03-03 14:47:18 +01:00
chmod +x /opt/.env
2023-10-02 12:05:15 +02:00
# shellcheck disable=SC1091
2023-03-03 14:47:18 +01:00
. /opt/.env
# create terraform env
mkdir "/tmp/$1"
cp ./tests/terraform/providers.tf "/tmp/$1"
cp -r ./tests/terraform/templates "/tmp/$1"
cp "./tests/terraform/${1}.tf" "/tmp/$1"
old_dir="$(pwd)"
2023-10-02 12:05:15 +02:00
cd "/tmp/$1" || exit 1
2023-03-03 14:47:18 +01:00
# terraform init
terraform init
2023-10-02 12:05:15 +02:00
# shellcheck disable=SC2181
2023-03-03 14:47:18 +01:00
if [ $? -ne 0 ] ; then
echo "terraform init failed"
exit 1
fi
# terraform apply
terraform apply -auto-approve
2023-10-02 12:05:15 +02:00
# shellcheck disable=SC2181
2023-03-03 14:47:18 +01:00
if [ $? -ne 0 ] ; then
echo "terraform apply failed"
terraform destroy -auto-approve
exit 2
fi
# run ansible playbook
if [ -f "/tmp/${1}_inventory" ] ; then
2023-10-02 12:05:15 +02:00
cd "${old_dir}/tests/ansible" || exit 1
2023-03-03 14:47:18 +01:00
export ANSIBLE_HOST_KEY_CHECKING=False
ansible-playbook -i "/tmp/${1}_inventory" "${1}_playbook"
2023-10-02 12:05:15 +02:00
# shellcheck disable=SC2181
2023-03-03 14:47:18 +01:00
if [ $? -ne 0 ] ; then
echo "ansible-playbook failed"
2023-10-02 12:05:15 +02:00
cd "/tmp/$1" || exit 1
2023-03-03 14:47:18 +01:00
terraform destroy -auto-approve
exit 3
fi
fi
# done
exit 0