-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·209 lines (186 loc) · 4.68 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
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
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
# Volumes: folders shared between hosts and docker containers
OS=$(uname -s)
if [[ "${OS}" =~ .*MINGW.* ]] || [[ "${OS}" =~ .*CYGWIN.* ]] || [[ "${OS}" =~ .*MSYS.* ]]; then
export MSYS_NO_PATHCONV=1
fi
export VOLUME_SLIDES="$PWD/Slides/:/data/Slides/"
export VOLUME_CAHIEREXERCICES="$PWD/CahierExercices:/data/CahierExercices"
export VOLUME_GRUNTFILE="$PWD/Gruntfile.js:/data/Gruntfile.js"
export VOLUME_PACKAGE="$PWD/package.json:/data/package.json"
export VOLUME_PDF_GENERATE="$PWD/PDF:/data/PDF"
# Docker image related informations
export DOCKER_IMAGE_NAME="zenika/formation-framework"
export DOCKER_IMAGE_VERSION="latest"
GREEN='\033[0;32m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
NC='\033[0m'
usage() {
echo "Usage: $(basename "$0") pdf|pdf-labs|dev|prod|clean" >&2
exit 1
}
checkInstall() {
commandExists docker || {
echo "docker must be installed" >&2
exit 1
}
}
commandExists() {
command -v "$1" 1>/dev/null 2>&1 \
&& return 0 || return 1
}
open-url() {
sleep 2
echo -e "${PURPLE}Ouverture de l'url $*${NC}"
if commandExists xdg-open; then
xdg-open "$@" &
elif commandExists open; then
open "$@"
fi
}
retrieveFormationVersion() {
VERSION=$(sed -n -e '/"zenika-formation-framework":/ s/.*"\^\([0-9]\+\).*".*/\1/p' package.json)
if [ ! -z "$VERSION" ]; then
export DOCKER_IMAGE_VERSION=v$VERSION
fi
}
getDockerAddress() {
case "${Z_DOCKER_PROVIDER:-}" in
machine | docker-machine)
getDockerAddressWhenMachine
;;
boot2docker | b2d)
getDockerAddressWhenB2D
;;
natif | native)
getDockerAddressWhenLocalhost
;;
*)
getDockerAddressWhenAuto
;;
esac
}
getDockerAddressWhenAuto() {
if commandExists docker-machine; then
if [ -z "${DOCKER_MACHINE_NAME:-}" ]; then
getDockerAddressWhenLocalhost
else
getDockerAddressWhenMachine
fi
elif commandExists boot2docker; then
getDockerAddressWhenB2D
else
getDockerAddressWhenLocalhost
fi
}
getDockerAddressWhenMachine() {
docker-machine ip "${DOCKER_MACHINE_NAME}"
}
getDockerAddressWhenB2D() {
boot2docker ip
}
getDockerAddressWhenLocalhost() {
echo localhost
}
getContainerName() {
echo "${PWD##*/}"
}
generatePdf() {
echo -e "${GREEN}Génération du pdf${NC}"
docker run --rm \
-v "$VOLUME_GRUNTFILE" -v "$VOLUME_SLIDES" -v "$VOLUME_PACKAGE" \
-v "$VOLUME_CAHIEREXERCICES" -v "$VOLUME_PDF_GENERATE" \
"$DOCKER_IMAGE_NAME":"$DOCKER_IMAGE_VERSION" \
grunt pdf
}
generatePdfForLabs() {
echo -e "${GREEN}Génération du pdf des TPs${NC}"
docker run --rm \
-v "$VOLUME_GRUNTFILE" -v "$VOLUME_SLIDES" -v "$VOLUME_PACKAGE" \
-v "$VOLUME_CAHIEREXERCICES" -v "$VOLUME_PDF_GENERATE" \
"$DOCKER_IMAGE_NAME":"$DOCKER_IMAGE_VERSION" \
grunt generateCahierExercice
}
runContainer() {
local containerName
local dockerPort
local dockerAddress
containerName=$(getContainerName)
docker run -d \
-P -p 32729:32729 \
-v "$VOLUME_GRUNTFILE" -v "$VOLUME_SLIDES" -v "$VOLUME_PACKAGE" \
-v "$VOLUME_CAHIEREXERCICES" \
--name="$containerName" \
"$DOCKER_IMAGE_NAME":"$DOCKER_IMAGE_VERSION" \
"$@" >/dev/null
echo " Nom de conteneur : $containerName"
dockerPort=$(docker ps -l | grep "$containerName" | sed 's/.*:\([0-9]*\)->8000.*/\1/')
dockerAddress=$(getDockerAddress)
open-url "http://${dockerAddress}:${dockerPort}/"
}
runProd() {
echo -e "${GREEN}Demarrage en mode prod${NC}"
runContainer grunt sed copy:rename displaySlides
}
runDev() {
echo -e "${GREEN}Demarrage en mode dev${NC}"
runContainer grunt displaySlides
}
followLogs() {
echo -e "${GREEN}Affichage des logs${NC}"
docker logs -f "$(getContainerName)"
}
clean() {
local containerName
containerName=$(getContainerName)
if docker ps -a --format "{{.ID}} {{.Names}}" | grep -q "$containerName"; then
echo -e "${BLUE}Arret et suppression du conteneur existant ($containerName)${NC}"
docker stop --time 1 "$containerName" >/dev/null 2>&1
docker rm "$containerName" >/dev/null 2>&1
else
echo -e "${BLUE}Aucun conteneur identifié à nettoyer ($containerName)${NC}"
fi
}
checkInstall
if [ "$#" -eq 0 ]; then
usage
fi
for arg in "$@"; do
case "$arg" in
pdf)
retrieveFormationVersion
generatePdf
shift
;;
pdf-labs)
retrieveFormationVersion
generatePdfForLabs
shift
;;
dev)
retrieveFormationVersion
runDev
shift
;;
prod)
retrieveFormationVersion
runProd
shift
;;
clean)
clean
shift
;;
logs)
followLogs
shift
;;
*)
echo "Argument invalide '$arg'" >&2
usage # unknown option
;;
esac
done