Commit c4e8117b authored by nimrod's avatar nimrod
Browse files

deploy: Use Docker contexts.

Use Docker contexts in the deploy script and the Compose plugin to
deploy (instead of forwarding sockets over SSH).
parent 1947ea8a
Loading
Loading
Loading
Loading
Loading
+12 −21
Original line number Diff line number Diff line
#!/bin/sh
set -eu

_docker_compose() {
    docker-compose "$@" 2>&1 | sed '/^  File /,+1d'
}

_check() {
    containers="$(until _docker_compose ps -q; do true; done)"
    host="$1"
    containers="$(until docker --context "$host" compose ps -q; do true; done)"
    # shellcheck disable=SC2086
    docker container inspect --format '{{ .State.Health.Status }}' $containers | grep -v '^healthy$'
    docker --context "$host" container inspect --format '{{ .State.Health.Status }}' $containers | grep -v '^healthy$'
}

_deploy() {
    host="$1"
    printf '\n\e[1;94m=== %s ====\e[0m\n\n' "Deploying to $host" >&2
    localsocket="$HOME/.ssh/docker_$host.sock"
    forward "$host" "$localsocket:/var/run/docker.sock"
    export DOCKER_HOST="unix://$localsocket"
    export RUNNER_NAME="$host"
    until _docker_compose up --detach --remove-orphans ; do true; done
    # shellcheck disable=SC2034
    printf '\n\e[1;94m=== %s ====\e[0m\n\n' "Deploying to ${host}.shore.co.il" >&2
    export RUNNER_NAME="${host}.shore.co.il"
    until docker --context "$host" compose up --detach --remove-orphans ; do true; done
    for i in $(seq 12)
    do
        # shellcheck disable=SC2046
        _check || break
        _check "$host" || break
        sleep 10
    done
    # shellcheck disable=SC2046
    ! _check
    unset DOCKER_HOST
    # shellcheck disable=SC2251
    ! _check "$host"
    unset RUNNER_NAME
}

@@ -48,15 +39,15 @@ do
    case "$i" in
        ns4) export REGISTER_RUN_UNTAGGED="true"
            export BUILDS='/builds'
            _deploy ns4.shore.co.il
            _deploy ns4
            ;;
        kodi) export REGISTER_RUN_UNTAGGED="false"
            unset BUILDS
            _deploy kodi.shore.co.il
            _deploy kodi
            ;;
        host01) export REGISTER_RUN_UNTAGGED="false"
            unset BUILDS
            _deploy host01.shore.co.il
            _deploy host01
            ;;
        all) "$0" ns4 host01 kodi ;;
        *) echo 'Unknown host.' >&2; exit 1;;