-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·182 lines (149 loc) · 4.16 KB
/
run.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
#!/usr/bin/env bash
####################
# Couleur en Shell #
####################
RESTORE='\033[0m'
export RESTORE
RED='\033[00;31m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
PURPLE='\033[00;35m'
CYAN='\033[00;36m'
LIGHTGRAY='\033[00;37m'
export RED
export GREEN
export YELLOW
export BLUE
export PURPLE
export CYAN
export LIGHTGRAY
LRED='\033[01;31m'
LGREEN='\033[01;32m'
LYELLOW='\033[01;33m'
LBLUE='\033[01;34m'
LPURPLE='\033[01;35m'
LCYAN='\033[01;36m'
WHITE='\033[01;37m'
export LRED
export LGREEN
export LYELLOW
export LBLUE
export LPURPLE
export LCYAN
export WHITE
####################
# Fonctions de Log #
####################
# Fonction de log info
function logInfo {
DATE=`date +"%d-%m-%y %T"`
echo -e " [ ${LGREEN}INFO${RESTORE} : ${DATE} ] $1"
}
# Fonction de log warning
function logWarn {
DATE=`date +"%d-%m-%y %T"`
echo -e " [ ${LYELLOW}WARN${RESTORE} : ${DATE} ] $1"
}
# Fonctions de log erreur
function logError {
DATE=`date +"%d-%m-%y %T"`
echo -e " [ ${LRED}ERROR${RESTORE} : ${DATE} ] $1"
}
# Affiche une chaine et termine le script
function logErrorAndExit {
logError "${1}"
exit ${2}
}
# Affiche une chaine et termine le script avec 0
function logInfoAndExit {
logInfo "${1}"
exit 0
}
# Fonctions #
#############
function checkBinaries {
# Contrôle présence des binaires obligatoire
logInfo "Contrôle présence binaire obligatoire ..."
docker version > /dev/null || logErrorAndExit "Docker doit être installé" 1
logInfo " * docker : [${LGREEN}OK${RESTORE}]"
docker-compose version > /dev/null || logErrorAndExit "Docker compose doit être installé" 2
logInfo " * docker-compose : [${LGREEN}OK${RESTORE}]"
terraform version > /dev/null || logErrorAndExit "Terraform doit être installé" 3
logInfo " * terraform : [${LGREEN}OK${RESTORE}]"
logInfo "Version des binaires ..."
logInfo " * Docker ..."
docker version
logInfo " * Docker compose ..."
docker-compose version
logInfo " * Terraform ..."
TF_REQUIRED="v1.0.3"
TF_VERSION=$(terraform --version | head -1 | cut -d ' ' -f2)
terraform version
if [[ "$(printf '%s\n' "${TF_REQUIRED}" "${TF_VERSION}" | sort -V | head -n1)" != "${TF_REQUIRED}" ]] ; then
logWarn " /!\ Version minimal de Terraform requise : ${TF_REQUIRED}"
which terraform-select-version && terraform-select-version
fi
}
function printUsage {
logErrorAndExit " Usage: ./run.sh start|stop|destroy" 99
}
# Corps du programme #
######################
checkBinaries
# Definition de la socket SSH a utilisé en fonction de l'implémentation Docker
DOCKER_IMPL=$(docker info -f json | jq -r '.OperatingSystem')
logInfo "Implémentation docker : ${DOCKER_IMPL}"
if [ "${DOCKER_IMPL}" == "Docker Desktop" ] ; then
export SSH_AUTH_SOCK_DEFINED="/run/host-services/ssh-auth.sock"
else
export SSH_AUTH_SOCK_DEFINED=${SSH_AUTH_SOCK}
fi
logInfo "Socket SSH :"
logInfo " * host : ${SSH_AUTH_SOCK}"
logInfo " * docker : ${SSH_AUTH_SOCK_DEFINED}"
if [ "$1" == "start" ] ; then
which sglk-run && sglk-run stop --force
which sglk-dev-stack && sglk-dev-stack stop
# Démarrage infra docker
f="-f docker-compose.yml"
if [ "$2" == "dev" ] ; then
logInfo "Versions local de reader et superviseur"
f+=" -f docker-compose.local.yml"
fi
eval "docker-compose ${f} pull"
eval "docker-compose ${f} up --detach"
# Provision de l'infra terraform
cd infrastructure
# Initialisation des plugins (provider) terraform
terraform init
while ! terraform apply -auto-approve ; do
echo ""
logError "Failed to initiate infrastructure. Wait 5 seconds"
echo ""
sleep 5
done
cd ..
# Lancement de l'IHM du superviseur
if [ "$(uname)" == "Darwin" ]; then
open http://superviseur.arig.org > /dev/null
else
xdg-open http://superviseur.arig.org > /dev/null
fi
elif [ "$1" == "stop" ] ; then
# Arret infra docker
docker-compose stop
elif [ "$1" == "destroy" ] ; then
# Destruction infra
logInfo "Destruction terraform ..."
cd infrastructure
terraform destroy -auto-approve
rm -Rvf .terraform
rm -vf *.tfstate*
cd ..
# Arret infra docker
logInfo "Destruction docker ..."
docker-compose down --remove-orphans --volumes
else
printUsage
fi