forked from shippableSamples/sample_pipelines_terraform
/
provInfra.sh
87 lines (79 loc) · 2.46 KB
/
provInfra.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#! /bin/bash
# Install tools
echo -e "\n*** installing Ubuntu tools ***"
sudo apt-get update && apt-get install wget jq unzip
echo -e "\n*** installing Terraform ***"
install_terraform() {
local FILE_NAME=terraform_0.11.7_linux_amd64.zip
wget https://releases.hashicorp.com/terraform/0.11.7/$FILE_NAME
pwd
unzip $FILE_NAME -d $(pwd)
export PATH=$PATH:/build
terraform -v
}
install_terraform
# Extract previous state
echo -e "\n*** extracting previous state for this job ***"
get_previous_statefile() {
local previous_statefile_location="/build/previousState/terraform.tfstate"
if [ -f "$previous_statefile_location" ]; then
cp $previous_statefile_location /build/IN/repo_tfScripts/gitRepo
echo 'restored previous statefile'
else
echo "no previous statefile exists"
fi
}
get_previous_statefile
# Extract integration data
echo -e "\n*** extracting AWS integration information ***"
# Load the integration values into environment variables for aws_access_key_id
# and aws_secret_access_key
get_aws_integration() {
local INTEGRATION_FILE="./IN/integration_aws/integration.env"
if [ -f "$INTEGRATION_FILE" ]; then
. $INTEGRATION_FILE
echo $REGION
echo "loaded integration file"
else
echo "no integration file exists"
fi
}
get_aws_integration
# Extract params data
echo -e "\n*** extracting params information ***"
get_params() {
local PARAMS_FILE="./IN/params_tfScripts/version.json"
if [ -f "$PARAMS_FILE" ]; then
PARAMS_VALUES=$(jq -r '.version.propertyBag.params' $PARAMS_FILE)
PARAMS_LENGTH=$(echo $PARAMS_VALUES | jq '. | length')
PARAMS_KEYS=$(echo $PARAMS_VALUES | jq '. | keys')
for (( i=0; i<$PARAMS_LENGTH; i++ )) do
PARAM_KEY=$(echo $PARAMS_KEYS | jq -r .[$i])
export $PARAM_KEY=$(echo $PARAMS_VALUES | jq -r .[\"$PARAM_KEY\"])
done
echo "loaded params file"
else
echo "no params file exists"
fi
}
get_params
# Provision infrastructure via scripts
echo -e "\n*** provisioning infrastructure on AWS ***"
provision_infra() {
cd /build/IN/repo_tfScripts/gitRepo
export AWS_ACCESS_KEY_ID=$accessKey
export AWS_SECRET_ACCESS_KEY=$secretKey
export AWS_DEFAULT_REGION=$REGION
terraform init
terraform apply -input=false -auto-approve
}
provision_infra
# Save state
echo -e "\n*** saving state ***"
createOutState() {
STATEFILE_LOCATION=/build/state/
cp terraform.tfstate $STATEFILE_LOCATION
}
createOutState
# Processing complete
echo -e "\n*** processing complete - ${BASH_SOURCE[0]} ***"