-
Notifications
You must be signed in to change notification settings - Fork 0
/
ami-deploy-manager.sh
executable file
·340 lines (301 loc) · 10.9 KB
/
ami-deploy-manager.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/bin/bash
#### Main configurations
export IMAGE_NAME=""
export CONTROL_TAG=""
export VERSION_TAG=""
export AUTO_SCALING_GROUP_NAME=""
export AUTO_SCALING_GROUP_REGION="sa-east-1"
export AWS_HA_RELEASE="../aws-missing-tools/aws-ha-release/aws-ha-release.sh"
## Script constants
export RED='\033[1;31m'
export ORANGE='\033[0;33m'
export YELLOW='\033[1;33m'
export GREEN='\033[1;32m'
export CYAN='\033[0;36m'
export NC='\033[0m'
## Helper functions
function printHeader {
clear
echo "######################################################################"
echo "## ${1}"
echo "######################################################################"
}
function printSeparator {
echo "######################################################################"
}
##### MAIN MENU ####
function mainMenuScreen {
while :
do
printHeader "${IMAGE_NAME} AMI & Deploy Manager"
echo -e "Escolha uma opcão:"
echo -e " ${YELLOW}1)${NC} Criar nova versão de imagem AMI à partir dessa máquina"
echo -e " ${YELLOW}2)${NC} Listar versoes AMI"
echo -e " ${YELLOW}3)${NC} Deletar Versão AMI"
echo -e " ${YELLOW}4)${NC} Modificar versão AMI do Auto Scaling Group"
echo -e " ${YELLOW}5)${NC} Forçar atualização das instancias do Auto Scaling Group"
echo -e " ${YELLOW}0)${NC} Sair"
printSeparator
read -p "Opcão: " -r -n1
echo
case "$REPLY" in
1)
newAmiScreen
;;
2)
listAmiScreen
;;
3)
deleteAmiScreen
;;
4)
updateLaunchConfigurationScreen
;;
5)
awsHaReleaseScreen
;;
0)
printSeparator
echo -e "${CYAN}Até logo!${NC}"
printSeparator
exit 0
;;
*)
echo -e " ${RED}Ei! ${YELLOW} '${REPLY}' ${RED} não é opcão válida! Vamos tentar denovo?${NC}"
sleep 2
mainMenuScreen
esac
done
}
### New AMI screen
function newAmiScreen {
clear
printHeader "Criar nova versão AMI - ${IMAGE_NAME}"
read -p "Digite o nome da versão: " -r
if [ -z "$REPLY" ]
then
echo -e "${RED}Você não digitou a versão!"
echo -e "${CYAN}Na duvida? Dê uma olhada na lista de versões disponíveis. "
echo -e "Voltamos ao menu principal logo após os intervalos comerciais...${NC}"
printSeparator
sleep 5
return 1
fi
versionName="$REPLY"
echo -n "Detectando o instance-id AWS... "
INSTANCE_ID=`curl http://169.254.169.254/latest/meta-data/instance-id -s` 2> /dev/null
if [ -z "$INSTANCE_ID" ]; then
echo -e "${RED} [FALHOU]${NC}"
printSeparator
echo -e "${RED}Ooooops. Não foi possível detectar o instance-id aws."
echo -e "Essa é uma instância ec2?${NC}"
echo -e "Em instantes voltamos com a nossa programacão normal...${NC}"
printSeparator
sleep 3
return 2
fi
echo -e "${GREEN} [OK]${NC}"
echo -e "Será criada uma AMI com a versão ${YELLOW}'$versionName'.${NC}"
read -p "Confirma? [s/n] " -r
printSeparator
if [[ $REPLY =~ ^[Ss]$ ]]
then
echo -n "Enviando solicitacão de criacão da AMI... "
jsonFile=/tmp/createAmiVersion`date +%s`.json
aws ec2 create-image --instance-id "$INSTANCE_ID" --name "$IMAGE_NAME - $versionName" --no-reboot > $jsonFile
if [ "$?" -ne 0 ] ; then
echo -e "${RED} [FALHOU]${NC}"
echo "Erro ao criar ami!! Abortando."
printSeparator
exit 3
fi
echo -e "${GREEN} [OK]${NC}"
# Tags
amiId=`cat $jsonFile | jq -r .ImageId`
echo -n "Enviando solicitacão de criacão das TAGS... "
aws ec2 create-tags --resources $amiId --tags Key=${CONTROL_TAG},Value=true Key=${VERSION_TAG},Value=$versionName
if [ "$?" -ne 0 ] ; then
echo -e "${RED} [FALHOU]${NC}"
echo "Erro ao criar tags!! Abortando."
printSeparator
exit 4
fi
echo -e "${GREEN} [OK]${NC}"
echo -e "Criacão da AMI deu rock'n roll! Pressione ${CYAN}Enter${NC} para voltar."
printSeparator
read
fi
}
### Delete AMI screen
function listAmiScreen {
clear
printHeader "LISTAGEM DE IMAGENS AMI"
listAmiFrame
printSeparator
echo -e "Pressione ${CYAN}Enter${NC} para voltar."
read
}
### Delete AMI screen
function deleteAmiScreen {
clear
printHeader "DELECÃO DE IMAGEM AMI"
listAmiFrame
printSeparator
echo -e "Digite o ID da imagem a ser ${RED}DELETADA${NC} ou enter para volar:"
read -p "AMI ID: "
printSeparator
if [ -z "$REPLY" ] ; then
return
fi
## Queries AMI information and get EBS volume ID
echo -n "Consultando dados da Imagem..."
snapshotId=`aws ec2 describe-images --image-ids "$REPLY" | jq -r '.[] | .[] | .BlockDeviceMappings | .[] | .Ebs | .SnapshotId'`
if [ "$?" -ne 0 ] ; then
echo -e "${RED} [FALHOU]${NC}"
echo "Erro ao consultar dados da AMI. Abortando."
printSeparator
exit 4
fi
echo -e "${GREEN} [OK]${NC}"
## Deregister AMI
echo -n "Enviando request para desregistro da AMI: ${REPLY})..."
aws ec2 deregister-image --image-id "$REPLY"
if [ "$?" -ne 0 ] ; then
echo -e "${RED} [FALHOU]${NC}"
echo "Erro ao tentar desregistrar AMI ${REPLY}. Abortando."
printSeparator
exit 4
fi
echo -e "${GREEN} [OK]${NC}"
## Delete EBS snapshot
echo -n "Enviando request de delecão do Snapshot EBS (ID: ${snapshotId})..."
aws ec2 delete-snapshot --snapshot-id ${snapshotId}
if [ "$?" -ne 0 ] ; then
echo -e "${RED} [FALHOU]${NC}"
echo "Erro ao tentar remover Snapshot EBS ${snapshotId}. Abortando."
printSeparator
exit 4
fi
echo -e "${GREEN} [OK]${NC}"
echo -e "AMI Deletada! Pressione ${CYAN}Enter${NC} para voltar."
read
}
### Delete AMI screen
function updateLaunchConfigurationScreen {
clear
printHeader "ALTERAÇÃO DA VERSÃO AMI NO AUTO SCALING GROUP"
## Queries Auto Scaling group for current launch configuration name
echo -n "Consultando dados do auto scaling group '${AUTO_SCALING_GROUP_NAME}'"
currentLaunchConfigurationName=`aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name "${AUTO_SCALING_GROUP_NAME}" | jq -r '.AutoScalingGroups | .[] | .LaunchConfigurationName'`
checkError $?
## Queries for current launch configuration data
echo -n "Consultando dados do launch configuration '${currentLaunchConfigurationName}'"
currentLaunchConfigurationJson=`aws autoscaling describe-launch-configurations --launch-configuration-names "${currentLaunchConfigurationName}"`
checkError $?
currentAmi=`echo $currentLaunchConfigurationJson | jq -r '.LaunchConfigurations | .[0] | .ImageId'`
instanceType=`echo $currentLaunchConfigurationJson | jq -r '.LaunchConfigurations | .[0] | .InstanceType'`
securityGroup=`echo $currentLaunchConfigurationJson | jq -r '.LaunchConfigurations | .[0] | .SecurityGroups | .[0]'`
keyName=`echo $currentLaunchConfigurationJson | jq -r '.LaunchConfigurations | .[0] | .KeyName'`
listAmiFrame $currentAmi
printSeparator
echo -e "Digite o ID da imagem a ser Substituída no Auto Scaling Group ou enter para voltar:"
read -p "AMI ID: "
if [ -z "$REPLY" ] ; then
return
fi
newAmiId=$REPLY
if [ "$newAmiId" = "$currentAmi" ] ; then
echo -e "${RED}Eeeiii!!! ${YELLOW}Você digitou o id da AMI que já está ativo no Auto Scaling Group."
echo -e "${CYAN}Que tal lavar o rosto e pegar um café enquanto eu volto para o menu principal?${NC}"
sleep 8
return
fi
## Queries for AMI version
echo -n "Consultando dados da Imagem..."
newAmiVersion=`aws ec2 describe-images --image-ids "$newAmiId" | jq -r ".Images | .[0] | .Tags | .[] | if .Key == \"${VERSION_TAG}\"then .Value else \"\" end | select(length > 0)"`
newLaunchConfigurationName="${AUTO_SCALING_GROUP_NAME}-${newAmiVersion}"
checkError $?
printSeparator
echo "Novo launch config:"
echo -e "Nome: ${CYAN}${newLaunchConfigurationName}${NC}"
echo -e "Ami: ${CYAN}${newAmiId}${NC}"
echo -e "Versão: ${CYAN}${newAmiVersion}${NC}"
echo -e "Instance Type: ${CYAN}${instanceType}${NC}"
echo -e "Key: ${CYAN}${keyName}${NC}"
echo -e "Security Group: ${CYAN}${securityGroup}${NC}"
read -p "Confirma? [s/n] " -r
printSeparator
if [[ $REPLY =~ ^[Ss]$ ]] ; then
# Create new launch configuration
echo -n "Criando novo launch configuration..."
aws autoscaling create-launch-configuration --launch-configuration-name "${newLaunchConfigurationName}" --key-name ${keyName} --security-groups ${securityGroup} --instance-type ${instanceType} --image-id ${newAmiId}
checkError $?
echo -n "Substituindo launch configuration no Auto Scaling Group..."
aws autoscaling update-auto-scaling-group --auto-scaling-group-name "${AUTO_SCALING_GROUP_NAME}" --launch-configuration-name "${newLaunchConfigurationName}"
checkError $?
echo -n "Deletando antigo launch configuration (${currentLaunchConfigurationName})..."
aws autoscaling delete-launch-configuration --launch-configuration-name "${currentLaunchConfigurationName}"
checkError $?
printSeparator
read -p "Auto Scaling Group atualizado com a AMI selecionada. Deseja forcar o reload das instancias? [s/n] " -r
if [[ $REPLY =~ ^[Ss]$ ]] ; then
callAwsHaRelease
checkError $?
echo -e "Update de versão finalizado! Pressione ${CYAN}Enter${NC} para voltar."
read
fi
fi
}
## AwsHaReleaseScreen
function awsHaReleaseScreen {
clear
printHeader "FORCAR RELOAD DAS INSTANCIAS NO AUTO SCALING GROUP"
read -p "Tem certeza? [s/n] " -r
if [[ $REPLY =~ ^[Ss]$ ]] ; then
callAwsHaRelease
checkError $?
echo -e "Reload finalizado! Pressione ${CYAN}Enter${NC} para voltar."
read
fi
}
## List AMI 'frame'
function listAmiFrame {
echo -n "Carregando listagem..."
json=`aws ec2 describe-images --filters Name=tag:${CONTROL_TAG},Values=true`
checkError $?
printSeparator
amiIdList=`echo $json | jq -r '.[] | .[] | .ImageId' | sed 's/^/ /g'` # Last sed just adds two empty spaces to the beginning
versionList=`echo $json | jq -r ".[] | .[] | .Tags | .[] | if .Key == \"${VERSION_TAG}\"then .Value else \"\" end | select(length > 0)"`
if [ -z "$1" ] ; then
finalList=`paste <(echo "$amiIdList") <(echo "$versionList") | sed 's/\t/ | /'`
else
finalList=`paste <(echo "$amiIdList") <(echo "$versionList") | sed "s/ ${1}/* ${1}/" |sed 's/\t/ | /'`
fi
echo -e "${ORANGE} AMI ID | Versão ${NC}"
echo -e "${YELLOW}${finalList}${NC}"
if [ -n "$1" ] ; then
echo
echo -e "${CYAN}* indica a imagem atualmente configurada no Auto Scaling group ${NC}"
fi
}
function callAwsHaRelease
{
printSeparator
echo "Executando:"
echo $AWS_HA_RELEASE -a "${AUTO_SCALING_GROUP_NAME}" -r ${AUTO_SCALING_GROUP_REGION}
echo
$AWS_HA_RELEASE -a "${AUTO_SCALING_GROUP_NAME}" -r ${AUTO_SCALING_GROUP_REGION}
printSeparator
return $?
}
function checkError {
if [ "$1" -ne 0 ] ; then
echo -e "${RED} [FALHOU]${NC}"
echo "Erro na chamada do comando. Abortando."
printSeparator
exit 4
fi
echo -e "${GREEN} [OK]${NC}"
}
### Script excution begin
mainMenuScreen