Commit 350053ad authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Merge branch 'dev' into 'master'

Documenta mínimamente funcionamiento y uso

See merge request redmic-project/docker/docker-deploy!40
parents 19ed9296 60ad821e
Loading
Loading
Loading
Loading

LICENSE

0 → 100644
+21 −0
Original line number Diff line number Diff line
MIT License

Copyright (c) 2019 REDMIC Project / Docker

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+47 −2
Original line number Diff line number Diff line
# docker-deploy
# Docker deploy

Docker deployment utilities for REDMIC infrastructure
 No newline at end of file
Docker deployment utilities for REDMIC infrastructure. You can use it to deploy your own services into your servers.

## Actions

* **deploy**: Perform a deployment of a service on a remote Docker environment. Contains 3 stages:

  * *prepare-deploy*: Copy resources to remote environment (*docker-compose* files, service configurations, ...), prepare environment variables and directories, etc.

  * *do-deploy*: Launch service on Docker environment. Both standard and Swarm modes are supported on remote Docker environment, but Swarm mode is recommended (even for a single-node clusters).

  * *check-deploy*: Once deployment is done, this stage wait a defined period of time for the service to being up and running (or stopped after run successfully). If service status remains stable after several checks, then it is considered successfully deployed.

* **create-nets**: Prepare remote environment creating Docker networks which are external to service (not created by service deployment itself).

* **relaunch**: Force a previously deployed service to update, relaunching it with the same service configuration.

## Usage

For REDMIC, we use this image from GitLabCI configuration, but you can use it directly like:

```
$ 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> \
	registry.gitlab.com/redmic-project/docker/docker-deploy:latest \
	<action> <VAR1>=<value1> <VAR2>=<value2> ...
```

As you can see, there is configuration through environment variables and by action script parameters. By environment variables, you configure the behaviour of this image itself. The script parameters are used like environment variables, but in the remote environment (where you are deploying to), in order to configure your service.

For environment variables, you may define these variables (**bold** are mandatory):

* **STACK** / **SERVICE**: Name of Docker stack (Swarm mode) or service (standard mode) to deploy.
* **SSH_REMOTE**: SSH user and host of remote machine where you are going to deploy.
* **GITLAB_DEPLOY_KEY**: Private key paired with a public key accepted by remote machine, used to authenticate.
* *COMPOSE_FILE*: Default `docker-compose.yml`.
* *DEPLOY_PATH*: Default `~`.
* *DEPLOY_DIR_NAME*: Default `deploy`.
* *DEFAULT_DEPLOY_FILES*: Default `docker-compose*.yml .env`.
* *REGISTRY_USER*: Default `gitlab-ci-token`.
* *STATUS_CHECK_RETRIES*: Default `10`.
* *STATUS_CHECK_INTERVAL*: Default `20`.
* *STATUS_CHECK_DELAY*: Default `120`.
* *STATUS_CHECK_MIN_HITS*: Default `3`.
* *GREP_BIN*: Default `grep`.

Action may be one of `deploy.sh`, `create-nets.sh` or `relaunch.sh`, and parameters with variable/value depends on your service needs.
+1 −2
Original line number Diff line number Diff line
#!/bin/sh

SERVICES_TO_CHECK="${SERVICES_TO_CHECK:-${STACK:-${SERVICE}}}"
GREP_BIN="${GREP_BIN:-grep}"

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

@@ -36,7 +35,7 @@ do
						replicaStoppedTaskState=\$(docker service ps --format '{{.CurrentState}}' \
							-f 'desired-state=shutdown' -f \"name=\${runningServiceName}.\${j}\" \
							\${runningServiceName} | head -1) ; \
						if echo \"\${replicaStoppedTaskState}\" | grep 'Complete' > /dev/null 2> /dev/null ; \
						if echo \"\${replicaStoppedTaskState}\" | ${GREP_BIN} 'Complete' > /dev/null 2> /dev/null ; \
						then \
							completedTaskCount=\$((\${completedTaskCount} + 1)) ; \
						fi ; \
+2 −0
Original line number Diff line number Diff line
@@ -11,6 +11,8 @@ STATUS_CHECK_INTERVAL="${STATUS_CHECK_INTERVAL:-20}"
STATUS_CHECK_DELAY="${STATUS_CHECK_DELAY:-120}"
STATUS_CHECK_MIN_HITS="${STATUS_CHECK_MIN_HITS:-3}"

GREP_BIN="${GREP_BIN:-grep}"

INFO_COLOR='\033[1;36m'
DATA_COLOR='\033[1;33m'
FAIL_COLOR='\033[0;31m'
+1 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ deployCmd="\
	if docker stack ls > /dev/null 2> /dev/null ; \
	then \
		composeFileSplitted=\$(echo ${COMPOSE_FILE} | sed 's/:/ -c /g') && \
		env -i \$(grep -v '^#\\| ' .env | xargs) \
		env -i \$(${GREP_BIN} -v '^#\\| ' .env | xargs) \
			docker stack deploy -c \${composeFileSplitted} --with-registry-auth ${STACK:-${SERVICE}} ; \
	else \
		docker-compose stop ${SERVICE} && \