Skip to content

Commit

Permalink
feat(docker): add Docker stack tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarlier committed Dec 15, 2017
1 parent 775a25d commit c249aaa
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
68 changes: 68 additions & 0 deletions docker/stack.Makefile
@@ -0,0 +1,68 @@
.SILENT :

# Docker compose configuration files
COMPOSE_FILES?=-f docker-compose.yml

# Stack name
STACK?=default

# Wait until a service ($$service) is up and running (needs health run flag)
stack-service-wait:
service=$(STACK)_$(service); \
sid=`docker service ps $${service} -q | head -1`; \
cid=`docker inspect --format "{{.Status.ContainerStatus.ContainerID }}" $${sid}`;\
n=30;\
while [ $${n} -gt 0 ] ; do\
status=`docker inspect --format "{{json .State.Health.Status }}" $${cid}`;\
if [ -z $${status} ]; then echo "No status informations."; exit 1; fi;\
echo "Waiting for $(service) up and ready ($${status})...";\
if [ "\"healthy\"" = $${status} ]; then exit 0; fi;\
sleep 2;\
n=`expr $$n - 1`;\
done;\
echo "Timeout" && exit 1
.PHONY: stack-service-wait

# Deploy Docker stack
stack-deploy:
echo "Deploying Docker stack ($(STACK))..."
-cat .env
docker stack deploy --compose-file=<(docker-compose $(COMPOSE_FILES) config) $(STACK)
.PHONY: stack-deploy

# Un-deploy Docker stack
stack-rm:
echo "Un-deploying Docker stack ($(STACK))..."
@while [ -z "$$CONTINUE" ]; do \
read -r -p "Are you sure? [y/N]: " CONTINUE; \
done ; \
[ $$CONTINUE = "y" ] || [ $$CONTINUE = "Y" ] || (echo "Exiting."; exit 1;)
docker stack rm $(STACK)
echo "Docker stack \"$(STACK)\" un-deployed."
.PHONY: stack-rm

# Un-deploy Docker stack (without user confirmation)
stack-rm-force:
echo "Un-deploying Docker stack ($(STACK))..."
docker stack rm $(STACK)
echo "Docker stack \"$(STACK)\" un-deployed."
.PHONY: stack-rm-force

# View stack logs
stack-logs:
echo "Viewing stack logs ..."
docker service logs -f $(STACK)
.PHONY: stack-logs

# View stack status
stack-ps:
echo "Viewing stack status ..."
docker stack ps $(STACK)
.PHONY: stack-ps

# View stack services
stack-services:
echo "Viewing stack services ..."
docker stack services $(STACK)
.PHONY: stack-services

54 changes: 54 additions & 0 deletions docker/stack.README
@@ -0,0 +1,54 @@
<!-- vi: set ft=markdown :-->

# Docker stack

## Tasks

- `stack-deploy`: Deploy Docker stack
- `stack-rm`: Un-deploy Docker stack
- `stack-rm-force`: Un-deploy Docker stack (without asking for confirmation)
- `stack-logs`: Get stack logs
- `stack-ps`: Get stack status
- `stack-services`: Get stack services
- `stack-service-wait`: Wait for the health check of the `${service}`

## Configuration

- `COMPOSE_FILES`: Docker compose configuration files (-f a.yml -f b.yml ...)

## Usage

Create a `Makefile`:

```makefile
# Docker compose configuration files
COMPOSE_FILES?=-f docker-compose.yml

# Include common Make tasks
root_dir:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
makefiles:=$(root_dir)/makefiles
include $(makefiles)/help.Makefile
include $(makefiles)/docker/stack.Makefile

## Apply configuration
apply: stack-deploy stack-logs
.PHONY : apply

## Un-apply configuration
unapply: stack-rm
.PHONY : unapply

## Get stack status
status: stack-ps
.PHONY : status

```

Play:

```bash
$ make apply
$ make status
$ make unapply
```
---

0 comments on commit c249aaa

Please sign in to comment.