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

# br-ct-sync-chroots -- Synchronise Baserock chroots and schroot configuration

# Copyright (C) 2014  Codethink Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

ACTION="$1"

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

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 "profile=baserock-$entry"
        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 "^profile" "${SCHROOT_CONF}" | grep baserock | sed -e's at profile *= *@@'
}

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

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"
NSSDATABASES="${SCHROOT_BASE}/baserock-${entry}/nssdatabases"
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
${SRC_BASE}     /src            none    rw,bind         0       0
EOF
            cat > "${SCHROOT_BASE}/baserock-${entry}/copyfiles" <<'EOF'
/etc/resolv.conf
EOF
            cat > "${SCHROOT_BASE}/baserock-${entry}/nssdatabases" <<'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