Commit 9789ba6e authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Centraliza comandos SSH con parámetros en función

Evita la repetición de la fórmula para hacer ejecuciones remotas a lo
largo de los scripts. En su lugar, se define una función que recibe
únicamente el comando a ejecutar, y se reutiliza en todas partes.
parent 8a642c3c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -104,7 +104,7 @@ checkDeployCmd="\
		fi ; \
	done"

if ssh ${SSH_PARAMS} "${SSH_REMOTE}" "${checkDeployCmd}"
if runRemoteCmd "${checkDeployCmd}"
then
	echo -e "\n${PASS_COLOR}All services seems ok!${NULL_COLOR}"
else
+5 −5
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@

# Se comprueba si está disponible el binario docker en el entorno donde se va a desplegar.
checkDockerCmd="command -v docker > /dev/null"
if ! ssh ${SSH_PARAMS} "${SSH_REMOTE}" ${checkDockerCmd}
if ! runRemoteCmd "${checkDockerCmd}"
then
	echo -e "\n${FAIL_COLOR}Docker is not available at deployment target host environment!${NULL_COLOR}"
	eval "${closeSshCmd}"
@@ -11,7 +11,7 @@ fi

# Se obtiene la versión de Docker disponible en el entorno donde se va a desplegar.
getDockerVersionCmd="docker version --format '{{.Server.Version}}'"
dockerVersion=$(ssh ${SSH_PARAMS} "${SSH_REMOTE}" ${getDockerVersionCmd})
dockerVersion=$(runRemoteCmd "${getDockerVersionCmd}")

echo -e "  ${INFO_COLOR}host Docker version [ ${DATA_COLOR}${dockerVersion}${INFO_COLOR} ]${NULL_COLOR}"

@@ -26,7 +26,7 @@ then
	deployingToSwarm=1
else
	checkSwarmManagerAvailabilityCmd="[ \$(docker info --format '{{.Swarm.ControlAvailable}}') = true ]"
	ssh ${SSH_PARAMS} "${SSH_REMOTE}" ${checkSwarmManagerAvailabilityCmd}
	runRemoteCmd "${checkSwarmManagerAvailabilityCmd}"
	deployingToSwarm=${?}
fi

@@ -50,7 +50,7 @@ else
		composeBaseCmd="docker-compose"

		checkDockerComposeBinaryCmd="command -v docker-compose > /dev/null"
		if ! ssh ${SSH_PARAMS} "${SSH_REMOTE}" ${checkDockerComposeBinaryCmd}
		if ! runRemoteCmd "${checkDockerComposeBinaryCmd}"
		then
			echo -e "\n${FAIL_COLOR}Legacy docker-compose binary is not available at deployment target host environment!${NULL_COLOR}"
			eval "${closeSshCmd}"
@@ -60,7 +60,7 @@ else

	# Se obtiene la versión de Docker Compose disponible en el entorno donde se va a desplegar.
	getComposeVersionCmd="${composeBaseCmd} version --short"
	composeVersion=$(ssh ${SSH_PARAMS} "${SSH_REMOTE}" ${getComposeVersionCmd})
	composeVersion=$(runRemoteCmd "${getComposeVersionCmd}")

	echo -e "  ${INFO_COLOR}host Docker Compose version [ ${DATA_COLOR}${composeVersion}${INFO_COLOR} ]${NULL_COLOR}"
fi
+3 −3
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ then
		${GREP_BIN} \"^${ddRegistryPassVarName}=\" \"${COMPOSE_ENV_FILE_NAME}\" | cut -d= -f2- | \
		docker login -u \"${REGISTRY_USER}\" --password-stdin ${REGISTRY_URL}"

	if ssh ${SSH_PARAMS} "${SSH_REMOTE}" "${moveToDeployDirCmd}${loginCmd}"
	if runRemoteCmd "${moveToDeployDirCmd}${loginCmd}"
	then
		echo -e "\n${PASS_COLOR}Login to registry was successful!${NULL_COLOR}"
	else
@@ -60,13 +60,13 @@ fi

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

ssh ${SSH_PARAMS} "${SSH_REMOTE}" "${deployCmd}"
runRemoteCmd "${deployCmd}"
deployExitCode=${?}

if [ "${OMIT_CLEAN_DEPLOY}" -eq 0 ]
then
	cleanDeployCmd="rm -rf \"${deployHome}\""
	ssh ${SSH_PARAMS} "${SSH_REMOTE}" "${cleanDeployCmd}"
	runRemoteCmd "${cleanDeployCmd}"
else
	echo -e "\n${INFO_COLOR}Deployment resources cleaning omitted${NULL_COLOR}"
fi
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ echo -e " ${INFO_COLOR}deployment path [ ${DATA_COLOR}${deployHome}${INFO_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}
if ! runRemoteCmd "${createDirCmd}"
then
	echo -e "${FAIL_COLOR}Deployment path ${DATA_COLOR}${deployHome}${FAIL_COLOR} creation failed!${NULL_COLOR}"
	eval "${restoreEnvFileCmd}"
+5 −1
Original line number Diff line number Diff line
@@ -27,8 +27,12 @@ echo "${DEPLOY_KEY}" | tr -d '\r' | ssh-add - > /dev/null 2>&1

closeSshCmd="ssh ${SSH_PARAMS} -q -O exit \"${SSH_REMOTE}\""

runRemoteCmd() {
	ssh ${SSH_PARAMS} "${SSH_REMOTE}" "${1}"
}

# Se comprueba si está disponible la conexión hacia el entorno donde se va a desplegar.
if ! ssh ${SSH_PARAMS} "${SSH_REMOTE}" : &> /dev/null
if ! runRemoteCmd ":" &> /dev/null
then
	echo -e "\n${FAIL_COLOR}Failed to connect to host ${DATA_COLOR}${remoteHost}${INFO_COLOR} at port ${DATA_COLOR}${SSH_PORT}${INFO_COLOR} with user ${DATA_COLOR}${remoteUser}${INFO_COLOR}!${NULL_COLOR}"
	eval "${closeSshCmd}"
Loading