From 9354abc3c1303054fe11f90c89a685803e42678c Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Fri, 28 Feb 2014 09:32:16 +0000 Subject: Add tool to simplify managing baserock chroots --- manage-baserock | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100755 manage-baserock 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" < "${BASEROCK_BASE}/${SYSTEM}/root/.bash_profile" <&2 "usage: $0 list | add | rm | set-default " + exit 1 + ;; +esac -- cgit v1.2.1