This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
deploy.sh
65 lines (50 loc) · 2.45 KB
/
deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env bash
# Set color variable
CYAN='\033[1;36m'
NC='\033[0m' # No Color
set -e
# Provisions a virtual machine instance
# Local variables
export APP="${PORTAL_APP_REPO_FOLDER}"
echo "export APP=${APP}"
export TF_VAR_deployment_path="${PORTAL_DEPLOYMENTS_ROOT}/${PORTAL_DEPLOYMENT_REFERENCE}"
echo "export TF_VAR_deployment_path=${TF_VAR_deployment_path}"
export DPL="${PORTAL_DEPLOYMENTS_ROOT}/${PORTAL_DEPLOYMENT_REFERENCE}/"
echo "export DPL=${DPL}"
export PRIV_KEY_PATH="${DPL}${PORTAL_DEPLOYMENT_REFERENCE}"
echo "export PRIV_KEY_PATH=${PRIV_KEY_PATH}"
# Export input variables in the bash environment
export TF_VAR_name="$(awk -v var="${PORTAL_DEPLOYMENT_REFERENCE}" 'BEGIN {print tolower(var)}')"
echo "export TF_VAR_name=${TF_VAR_name}"
export KEY_PATH="${DPL}${PORTAL_DEPLOYMENT_REFERENCE}.pub"
echo "export KEY_PATH=${KEY_PATH}"
export TF_VAR_key_path="${KEY_PATH}"
echo "export TF_VAR_key_path=${TF_VAR_key_path}"
export TF_STATE=${DPL}'terraform.tfstate'
echo "export TF_STATE=${TF_STATE}"
# Launch provisioning of the VM
echo -e "\n\t${CYAN}Terraform apply${NC}\n"
terraform -chdir=${APP}'/ostack/terraform' init
terraform -chdir=${APP}'/ostack/terraform' apply --state=${DPL}'terraform.tfstate' -auto-approve
# Extract the external IP of the instance
external_ip=$(terraform output -state=${DPL}'terraform.tfstate' external_ip)
# Install Ansible requirements with ansible galaxy
echo -e "\n\t${CYAN}Install Ansible requirements with ansible galaxy${NC}\n"
cd ostack/ansible || exit
ansible-galaxy install -r requirements.yml
# Used by terraform.py - env var holds directory containing terraform.tfstate file
export TERRAFORM_STATE_ROOT=${DPL}
echo "export TERRAFORM_STATE_ROOT=${DPL}"
# Used by ansible-playbook - local var holds host name (e.g. tsi1629995083720-1)
terraform_default_host="${TF_VAR_name}-1"
echo "Script var terraform_default_host=\"${TF_VAR_name}-1\""
# Set default value for Ansible variables if they are either empty or undefined
export TF_VAR_remote_folder="${TF_VAR_remote_folder:-/var/nfs}"
echo "export TF_VAR_remote_folder=${TF_VAR_remote_folder}"
export TF_VAR_local_folder="${TF_VAR_local_folder:-/nfs-imports}"
echo "export TF_VAR_local_folder=${TF_VAR_local_folder}"
export ANSIBLE_REMOTE_USER="${TF_VAR_remote_user:-ubuntu}"
echo "export ANSIBLE_REMOTE_USER=${ANSIBLE_REMOTE_USER}"
# Launch Ansible playbook
echo -e "\n\t${CYAN}Launch Ansible playbook${NC}\n"
ansible-playbook -b playbook.yml --extra-vars "ecp_host=${terraform_default_host}"