Commit ffd4c095 authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Controla interpolación compose en check config

Para no afectar a otros usos del fichero .env del usuario, se controla
la interpolación solamente para el uso de comprobación de configuración
compose, y luego se elimina el contenido generado.

Falta aplicar el mismo principio al despliegue con compose, aunque la
documentación ya lo de por hecho.
parent e0087dd8
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -96,6 +96,7 @@ You may define these environment variables (**bold** are mandatory):
| *FORCE_DOCKER_COMPOSE* | `0` | Use always standard (*Compose*) mode instead of Docker *Swarm*, even if it is available at deployment target host. |
| *GREP_BIN* | `grep` | Path to *grep* binary in deployment target host. |
| *OMIT_CLEAN_DEPLOY* | `0` | Leave at deployment target host all deployment resources after doing a deploy. Useful when using bind mounts or *Compose* secrets (pointing to static content in deployment resources) or you want to check sent contents. |
| *OMIT_COMPOSE_ENV_FILE_INTERPOLATION* | `0` | Allow passing variable values directly from `COMPOSE_ENV_FILE_NAME` file (`.env` by default), to let *Compose* interpolate variables used into values. By default, values will be single-quoted before checking config and deploy with *Compose*, to avoid getting unwanted variable resolution. Useful only for *Compose* mode. |
| *OMIT_STATUS_CHECK* | `0` | Bypass status check process after deploying services. Useful when you need to be fast. |
| *REGISTRY_PASS* | - | Docker registry password, corresponding to a user with read permissions. **Required** for private registry or repository. |
| *REGISTRY_URL* | - | Docker registry address, where Docker must log in to retrieve images. Useful only when using private registry or repository. Default is empty, to use Docker Hub registry. |
+34 −4
Original line number Diff line number Diff line
@@ -5,19 +5,49 @@ echo -e " ${INFO_COLOR}compose files [ ${DATA_COLOR}${COMPOSE_FILE}${INFO_COLOR
echo -en "  ${INFO_COLOR}check command [ ${DATA_COLOR}"

# Antes de continuar, se comprueba que la configuración de despliegue sea válida para compose o swarm.
tempEnvFile=".env-config"
if [ ${docker23CompatibleTarget} -eq 0 ] && [ ${deployingToSwarm} -eq 0 ]
then
	echo -e "docker stack config${INFO_COLOR} ]${NULL_COLOR}\n"
	grep -v '^[#| ]' "${COMPOSE_ENV_FILE_NAME}" | sed -r "s/(\w+)=(.*)/export \1='\2'/g" > .env-config

	grep -v '^[#| ]' "${COMPOSE_ENV_FILE_NAME}" | sed -r "s/(\w+)=(.*)/export \1='\2'/g" > "${tempEnvFile}"

	env -i /bin/sh -c "\
		. $(pwd)/.env-config && \
		rm $(pwd)/.env-config && \
		. $(pwd)/${tempEnvFile} && \
		rm $(pwd)/${tempEnvFile} && \
		/usr/local/bin/docker stack config -c ${swarmComposeFileSplitted} > /dev/null"
else
	echo -e "docker compose config${INFO_COLOR} ]${NULL_COLOR}\n"

	docker compose --env-file "${COMPOSE_ENV_FILE_NAME}" config -q
	if [ ${OMIT_COMPOSE_ENV_FILE_INTERPOLATION} -eq 0 ]
	then
		envConfigContent=""
		while IFS= read -r envLine
		do
			if [ -z "${envLine}" ] || echo "${envLine}" | grep -q '^[#| ]'
			then
				continue
			else
				variableName=$(echo "${envLine}" | cut -d '=' -f 1)
				variableValue=$(echo "${envLine}" | cut -d '=' -f 2-)

				# Si la variable ya tiene valor entrecomillado simple o los dólares duplicados, se usa tal cual
				if echo "${variableValue}" | grep -q '\$\$' || echo "${variableValue}" | grep -q '^'''
				then
					envConfigContent="${envConfigContent}${variableName}=${variableValue}\\n"
				else
					envConfigContent="${envConfigContent}${variableName}='${variableValue}'\\n"
				fi
			fi
		done < "${COMPOSE_ENV_FILE_NAME}"
		echo -e "${envConfigContent}" > "${tempEnvFile}"
	else
		tempEnvFile="${COMPOSE_ENV_FILE_NAME}"
	fi

	docker compose --env-file "${tempEnvFile}" config -q

	rm "${tempEnvFile}"
fi

if [ ${?} -eq 0 ]
+1 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ DEFAULT_DEPLOY_FILES="${DEFAULT_DEPLOY_FILES:-*compose*.y*ml ${COMPOSE_ENV_FILE_
FORCE_DOCKER_COMPOSE="${FORCE_DOCKER_COMPOSE:-0}"
OMIT_CLEAN_DEPLOY="${OMIT_CLEAN_DEPLOY:-0}"
SWARM_RESOLVE_IMAGE="${SWARM_RESOLVE_IMAGE:-always}"
OMIT_COMPOSE_ENV_FILE_INTERPOLATION="${OMIT_COMPOSE_ENV_FILE_INTERPOLATION:-0}"

OMIT_STATUS_CHECK="${OMIT_STATUS_CHECK:-0}"
STATUS_CHECK_RETRIES="${STATUS_CHECK_RETRIES:-10}"