From 4b8220afc3278b65645133d484000e151b973dc2 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Fri, 11 Dec 2020 22:00:14 +0200
Subject: [PATCH] Manual deployment.

Can't deploy with the runner, the jobs fail because the runner is
restarted during. Do the pull and build with the CI, but the actual
deployment with a script locally.
---
 .gitlab-ci.yml          | 43 -----------------------------------------
 .pre-commit-config.yaml |  5 +++++
 README.md               |  8 +++++---
 deploy                  | 32 ++++++++++++++++++++++++++++++
 4 files changed, 42 insertions(+), 46 deletions(-)
 create mode 100755 deploy

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 4ceaa6c..d01ca6c 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -4,7 +4,6 @@ image: adarnimrod/ci-images:docker
 stages:
   - test
   - build
-  - run
 
 pre-commit:
   stage: test
@@ -44,45 +43,3 @@ build-ns4:
     - ns4.shore.co.il
   variables: *build_vars
   script: *build_script
-
-run-host01:
-  stage: run
-  tags:
-    - host01.shore.co.il
-  variables:
-    RUNNER_NAME: host01.shore.co.il
-    RUNNER_TAG_LIST: host01.shore.co.il
-    REGISTER_RUN_UNTAGGED: "false"
-  when: manual
-  script: &run-script
-    - docker-compose up --detach --remove-orphans
-    # yamllint disable rule:line-length
-    - |
-        for i in $(seq 12)
-        do
-            docker container inspect --format '{{ .State.Health.Status }}' $(docker-compose ps -q) | grep -v '^healthy$' || break
-            sleep 10
-        done
-        ! docker container inspect --format '{{ .State.Health.Status }}' $(docker-compose ps -q) | grep -v '^healthy$'
-
-run-kodi:
-  stage: run
-  tags:
-    - kodi.shore.co.il
-  variables:
-    RUNNER_NAME: kodi.shore.co.il
-    RUNNER_TAG_LIST: kodi.shore.co.il
-    REGISTER_RUN_UNTAGGED: "false"
-  when: manual
-  script: *run-script
-
-run-ns4:
-  stage: run
-  tags:
-    - ns4.shore.co.il
-  variables:
-    RUNNER_NAME: ns4.shore.co.il
-    RUNNER_TAG_LIST: ns4.shore.co.il
-    REGISTER_RUN_UNTAGGED: "true"
-  when: manual
-  script: *run-script
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 7e7b050..f214fdc 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -27,3 +27,8 @@ repos:
     hooks:
       - id: docker-compose
       - id: hadolint
+  - repo: https://git.shore.co.il/nimrod/shell-pre-commit.git
+    sha: v0.6.0
+    hooks:
+      - id: shell-lint
+      - id: shellcheck
diff --git a/README.md b/README.md
index d2c4eac..cade3a6 100644
--- a/README.md
+++ b/README.md
@@ -11,10 +11,12 @@ dockerd socket mounted inside the container).
 
 ## Usage
 
-First, get the registration token from https://git.shore.co.il/admin/runners. To
-build and run on `ns4.shore.co.il`, just run:
+The CI will pull and build images, all that's left to do is recreate the
+containers. But the jobs fail is the runner is recreated during the job. The
+workaround is the `deploy` script.
+
 ```
-DOCKER_HOST=ssh://ns4.shore.co.il docker-compose build && DOCKER_HOST=ssh://ns4.shore.co.il REGISTRATION_TOKEN='INSERT_TOKEN' RUNNER_NAME=ns4.shore.co.il REGISTER_RUN_UNTAGGED="true" docker-compose up -d
+REGISTRATION_TOKEN='INSERT_TOKEN' ./deploy
 ```
 
 ## License
diff --git a/deploy b/deploy
new file mode 100755
index 0000000..14b0389
--- /dev/null
+++ b/deploy
@@ -0,0 +1,32 @@
+#!/bin/sh
+set -eu
+
+deploy() {
+    echo "Deploying to $1" >&2
+    export DOCKER_HOST="ssh://$1"
+    export RUNNER_NAME="$1"
+    until docker-compose up --detach --remove-orphans 2>/dev/null; do true; done
+    # shellcheck disable=SC2034
+    for i in $(seq 12)
+    do
+        # shellcheck disable=SC2046
+        docker container inspect --format '{{ .State.Health.Status }}' $(docker-compose ps -q) | grep -v '^healthy$' || break
+        sleep 10
+    done
+    # shellcheck disable=SC2046
+    ! docker container inspect --format '{{ .State.Health.Status }}' $(docker-compose ps -q) | grep -v '^healthy$'
+    unset DOCKER_HOST
+    unset RUNNER_NAME
+}
+
+if [ -z "${REGISTRATION_TOKEN:-}" ]
+then
+    echo "REGISTRATION_TOKEN isn't set." >&2
+    exit 1
+fi
+
+export REGISTER_RUN_UNTAGGED="false"
+deploy "host01.shore.co.il"
+deploy "kodi.shore.co.il"
+export REGISTER_RUN_UNTAGGED="true"
+deploy "ns4.shore.co.il"
-- 
GitLab