From f5009806bb42a8d2a8f71b1ecf7552db2897ab8e Mon Sep 17 00:00:00 2001 From: SKP Date: Mon, 20 Jan 2020 13:05:35 +0100 Subject: [PATCH 1/5] Make swarm single host mode explicit --- .../installer/docker/docker-compose.yaml | 200 +++++++++++++----- dist/setup.sh | 42 +++- 2 files changed, 182 insertions(+), 60 deletions(-) diff --git a/cyphernodeconf_docker/templates/installer/docker/docker-compose.yaml b/cyphernodeconf_docker/templates/installer/docker/docker-compose.yaml index 99d0196..20c6ab1 100644 --- a/cyphernodeconf_docker/templates/installer/docker/docker-compose.yaml +++ b/cyphernodeconf_docker/templates/installer/docker/docker-compose.yaml @@ -22,12 +22,20 @@ services: networks: - cyphernodenet - cyphernodeappsnet - <% if (docker_mode == 'compose') { %> + <% if ( docker_mode === 'swarm' ) { %> + deploy: + replicas: 1 + placement: + constraints: + - node.labels.io.cyphernode.core == true + restart_policy: + condition: "any" + delay: 1s + update_config: + parallelism: 1 + <% } else { %> restart: always <% } %> -# deploy: -# placement: -# constraints: [node.hostname==dev] <% } %> <% if ( features.indexOf('lightning') !== -1 && lightning_implementation === 'c-lightning' ) { %> @@ -54,16 +62,24 @@ services: stop_grace_period: 30s networks: - cyphernodenet - <% if (docker_mode == 'compose') { %> - restart: always - <% } %> <% if ( features.indexOf('tor') !== -1 ) { %> depends_on: - tor <% } %> -# deploy: --lightning-dir=/.lightning -# placement: -# constraints: [node.hostname==dev] + <% if ( docker_mode === 'swarm' ) { %> + deploy: + replicas: 1 + placement: + constraints: + - node.labels.io.cyphernode.core == true + restart_policy: + condition: "any" + delay: 1s + update_config: + parallelism: 1 + <% } else { %> + restart: always + <% } %> <% } %> <% if( bitcoin_mode === 'internal' ) { %> @@ -93,16 +109,24 @@ services: stop_grace_period: 30s networks: - cyphernodenet - <% if (docker_mode == 'compose') { %> - restart: always - <% } %> <% if ( features.indexOf('tor') !== -1 ) { %> depends_on: - tor <% } %> -# deploy: -# placement: -# constraints: [node.hostname==dev] + <% if ( docker_mode === 'swarm' ) { %> + deploy: + replicas: 1 + placement: + constraints: + - node.labels.io.cyphernode.core == true + restart_policy: + condition: "any" + delay: 1s + update_config: + parallelism: 1 + <% } else { %> + restart: always + <% } %> <% } %> ########################## @@ -153,12 +177,20 @@ services: <% } %> networks: - cyphernodenet - <% if (docker_mode == 'compose') { %> + <% if ( docker_mode === 'swarm' ) { %> + deploy: + replicas: 1 + placement: + constraints: + - node.labels.io.cyphernode.core == true + restart_policy: + condition: "any" + delay: 1s + update_config: + parallelism: 1 + <% } else { %> restart: always <% } %> -# deploy: -# placement: -# constraints: [node.hostname==dev] ########################## # PROXYCRON # @@ -171,14 +203,22 @@ services: - "OTS_URL=proxy:8888/ots_backoffice" networks: - cyphernodenet - <% if (docker_mode == 'compose') { %> - restart: always - <% } %> depends_on: - proxy -# deploy: -# placement: -# constraints: [node.hostname==dev] + <% if ( docker_mode === 'swarm' ) { %> + deploy: + replicas: 1 + placement: + constraints: + - node.labels.io.cyphernode.core == true + restart_policy: + condition: "any" + delay: 1s + update_config: + parallelism: 1 + <% } else { %> + restart: always + <% } %> ########################## # BROKER # @@ -189,12 +229,20 @@ services: networks: - cyphernodenet - cyphernodeappsnet - <% if (docker_mode == 'compose') { %> + <% if ( docker_mode === 'swarm' ) { %> + deploy: + replicas: 1 + placement: + constraints: + - node.labels.io.cyphernode.core == true + restart_policy: + condition: "any" + delay: 1s + update_config: + parallelism: 1 + <% } else { %> restart: always <% } %> -# deploy: -# placement: -# constraints: [node.hostname==dev] ########################## # NOTIFIER # @@ -213,14 +261,22 @@ services: networks: - cyphernodenet - cyphernodeappsnet - <% if (docker_mode == 'compose') { %> - restart: always - <% } %> depends_on: - broker -# deploy: -# placement: -# constraints: [node.hostname==dev] + <% if ( docker_mode === 'swarm' ) { %> + deploy: + replicas: 1 + placement: + constraints: + - node.labels.io.cyphernode.core == true + restart_policy: + condition: "any" + delay: 1s + update_config: + parallelism: 1 + <% } else { %> + restart: always + <% } %> ########################## # PYCOIN # @@ -240,12 +296,20 @@ services: - "<%= logs_datapath %>:/cnlogs" networks: - cyphernodenet - <% if (docker_mode == 'compose') { %> + <% if ( docker_mode === 'swarm' ) { %> + deploy: + replicas: 1 + placement: + constraints: + - node.labels.io.cyphernode.core == true + restart_policy: + condition: "any" + delay: 1s + update_config: + parallelism: 1 + <% } else { %> restart: always <% } %> -# deploy: -# placement: -# constraints: [node.hostname==dev] <% if ( features.indexOf('otsclient') !== -1 ) { %> ########################## @@ -272,12 +336,20 @@ services: command: $USER /script/startotsclient.sh networks: - cyphernodenet - <% if (docker_mode == 'compose') { %> + <% if ( docker_mode === 'swarm' ) { %> + deploy: + replicas: 1 + placement: + constraints: + - node.labels.io.cyphernode.core == true + restart_policy: + condition: "any" + delay: 1s + update_config: + parallelism: 1 + <% } else { %> restart: always <% } %> -# deploy: -# placement: -# constraints: [node.hostname==dev] <% } %> ########################## @@ -308,14 +380,22 @@ services: networks: - cyphernodenet - cyphernodeappsnet - <% if (docker_mode == 'compose') { %> - restart: always - <% } %> depends_on: - proxy -# deploy: -# placement: -# constraints: [node.hostname==dev] + <% if ( docker_mode === 'swarm' ) { %> + deploy: + replicas: 1 + placement: + constraints: + - node.labels.io.cyphernode.core == true + restart_policy: + condition: "any" + delay: 1s + update_config: + parallelism: 1 + <% } else { %> + restart: always + <% } %> ########################## # TRAEFIK # @@ -333,14 +413,22 @@ services: - "<%= traefik_datapath%>/htpasswd:/htpasswd/htpasswd:ro" networks: - cyphernodeappsnet - <% if (docker_mode == 'compose') { %> - restart: always - <% } %> depends_on: - gatekeeper -# deploy: -# placement: -# constraints: [node.hostname==dev] + <% if ( docker_mode === 'swarm' ) { %> + deploy: + replicas: 1 + placement: + constraints: + - node.labels.io.cyphernode.infra == true + restart_policy: + condition: "any" + delay: 1s + update_config: + parallelism: 1 + <% } else { %> + restart: always + <% } %> volumes: container_monitor: diff --git a/dist/setup.sh b/dist/setup.sh index 893838e..262fa92 100755 --- a/dist/setup.sh +++ b/dist/setup.sh @@ -538,10 +538,44 @@ install_docker() { docker swarm join-token worker > /dev/null 2>&1 local noSwarm=$?; - if [[ $DOCKER_MODE == 'swarm' && $noSwarm == 1 ]]; then - step " init docker swarm" - try docker swarm init --task-history-limit 1 > /dev/null 2>&1 - next + if [[ $DOCKER_MODE == 'swarm' ]]; then + if [[ $noSwarm == 1 ]]; then + step " init docker swarm" + try docker swarm init --task-history-limit 1 > /dev/null 2>&1 + next + fi + + local nodeid + nodeid=$(docker node ls -f role=manager --format="{{.ID}}") + + # we only support swarm in single host mode, so all labels needed to spawn containers in the swarm + # are given to the manager of the swarm + # it is possible to move the io.cyphernode.apps label to a different node, + # for apps which rely on shared volumes with core components, we have the io.cyphernode.clingyapps + if [[ $(docker node inspect ${nodeid} --format '{{ index .Spec.Labels "io.cyphernode.core" }}') == "true" ]]; then + step "  [32madd docker node label: io.cyphernode.core" + try docker node update --label-add io.cyphernode.core=true ${nodeid} > /dev/null 2>&1 + next + fi + + if [[ $(docker node inspect ${nodeid} --format '{{ index .Spec.Labels "io.cyphernode.infra" }}') == "true" ]]; then + step "  [32madd docker node label: io.cyphernode.infra" + try docker node update --label-add io.cyphernode.infra=true ${nodeid} > /dev/null 2>&1 + next + fi + + if [[ $(docker node inspect ${nodeid} --format '{{ index .Spec.Labels "io.cyphernode.apps" }}') == "true" ]]; then + step "  [32madd docker node label: io.cyphernode.apps" + try docker node update --label-add io.cyphernode.apps=true ${nodeid} > /dev/null 2>&1 + next + fi + + if [[ $(docker node inspect ${nodeid} --format '{{ index .Spec.Labels "io.cyphernode.clingyapps" }}') == "true" ]]; then + step "  [32madd docker node label: io.cyphernode.clingyapps" + try docker node update --label-add io.cyphernode.clingyapps=true ${nodeid} > /dev/null 2>&1 + next + fi + fi local net_entry=$(docker network ls | grep cyphernodenet); From aa85b436607a99ddcfb02fe9b8cbf967ea9afb06 Mon Sep 17 00:00:00 2001 From: SKP Date: Tue, 21 Jan 2020 15:22:16 +0100 Subject: [PATCH 2/5] Added swarm mode config to traefik config --- cyphernodeconf_docker/templates/traefik/traefik.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/cyphernodeconf_docker/templates/traefik/traefik.toml b/cyphernodeconf_docker/templates/traefik/traefik.toml index ccd33d6..e589790 100644 --- a/cyphernodeconf_docker/templates/traefik/traefik.toml +++ b/cyphernodeconf_docker/templates/traefik/traefik.toml @@ -17,6 +17,7 @@ endpoint = "unix:///var/run/docker.sock" domain = "cyphernode.localhost" watch = true exposedByDefault = false +swarmMode = <%= ( docker_mode === 'swarm' )?'true':'false' %> [acme] email = "letsencrypt@yourdomain.com" From 1c143682c4ab0f2258a80db67aad524140576f60 Mon Sep 17 00:00:00 2001 From: SKP Date: Tue, 21 Jan 2020 15:22:41 +0100 Subject: [PATCH 3/5] Reliably get swarm nodeid of current host --- dist/setup.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/dist/setup.sh b/dist/setup.sh index 262fa92..1a9eaf9 100755 --- a/dist/setup.sh +++ b/dist/setup.sh @@ -545,34 +545,34 @@ install_docker() { next fi - local nodeid - nodeid=$(docker node ls -f role=manager --format="{{.ID}}") + local localnodeid + localnodeid=$(docker info -f '{{.Swarm.NodeID}}') # we only support swarm in single host mode, so all labels needed to spawn containers in the swarm # are given to the manager of the swarm # it is possible to move the io.cyphernode.apps label to a different node, # for apps which rely on shared volumes with core components, we have the io.cyphernode.clingyapps - if [[ $(docker node inspect ${nodeid} --format '{{ index .Spec.Labels "io.cyphernode.core" }}') == "true" ]]; then + if [[ $(docker node inspect ${localnodeid} --format '{{ index .Spec.Labels "io.cyphernode.core" }}') == "true" ]]; then step "  [32madd docker node label: io.cyphernode.core" - try docker node update --label-add io.cyphernode.core=true ${nodeid} > /dev/null 2>&1 + try docker node update --label-add io.cyphernode.core=true ${localnodeid} > /dev/null 2>&1 next fi - if [[ $(docker node inspect ${nodeid} --format '{{ index .Spec.Labels "io.cyphernode.infra" }}') == "true" ]]; then + if [[ $(docker node inspect ${localnodeid} --format '{{ index .Spec.Labels "io.cyphernode.infra" }}') == "true" ]]; then step "  [32madd docker node label: io.cyphernode.infra" - try docker node update --label-add io.cyphernode.infra=true ${nodeid} > /dev/null 2>&1 + try docker node update --label-add io.cyphernode.infra=true ${localnodeid} > /dev/null 2>&1 next fi - if [[ $(docker node inspect ${nodeid} --format '{{ index .Spec.Labels "io.cyphernode.apps" }}') == "true" ]]; then + if [[ $(docker node inspect ${localnodeid} --format '{{ index .Spec.Labels "io.cyphernode.apps" }}') == "true" ]]; then step "  [32madd docker node label: io.cyphernode.apps" - try docker node update --label-add io.cyphernode.apps=true ${nodeid} > /dev/null 2>&1 + try docker node update --label-add io.cyphernode.apps=true ${localnodeid} > /dev/null 2>&1 next fi - if [[ $(docker node inspect ${nodeid} --format '{{ index .Spec.Labels "io.cyphernode.clingyapps" }}') == "true" ]]; then + if [[ $(docker node inspect ${localnodeid} --format '{{ index .Spec.Labels "io.cyphernode.clingyapps" }}') == "true" ]]; then step "  [32madd docker node label: io.cyphernode.clingyapps" - try docker node update --label-add io.cyphernode.clingyapps=true ${nodeid} > /dev/null 2>&1 + try docker node update --label-add io.cyphernode.clingyapps=true ${localnodeid} > /dev/null 2>&1 next fi From 357f0ecd5e68a725f00357c333f65d071cf78f20 Mon Sep 17 00:00:00 2001 From: SKP Date: Tue, 21 Jan 2020 15:39:40 +0100 Subject: [PATCH 4/5] Simplified one label swarm. --- .../installer/docker/docker-compose.yaml | 22 +++++++------- dist/setup.sh | 30 ++++--------------- 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/cyphernodeconf_docker/templates/installer/docker/docker-compose.yaml b/cyphernodeconf_docker/templates/installer/docker/docker-compose.yaml index 20c6ab1..280cee3 100644 --- a/cyphernodeconf_docker/templates/installer/docker/docker-compose.yaml +++ b/cyphernodeconf_docker/templates/installer/docker/docker-compose.yaml @@ -27,7 +27,7 @@ services: replicas: 1 placement: constraints: - - node.labels.io.cyphernode.core == true + - node.labels.io.cyphernode == true restart_policy: condition: "any" delay: 1s @@ -71,7 +71,7 @@ services: replicas: 1 placement: constraints: - - node.labels.io.cyphernode.core == true + - node.labels.io.cyphernode == true restart_policy: condition: "any" delay: 1s @@ -118,7 +118,7 @@ services: replicas: 1 placement: constraints: - - node.labels.io.cyphernode.core == true + - node.labels.io.cyphernode == true restart_policy: condition: "any" delay: 1s @@ -182,7 +182,7 @@ services: replicas: 1 placement: constraints: - - node.labels.io.cyphernode.core == true + - node.labels.io.cyphernode == true restart_policy: condition: "any" delay: 1s @@ -210,7 +210,7 @@ services: replicas: 1 placement: constraints: - - node.labels.io.cyphernode.core == true + - node.labels.io.cyphernode == true restart_policy: condition: "any" delay: 1s @@ -234,7 +234,7 @@ services: replicas: 1 placement: constraints: - - node.labels.io.cyphernode.core == true + - node.labels.io.cyphernode == true restart_policy: condition: "any" delay: 1s @@ -268,7 +268,7 @@ services: replicas: 1 placement: constraints: - - node.labels.io.cyphernode.core == true + - node.labels.io.cyphernode == true restart_policy: condition: "any" delay: 1s @@ -301,7 +301,7 @@ services: replicas: 1 placement: constraints: - - node.labels.io.cyphernode.core == true + - node.labels.io.cyphernode == true restart_policy: condition: "any" delay: 1s @@ -341,7 +341,7 @@ services: replicas: 1 placement: constraints: - - node.labels.io.cyphernode.core == true + - node.labels.io.cyphernode == true restart_policy: condition: "any" delay: 1s @@ -387,7 +387,7 @@ services: replicas: 1 placement: constraints: - - node.labels.io.cyphernode.core == true + - node.labels.io.cyphernode == true restart_policy: condition: "any" delay: 1s @@ -420,7 +420,7 @@ services: replicas: 1 placement: constraints: - - node.labels.io.cyphernode.infra == true + - node.labels.io.cyphernode == true restart_policy: condition: "any" delay: 1s diff --git a/dist/setup.sh b/dist/setup.sh index 1a9eaf9..f86b2a6 100755 --- a/dist/setup.sh +++ b/dist/setup.sh @@ -549,33 +549,13 @@ install_docker() { localnodeid=$(docker info -f '{{.Swarm.NodeID}}') # we only support swarm in single host mode, so all labels needed to spawn containers in the swarm - # are given to the manager of the swarm - # it is possible to move the io.cyphernode.apps label to a different node, - # for apps which rely on shared volumes with core components, we have the io.cyphernode.clingyapps - if [[ $(docker node inspect ${localnodeid} --format '{{ index .Spec.Labels "io.cyphernode.core" }}') == "true" ]]; then - step "  [32madd docker node label: io.cyphernode.core" - try docker node update --label-add io.cyphernode.core=true ${localnodeid} > /dev/null 2>&1 + # are given to the host setup is run on. Setup must be run on any manager, if swarm mode is enabled + # and a swarm was already initialised + if [[ $(docker node inspect ${localnodeid} --format '{{ index .Spec.Labels "io.cyphernode" }}') == "true" ]]; then + step "  [32madd docker node label: io.cyphernode" + try docker node update --label-add io.cyphernode=true ${localnodeid} > /dev/null 2>&1 next fi - - if [[ $(docker node inspect ${localnodeid} --format '{{ index .Spec.Labels "io.cyphernode.infra" }}') == "true" ]]; then - step "  [32madd docker node label: io.cyphernode.infra" - try docker node update --label-add io.cyphernode.infra=true ${localnodeid} > /dev/null 2>&1 - next - fi - - if [[ $(docker node inspect ${localnodeid} --format '{{ index .Spec.Labels "io.cyphernode.apps" }}') == "true" ]]; then - step "  [32madd docker node label: io.cyphernode.apps" - try docker node update --label-add io.cyphernode.apps=true ${localnodeid} > /dev/null 2>&1 - next - fi - - if [[ $(docker node inspect ${localnodeid} --format '{{ index .Spec.Labels "io.cyphernode.clingyapps" }}') == "true" ]]; then - step "  [32madd docker node label: io.cyphernode.clingyapps" - try docker node update --label-add io.cyphernode.clingyapps=true ${localnodeid} > /dev/null 2>&1 - next - fi - fi local net_entry=$(docker network ls | grep cyphernodenet); From d5ddbbd6faacc7224d53d172424453ab08cc00f6 Mon Sep 17 00:00:00 2001 From: SKP Date: Tue, 21 Jan 2020 15:56:45 +0100 Subject: [PATCH 5/5] NOT!!! --- dist/setup.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/setup.sh b/dist/setup.sh index f86b2a6..7c894a3 100755 --- a/dist/setup.sh +++ b/dist/setup.sh @@ -551,8 +551,8 @@ install_docker() { # we only support swarm in single host mode, so all labels needed to spawn containers in the swarm # are given to the host setup is run on. Setup must be run on any manager, if swarm mode is enabled # and a swarm was already initialised - if [[ $(docker node inspect ${localnodeid} --format '{{ index .Spec.Labels "io.cyphernode" }}') == "true" ]]; then - step "  [32madd docker node label: io.cyphernode" + if [[ $(docker node inspect ${localnodeid} --format '{{ index .Spec.Labels "io.cyphernode" }}') != "true" ]]; then + step " add docker node label io.cyphernode" try docker node update --label-add io.cyphernode=true ${localnodeid} > /dev/null 2>&1 next fi