diff options
-rwxr-xr-x | manage-baserock | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/manage-baserock b/manage-baserock new file mode 100755 index 0000000..143bced --- /dev/null +++ b/manage-baserock @@ -0,0 +1,118 @@ +#!/bin/bash + +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 |