File: clustering.sh

package info (click to toggle)
incus 6.0.5-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,788 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (304 lines) | stat: -rw-r--r-- 7,337 bytes parent folder | download | duplicates (2)
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
# Test helper for clustering

setup_clustering_bridge() {
    name="br$$"

    echo "==> Setup clustering bridge ${name}"

    ip link add "${name}" up type bridge
    ip addr add 100.64.1.1/16 dev "${name}"

    iptables -w -t nat -A POSTROUTING -s 100.64.0.0/16 -d 0.0.0.0/0 -j MASQUERADE
    echo 1 > /proc/sys/net/ipv4/ip_forward
}

teardown_clustering_bridge() {
    name="br$$"

    if [ -e "/sys/class/net/${name}" ]; then
        echo "==> Teardown clustering bridge ${name}"
        echo 0 > /proc/sys/net/ipv4/ip_forward
        iptables -w -t nat -D POSTROUTING -s 100.64.0.0/16 -d 0.0.0.0/0 -j MASQUERADE
        ip link del dev "${name}"
    fi
}

setup_clustering_netns() {
    id="${1}"
    shift

    prefix="inc$$"
    ns="${prefix}${id}"

    echo "==> Setup clustering netns ${ns}"

    cat << EOF | unshare -m -n /bin/sh
set -e
mkdir -p "${TEST_DIR}/ns/${ns}"
touch "${TEST_DIR}/ns/${ns}/net"
mount -o bind /proc/self/ns/net "${TEST_DIR}/ns/${ns}/net"
mount --move /sys /mnt
umount -l /proc
mount -t sysfs sysfs /sys
mount --move /mnt/fs/cgroup /sys/fs/cgroup
mount -t proc proc /proc
mount -t securityfs securityfs /sys/kernel/security
umount -l /mnt

# Setup host netns access
mkdir -p /run/netns
mount -t tmpfs tmpfs /run/netns
touch /run/netns/hostns
mount --bind /proc/1/ns/net /run/netns/hostns

mount -t tmpfs tmpfs /usr/local/bin
cat << EOE > /usr/local/bin/in-hostnetns
#!/bin/sh
exec ip netns exec hostns /usr/bin/\\\$(basename \\\$0) "\\\$@"
EOE
chmod +x /usr/local/bin/in-hostnetns
# Setup ceph
ln -s in-hostnetns /usr/local/bin/ceph
ln -s in-hostnetns /usr/local/bin/rbd

sleep 300&
echo \$! > "${TEST_DIR}/ns/${ns}/PID"
EOF

    veth1="v${ns}1"
    veth2="v${ns}2"
    nspid=$(cat "${TEST_DIR}/ns/${ns}/PID")

    ip link add "${veth1}" type veth peer name "${veth2}"
    ip link set "${veth2}" netns "${nspid}"

    nsbridge="br$$"
    ip link set dev "${veth1}" master "${nsbridge}" up
    cat << EOF | nsenter -n -m -t "${nspid}" /bin/sh
set -e

ip link set dev lo up
ip link set dev "${veth2}" name eth0
ip link set eth0 up
ip addr add "100.64.1.10${id}/16" dev eth0
ip route add default via 100.64.1.1
EOF
}

teardown_clustering_netns() {
    prefix="inc$$"
    nsbridge="br$$"

    [ ! -d "${TEST_DIR}/ns/" ] && return

    # shellcheck disable=SC2045
    for ns in $(ls -1 "${TEST_DIR}/ns/"); do
        echo "==> Teardown clustering netns ${ns}"

        veth1="v${ns}1"
        if [ -e "/sys/class/net/${veth1}" ]; then
            ip link del "${veth1}"
        fi

        pid="$(cat "${TEST_DIR}/ns/${ns}/PID")"
        kill -9 "${pid}" 2> /dev/null || true

        umount -l "${TEST_DIR}/ns/${ns}/net" > /dev/null 2>&1 || true
        rm -Rf "${TEST_DIR}/ns/${ns}"
    done
}

spawn_incus_and_bootstrap_cluster() {
    # shellcheck disable=SC2039,SC3043
    local INCUS_NETNS

    set -e
    ns="${1}"
    bridge="${2}"
    INCUS_DIR="${3}"
    driver="dir"
    port=""
    if [ "$#" -ge "4" ]; then
        driver="${4}"
    fi
    if [ "$#" -ge "5" ]; then
        port="${5}"
    fi

    echo "==> Spawn bootstrap cluster node in ${ns} with storage driver ${driver}"

    INCUS_NETNS="${ns}" spawn_incus "${INCUS_DIR}" false
    (
        set -e

        cat > "${INCUS_DIR}/preseed.yaml" << EOF
config:
  core.https_address: 100.64.1.101:8443
EOF
        if [ "${port}" != "" ]; then
            cat >> "${INCUS_DIR}/preseed.yaml" << EOF
  cluster.https_address: 100.64.1.101:${port}
EOF
        fi
        cat >> "${INCUS_DIR}/preseed.yaml" << EOF
  images.auto_update_interval: 0
storage_pools:
- name: data
  driver: $driver
EOF
        if [ "${driver}" = "btrfs" ]; then
            cat >> "${INCUS_DIR}/preseed.yaml" << EOF
  config:
    size: 1GiB
EOF
        fi
        if [ "${driver}" = "zfs" ]; then
            cat >> "${INCUS_DIR}/preseed.yaml" << EOF
  config:
    size: 1GiB
    zfs.pool_name: incustest-$(basename "${TEST_DIR}")-${ns}
EOF
        fi
        if [ "${driver}" = "lvm" ]; then
            cat >> "${INCUS_DIR}/preseed.yaml" << EOF
  config:
    volume.size: 25MiB
    size: 1GiB
    lvm.vg_name: incustest-$(basename "${TEST_DIR}")-${ns}
EOF
        fi
        if [ "${driver}" = "ceph" ]; then
            cat >> "${INCUS_DIR}/preseed.yaml" << EOF
  config:
    source: incustest-$(basename "${TEST_DIR}")
    volume.size: 25MiB
    ceph.osd.pg_num: 16
EOF
        fi
        cat >> "${INCUS_DIR}/preseed.yaml" << EOF
networks:
- name: $bridge
  type: bridge
  config:
    ipv4.address: none
    ipv6.address: none
profiles:
- name: default
  devices:
    root:
      path: /
      pool: data
      type: disk
cluster:
  server_name: node1
  enabled: true
EOF
        incus admin init --preseed < "${INCUS_DIR}/preseed.yaml"
    )
}

spawn_incus_and_join_cluster() {
    # shellcheck disable=SC2039,SC3043
    local INCUS_NETNS

    set -e
    ns="${1}"
    bridge="${2}"
    cert="${3}"
    index="${4}"
    target="${5}"
    INCUS_DIR="${6}"
    if [ -d "${7}" ]; then
        token="$(INCUS_DIR=${7} incus cluster add "node${index}" --quiet)"
    else
        token="${7}"
    fi
    driver="dir"
    port="8443"
    if [ "$#" -ge "8" ]; then
        driver="${8}"
    fi
    if [ "$#" -ge "9" ]; then
        port="${9}"
    fi

    echo "==> Spawn additional cluster node in ${ns} with storage driver ${driver}"

    INCUS_NETNS="${ns}" spawn_incus "${INCUS_DIR}" false
    (
        set -e

        # If a custom cluster port was given, we need to first set the REST
        # API address.
        if [ "${port}" != "8443" ]; then
            incus config set core.https_address "100.64.1.10${index}:8443"
        fi

        cat > "${INCUS_DIR}/preseed.yaml" << EOF
cluster:
  enabled: true
  server_name: node${index}
  server_address: 100.64.1.10${index}:${port}
  cluster_address: 100.64.1.10${target}:8443
  cluster_certificate: "$cert"
  cluster_token: ${token}
  member_config:
EOF
        # Declare the pool only if the driver is not ceph, because
        # the ceph pool doesn't need to be created on the joining
        # node (it's shared with the bootstrap one).
        if [ "${driver}" != "ceph" ]; then
            cat >> "${INCUS_DIR}/preseed.yaml" << EOF
  - entity: storage-pool
    name: data
    key: source
    value: ""
EOF
            if [ "${driver}" = "zfs" ]; then
                cat >> "${INCUS_DIR}/preseed.yaml" << EOF
  - entity: storage-pool
    name: data
    key: zfs.pool_name
    value: incustest-$(basename "${TEST_DIR}")-${ns}
  - entity: storage-pool
    name: data
    key: size
    value: 1GiB
EOF
            fi
            if [ "${driver}" = "lvm" ]; then
                cat >> "${INCUS_DIR}/preseed.yaml" << EOF
  - entity: storage-pool
    name: data
    key: lvm.vg_name
    value: incustest-$(basename "${TEST_DIR}")-${ns}
  - entity: storage-pool
    name: data
    key: size
    value: 1GiB
EOF
            fi
            if [ "${driver}" = "btrfs" ]; then
                cat >> "${INCUS_DIR}/preseed.yaml" << EOF
  - entity: storage-pool
    name: data
    key: size
    value: 1GiB
EOF
            fi
        fi

        incus admin init --preseed < "${INCUS_DIR}/preseed.yaml"
    )
}

respawn_incus_cluster_member() {
    # shellcheck disable=SC2039,SC2034,SC3043
    local INCUS_NETNS

    set -e
    ns="${1}"
    INCUS_DIR="${2}"

    INCUS_NETNS="${ns}" respawn_incus "${INCUS_DIR}" true
}