summaryrefslogtreecommitdiff
path: root/manage-baserock
blob: f1d76110055e58850a3aa6014291744d62e128e0 (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
#!/bin/bash

# manage-baserock -- Tool to manage (add/list/remove etc) Baserock chroots

# 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.

BASEROCK_BASE="${BASEROCK_BASE:-/opt/baserock/chroots}"

ACTION="$1"
SYSTEM="$2"
URL="$3"

check_system_name () {
    local system="$1"
    local valid_chars='A-Za-z0-9_-.'
    case "$system" in
    *[^${valid_chars}]*)
        echo "The specified name '${system}' contains invalid characters."
        echo "Valid characters are ${valid_chars}."
        exit 1
    esac
}

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

list_sessions_for_system () {
    local system="$1"
    schroot --list --all-sessions | grep "^session:baserock-${system}"
}

case "${ACTION}" in
    list)
        for SYSTEM in $(list_baserocks); do
            if test -e "${BASEROCK_BASE}/${SYSTEM}/default"; then
                echo "${SYSTEM} *"
            else
                echo "${SYSTEM}"
            fi
        done
        ;;
    add)
        if test $(id -u) != "0"; then
            exec sudo "$0" "$@"
        fi

        check_system_name "$SYSTEM"

        if test -d "${BASEROCK_BASE}/${SYSTEM}/baserock"; then
            echo "Sorry, $SYSTEM already exists"
            exit 1
        fi

        if test -z "$URL"; then
            echo "Usage: $0 add <name> <url>"
            exit 1
        fi

        mkdir -p "${BASEROCK_BASE}"

        TEMP_BASE=$(mktemp -d --tmpdir="${BASEROCK_BASE}")
        cleanup () { rm -rf "${TEMP_BASE}"; }
        trap cleanup 0

        if test "x${URL}" = "x${URL##http://}" -a "x${URL}" = "x${URL##https://}"; then
            if ! test -e "${URL}"; then
                echo "${URL} is neither a HTTP/HTTPS url nor a path to a local file";
                exit 1;
            fi
            if ! ln -s "$(readlink -e "${URL}")" "${TEMP_BASE}/tarball"; then
                echo "Sorry, could not link ${URL} into place";
                exit 1;
            fi
        else
            if ! wget -O "${TEMP_BASE}/tarball" "$URL"; then
                echo "Sorry, could not download ${URL}";
                exit 1;
            fi
        fi

        mkdir "${TEMP_BASE}/root"
        if ! tar -C "${TEMP_BASE}/root" -xf "${TEMP_BASE}/tarball"; then
            echo "Sorry, could not unpack ${URL}";
            exit 1;
        fi

        ENTRIES=$(ls "${TEMP_BASE}/root" | wc -l | sed -e's/^ *//;s/ *$//')
        if test "x${ENTRIES}" = "x1"; then
            mv "${TEMP_BASE}/root/"* "${BASEROCK_BASE}/${SYSTEM}"
        else
            mv "${TEMP_BASE}/root" "${BASEROCK_BASE}/${SYSTEM}"
        fi

        if ! test -d "${BASEROCK_BASE}/${SYSTEM}/baserock"; then
            echo "Sorry, ${URL} does not appear to contain a baserock system";
            rm -rf "${BASEROCK_BASE}/${SYSTEM}";
            exit 1;
        fi

        NR_BRS=$(list_baserocks | wc -l | sed -e's/^ *//;s/ *$//')
        if test "$NR_BRS" = "1"; then
            touch "${BASEROCK_BASE}/${SYSTEM}/default"
        fi

        if ! test -r "${BASEROCK_BASE}/${SYSTEM}/root/.bashrc"; then
            cat > "${BASEROCK_BASE}/${SYSTEM}/root/.bashrc" <<EOF
PS1="\u@\h(baserock-${SYSTEM}):\w\$ "
EOF
        fi

        if ! test -r "${BASEROCK_BASE}/${SYSTEM}/root/.bash_profile"; then
            cat > "${BASEROCK_BASE}/${SYSTEM}/root/.bash_profile" <<EOF
. ~/.bashrc
EOF
        fi

        if ! test -d "${BASEROCK_BASE}/${SYSTEM}/src"; then
            mkdir "${BASEROCK_BASE}/${SYSTEM}/src"
        fi

        br-ct-sync-chroots

        ;;
    rm)
        if test $(id -u) != "0"; then
            exec sudo "$0" "$@"
        fi

        if ! test -d "${BASEROCK_BASE}/${SYSTEM}/baserock"; then
            echo "Sorry, $SYSTEM does not exist or is not a baserock chroot"
            exit 1
        fi

        sessions="$(list_sessions_for_system $SYSTEM)"
        if ! test -z $sessions; then
            echo "I found the following active sessions for chroot '$SYSTEM':"
            echo
            echo "$sessions"
            echo
            echo "You might lose data in mounted filesystems if I were to"
            echo "recursively delete path ${BASEROCK_BASE}/${SYSTEM}."
            echo
            echo "If you think these sessions are stale, you can run the "
            echo "following command to end ALL schroot sessions:"
            echo
            echo "    schroot --end-session --all-sessions"
            exit 1
        fi

        rm -rf "${BASEROCK_BASE}/${SYSTEM}"
        br-ct-sync-chroots
        ;;
    set-default)
        if test $(id -u) != "0"; then
            exec sudo "$0" "$@"
        fi

        if ! test -d "${BASEROCK_BASE}/${SYSTEM}/baserock"; then
            echo "Sorry, $SYSTEM does not exist or is not a baserock chroot"
            exit 1
        fi
        
        rm -f "${BASEROCK_BASE}/"*/default
        touch "${BASEROCK_BASE}/${SYSTEM}/default"
        br-ct-sync-chroots
        ;;
    *)
        echo >&2 "usage: $0 list | add <name> <url> | rm <name> | set-default <name>"
        exit 1
        ;;
esac