-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild
More file actions
executable file
·256 lines (239 loc) · 8.18 KB
/
build
File metadata and controls
executable file
·256 lines (239 loc) · 8.18 KB
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
#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
MYDIR="$(cd "$(dirname -- "$0")"; pwd)"
RUNPHP="$MYDIR/sbin/runphp"
"$RUNPHP" --bs --ue --ci || exit 1
RUNPHP_STANDALONE=
PROJDIR=; COMPOSERDIR=; COMPOSERPHAR=; VENDORDIR=; BUILDENV0=; BUILDENV=
BUILD_IMAGES=(php-apache mariadb10); export BUILD_FLAVOUR=; DIST=; IMAGENAME=
DISTFILES=(); TEMPLATEFILES=(); VARFILES=()
source "$RUNPHP" || exit 1
source "$PROJDIR/$VENDORDIR/nulib/base/load.sh" || exit 1
require: template
# recenser les valeur de proxy
declare -A PROXY_VARS
for var in {HTTPS,ALL,NO}_PROXY {http,https,all,no}_proxy; do
is_defined "$var" && PROXY_VARS[${var,,}]="${!var}"
done
BUILD_ARGS=(
DIST NDIST
REGISTRY
APT_PROXY
APT_MIRROR
SEC_MIRROR
TIMEZONE
)
function dklsnet() {
docker network ls --no-trunc --format '{{.Name}}' -f name="$1" 2>/dev/null
}
function dklsimg() {
local image="$1" version="$2"
docker image ls --no-trunc --format '{{.Repository}}:{{.Tag}}' "$image${version:+:$version}" 2>/dev/null
}
function dklsct() {
# afficher le container dont l'image correspondante est $1
docker ps --no-trunc --format '{{.Image}} {{.Names}}' | awk -v image="$1" '$1 == image { print $2 }'
}
function dkrunning() {
# vérifier si le container d'image $1 tourne
[ -n "$(dklsct "$@")" ]
}
function dclsct() {
# afficher les containers correspondant à $1(=docker-compose.yml)
docker compose ${1:+-f "$1"} ps -q
}
function dcrunning() {
# vérifier si les containers correspondant à $1(=docker-compose.yml) tournent
# si $2 est spécifié, c'est le nombre de service qui doit tourner
if [ -n "$2" ]; then
[ "$(dclsct "${@:1:1}" | wc -l)" -eq "$2" ]
else
[ -n "$(dclsct "${@:1:1}")" ]
fi
}
function build_check_env() {
eval "$(template_locals)"
local updatedenv file name
local -a updatedfiles files
if template_copy_missing "$PROJDIR/$BUILDENV0"; then
updated=1
updatedenv=1
fi
for file in "${DISTFILES[@]}"; do
if [ -f "$PROJDIR/$file" ]; then
if template_copy_missing "$PROJDIR/$file"; then
updated=1
setx name=basename -- "$file"
name="${name#.}"; name="${name%.*}"
setx file=dirname -- "$file"
file="$file/$name"
updatedfiles+=("$file")
fi
elif [ -d "$PROJDIR/$file" ]; then
local dir="$PROJDIR/$file"
setx -a files=find "$dir" -type f -name ".*.dist"
for file in "${files[@]}"; do
if template_copy_missing "$file"; then
updated=1
setx name=basename -- "$file"
name="${name#.}"; name="${name%.*}"
setx file=dirname -- "$file"
file="$file/$name"
updatedfiles+=("${file#$PROJDIR/}")
fi
done
else
ewarn "$file: fichier dist introuvable"
fi
done
for file in "${TEMPLATEFILES[@]}"; do
if [ -f "$PROJDIR/$file" ]; then
template_copy_replace "$PROJDIR/$file"
elif [ -d "$PROJDIR/$file" ]; then
local dir="$PROJDIR/$file"
setx -a files=find "$dir" -type f -name ".*.template"
for file in "${files[@]}"; do
template_copy_replace "$file"
done
else
ewarn "$file: fichier template introuvable"
fi
done
local -a varfiles
for file in "${VARFILES[@]}"; do
varfiles+=("$PROJDIR/$file")
done
template_process_userfiles "${varfiles[@]}"
if [ -n "$updated" ]; then
enote "IMPORTANT: vous devez paramétrer certains fichiers avant de pouvoir construire les images"
if [ -n "$updatedenv" ]; then
if [ $(id -u) -ne 0 ]; then
setx userent=getent passwd "$(id -un)"
setx userent=qval "$userent"
setx groupent=getent group "$(id -gn)"
setx groupent=qval "$groupent"
sed -i "
/^#DEVUSER_.*=/s/^#//
/^DEVUSER_USERENT=/s/=.*/=${userent//\//\\\/}/
/^DEVUSER_GROUPENT=/s/=.*/=${groupent//\//\\\/}/
" "$PROJDIR/$BUILDENV"
fi
einfo "\
Veuillez vérifier le fichier $BUILDENV
${EDITOR:-nano} $BUILDENV"
fi
[ ${#updatedfiles[*]} -gt 0 ] && einfo "\
Le cas échéant, veuillez vérifier ce(s) fichier(s)
${EDITOR:-nano} $(qvals "${updatedfiles[@]}")"
enote "ENSUITE, vous pourrez relancer la commande"
return 1
fi
}
function _build() {
local dockerfile image="${PRIVAREG:+$PRIVAREG/}${IMAGENAME%/*}/$1"
if [ -n "$ForceBuild" -o -z "$(dklsimg "$image")" ]; then
estep "Construction de $image"
dockerfiles=(
"$MYDIR/Dockerfile.$1.local"
"$MYDIR/Dockerfile.$1$BUILD_FLAVOUR"
"$PROJDIR/$VENDORDIR/nulib/base/dockerfiles/Dockerfile.$1$BUILD_FLAVOUR"
"$MYDIR/Dockerfile.$1"
"$PROJDIR/$VENDORDIR/nulib/base/dockerfiles/Dockerfile.$1"
)
for dockerfile in "${dockerfiles[@]}"; do
[ -f "$dockerfile" ] && break
done
args=(
-f "$dockerfile"
${Pull:+--pull}
${NoCache:+--no-cache}
${PlainOutput:+--progress plain}
-t "$image"
)
for arg in "${BUILD_ARGS[@]}"; do
args+=(--build-arg "$arg=${!arg}")
done
for arg in "${!PROXY_VARS[@]}"; do
args+=(--build-arg "$arg=${PROXY_VARS[$arg]}")
done
for host in "${HOST_MAPPINGS[@]}"; do
args+=(--add-host "$host")
done
docker build "${args[@]}" "$PROJDIR" || die
if [ -n "$Push" ]; then
if [ -n "$PRIVAREG" ]; then
estep "Poussement de $image"
docker push "$image" || die
else
ewarn "PRIVAREG non défini: impossible de pousser l'image"
fi
fi
fi
}
function build_images() {
local image sourced
[ $# -gt 0 ] || set -- runphp "${BUILD_IMAGES[@]}"
for image in "$@"; do
case "$image" in
runphp)
[ ${#Configs[*]} -gt 0 ] && export RUNPHP_FORCE_BUILDENVS="${Configs[*]}"
local -a args=(--bs)
[ "$ForceBuild" != all ] && args+=(--ue)
[ -n "$Pull" ] && args+=(--pull)
[ -n "$NoCache" ] && args+=(--no-cache)
"$RUNPHP" "${args[@]}" || die
;;
*)
if [ -z "$sourced" ]; then
[ ${#Configs[*]} -gt 0 ] || Configs=("$PROJDIR/$BUILDENV")
for config in "${Configs[@]}"; do
source "$config"
done
after_source_buildenv
read -a HOST_MAPPINGS <<<"${HOST_MAPPINGS//
/ }"
sourced=1
fi
_build "$image"
;;
esac
done
}
action=build
Configs=()
ForceBuild=
Pull=
NoCache=
PlainOutput=
Push=
args=(
"Construire les images pour le projet"
#"usage"
--check-only action=none "++Ne faire que la vérification de l'environnement"
-c:,--config:BUILDENV Configs "Spécifier un fichier d'environnement pour le build"
-r,--rebuild ForceBuild=1 "Forcer la (re)construction des images"
-R,--rebuild-all ForceBuild=all "++Comme --rebuild, mais reconstruire aussi runphp"
-U,--pull Pull=1 "++Forcer le re-téléchargement des images dépendantes"
-j,--no-cache NoCache=1 "++Construire l'image en invalidant le cache"
-D,--plain-output PlainOutput=1 "++Afficher le détail du build"
-p,--push Push=1 "Pousser les images vers le registry après construction"
)
parse_args "$@"; set -- "${args[@]}"
if [ ${#Configs[*]} -gt 0 ]; then
aconfigs=()
for config in "${Configs[@]}"; do
setx config=abspath "$config"
aconfigs+=("$config")
done
Configs=("${aconfigs[@]}")
# pas de vérification d'environnement si on spécifie Configs
# ne pas oublier d'implémenter un traitement spécifique si build_check_env()
# contient d'autres vérifications
else
build_check_env || die
fi
[ "$action" == none ] && exit 0
case "$action" in
build) build_images "$@";;
*) die "$action: action non implémentée";;
esac