aboutsummaryrefslogtreecommitdiff
path: root/guix-container.sh
blob: d117ae326e6bfd68e209bd477927a621300ad6bb (about) (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/sh

# SPDX-License-Identifier: CC0-1.0

# Copyright (C) 2022-2023 Wojtek Kosior <koszko@koszko.org>
#
# Available under the terms of Creative Commons Zero v1.0 Universal.

### BEGIN INIT INFO
# Provides:          guix-container
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Wojtek's Guix container with various services
### END INIT INFO

set -e

if [ -r /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
else
    log_anything() {
        while [ 0 -lt $# ]; do
            printf "%s" "$1"
            shift
            if [ 0 -lt $# ]; then
                printf " "
            else
                printf "\n"
            fi
        done
    }
    log_action_msg() {
        log_anything "$@"
    }
    log_failure_msg() {
        printf "Failure\n"
    }
    log_success_msg() {
        printf "Success\n"
    }
    log_daemon_msg() {
        log_anything "$@"
    }
    log_warning_msg() {
        log_anything "$@"
    }
    status_of_proc() {
        if is_running; then
            printf "Guix container running\n"
        else
            printf "Guix container not running\n"
        fi
    }
fi

if [ 0 != $(id -u) ]; then
    log_action_msg "Script '$0' must be run as root"
    exit 1
fi

PIDFILE=/run/guix-container.pid
EXECUTABLE=/usr/local/bin/guix-container
HOST_SYSTEM_ROOT=
MAX_CONTAINER_SPINUP_WAIT=60

ACTION="$1"
shift

OPTIND=1
while getopts p:e:r:s: OPTION_LETTER ; do
    case "$OPTION_LETTER" in
        p)  PIDFILE="$OPTARG"    ;;
        e)  EXECUTABLE="$OPTARG" ;;
        r)  HOST_SYSTEM_ROOT="$OPTARG" ;;
        s)  MAX_CONTAINER_SPINUP_WAIT="$OPTARG" ;;
    esac
done

GUILE_PID=
SUCCESS=
QUIET_EXIT=

is_running() {
    test -e "$PIDFILE" && test -n "$(ps -o pid= --pid $(cat "$PIDFILE"))"
    return $?
}

network_setup() {
    SHEPHERD_PID="$1"

    ip link add veth-guix-out type veth peer name veth-guix-in
    ip link set veth-guix-in netns "$SHEPHERD_PID"

    ip link set veth-guix-out up
    ip addr add 10.207.87.1/24 dev veth-guix-out

    nsenter --target "$SHEPHERD_PID" --net ip link set lo up
    nsenter --target "$SHEPHERD_PID" --net ip link set veth-guix-in up
    nsenter --target "$SHEPHERD_PID" --net ip addr add \
            10.207.87.2/24 dev veth-guix-in
    nsenter --target "$SHEPHERD_PID" --net ip route add \
            default via 10.207.87.1 dev veth-guix-in

    if [ -n "$HOST_SYSTEM_ROOT" ]; then
        # Don't connect to the real net when running in a test environment.
        return
    fi

    for LINKNAME in $(ip route | grep default | awk '{print $5}'); do
        iptables -t nat -A POSTROUTING \
                 -s 10.207.87.1/24 -o "$LINKNAME" -j MASQUERADE
        iptables -t nat -A PREROUTING                   \
                 -i "$LINKNAME" -p tcp                  \
                 -m multiport --dports 25,12525,465,587 \
                 -j DNAT --to-destination 10.207.87.2
    done

    cat /etc/resolv.conf |
        nsenter --target "$SHEPHERD_PID" --all \
                /run/current-system/profile/bin/tee /etc/resolv.conf > /dev/null

    echo 1 > /proc/sys/net/ipv4/ip_forward
}

iptables_rip_rule() {
    while iptables "$@" 2>/dev/null; do
        true
    done
}

network_rip() {
    ip link delete veth-guix-out 2>/dev/null || true

    if [ -n "$HOST_SYSTEM_ROOT" ]; then
        # There's no connection to the real net when running in a test
        # environment.
        return
    fi

    echo 0 > /proc/sys/net/ipv4/ip_forward

    for LINKNAME in $(ip route | grep default | awk '{print $5}'); do
        iptables_rip_rule -t nat -D PREROUTING                   \
                          -i "$LINKNAME" -p tcp                  \
                          -m multiport --dports 25,12525,465,587 \
                          -j DNAT --to-destination 10.207.87.2
        iptables_rip_rule -t nat -D POSTROUTING            \
                          -s 10.207.87.1/24 -o "$LINKNAME" \
                          -j MASQUERADE
    done
}

stop() {
    network_rip

    if ! is_running; then
        return
    fi

    if [ -x /sbin/start-stop-daemon ]; then
        /sbin/start-stop-daemon                                                \
            --stop --signal TERM --pidfile "$PIDFILE" --remove-pidfile --quiet \
            --retry 60  2>/dev/null || true
    else
        DAEMON_PID="$(cat "$PIDFILE")"

        while is_running; do
            kill -TERM "$DAEMON_PID" || true
            printf "Sent TERM, waiting for process to finish\n"
            for I in $(seq 30); do
                if is_running; then
                    sleep 2
                fi
            done
        done

        rm -rf "$PIDFILE"
    fi
}

onexit() {
    if [ -z "$SUCCESS" ]; then
        if [ "x$ACTION" = "xstart" -a -n "$GUILE_PID" ]; then
            stop
            kill $GUILE_PID >/dev/null || true
        fi
        if [ -z "$QUIET_EXIT" ]; then
            log_failure_msg
        fi
    else
        if [ -z "$QUIET_EXIT" ]; then
            log_success_msg
        fi
    fi
}

start() {
    LOG_DIR="$HOST_SYSTEM_ROOT"/var/log/guix-container

    KOSZKO_SIDELOAD_REAL="$HOST_SYSTEM_ROOT"/var/www/koszko.org/html
    HYDRILLA_HTTP_REAL="$HOST_SYSTEM_ROOT"/var/www/hydrilla.koszko.org/html
    HYDRILLAREPOS_HTTP_REAL="$HOST_SYSTEM_ROOT"/var/www/hydrillarepos.koszko.org/html
    LOG_REAL="$LOG_DIR"/container
    ETC_LETSENCRYPT_REAL="$HOST_SYSTEM_ROOT"/etc/letsencrypt
    ETC_EXIM_REAL="$HOST_SYSTEM_ROOT"/etc/exim
    ETC_REAL="$HOST_SYSTEM_ROOT"/etc/guix-container
    VAR_SPOOL_EXIM_REAL="$HOST_SYSTEM_ROOT"/var/spool/exim
    VAR_HYDRILLA_REAL="$HOST_SYSTEM_ROOT"/var/lib/hydrilla
    VAR_GITOLITE_REAL="$HOST_SYSTEM_ROOT"/var/lib/gitolite3
    HOME_REAL="$HOST_SYSTEM_ROOT"/home

    KOSZKO_SIDELOAD_DIR_SHARE_OPT=--share="$KOSZKO_SIDELOAD_REAL"=/srv/http/koszko.org
    HYDRILLA_HTTP_DIR_SHARE_OPT=--share="$HYDRILLA_HTTP_REAL"=/srv/http/hydrilla.koszko.org
    HYDRILLAREPOS_HTTP_DIR_SHARE_OPT=--share="$HYDRILLAREPOS_HTTP_REAL"=/srv/http/hydrillarepos.koszko.org
    LOG_DIR_SHARE_OPT=--share="$LOG_REAL"=/var/log
    ETC_LETSENCRYPT_DIR_SHARE_OPT=--share="$ETC_LETSENCRYPT_REAL"=/etc/letsencrypt
    ETC_EXIM_DIR_SHARE_OPT=--share="$ETC_EXIM_REAL"=/etc/exim
    ETC_DIR_SHARE_OPT=--share="$ETC_REAL"=/etc
    VAR_SPOOL_EXIM_DIR_SHARE_OPT=--share="$VAR_SPOOL_EXIM_REAL"=/var/spool/exim
    VAR_HYDRILLA_DIR_SHARE_OPT=--share="$VAR_HYDRILLA_REAL"=/var/lib/hydrilla
    VAR_GITOLITE_DIR_SHARE_OPT=--share="$VAR_GITOLITE_REAL"=/var/lib/gitolite3
    HOME_DIR_SHARE_OPT=--share="$HOME_REAL"=/home

    mkdir --mode=700 -p "$LOG_DIR"
    mkdir --mode=700 -p "$LOG_DIR"/container

    "$EXECUTABLE" "$KOSZKO_SIDELOAD_DIR_SHARE_OPT"                     \
                  "$HYDRILLA_HTTP_DIR_SHARE_OPT"                       \
                  "$HYDRILLAREPOS_HTTP_DIR_SHARE_OPT"                  \
                  "$LOG_DIR_SHARE_OPT"                                 \
                  "$ETC_LETSENCRYPT_DIR_SHARE_OPT"                     \
                  "$ETC_EXIM_DIR_SHARE_OPT"                            \
                  "$ETC_DIR_SHARE_OPT"                                 \
                  "$VAR_SPOOL_EXIM_DIR_SHARE_OPT"                      \
                  "$VAR_HYDRILLA_DIR_SHARE_OPT"                        \
                  "$VAR_GITOLITE_DIR_SHARE_OPT"                        \
                  "$HOME_DIR_SHARE_OPT"                                \
                  >> "$LOG_DIR"/stdout.log 2>> "$LOG_DIR"/stderr.log &

    GUILE_PID=$!
    WAIT_TIME=0
    SHEPHERD_PID=

    while [ $WAIT_TIME -lt "$MAX_CONTAINER_SPINUP_WAIT" ]; do
        sleep 1
        WAIT_TIME=$((WAIT_TIME + 1))
        SHEPHERD_PID=$(ps -o pid= --ppid $GUILE_PID || true)
        if [ -n "$SHEPHERD_PID" ]; then
            mkdir -p "$(dirname "$PIDFILE")"
            printf '%s' $SHEPHERD_PID > "$PIDFILE"
            break
        fi
    done

    if [ -z "$SHEPHERD_PID" ]; then
        exit 1
    fi

    network_rip

    network_setup "$SHEPHERD_PID"
}

trap onexit EXIT

case "$ACTION" in
    start)
        if is_running; then
            log_daemon_msg "Guix container" "already running"
            log_warning_msg
            QUIET_EXIT=1
        else
            log_daemon_msg "Guix container" "starting"
            start
        fi
        ;;
    stop)
        log_daemon_msg "Guix container" "stopping"
        stop
        ;;
    restart)
        QUIET_EXIT=1
        "$0" stop "$@"
        "$0" start "$@"
        ;;
    reload|force-reload)
        QUIET_EXIT=1
        "$0" stop "$@"
        "$0" start "$@"
        ;;
    status)
        status_of_proc -p "$PIDFILE" "$EXECUTABLE" "Guix container"
        QUIET_EXIT=1
        ;;
    *)
        log_action_msg "Usage: $0 {start|stop|status|restart|reload|force-reload}"
        QUIET_EXIT=1
        exit 2
        ;;
esac

SUCCESS=1

exit 0