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

Refactoriza y muestra más info al inicio

Revisa scripts de despliegue y chequeo del mismo, para reaprovechar
comprobaciones iguales y que además pueden hacerse previamente en local.
Simplifica string de comando y adelanta trabajo.

Renombra variables creadas por el script pero que se escribieron en
mayúsculas, para evitar confundirlas con variables de entorno que puedan
ser asignadas desde fuera.

Añade mensaje inicial con información básica de las comprobaciones
previas, para saber a qué entorno nos enfrentamos durante el despliegue.
Elimina contenido innecesario.
parent 28a00cce
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
# Docker deploy

Docker deployment utilities for REDMIC infrastructure.
You can use it to deploy your own services, supporting **Docker Compose** and **Docker Swarm** environments.
You can use it to deploy your own services, supporting **Docker Compose** (both v1 and v2) and **Docker Swarm** environments.

## Actions

@@ -129,5 +129,4 @@ $ docker run --rm --name docker-deploy \
   * identified as `user`
   * authenticated through a RSA-1024 private key
   * into `example` stack
   * check service `example_service-name` deployment
   * with `VARIABLE_1` and `VARIABLE_2` set in service
+2 −12
Original line number Diff line number Diff line
#!/bin/sh

servicesToCheck="${SERVICES_TO_CHECK}"
if [ -z "${servicesToCheck}" ]
then
	standardComposeFileSplitted=$(echo ${COMPOSE_FILE} | sed 's/:/ -f /g')
	if [ ${DOCKER_23_COMPATIBLE_TARGET} -eq 0 ]
	then
		servicesToCheck=$(docker --log-level error compose -f ${standardComposeFileSplitted} config --services | sed "s/^/${STACK}_/g")
	else
		servicesToCheck=$(docker-compose --log-level ERROR -f ${standardComposeFileSplitted} config --services | sed "s/^/${STACK}_/g")
	fi
fi
servicesToCheck="${SERVICES_TO_CHECK:-${servicesInComposeFiles}}"

echo -e "\n${INFO_COLOR}Checking deployment of services [${DATA_COLOR} $(echo ${servicesToCheck}) ${INFO_COLOR}] at ${DATA_COLOR}${remoteHost}${INFO_COLOR} ..${NULL_COLOR}"

@@ -23,7 +13,7 @@ checkDeployCmd="\
		hits=0 && \
		for i in \$(seq 1 ${STATUS_CHECK_RETRIES}) ; \
		do \
			if [ ${DEPLOYING_TO_SWARM} -eq 0 ] ; \
			if [ ${deployingToSwarm} -eq 0 ] ; \
			then \
				stackServices=\$(docker service ls -f name=\${serviceToCheck} --format '{{.Replicas}}') ; \
				serviceToCheckReplication=\$(echo \"\${stackServices}\" | head -1) ; \
+15 −20
Original line number Diff line number Diff line
@@ -2,8 +2,18 @@

echo -e "\n${INFO_COLOR}Deploying at remote target ${DATA_COLOR}${remoteHost}${INFO_COLOR} ..${NULL_COLOR}\n"

if [ ${deployingToSwarm} -ne 0 ]
then
	if [ ${docker23CompatibleTarget} -eq 0 ]
	then
		composeBaseCmd="docker compose"
	else
		composeBaseCmd="docker-compose"
	fi
fi

deployCmd="\
	cd ${DEPLOY_HOME} && \
	cd ${deployHome} && \
	if [ ! -z \"${REGISTRY_USER}\" ] ; \
	then \
		docker login -u \"${REGISTRY_USER}\" -p \"${REGISTRY_PASS}\" ${REGISTRY_URL} ; \
@@ -11,8 +21,7 @@ deployCmd="\
	else \
		deployAuthParam=\"\" ; \
	fi ; \
	standardComposeFileSplitted=\$(echo ${COMPOSE_FILE} | sed 's/:/ -f /g') ; \
	if [ ${DEPLOYING_TO_SWARM} -eq 0 ] ; \
	if [ ${deployingToSwarm} -eq 0 ] ; \
	then \
		swarmComposeFileSplitted=\$(echo ${COMPOSE_FILE} | sed 's/:/ -c /g') && \
		${GREP_BIN} -v '^[#| ]' .env | sed -r \"s/(\w+)=(.*)/export \1='\2'/g\" > .env-deploy && \
@@ -20,16 +29,7 @@ deployCmd="\
			docker stack deploy \${deployAuthParam} --resolve-image ${SWARM_RESOLVE_IMAGE} -c \${swarmComposeFileSplitted} ${STACK}\" && \
		if [ ! -z \"\${deployAuthParam}\" ] ; \
		then \
			servicesToAuth=\"${SERVICES_TO_AUTH}\" && \
			if [ -z \"\${servicesToAuth}\" ] ; \
			then \
				if [ ${DOCKER_23_COMPATIBLE_TARGET} -eq 0 ] ; \
				then \
					servicesToAuth=\"\$(docker --log-level error compose -f \${standardComposeFileSplitted} config --services | sed \"s/^/${STACK}_/g\")\" ; \
				else \
					servicesToAuth=\"\$(docker-compose --log-level ERROR -f \${standardComposeFileSplitted} config --services | sed \"s/^/${STACK}_/g\")\" ; \
				fi ; \
			fi && \
			servicesToAuth=\"${SERVICES_TO_AUTH:-${servicesInComposeFiles}}\" && \
			if [ ! -z \"\${servicesToAuth}\" ] ; \
			then \
				for serviceToAuth in \${servicesToAuth} ; \
@@ -39,19 +39,14 @@ deployCmd="\
			fi ; \
		fi ; \
	else \
		if [ ${DOCKER_23_COMPATIBLE_TARGET} -eq 0 ] ; \
		then \
			composeCmd=\"docker compose -f \${standardComposeFileSplitted} -p ${STACK}\" ; \
		else \
			composeCmd=\"docker-compose -f \${standardComposeFileSplitted} -p ${STACK}\" ; \
		fi && \
		composeCmd=\"COMPOSE_FILE=${COMPOSE_FILE} ${composeBaseCmd} -p ${STACK}\" ; \
		\${composeCmd} stop ${SERVICES_TO_DEPLOY} && \
		\${composeCmd} rm -f ${SERVICES_TO_DEPLOY} && \
		\${composeCmd} pull ${SERVICES_TO_DEPLOY} && \
		\${composeCmd} up -d ${SERVICES_TO_DEPLOY} ; \
	fi"

cleanDeployCmd="ssh ${SSH_PARAMS} \"${SSH_REMOTE}\" \"rm -rf ${DEPLOY_HOME}\""
cleanDeployCmd="ssh ${SSH_PARAMS} \"${SSH_REMOTE}\" \"rm -rf ${deployHome}\""

if ssh ${SSH_PARAMS} "${SSH_REMOTE}" "${deployCmd}"
then
+28 −17
Original line number Diff line number Diff line
#!/bin/sh

# Se comprueba si está disponible el binario docker en el entorno donde se va a desplegar
# Se comprueba si está disponible el binario docker en el entorno donde se va a desplegar.
checkDockerCmd="docker --version > /dev/null 2>&1"
if ! ssh ${SSH_PARAMS} "${SSH_REMOTE}" ${checkDockerCmd}
then
@@ -8,19 +8,21 @@ then
	exit 1
fi

# Se comprueba si la versión de Docker en el entorno donde se va a desplegar es >= v23.0.0
# Se comprueba si la versión de Docker en el entorno donde se va a desplegar es >= v23.0.0.
checkDocker23Cmd="[ \$(docker --version | sed -r 's/.* ([0-9]+)\..*/\1/g') -ge 23 ]"
ssh ${SSH_PARAMS} "${SSH_REMOTE}" ${checkDocker23Cmd}
DOCKER_23_COMPATIBLE_TARGET=${?}
docker23CompatibleTarget=${?}

# Se comprueba si está disponible el plugin compose de docker o el antiguo binario docker-compose
if [ ${DOCKER_23_COMPATIBLE_TARGET} -eq 0 ]
# Se comprueba si está disponible el plugin compose de docker o el antiguo binario docker-compose.
if [ ${docker23CompatibleTarget} -eq 0 ]
then
	checkDockerComposeCmd="docker compose version > /dev/null 2>&1"
	composeVersionLabel="v2"
	dockerVersionLabel=">= v23"
	composeVersionLabel="current >=v2 plugin"
else
	checkDockerComposeCmd="docker-compose version > /dev/null 2>&1"
	composeVersionLabel="v1"
	dockerVersionLabel="< v23"
	composeVersionLabel="deprecated v1 binary"
fi
if ! ssh ${SSH_PARAMS} "${SSH_REMOTE}" ${checkDockerComposeCmd}
then
@@ -28,17 +30,17 @@ then
	exit 1
fi

# Se comprueba si se desea y si es posible desplegar en modo Swarm.
checkDeploymentTypeCmd="[ ${FORCE_DOCKER_COMPOSE} -eq 0 ] && docker stack ls > /dev/null 2>&1"
ssh ${SSH_PARAMS} "${SSH_REMOTE}" ${checkDeploymentTypeCmd}
DEPLOYING_TO_SWARM=${?}

echo -e "\n${INFO_COLOR}Preparing deployment configuration and resources ..${NULL_COLOR}"
deployingToSwarm=${?}

# Se preparan rutas de despliegue.
randomValue="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)"
deployHomeParent="${DEPLOY_PATH}/docker-deploy"
DEPLOY_HOME="${deployHomeParent}/${randomValue}"
deployHome="${deployHomeParent}/${randomValue}"

# Se comprueba si se despliega desde dentro de 'deploy' o desde la raíz del proyecto.
# Se comprueba si existe directorio con recursos de despliegue o están en la raíz del proyecto.
if [ -d "${DEPLOY_DIR_NAME}" ]
then
	cd "${DEPLOY_DIR_NAME}"
@@ -46,9 +48,18 @@ then
	createDirCmd="mkdir -p ${deployHomeParent}"
else
	deployFiles=${DEFAULT_DEPLOY_FILES}
	createDirCmd="mkdir -p ${DEPLOY_HOME}"
	createDirCmd="mkdir -p ${deployHome}"
fi

# Se obtienen los nombres de servicio presentes en ficheros compose, con prefijo de stack.
servicesInComposeFiles=$(docker --log-level error compose config --services | sed "s/^/${STACK}_/g" | tr '\n' ' ')
servicesToDeployLabel=${SERVICES_TO_DEPLOY:-${servicesInComposeFiles}}

echo -e "${DATA_COLOR}Docker deploy${INFO_COLOR} is about to perform a deployment to host ${DATA_COLOR}${remoteHost}${INFO_COLOR} ..${NULL_COLOR}"
echo -e "  ${INFO_COLOR}host Docker version [ ${DATA_COLOR}${dockerVersionLabel}${INFO_COLOR} ]${NULL_COLOR}"
echo -e "  ${INFO_COLOR}host Docker Compose version [ ${DATA_COLOR}${composeVersionLabel}${INFO_COLOR} ]${NULL_COLOR}"
echo -e "  ${INFO_COLOR}services to deploy [ ${DATA_COLOR}${servicesToDeployLabel}${INFO_COLOR}]${NULL_COLOR}"

echo -e "\n${INFO_COLOR}Setting environment variables to local and remote environments ..${NULL_COLOR}"
echo -en "  ${INFO_COLOR}variable names [ ${DATA_COLOR}STACK${INFO_COLOR}"

@@ -87,7 +98,7 @@ echo -en " ${INFO_COLOR}check command [ ${DATA_COLOR}"
validComposeMessage="${PASS_COLOR}Valid compose configuration!${NULL_COLOR}"
invalidComposeMessage="${FAIL_COLOR}Invalid compose configuration!${NULL_COLOR}"

if [ ${DOCKER_23_COMPATIBLE_TARGET} -eq 0 ] && [ ${DEPLOYING_TO_SWARM} -eq 0 ]
if [ ${docker23CompatibleTarget} -eq 0 ] && [ ${deployingToSwarm} -eq 0 ]
then
	echo -e "docker stack config${INFO_COLOR} ]${NULL_COLOR}\n"
	swarmComposeFileSplitted=$(echo ${COMPOSE_FILE} | sed 's/:/ -c /g')
@@ -112,19 +123,19 @@ else
fi

echo -e "\n${INFO_COLOR}Sending deployment resources to remote ${DATA_COLOR}${remoteHost}${INFO_COLOR} ..${NULL_COLOR}"
echo -e "  ${INFO_COLOR}deployment path [ ${DATA_COLOR}${DEPLOY_HOME}${INFO_COLOR} ]${NULL_COLOR}"
echo -e "  ${INFO_COLOR}deployment path [ ${DATA_COLOR}${deployHome}${INFO_COLOR} ]${NULL_COLOR}"
echo -e "  ${INFO_COLOR}deployment files [ ${DATA_COLOR}${deployFiles}${INFO_COLOR} ]${NULL_COLOR}\n"

# Se crea el directorio donde guardar los ficheros de despliegue del servicio.
if ! ssh ${SSH_PARAMS} "${SSH_REMOTE}" ${createDirCmd}
then
	echo -e "${FAIL_COLOR}Deployment path ${DATA_COLOR}${DEPLOY_HOME}${FAIL_COLOR} creation failed!${NULL_COLOR}"
	echo -e "${FAIL_COLOR}Deployment path ${DATA_COLOR}${deployHome}${FAIL_COLOR} creation failed!${NULL_COLOR}"
	ssh ${SSH_PARAMS} -q -O exit "${SSH_REMOTE}"
	exit 1
fi

# Se envían a su destino los ficheros de despliegue del servicio y se restaura el .env local.
scp ${SSH_PARAMS} ${deployFiles} "${SSH_REMOTE}:${DEPLOY_HOME}"
scp ${SSH_PARAMS} ${deployFiles} "${SSH_REMOTE}:${deployHome}"
sendResourcesExitCode=${?}
mv .env-original .env