I have setup with 2 servers, production one called prod
and staging one called staging
. Both servers are in same Docker swarm.
I want to write bash script to move one docker swarm service from production to staging server.
I have created this 2 bash scripts i'm running at node manager.
#!/usr/bin/env bash
# move_service_to_prod.sh
/usr/bin/docker service update \
--constraint-rm "node.hostname==staging" \
--constraint-rm "node.hostname==prod" \
--constraint-add "node.hostname==prod" \
some_service
#!/usr/bin/env bash
# move_service_to_staging.sh
/usr/bin/docker service update \
--constraint-rm "node.hostname==staging" \
--constraint-rm "node.hostname==prod" \
--constraint-add "node.hostname==staging" \
some_service
As you can see, script removes all constraints, and than add new one. When i start 1st script, it sometimes creates 2 prod constraints for service. Like this output of `docker inspect some_service'
"Placement": {
"Constraints": [
"node.hostname==staging",
"node.hostname==staging"
],
Sometimes it not works, - service still running on staging machine. If there will be few more environments (like staging1, staging2, etc), code will be much bigger.
Is there any reliable bash oneliner to remove all constraints and to add proper ones for service?