Commit 33e4b745 authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Merge branch 'dev' into 'master'

Documenta variable olvidada, corrige mensaje

See merge request redmic-project/docker/docker-deploy!41
parents 350053ad 21b62736
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ For REDMIC, we use this image from GitLabCI configuration, but you can use it di

```
$ docker run --rm --name docker-deploy \
	-e STACK=your-stack-name -e SSH_REMOTE=ssh-user@ssh-host -e GITLAB_DEPLOY_KEY=<your-private-key> \
	-e STACK=your-stack-name -e SSH_REMOTE=ssh-user@ssh-host -e GITLAB_DEPLOY_KEY="<your-private-key>" \
	registry.gitlab.com/redmic-project/docker/docker-deploy:latest \
	<action> <VAR1>=<value1> <VAR2>=<value2> ...
```
@@ -39,6 +39,7 @@ For environment variables, you may define these variables (**bold** are mandator
* *DEPLOY_DIR_NAME*: Default `deploy`.
* *DEFAULT_DEPLOY_FILES*: Default `docker-compose*.yml .env`.
* *REGISTRY_USER*: Default `gitlab-ci-token`.
* *SERVICES_TO_CHECK*: Names of services to check, separated by space.
* *STATUS_CHECK_RETRIES*: Default `10`.
* *STATUS_CHECK_INTERVAL*: Default `20`.
* *STATUS_CHECK_DELAY*: Default `120`.
+60 −35
Original line number Diff line number Diff line
@@ -4,26 +4,34 @@ SERVICES_TO_CHECK="${SERVICES_TO_CHECK:-${STACK:-${SERVICE}}}"

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

for serviceToCheck in ${SERVICES_TO_CHECK}
do
	echo -e "\n${INFO_COLOR}Checking deployment of service ${DATA_COLOR}${serviceToCheck}${INFO_COLOR} ..${NULL_COLOR}"
	echo -e "  ${INFO_COLOR}retries ${DATA_COLOR}${STATUS_CHECK_RETRIES}${INFO_COLOR}, interval ${DATA_COLOR}${STATUS_CHECK_INTERVAL}${INFO_COLOR}s, hits ${DATA_COLOR}${STATUS_CHECK_MIN_HITS}${NULL_COLOR}\n"

checkDeployCmd="\
	success='' ; \
	for serviceToCheck in ${SERVICES_TO_CHECK} ; \
	do \
		echo -e \"\\n${INFO_COLOR}Checking deployment of service ${DATA_COLOR}\${serviceToCheck}${INFO_COLOR} ..${NULL_COLOR}\" ; \
		echo -e \"  ${INFO_COLOR}retries ${DATA_COLOR}${STATUS_CHECK_RETRIES}${INFO_COLOR}, interval ${DATA_COLOR}${STATUS_CHECK_INTERVAL}${INFO_COLOR}s, hits ${DATA_COLOR}${STATUS_CHECK_MIN_HITS}${NULL_COLOR}\\n\" ; \
		hits=0 && \
		for i in \$(seq 1 ${STATUS_CHECK_RETRIES}) ; \
		do \
			if docker stack ls > /dev/null 2> /dev/null ; \
			then \
				stackServices=\$(docker service ls -f name=${serviceToCheck} --format '{{.Replicas}}') ; \
				stackServices=\$(docker service ls -f name=\${serviceToCheck} --format '{{.Replicas}}') ; \
				serviceToCheckReplication=\$(echo \"\${stackServices}\" | head -1) ; \
				runningServiceName=\$(docker service ls -f name=${serviceToCheck} --format '{{.Name}}' | head -1) ; \
				runningServiceName=\$(docker service ls -f name=\${serviceToCheck} --format '{{.Name}}' | head -1) ; \
				serviceCount=\$(echo \"\${stackServices}\" | ${GREP_BIN} -cE '.+') ; \
				if [ \${serviceCount} -gt 1 -a \${i} -eq 1 ] ; \
				then \
					echo -e \"${FAIL_COLOR}Found ${DATA_COLOR}\${serviceCount}${FAIL_COLOR} services filtering by name ${DATA_COLOR}${serviceToCheck}${FAIL_COLOR},${NULL_COLOR}\" ; \
					echo -e \"${FAIL_COLOR}Found ${DATA_COLOR}\${serviceCount}${FAIL_COLOR} services filtering by name ${DATA_COLOR}\${serviceToCheck}${FAIL_COLOR},${NULL_COLOR}\" ; \
					echo -e \"  ${FAIL_COLOR}will check only the service exactly named ${DATA_COLOR}\${runningServiceName}${NULL_COLOR}\\n\" ; \
				fi ; \
				if [ \${serviceCount} -eq 0 ] ; \
				then \
					if [ \${i} -eq 1 ] ; \
					then \
						echo -e \"${FAIL_COLOR}Cannot find running service by name ${DATA_COLOR}\${serviceToCheck}${NULL_COLOR}\\n\" ; \
					fi ; \
					serviceIsRunning=\"[ 0 -ne 0 ]\" ; \
				else \
					runningServiceCount=\$(echo \"\${serviceToCheckReplication}\" | ${GREP_BIN} -cE '([0-9]+)\/\1') ; \
					serviceIsRunning=\"[ \${runningServiceCount} -eq 1 ]\" ; \
					if ! \${serviceIsRunning} ; \
@@ -45,11 +53,12 @@ do
							serviceIsRunning=true ; \
						fi ; \
					fi ; \
				fi ; \
				statusCheckCmd=\${serviceIsRunning} ; \
			else \
				runningContainersIds=\$(docker ps -f status=running --format '{{.ID}}' --no-trunc) ; \
				successfullyExitedContainersIds=\$(docker ps -a -f exited=0 --format '{{.ID}}' --no-trunc) ; \
				serviceContainerId=\$(docker inspect --format='{{.ID}}' ${serviceToCheck} 2> /dev/null) ; \
				serviceContainerId=\$(docker inspect --format='{{.ID}}' \${serviceToCheck} 2> /dev/null) ; \
				runningService=\$(echo \"\${runningContainersIds}\" | ${GREP_BIN} \"\${serviceContainerId:--}\") ; \
				successfullyExitedService=\$(echo \"\${successfullyExitedContainersIds}\" | \
					${GREP_BIN} \"\${serviceContainerId:--}\") ; \
@@ -63,20 +72,36 @@ do
				hits=\$((\${hits} + 1)) && \
				if [ \"\${hits}\" -eq \"${STATUS_CHECK_MIN_HITS}\" ] ; \
				then \
					echo -e \"${PASS_COLOR}Service ${DATA_COLOR}${serviceToCheck}${PASS_COLOR} is running!${NULL_COLOR}\" && \
					echo -e \"\\n${PASS_COLOR}Service ${DATA_COLOR}\${serviceToCheck}${PASS_COLOR} is running!${NULL_COLOR}\" && \
					echo -e \"  got ${PASS_COLOR}\${hits}/${STATUS_CHECK_MIN_HITS}${NULL_COLOR} status hits\" && \
					exit 0 ; \
					success=\"\${success} 1\" ; \
					break ; \
				fi ; \
			else \
				echo -e \"${FAIL_COLOR}[FAIL]${NULL_COLOR}\" ; \
				if [ \"\${i}\" -eq \"${STATUS_CHECK_RETRIES}\" ] ; \
				then \
					echo -e \"\\n${FAIL_COLOR}Service ${DATA_COLOR}\${serviceToCheck}${FAIL_COLOR} is not running!${NULL_COLOR}\" && \
					echo -e \"  got ${FAIL_COLOR}\${hits}/${STATUS_CHECK_MIN_HITS}${NULL_COLOR} status hits\" && \
					success=\"\${success} 0\" ; \
				fi ; \
			fi ; \
			sleep ${STATUS_CHECK_INTERVAL} ; \
		done ; \
		echo -e \"${FAIL_COLOR}Service ${DATA_COLOR}${serviceToCheck}${FAIL_COLOR} is not running!${NULL_COLOR}\" && \
		echo -e \"  got ${FAIL_COLOR}\${hits}/${STATUS_CHECK_MIN_HITS}${NULL_COLOR} status hits\" && \
		exit 1"

	ssh ${SSH_PARAMS} "${SSH_REMOTE}" "${checkDeployCmd}"
done
	done ; \
	for serviceSuccess in \${success[*]} ; \
	do \
		if [ \${serviceSuccess} -eq 0 ] ; \
		then \
			exit 1 ; \
		fi ; \
	done"

if ssh ${SSH_PARAMS} "${SSH_REMOTE}" "${checkDeployCmd}"
then
	echo -e "\\n${PASS_COLOR}All services seems ok!${NULL_COLOR}"
else
	echo -e "\\n${FAIL_COLOR}One or more services seems failed!${NULL_COLOR}"
	ssh ${SSH_PARAMS} -q -O exit "${SSH_REMOTE}"
	exit 1
fi
+2 −2
Original line number Diff line number Diff line
@@ -21,10 +21,10 @@ cleanDeployCmd="ssh ${SSH_PARAMS} \"${SSH_REMOTE}\" \"rm -rf ${DEPLOY_HOME}\""

if ssh ${SSH_PARAMS} "${SSH_REMOTE}" "${deployCmd}"
then
	echo -e "${PASS_COLOR}Service successfully deployed!${NULL_COLOR}"
	echo -e "${PASS_COLOR}Services successfully deployed!${NULL_COLOR}"
	eval "${cleanDeployCmd}"
else
	echo -e "${FAIL_COLOR}Service deploy failed!${NULL_COLOR}"
	echo -e "${FAIL_COLOR}Services deploy failed!${NULL_COLOR}"
	eval "${cleanDeployCmd}"
	ssh ${SSH_PARAMS} -q -O exit "${SSH_REMOTE}"
	exit 1