Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 245 lines (228 sloc) 5.72 KB
#!/bin/sh
set -e
if [ "$1" = "--debug" ]; then
shift
DEBUG=echo
fi
# settings
APT="apt-get --assume-yes"
ACCOUNT=jcdubacq
HOSTINSTALL=fiona
ACCOUNTHOME=$(getent passwd $ACCOUNT|cut -f6 -d:)
INSTALL=$(dirname "$0")
if [ -x /usr/bin/realpath ]; then
INSTALL=$(realpath "$INSTALL")
fi
bold=$(tput smso)
offbold=$(tput rmso)
cd /etc
export ACCOUNT ACCOUNTHOME INSTALL REVERT HOSTINSTALL APT
# run
mkdir -p /var/lib/INSTALL
checkPackage() {
local PKG
local REVERT
local PKGDIR
local ACTION
local INSTALLED
PKG="$1"
REVERT="$2"
PKGDIR="$3"
ACTION="$4"
INSTALLED="/var/lib/INSTALL/${PKG}"
if [ "$REVERT" = "0" ]; then
CURRENT=$(find "$PKGDIR" -type f -print0 |xargs -0 md5sum|md5sum -|cut -c1-32)
else
CURRENT="XXX"
fi
if [ -f "${INSTALLED}" ]; then
VALUE=$(cat "${INSTALLED}")
else
VALUE="XXX"
fi
if [ "$ACTION" = "test" ]; then
if [ "$VALUE" != "$CURRENT" ]; then
return 1
else
return 0
fi
elif [ "$ACTION" = "stamp" ]; then
if [ "$CURRENT" = "XXX" ]; then
rm -f "${INSTALLED}"
else
echo -n "$CURRENT" > "$INSTALLED"
fi
else
die "Unknown action $ACTION"
fi
}
chapter() {
echo "${bold}[$@]${offbold}"
}
runtag() {
local TAG
local REVERT
local CMD
local PKG
TAG="$1"
REVERT="$2"
TAGDIR="${INSTALL}/tags"
if [ "$TAG" = "basic" ]&&[ "$REVERT" = 1 ]; then
echo "Basic tasks really shouldn't be removed"
echo "Please remove them individually"
tac ${TAGDIR}/basic|xargs echo
return
fi
if [ "$REVERT" = 1 ]; then
CMD=tac
else
CMD=cat
fi
if [ -f "${TAGDIR}/${TAG}" ]; then
$CMD "${TAGDIR}/${TAG}"| while read PKG; do
runitreal "$PKG" "$REVERT"
done
fi
}
runit() {
local TAG
local REVERT
TAG="$1"
REVERT=0
if [ "${TAG%-}" != "${TAG}" ]; then
TAG="${TAG%-}"
REVERT=1
fi
runitreal "$TAG" "$REVERT"
}
runitreal() {
local TAG
local REVERT
local PKGDIR
local TAGS
local PKGDIR
local PKG
TAG="$1"
REVERT="$2"
if [ "${TAG#tag:}" != "${TAG}" ]; then
runtag "${TAG#tag:}" "$REVERT"
else
find "${INSTALL}/lib" -type d -maxdepth 1 -mindepth 1 -print\
|while read PKG; do
PKG=$(basename "$PKG")
TAGS=" ${PKG} "
PKGDIR="$INSTALL/lib/$PKG"
if [ -f "$PKGDIR/TAGS" ]; then
for NEWTAG in $(cat "$PKGDIR/TAGS"); do
TAGS="${TAGS}${NEWTAG} "
done
fi
if [ "${TAGS#* ${TAG} }" != "${TAGS}" ]; then
# The package has the requisited tag
checkPackage "$PKG" "$REVERT" "$PKGDIR" "test" \
&& echo "$PKG already in requisited state" \
|| execPackage "$PKG" "$REVERT" "$PKGDIR"
fi
done
fi
}
execPackage() {
local PKG
local REVERT
local PKGDIR
local FILE
local COMMENT
local FILEDIR
local TARGET
local DEP
PKG="$1"
REVERT="$2"
PKGDIR="$3"
if [ -f "${PKGDIR}/DEPS" ]; then
chapter "${PKG}: Processing dependencies"
for DEP in $(cat "${PKGDIR}/DEPS"); do
runit "$DEP"
done
fi
if [ -f "${PKGDIR}/README" ]; then
COMMENT="${PKG}: $(cat < "${PKGDIR}/README")"
else
COMMENT="${PKG}"
fi
[ "$REVERT" = 0 ]||COMMENT="Removing ${COMMENT}"
chapter "$COMMENT"
find "$PKGDIR" -type f -print |sort| while read FILE; do
TARGET="/${FILE#${PKGDIR}/}"
THISFILE="$FILE"; export THISFILE
FILEDIR=$(dirname "$TARGET")
case "$TARGET" in
/README|/KEEPDIR|/TAGS|/DEPS)
true;;
/_PRERUN|/POSTRUN|*._run)
[ "$REVERT" = 0 ] && $DEBUG sh "$FILE" || true;;
/_PREREV|/POSTREV|*._rev)
[ "$REVERT" = 1 ] && $DEBUG sh "$FILE" || true;;
/etc/*|/root/*)
if [ "$REVERT" = 1 ]; then
echo "Removing $TARGET"
$DEBUG rm -- "$TARGET"
[ -f "$PKGDIR/KEEPDIR" ] && grep -q "^$FILEDIR$" "$PKGDIR/KEEPDIR" \
&& echo "Keeping $FILEDIR" \
|| $DEBUG rmdir --ignore-fail-on-non-empty "$FILEDIR"
else
echo "Copying $FILE to $TARGET"
$DEBUG mkdir -p "$FILEDIR"
$DEBUG cp -- "$FILE" "$TARGET"
fi
if [ "${FILE%.crypt}" != "${FILE}" ]; then
$DEBUG /usr/bin/decryptfile "$TARGET"
fi
;;
esac
done
$DEBUG etckeeper commit "$COMMENT"||true
$DEBUG checkPackage "$PKG" "$REVERT" "$PKGDIR" stamp
return 0
}
# doing
if [ -z "$1" ]; then
runit "tag:$(hostname -s)"
else
runit "$1"
fi
exit 0
if [ "$REVERT" = 0 ]; then
chapter "Checking basic packages"
${APT} install debconf-utils
${APT} install autossh
${APT} install gnome
${APT} install texlive-full
${APT} install vim
${APT} --purge remove nano
chapter "Initializing /etc monitoring"
${APT} install etckeeper
rm -rf /etc/.git
etckeeper init
etckeeper commit "Initial commit"
runit record-manual
runit network
runit repository-dubacq
runit repository-sidexp
runit superusers
runit fix-groups
runit postfix
fi
if [ "$REVERT" = 1 ]; then
runit network
runit superusers
runit fix-groups
runit postfix
runit repository-dubacq
runit repository-sidexp
runit record-manual
chapter "Destroying /etc monitoring"
git checkout $(git log |grep commit|tail -n 1|cut -f2 -d' ')
git status
echo rm -rf /etc/.git
echo rm -rf /root/.gitconfig
fi