summaryrefslogtreecommitdiff
path: root/manage-baserock
blob: d45a6b217331b6951c088182b3d2492208990aa0 (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
#!/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=$(echo $2 | tr -c -d A-Za-z0-9_-)
URL="$3"

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

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

        if test -d "${BASEROCK_BASE}/${SYSTEM}/baserock"; then
            echo "Sorry, $SYSTEM already exists"
            exit 1
        fi
        
        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 ! 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

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