-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap-vpc.sh
executable file
·194 lines (181 loc) · 4.87 KB
/
bootstrap-vpc.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
usage () {
echo "bootstrap-vpc.sh [create | delete] <args>
args:
-n name to use for all resources and cluster default: rancher-lab
-p provider (aws only so far) default: aws
-r region to run the resources default: us-east-1
-c VPC CIDR block /16 to use (ex: 172.31) default: 10.99
-a optional | i'm feeling lucky (yes to everything) default: prompt me
example:
Create everything, but prompt during the functions:
bootstrap-vpc.sh create -n rancher-vpc -p aws -r us-east-1 -c 10.20
Assume all defaults, but set a name:
bootstrap-vpc.sh -n testing123"
}
testcredentials () {
case ${_provider} in
aws)
if aws --version > /dev/null 2>&1
then
_iam=`aws sts get-caller-identity | jq -r '.Arn'`
if [ $? -eq 0 ]
then
echo "[Info] ${_scope} | Using the following IAM entity: ${_iam}"
else
echo "[Error] ${_scope} | We'll need the AWS CLI configured with credentials, please configure and run me again"
exit 1
fi
fi
;;
*)
echo " ${_scope} | Sorry, on the to do list"
exit 1
;;
esac
}
testprereqs () {
for _command in aws terraform curl
do
if ! ${_command} --version > /dev/null 2>&1
then
echo "[Error] ${_scope} | Can't find the ${_command}, please check or install it"
exit 1
fi
done
}
terraformvars () {
if [[ -n ${_name} && -n ${_region} ]]
then
cat <<- EOF > .${_region}.${_name}.vpc.terraform.tfvars
name = "${_name}"
region = "${_region}"
vpc-cidr = "${_cidr}"
EOF
fi
}
terraformapply () {
if [ ! -d .terraform ]
then
if ! terraform init terraform/${_provider}/rancher-vpc > /dev/null 2>&1
then
echo "[Error] ${_scope} | Something went wrong with initialising terraform"
exit 1
fi
fi
terraform apply -var-file=./.${_region}.${_name}.vpc.terraform.tfvars -state=./terraform.vpc.${_region}.tfstate ${_imfeelinglucky} terraform/${_provider}/rancher-vpc
if [ $? -ne 0 ]
then
echo "[Error] ${_scope} | Something went wrong with applying terraform"
exit 1
fi
}
terraformdestroy () {
if [ -z ${_imfeelinglucky} ]
then
echo "[Info] ${_scope} | Careful there, are you sure you want to destory everything?"
echo -n " y/n: "
read _reply
if [[ ! ${_reply} =~ ^[Yy]$ ]]
then
echo -e "\n Ok, lets revist this later..."
exit 1
fi
fi
terraform destroy -var-file=./.${_region}.${_name}.vpc.terraform.tfvars -state=./terraform.vpc.${_region}.tfstate ${_imfeelinglucky} terraform/${_provider}/rancher-vpc
if [ $? -ne 0 ]
then
echo "[Error] ${_scope} | Something went wrong with terraform"
exit 1
fi
rm .${_region}.${_name}.vpc.terraform.tfvars
rm terraform.vpc.${_region}.tfstate*
}
case "$1" in
create)
_create=1
shift 1
;;
delete)
_delete=1
shift 1
;;
esac
while getopts "han:r:p:c:" opt
do
case ${opt} in
a)
_imfeelinglucky="-auto-approve"
;;
n)
_opt_name=${OPTARG}
;;
r)
_opt_region=${OPTARG}
;;
p)
_opt_provider=${OPTARG}
;;
c)
_opt_cidr=${OPTARG}
;;
h)
usage
exit 1
;;
*)
usage
exit 1
;;
esac
done
# Defaults
_region=${_opt_region:-us-east-1}
_name=${_opt_name:-rancher-lab}
_cidr=${_opt_cidr:-10.99}
_provider=${_opt_provider:-aws}
_scope=VPC
if ! [[ -n "${_create}" || -n "${_delete}" ]]
then
echo "[Error] ${_scope} | Sorry, I need a create or delete operation, check out the -h usage."
exit 1
fi
if [ -n "${_create}" ]
then
testcredentials
testprereqs
terraformvars
if [ -z ${_imfeelinglucky} ]
then
echo "[Info] ${_scope} | We're ready to terraform apply, are we good to go?"
echo -n " y/n: "
read _reply
if [[ ! ${_reply} =~ ^[Yy]$ ]]
then
echo -e "\nOk, lets revist this later..."
exit 1
else
terraformapply
fi
else
terraformapply
fi
fi
if [ -n "${_delete}" ]
then
if [ -z ${_imfeelinglucky} ]
then
echo "[Info] ${_scope} | We're ready to terraform destroy, are we good to go?"
echo -n " y/n: "
read _reply
if [[ ! ${_reply} =~ ^[Yy]$ ]]
then
echo -e "\nOk, lets revist this later..."
exit 1
else
terraformdestroy
fi
else
terraformdestroy
fi
fi