-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform.sh
executable file
·135 lines (116 loc) · 3.33 KB
/
terraform.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
PATH=$(pwd):$PATH
TERRAFORM=$(terraform -v | head -n 1)
VERSION="Terraform v1.1.8"
DOWNLOAD="https://releases.hashicorp.com/terraform/1.1.8/terraform_1.1.8_linux_amd64.zip"
PACKAGE="terraform_1.1.8_linux_amd64.zip"
ENVIRONMENT=$1
TF_PARAM=$2
ENV_REGEX="^(qa$|dev$|prod$)"
TF_REGEX="^(init$|plan$|apply$|import$)"
# latest terraform 15.X:
# DOWNLOAD="https://releases.hashicorp.com/terraform/0.15.5/terraform_0.15.5_linux_amd64.zip"
# latest terraform 14.X:
# DOWNLOAD="https://releases.hashicorp.com/terraform/0.14.11/terraform_0.14.11_linux_amd64.zip"
# latest terraform 13.X:
# DOWNLOAD="https://releases.hashicorp.com/terraform/0.13.7/terraform_0.13.7_linux_amd64.zip"
# latest terraform 12.X:
# DOWNLOAD="https://releases.hashicorp.com/terraform/0.12.31/terraform_0.12.31_linux_amd64.zip"
# latest terraform 11.X:
# DOWNLOAD="https://releases.hashicorp.com/terraform/0.11.15/terraform_0.11.15_linux_amd64.zip"
yell() {
echo "$0: $*" >&2;
}
die() {
yell "$*"; exit 111;
}
try() {
"$@" || die "cannot $*";
}
function erroarg() {
echo "$0: $*" >&2;
echo "Não há parametros dev|prod e init|plan|apply|import"
echo "execute -h para mais informações"
exit 1
}
function erroarg1() {
echo "$0: $*" >&2;
echo "o parametro \"$ENVIRONMENT\" está errado"
echo "execute -h para mais informações"
exit 1
}
function erroarg2() {
echo "$0: $*" >&2;
echo "o parametro \"$TF_PARAM\" para o terraform não é suportado por este programa"
echo "execute -h para mais informações"
exit 1
}
function helpinfo() {
echo "$0: $*" >&2;
echo -e "\a Terraform Script \n
\v informe como parametro o ambiente a ser executado: \"dev\", \"qa\" ou \"prod\", e informe o parametro do terraform: \"init\", \"plan\", \"apply\". Exemplo:\n
# terraform dev init
\v Caso o parametro for \"import\", informe em sequencia o resource e o ARN/ID/Name do recurso na AWS. Exemplo:\n
# terraform dev import "resource" "ARN"
"
exit 1
}
function install() {
wget -c $DOWNLOAD && \
unzip -o $PACKAGE && \
rm $PACKAGE && \
chmod +x terraform
PATH=$(pwd):$PATH
TERRAFORM=$(terraform -v | head -n 1)
}
if ! command -v terraform &> /dev/null
then
install
fi
if [ "$TERRAFORM" == "$VERSION" ];
then
echo "$VERSION"
else
echo "$TERRAFORM"
install
fi
if [ -z "$1" ];
then
erroarg
elif
[ $1 == "--help" -o $1 == "-h" -o $1 == "-help" ]; then
helpinfo
die ;
else
echo "iniciando..."
fi
if [[ $ENVIRONMENT =~ $ENV_REGEX ]];
then
ENVM=$ENVIRONMENT
else
erroarg1
exit 1
fi
if [[ $TF_PARAM =~ $TF_REGEX ]];
then
TFARG=$TF_PARAM
else
erroarg2
exit 1
fi
if [ $TFARG == "init" ];
then
rm -rf .terraform/
terraform $TFARG --backend-config=$ENVM.backend.tfvars --var-file=$ENVM.tfvars
elif [ $TFARG == "plan" ];
then
terraform $TFARG --var-file=$ENVM.tfvars -out=tf.plan -input=false
elif [ $TFARG == "apply" ];
then
terraform $TFARG --var-file=$ENVM.tfvars -input=false tf.plan -auto-approve
elif [ $TFARG == "import" ];
then
terraform $TFARG --var-file=$ENVM.tfvars $3 $4
else
try
fi