|
28 | 28 |
|
29 | 29 | cleanup_on_exit() { |
30 | 30 | rm -f rules.props *-vpn.props *-envfile.props config.json |
| 31 | + [[ -d env ]] && rm -f env/*.tmp |
31 | 32 | } |
32 | 33 | trap cleanup_on_exit EXIT |
33 | 34 |
|
@@ -255,6 +256,8 @@ rm -f services/generated/*-vpn.yaml |
255 | 256 |
|
256 | 257 | ALL_SERVICES="-f docker-compose.yaml" |
257 | 258 |
|
| 259 | +GLOBAL_ENV_FILE=".env" |
| 260 | + |
258 | 261 | # Parse the config.yaml master configuration file |
259 | 262 | for json in $(yq eval -o json config.yaml | jq -c ".services[]"); do |
260 | 263 | name=$(echo $json | jq -r .name) |
@@ -304,6 +307,63 @@ for json in $(yq eval -o json config.yaml | jq -c ".services[]"); do |
304 | 307 | fi |
305 | 308 | fi |
306 | 309 |
|
| 310 | + ###### For services which have "command" field with environment variables ###### |
| 311 | + var_in_cmd_detected="0" |
| 312 | + if [[ $(yq ".services.${name}.command[]" services/${file} | { grep "\\$.*\}" || true; } | wc -l) -gt 0 ]]; then |
| 313 | + var_in_cmd_detected="1" |
| 314 | + echo-debug "[$0] Service ${name} has a command with environment variables..." |
| 315 | + # Extract variable names to test them |
| 316 | + yq ".services.${name}.command[]" services/${file} | { grep "\\$.*\}" || true; } | sed -n -e 's/.*${\(\w\+\)}.*/\1/p' > env/${name}-cmd.env.1.tmp |
| 317 | + ( |
| 318 | + # Check if these variables are defined in generated .env files (global or custom) |
| 319 | + set -a |
| 320 | + source ./env/${name}.env |
| 321 | + source .env |
| 322 | + set +a |
| 323 | + while read p; do |
| 324 | + # If the command references a variable which is not known, throw an error |
| 325 | + if [[ -z ${!p+x} ]]; then |
| 326 | + echo "ERROR. Variable \"$p\" is referenced in \"command\" for service ${name} (file $file) but this variable is not defined in .env (or in .env.custom with prefix \"${name^^}_\"). Please correct it or add a variable which will be used." |
| 327 | + exit 1 |
| 328 | + fi |
| 329 | + done < env/${name}-cmd.env.1.tmp |
| 330 | + |
| 331 | + # Does not work for now because of how docker handles merges for arrays. Original values with variables stay. |
| 332 | + # Disabled for now |
| 333 | + if [[ "0" == "1" ]]; then |
| 334 | + # Extract command block from original service yaml file |
| 335 | + yq ".services.${name}.command[]" services/${file} > env/${name}-cmd.env.2.tmp |
| 336 | + # Envsubst this file |
| 337 | + envsubst < env/${name}-cmd.env.2.tmp > env/${name}-cmd.env.3.tmp |
| 338 | + # Convert this file to a props file, used to source a new proper YAML file |
| 339 | + i=0 |
| 340 | + while read line; do |
| 341 | + echo "services.${name}.command.$i: $line" >> env/${name}-cmd.env.4.tmp |
| 342 | + i=$((i+1)) |
| 343 | + done < env/${name}-cmd.env.3.tmp |
| 344 | + # Generate a proper override file with substituted variables |
| 345 | + yq -p=props env/${name}-cmd.env.4.tmp -o yaml > services/generated/${name}-command.yaml |
| 346 | + fi |
| 347 | + ) |
| 348 | + rm -f env/*.tmp |
| 349 | + # echo-debug "[$0] Adding override file for service ${name} / command with subsituted environment variables..." |
| 350 | + # ALL_SERVICES="${ALL_SERVICES} -f services/generated/${name}-command.yaml" |
| 351 | + fi |
| 352 | + |
| 353 | + # Handle case for command in a single line, not in array |
| 354 | + if [[ $(yq ".services.${name}.command" services/${file} | { grep "\\$.*\}" || true; } | wc -l) -gt 0 ]]; then |
| 355 | + var_in_cmd_detected="1" |
| 356 | + fi |
| 357 | + |
| 358 | + # Workaround for now |
| 359 | + if [[ "${var_in_cmd_detected}" == "1" ]]; then |
| 360 | + cat ${GLOBAL_ENV_FILE} ./env/${name}.env >> .env.concat.tmp |
| 361 | + rm -f .env.concat |
| 362 | + mv .env.concat.tmp .env.concat |
| 363 | + export GLOBAL_ENV_FILE=".env.concat" |
| 364 | + var_in_cmd_detected="0" |
| 365 | + fi |
| 366 | + |
307 | 367 | ###################################### TRAEFIK RULES ###################################### |
308 | 368 |
|
309 | 369 | # Skip this part for services which have Traefik rules disabled in config |
@@ -397,8 +457,9 @@ if [[ "${SKIP_PULL}" != "1" ]]; then |
397 | 457 | fi |
398 | 458 |
|
399 | 459 | echo "[$0] ***** Recreating containers if required... *****" |
400 | | -${DOCKER_COMPOSE_BINARY} ${ALL_SERVICES} up -d --remove-orphans |
| 460 | +${DOCKER_COMPOSE_BINARY} --env-file ${GLOBAL_ENV_FILE} ${ALL_SERVICES} up -d --remove-orphans |
401 | 461 | echo "[$0] ***** Done updating containers *****" |
| 462 | +rm -f .env.concat |
402 | 463 |
|
403 | 464 | echo "[$0] ***** Clean unused images and volumes... *****" |
404 | 465 | docker image prune -af |
|
0 commit comments