Skip to content

Commit

Permalink
Add tags support
Browse files Browse the repository at this point in the history
  • Loading branch information
jcdubacq committed Mar 13, 2013
1 parent 0c11784 commit 1e75ad1
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 69 deletions.
6 changes: 1 addition & 5 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ Turning this program in a real program
Managing several computers
--------------------------

* add _tag_ lists
* remove order.run order.revert
* automatic dependency on tag:basic and tag:common
* reject removing tag:basic (can be done individually)
* default action is to do tag:hostname

A _tag list_ is just a dependency beginning with tag:, maybe tag- or
tag/. All these dependencies are processed as another dependency file
(read lines one by one, run line).
(read lines one by one, run line).
141 changes: 89 additions & 52 deletions enforce_config
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/sh
set -e
cd /etc
if [ "$1" = "--debug" ]; then
shift
DEBUG=echo
Expand All @@ -13,9 +12,12 @@ 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)
printf '[user]\n\tname = Jean-Christophe Dubacq (as root)\n\temail = jean-christophe.dubacq@ens-lyon.org\n' > /root/.gitconfig
cd /etc

export ACCOUNT ACCOUNTHOME INSTALL REVERT HOSTINSTALL APT

Expand Down Expand Up @@ -66,39 +68,74 @@ chapter() {
echo "${bold}[$@]${offbold}"
}

runit() {
runtag() {
local TAG
local REVERT
local PKGDIR
local TAGS
local PKGDIR
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
if [ "$REVERT" = 1 ]; then
LIST=order.revert
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
LIST=order.run
fi
while read PKG; do
TAGS=" all ${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
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 < $INSTALL/$LIST
checkPackage "$PKG" "$REVERT" "$PKGDIR" "test" \
&& echo "$PKG already in requisited state" \
|| execPackage "$PKG" "$REVERT" "$PKGDIR"
fi
done
fi
}

execPackage() {
Expand Down Expand Up @@ -127,33 +164,33 @@ execPackage() {
[ "$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
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
Expand All @@ -163,7 +200,7 @@ execPackage() {
# doing

if [ -z "$1" ]; then
runit all
runit "tag:$(hostname -s)"
else
runit "$1"
fi
Expand Down
3 changes: 3 additions & 0 deletions lib/record-manual/_PREREV
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
git config --unset user.name
git config --unset user.email
7 changes: 7 additions & 0 deletions lib/record-manual/_PRERUN
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#! /bin/sh
if [ -x /usr/bin/git ]; then
git config user.name 'Jean-Christophe Dubacq (as root)'
git config user.email 'jean-christophe.dubacq@ens-lyon.org'
else
printf '[user]\n\tname = Jean-Christophe Dubacq (as root)\n\temail = jean-christophe.dubacq@ens-lyon.org\n' > /root/.gitconfig
fi
7 changes: 0 additions & 7 deletions order.run

This file was deleted.

8 changes: 3 additions & 5 deletions order.revert → tags/basic
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
record-manual
fix-groups
network-manager
repository-sidexp
cryptstore
repository-dubacq
superusers
record-manual
cryptstore
network-manager
3 changes: 3 additions & 0 deletions tags/oberon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tag:basic
superusers
repository-sidexp

0 comments on commit 1e75ad1

Please sign in to comment.