Skip to content
Snippets Groups Projects
deploy 1.37 KiB
Newer Older
nimrod's avatar
nimrod committed
#!/bin/sh
set -eu

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

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

nimrod's avatar
nimrod committed
_deploy() {
nimrod's avatar
nimrod committed
    echo "Deploying to $1" >&2
    export DOCKER_HOST="ssh://$1"
    export RUNNER_NAME="$1"
nimrod's avatar
nimrod committed
    until _docker_compose up --detach --remove-orphans ; do true; done
nimrod's avatar
nimrod committed
    # shellcheck disable=SC2034
    for i in $(seq 12)
    do
        # shellcheck disable=SC2046
nimrod's avatar
nimrod committed
        _check || break
nimrod's avatar
nimrod committed
        sleep 10
    done
    # shellcheck disable=SC2046
nimrod's avatar
nimrod committed
    ! _check
nimrod's avatar
nimrod committed
    unset DOCKER_HOST
    unset RUNNER_NAME
}

if [ -z "${GITLAB_REGISTRATION_TOKEN:-}" ]
nimrod's avatar
nimrod committed
then
    echo "GITLAB_REGISTRATION_TOKEN isn't set." >&2
nimrod's avatar
nimrod committed
    exit 1
fi


[ "$#" -eq '0' ] && "$0" ns4 host01 kodi

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