summaryrefslogtreecommitdiff
path: root/br-ct-sync-chroots
blob: b6b38fc15c41314338d61e8bb635cf5cc6af2f90 (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
#!/bin/bash

ACTION="$1"

SCHROOT_BASE="${SCHROOT_BASE:-/etc/schroot}"
SCHROOT_CONF="${SCHROOT_CONF:-${SCHROOT_BASE}/schroot.conf}"
BASEROCK_BASE="${BASEROCK_BASE:-/opt/baserock/chroots}"

clear_entries () {
    sed -e'/BASEROCK_ENTRIES_BEGIN/,/BASEROCK_ENTRIES_END/d' -i "${SCHROOT_CONF}"
}

find_default () {
    grep "^aliases" "$SCHROOT_CONF" | grep -q "default"
}

add_conf_line () {
    echo "$@" >> "${SCHROOT_CONF}"
}

list_baserocks () {
    (cd "${BASEROCK_BASE}" && ls -d */baserock) 2>/dev/null | sed -e's@/baserock$@@'
}

add_entries () {
    add_conf_line "# BASEROCK_ENTRIES_BEGIN"
    while read entry; do
        add_conf_line "[baserock-$entry]"
        add_conf_line "type=directory"
        add_conf_line "directory=${BASEROCK_BASE}/${entry}"
        add_conf_line "description=Baserock in $entry"
        add_conf_line "users=root"
        add_conf_line "script-config=baserock-$entry/config"
        if ! find_default; then
            test -e "${BASEROCK_BASE}/$entry/default" && add_conf_line "aliases=default"
        fi
    done < <(list_baserocks)
    add_conf_line "# BASEROCK_ENTRIES_END"
}

find_configs () {
    (cd "${SCHROOT_BASE}"; ls -d baserock-*/config) 2>/dev/null | sed -e's@/config$@@'
}

find_used_configs () {
    grep "^script-config" "${SCHROOT_CONF}" | grep baserock | sed -e's@script-config *= *@@'
}

is_config_used () {
    grep "script-config *= *" "${SCHROOT_CONF}" | grep -q -F "$1/config"
}

add_new_configs () {
    for entry in $(list_baserocks); do
        if ! test -e "${SCHROOT_BASE}/baserock-${entry}/config"; then
            mkdir -p "${SCHROOT_BASE}/baserock-${entry}"
            cat > "${SCHROOT_BASE}/baserock-${entry}/config" <<EOF
FSTAB="${SCHROOT_BASE}/baserock-${entry}/fstab"
COPYFILES="${SCHROOT_BASE}/baserock-${entry}/copyfiles"
NSSDATABASE="${SCHROOT_BASE}/baserock-${entry}/fstab"
EOF
            cat > "${SCHROOT_BASE}/baserock-${entry}/fstab" <<'EOF'
/proc           /proc           none    rw,bind        0       0
/sys            /sys            none    rw,bind        0       0
/dev            /dev            none    rw,bind         0       0
/dev/pts        /dev/pts        none    rw,bind         0       0
/tmp            /tmp            none    rw,bind         0       0
EOF
            cat > "${SCHROOT_BASE}/baserock-${entry}/copyfiles" <<'EOF'
/etc/resolv.conf
EOF
            cat > "${SCHROOT_BASE}/baserock-${entry}/nssdatabase" <<'EOF'
networks
hosts
EOF
        fi
    done
}

clear_unused_configs () {
    while read config; do
        if ! is_config_used "$config"; then
            rm -rf "${SCHROOT_BASE}/${config}"
        fi
    done < <(find_configs)
}

case "$ACTION" in
    purge)
        clear_entries
        clear_unused_configs
        ;;
    *)
        clear_entries
        add_entries
        add_new_configs
        clear_unused_configs
        ;;
esac