Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docker): add Docker stack tasks
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` | ||
--- |