-
Notifications
You must be signed in to change notification settings - Fork 5
/
deploy
89 lines (72 loc) · 2.86 KB
/
deploy
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
#!/bin/bash
source <(otc-task-include lib/filesystem.sh)
source <(otc-task-include lib/helm.sh)
source <(otc-task-include lib/assert.sh)
source <(otc-task-include lib/console.sh)
# Param helm_dir - Helm chart directory
# Param namespace
# Param custom_name (OPTIONAL)
# Param custom_values (OPTIONAL) - custom helm values file (path relative to helm_dir)
# Look for ENVIRONMENT_NAME environment var
function main
{
local helm_dir="$1"
local namespace="$2"
local custom_name="$3" #optional
local custom_values="$4" #optional
if ! [ -z "$custom_values" ]
then
echo "Using '$custom_values' as helm values." >&2
fi
assert-not-empty helm_dir
assert-not-empty namespace
local release_name_file=$(mktemp -t "release_name-XXXXXXXX")
local helm_deploy_status_code=0 # assume success
helm-deploy "$helm_dir" "$namespace" "$custom_name" "$custom_values" \
> $release_name_file || helm_deploy_status_code=$?
if [ "$helm_deploy_status_code" -eq "$HELM_DEPLOY_CUSTOM_VALUES_FILE_NOT_FOUND" ]
then
echo "----------------------------------------------------------------------------------------------------" >&2
echo "O arquivo com os valores para o ambiente '$ENVIRONMENT_NAME' nao foi encontrado, logo nao " >&2
echo "sera possivel realizar o deploy neste ambiente." >&2
echo "" >&2
echo "Para corrigir este problema, adicione o arquivo '$custom_values' no diretorio do helm chart," >&2
echo "normalmente Kubernetes.Helm." >&2
echo "" >&2
echo "IMPORTANTE: Dados sensiveis, como strings de conexao, credenciais para servicos externos, " >&2
echo " etc, nao devem ser inseridos neste arquivo. Para isso, deverao ser utilizados " >&2
echo " secrets (em caso de dúvidas, conversar com arquitetura/infra-estrutura)." >&2
echo "----------------------------------------------------------------------------------------------------" >&2
exit 103
fi
local release_name=$(cat $release_name_file)
if [ -z "$release_name" ]
then
red "Could not get release name." >&2
echo $HELM_GENERAL_DEPLOY_ERROR_MESSAGE >&2
exit 109
fi
rm $release_name_file
if [ "$helm_deploy_status_code" -ne "0" ]
then
local history_count=$(($(helm history $release_name | wc -l) - 1))
if ! echo $history_count | egrep '^[0-9]+$' > /dev/null 2>&1
then
red "Could not get history for '$release_name'. At this point, release '$release_name' " >&2
red "should exists, neither it successfully deployed or not." >&2
echo $HELM_GENERAL_DEPLOY_ERROR_MESSAGE >&2
exit 117
fi
if [ "$history_count" -gt "0" ]
then
red "As helm deploy failed, rolling back to previous revision." >&2
assert-success helm rollback $release_name 0 >&2
else
red "Seems that is the first '$release_name' release." >&2
red "As helm deploy has failed, going to deleting it." >&2
assert-success helm delete $release_name --purge >&2
fi
fi
exit $helm_deploy_status_code
}
main "$@"