aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/update_hosts.sh
blob: 4f32722bb6b271855716c8e0d907c256a06c3e91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#! /usr/bin/env sh
set -e

base_dir="$(git rev-parse --show-toplevel)"

user="$USER"
set_hosts=""
branch="main"
direct=false

while [ "$#" -ne 0 ]; do
    case "$1" in
    --user)
        shift 1
        user="$1"
        ;;
    --host)
        shift 1
        set_hosts="$set_hosts $1"
        ;;
    --hosts)
        shift 1
        if [ -n "$set_hosts" ]; then
            echo "Warning: --hosts overrides --host"
        fi
        set_hosts="$1"
        ;;
    --branch)
        shift 1
        branch="$1"
        ;;
    --direct)
        direct=true
        ;;
    *)
        echo "Arg '$1' not known."
        exit 2
        ;;
    esac
    shift 1
done

hosts="${set_hosts-"server2 server3"}"

echo "update_hosts.sh --user '$user' --hosts '$hosts' --branch '$branch' --direct '$direct'"

for host in $hosts; do
    echo "Updating '$user@$host.vhack.eu' ..."

    printf "Deploying remote side script ..\n"
    scp "$base_dir/scripts/update_hosts.remote" "$user@$host.vhack.eu:update_host.remote"

    flake_path=""
    if [ "$direct" = true ]; then
        printf "Deploying git config ..\n"

        flake_path="path:$(git archive --format=tar.gz HEAD |
            ssh "$user@$host.vhack.eu" \
                'flake_path=$(mktemp -d -t update_hosts_XXXXX); cd $flake_path && tar -xz; echo $flake_path')"

    fi

    tmp="$(mktemp -d -t "server_build_script_XXXXX")"
    trap 'rm -r "$tmp"' EXIT
    if [ "$direct" = true ]; then
        git archive --format=tar.gz HEAD | {
            cd "$tmp"
            tar -xz
        }
    else
        git clone "git.foss-syndicate.org" --branch="$branch" "$tmp"
    fi
    cd "$tmp"

    printf "Copying closure ..\n"
    nix copy --substitute-on-destination --to "ssh://$user@$host.vhack.eu" ".#nixosConfigurations.$host.config.system.build.toplevel"

    printf "Executing remote side script ..\n"
    ssh -t "$user@$host.vhack.eu" "chmod +x update_host.remote; ./update_host.remote '$branch' '$flake_path'"
done

# Give the servers some time to reboot
sleep 5
ping_hosts.sh "$user" "$hosts"

# vim: ft=sh