Skip to content

Instantly share code, notes, and snippets.

@komeda-shinji
Last active December 28, 2020 18:26
Show Gist options
  • Save komeda-shinji/42f8ccce59258430d8a175f68e4bbad0 to your computer and use it in GitHub Desktop.
Save komeda-shinji/42f8ccce59258430d8a175f68e4bbad0 to your computer and use it in GitHub Desktop.
AWS CloudShell etckeeper patch
diff --git a/Makefile b/Makefile
index 73d2411..48e19af 100644
--- a/Makefile
+++ b/Makefile
@@ -3,8 +3,10 @@
CONFFILE=etckeeper.conf
include $(CONFFILE)
-DESTDIR?=
-prefix=/usr
+#DESTDIR?=
+DESTDIR?=${HOME}
+#prefix=/usr
+prefix=
bindir=${prefix}/bin
etcdir=/etc
mandir=${prefix}/share/man
diff --git a/commit.d/10vcs-test b/commit.d/10vcs-test
index e33d734..8e32a94 100755
--- a/commit.d/10vcs-test
+++ b/commit.d/10vcs-test
@@ -6,7 +6,7 @@ not_enabled_warning() {
echo "etckeeper warning: run etckeeper init to enable it" >&2
}
-if [ "$VCS" = git ] && [ ! -d .git ]; then
+if [ "$VCS" = git ] && [ ! -e .git ]; then
not_enabled_warning
elif [ "$VCS" = hg ] && [ ! -d .hg ]; then
not_enabled_warning
diff --git a/commit.d/30git-add b/commit.d/30git-add
index b08b583..e55e386 100755
--- a/commit.d/30git-add
+++ b/commit.d/30git-add
@@ -1,7 +1,7 @@
#!/bin/sh
set -e
-if [ "$VCS" = git ] && [ -d .git ]; then
+if [ "$VCS" = git ] && [ -e .git ]; then
if ! git add --all; then
echo "etckeeper warning: git add --all" >&2
fi
diff --git a/commit.d/50vcs-commit b/commit.d/50vcs-commit
index ca02479..1f3c42a 100755
--- a/commit.d/50vcs-commit
+++ b/commit.d/50vcs-commit
@@ -39,7 +39,7 @@ else
fi
fi
-if [ "$VCS" = git ] && [ -d .git ]; then
+if [ "$VCS" = git ] && [ -e .git ]; then
# When not su'd to root, still set environment variables,
# since git's own code to determine the author and committer
# has several edge cases where it fails and would prevent the
diff --git a/commit.d/99push b/commit.d/99push
index 3df1350..c3f14f9 100755
--- a/commit.d/99push
+++ b/commit.d/99push
@@ -1,6 +1,6 @@
#!/bin/sh
if [ -n "$PUSH_REMOTE" ]; then
- if [ "$VCS" = git ] && [ -d .git ]; then
+ if [ "$VCS" = git ] && [ -e .git ]; then
for REMOTE in $PUSH_REMOTE; do
git push "$REMOTE" || true
done
diff --git a/daily b/daily
index f98c6ad..cbbf7ab 100755
--- a/daily
+++ b/daily
@@ -1,17 +1,17 @@
#!/bin/sh
# Script that can be run daily to autocommit /etc changes.
set -e
-if [ -x /usr/bin/etckeeper ] && [ -e /etc/etckeeper/etckeeper.conf ]; then
+if [ -x /home/cloudshell-user/bin/etckeeper ] && [ -e /home/cloudshell-user/etc/etckeeper/etckeeper.conf ]; then
# avoid autocommit if an install run is in progress
- lockfile=/var/cache/etckeeper/packagelist.pre-install
+ lockfile=/home/cloudshell-user/var/cache/etckeeper/packagelist.pre-install
if [ -e "$lockfile" ] && [ -n "$(find "$lockfile" -mtime +1)" ]; then
rm -f "$lockfile" # stale
fi
if [ ! -e "$lockfile" ]; then
AVOID_SPECIAL_FILE_WARNING=1
export AVOID_SPECIAL_FILE_WARNING
- if etckeeper unclean; then
- etckeeper commit "daily autocommit" >/dev/null
+ if /home/cloudshell-user/bin/etckeeper unclean; then
+ /home/cloudshell-user/bin/etckeeper commit "daily autocommit" >/dev/null
fi
fi
fi
diff --git a/etckeeper b/etckeeper
index 25e6568..111644c 100755
--- a/etckeeper
+++ b/etckeeper
@@ -2,7 +2,7 @@
set -e
if [ -z "$ETCKEEPER_CONF_DIR" ]; then
- ETCKEEPER_CONF_DIR=/etc/etckeeper
+ ETCKEEPER_CONF_DIR=/home/cloudshell-user/etc/etckeeper
fi
conf="$ETCKEEPER_CONF_DIR/etckeeper.conf"
diff --git a/etckeeper.conf b/etckeeper.conf
index f13de1c..ec467f4 100644
--- a/etckeeper.conf
+++ b/etckeeper.conf
@@ -33,11 +33,11 @@ DARCS_COMMIT_OPTIONS="-a"
# The high-level package manager that's being used.
# (apt, pacman, pacman-g2, yum, dnf, zypper, apk etc)
-HIGHLEVEL_PACKAGE_MANAGER=apt
+HIGHLEVEL_PACKAGE_MANAGER=yum
# The low-level package manager that's being used.
# (dpkg, rpm, pacman, pacmatic, pacman-g2, apk etc)
-LOWLEVEL_PACKAGE_MANAGER=dpkg
+LOWLEVEL_PACKAGE_MANAGER=rpm
# To push each commit to a remote, put the name of the remote here.
# (eg, "origin" for git). Space-separated lists of multiple remotes
diff --git a/init.d/20restore-etckeeper b/init.d/20restore-etckeeper
index 0485e63..6ba30cf 100755
--- a/init.d/20restore-etckeeper
+++ b/init.d/20restore-etckeeper
@@ -1,6 +1,8 @@
#!/bin/sh
set -e
+ETCKEEPERROOT=/home/cloudshell-user/etckeeper
+
# Used by .etckeeper to run a command if the file it acts on
# (the last parameter) exists.
maybe () {
@@ -14,9 +16,9 @@ maybe () {
# Yes, this runs code from the repository. As documented, etckeeper-init
# should only be run on repositories you trust.
-if [ -e .etckeeper ]; then
- . ./.etckeeper
+if [ -e $ETCKEEPERROOT/.etckeeper ]; then
+ . $ETCKEEPERROOT/.etckeeper
else
- touch .etckeeper
- chmod 600 .etckeeper
+ touch $ETCKEEPERROOT/.etckeeper
+ chmod 600 $ETCKEEPERROOT/.etckeeper
fi
diff --git a/init.d/50vcs-ignore b/init.d/50vcs-ignore
index 33d79d3..ff5925d 100755
--- a/init.d/50vcs-ignore
+++ b/init.d/50vcs-ignore
@@ -1,4 +1,4 @@
#!/bin/sh
set -e
-etckeeper update-ignore -a || true
+/home/cloudshell-user/bin/etckeeper update-ignore -a || true
diff --git a/init.d/50vcs-pre-commit-hook b/init.d/50vcs-pre-commit-hook
index 6045981..bd1e707 100755
--- a/init.d/50vcs-pre-commit-hook
+++ b/init.d/50vcs-pre-commit-hook
@@ -3,18 +3,23 @@ set -e
case "$VCS" in
git)
- if [ -x .git/hooks/pre-commit ]; then
- if ! grep -q "etckeeper pre-commit" .git/hooks/pre-commit; then
- echo "etckeeper warning: .git/hooks/pre-commit needs to be manually modified to run: etckeeper pre-commit -d `pwd`" >&2
+ if [ -f .git ]; then
+ gitdir=$(sed -n '/gitdir: */s///p' .git)
+ else
+ gitdir=.git
+ fi
+ if [ -x $gitdir/hooks/pre-commit ]; then
+ if ! grep -q "etckeeper pre-commit" $gitdir/hooks/pre-commit; then
+ echo "etckeeper warning: $gitdir/hooks/pre-commit needs to be manually modified to run: etckeeper pre-commit -d `pwd`" >&2
fi
else
- cat >.git/hooks/pre-commit <<EOF
+ cat >$gitdir/hooks/pre-commit <<EOF
#!/bin/sh
# pre-commit hook for etckeeper, to store metadata and do sanity checks
set -e
-etckeeper pre-commit -d `pwd`
+/home/cloudshell-user/bin/etckeeper pre-commit -d `pwd`
EOF
- chmod +x .git/hooks/pre-commit
+ chmod +x $gitdir/hooks/pre-commit
fi
;;
hg)
diff --git a/init.d/60darcs-deleted-symlinks b/init.d/60darcs-deleted-symlinks
index 9c34e95..a3a4804 100755
--- a/init.d/60darcs-deleted-symlinks
+++ b/init.d/60darcs-deleted-symlinks
@@ -1,6 +1,8 @@
#!/bin/sh
set -e
+ETCKEEPERROOT=/home/cloudshell-user/etckeeper
+
filter_ignore() {
if [ "$VCS" = darcs ]; then
ignorefile=.darcsignore
@@ -28,9 +30,9 @@ if [ "$VCS" = darcs ];then
NOVCS='. -path ./.git -prune -o -path ./.bzr -prune -o -path ./.hg -prune -o -path ./_darcs -prune -o'
# We assume that if .etckeeper is empty this is the first run
- if [ -s .etckeeper ]; then
+ if [ -s $ETCKEEPERROOT/.etckeeper ]; then
linksindex="$( mktemp -t etckeeper-$VCS.XXXXXXXXXX )"
- grep '^ln -s' .etckeeper | while IFS="'" read n n n link n; do
+ grep '^ln -s' $ETCKEEPERROOT/.etckeeper | while IFS="'" read n n n link n; do
printf "%s\n" "$link" >> "$linksindex"
done
diff --git a/post-install.d/50vcs-commit b/post-install.d/50vcs-commit
index e8fa4fc..e241f8c 100755
--- a/post-install.d/50vcs-commit
+++ b/post-install.d/50vcs-commit
@@ -1,7 +1,7 @@
#!/bin/sh
set -e
-pl="/var/cache/etckeeper/packagelist"
+pl="/home/cloudshell-user/var/cache/etckeeper/packagelist"
# Parent process is etckeeper
# (Only procps ps is currently supported, others will fail,
@@ -51,7 +51,7 @@ get_changed_packages () {
fi
}
-if etckeeper unclean; then
+if /home/cloudshell-user/bin/etckeeper unclean; then
if [ -z "${ETCKEEPER_PARENT_COMMAND_LINE}" ]; then
message="committing changes in /etc after $HIGHLEVEL_PACKAGE_MANAGER run"
else
@@ -59,14 +59,14 @@ if etckeeper unclean; then
fi
set +e
- if [ -e $pl.pre-install ] && [ "$(cat $pl.fmt 2>/dev/null || true)" = "$(etckeeper list-installed fmt)" ]; then
+ if [ -e $pl.pre-install ] && [ "$(cat $pl.fmt 2>/dev/null || true)" = "$(/home/cloudshell-user/bin/etckeeper list-installed fmt)" ]; then
(
echo "$message"
echo
get_changed_packages | sort | uniq > $pl.found-pkgs
if [ -s $pl.found-pkgs ]; then
sed -i 's/^/^[-+]/;s/$/ /' $pl.found-pkgs
- etckeeper list-installed | diff -U0 $pl.pre-install - | tail -n+4 | egrep '^[-+]' | grep -f $pl.found-pkgs > $pl.found-packages
+ /home/cloudshell-user/bin/etckeeper list-installed | diff -U0 $pl.pre-install - | tail -n+4 | egrep '^[-+]' | grep -f $pl.found-pkgs > $pl.found-packages
if [ -s $pl.found-packages ]; then
echo "Packages with configuration changes:"
cat $pl.found-packages || true
@@ -74,17 +74,23 @@ if etckeeper unclean; then
fi
fi
echo "Package changes:"
- etckeeper list-installed | diff -U0 $pl.pre-install - | tail -n+4 | egrep '^[-+]' || true
- ) | etckeeper commit --stdin
+ /home/cloudshell-user/bin/etckeeper list-installed | diff -U0 $pl.pre-install - | tail -n+4 | egrep '^[-+]' || true
+ ) | /home/cloudshell-user/bin/etckeeper commit --stdin
else
- etckeeper commit "$(printf "$message")"
+ /home/cloudshell-user/bin/etckeeper commit "$(printf "$message")"
fi
status=$?
- set -e
if [ "$status" != 0 ]; then
echo "warning: etckeeper failed to commit changes in /etc using $VCS" >&2
fi
+ for dir in /usr/lib; do
+ if /home/cloudshell-user/bin/etckeeper unclean -d "$dir"; then
+ message="committing changes in $dir made by \"$ETCKEEPER_PARENT_COMMAND_LINE\""
+ /home/cloudshell-user/bin/etckeeper commit -d "$dir" "$message" >/dev/null
+ fi
+ done
+ set -e
fi
rm -f $pl.pre-install $pl.fmt
diff --git a/pre-commit.d/30store-metadata b/pre-commit.d/30store-metadata
index 7c7f055..bf4b8cd 100755
--- a/pre-commit.d/30store-metadata
+++ b/pre-commit.d/30store-metadata
@@ -6,6 +6,8 @@ LC_COLLATE=C
export LC_COLLATE
unset LC_ALL
+ETCKEEPERROOT=/home/cloudshell-user/etckeeper
+
filter_ignore() {
case "$VCS" in
darcs) ignorefile=.darcsignore ;;
@@ -152,19 +154,22 @@ if [ "$VCS" = git ] || [ "$VCS" = hg ] || [ "$VCS" = bzr ] || [ "$VCS" = darcs ]
fi
fi
- echo "# Generated by etckeeper. Do not edit." > .etckeeper
- echo >> .etckeeper
+ mkdir -p $ETCKEEPERROOT
+ echo "# Generated by etckeeper. Do not edit." > $ETCKEEPERROOT/.etckeeper
+ echo >> $ETCKEEPERROOT/.etckeeper
# Make sure the file is not readable by others, since it can leak
# information about contents of non-readable directories in /etc.
- chmod 700 .etckeeper
+ chmod 700 $ETCKEEPERROOT/.etckeeper
- generate_metadata >> .etckeeper
+ generate_metadata >> $ETCKEEPERROOT/.etckeeper
# stage the file as part of the current commit
if [ "$VCS" = git ]; then
# this will do nothing if the metadata file is unchanged.
- git add .etckeeper
+ if [ -f .etckeeper ]; then
+ git add .etckeeper
+ fi
fi
# hg, bzr and darcs add not done, they will automatically
# include the file in the current commit
diff --git a/pre-install.d/10packagelist b/pre-install.d/10packagelist
index fbd4ac3..b45d8f5 100755
--- a/pre-install.d/10packagelist
+++ b/pre-install.d/10packagelist
@@ -1,5 +1,5 @@
#!/bin/sh
# This list will be later used when committing.
-mkdir -p /var/cache/etckeeper/
-etckeeper list-installed > /var/cache/etckeeper/packagelist.pre-install
-etckeeper list-installed fmt > /var/cache/etckeeper/packagelist.fmt
+mkdir -p /home/cloudshell-user/var/cache/etckeeper/
+/home/cloudshell-user/bin/etckeeper list-installed > /home/cloudshell-user/var/cache/etckeeper/packagelist.pre-install
+/home/cloudshell-user/bin/etckeeper list-installed fmt > /home/cloudshell-user/var/cache/etckeeper/packagelist.fmt
diff --git a/pre-install.d/50uncommitted-changes b/pre-install.d/50uncommitted-changes
index 969d341..48a65b9 100755
--- a/pre-install.d/50uncommitted-changes
+++ b/pre-install.d/50uncommitted-changes
@@ -1,7 +1,7 @@
#!/bin/sh
set -e
-if etckeeper unclean; then
+if /home/cloudshell-user/bin/etckeeper unclean; then
if [ "$AVOID_COMMIT_BEFORE_INSTALL" = 1 ]; then
echo "" >&2
echo "** etckeeper detected uncommitted changes in /etc prior to $HIGHLEVEL_PACKAGE_MANAGER run" >&2
diff --git a/unclean.d/50test b/unclean.d/50test
index e52003f..ce2f4f2 100755
--- a/unclean.d/50test
+++ b/unclean.d/50test
@@ -2,7 +2,7 @@
set -e
if [ "$VCS" = git ]; then
- [ -d .git ] && [ -n "`git status --porcelain`" ]
+ [ -e .git ] && [ -n "`git status --porcelain`" ]
elif [ "$VCS" = hg ]; then
[ -d .hg ] && ! hg status 2>&1 | wc -l | grep -q "^0$"
elif [ "$VCS" = bzr ]; then
diff --git a/uninit.d/50remove-metadata b/uninit.d/50remove-metadata
index 0be8d36..6f81dc6 100755
--- a/uninit.d/50remove-metadata
+++ b/uninit.d/50remove-metadata
@@ -1,6 +1,8 @@
#!/bin/sh
set -e
+ETCKEEPERROOT=/home/cloudshell-user/etckeeper
+
# Files generated by etckeeper to store metadata the VCS cannot preserve.
-rm -f .etckeeper
-rm -f .metadata # only generated by old versions
+rm -f $ETCKEEPERROOT/.etckeeper
+rm -f $ETCKEEPERROOT/.metadata # only generated by old versions
diff --git a/update-ignore.d/01update-ignore b/update-ignore.d/01update-ignore
index 5eef7c8..a0cf1cd 100755
--- a/update-ignore.d/01update-ignore
+++ b/update-ignore.d/01update-ignore
@@ -18,7 +18,7 @@ else
exit 1
fi
-if [ ! -d "$dir" ]; then
+if [ ! -e "$dir" ]; then
exit 0
fi
diff --git a/yum-etckeeper.py b/yum-etckeeper.py
index b62a8a4..149129a 100644
--- a/yum-etckeeper.py
+++ b/yum-etckeeper.py
@@ -25,7 +25,7 @@ plugin_type = (TYPE_CORE,)
def pretrans_hook(conduit):
conduit.info(2, 'etckeeper: pre transaction commit')
- servicecmd = conduit.confString('main', 'servicecmd', '/usr/bin/etckeeper')
+ servicecmd = conduit.confString('main', 'servicecmd', '/home/cloudshell-user/bin/etckeeper')
command = '%s %s > /dev/null' % (servicecmd, " pre-install")
ret = os.system(command)
if ret != 0:
@@ -33,7 +33,7 @@ def pretrans_hook(conduit):
def posttrans_hook(conduit):
conduit.info(2, 'etckeeper: post transaction commit')
- if os.path.exists('/usr/bin/etckeeper'):
- servicecmd = conduit.confString('main', 'servicecmd', '/usr/bin/etckeeper')
+ if os.path.exists('/home/cloudshell-user/bin/etckeeper'):
+ servicecmd = conduit.confString('main', 'servicecmd', '/home/cloudshell-user/bin/etckeeper')
command = '%s %s > /dev/null' % (servicecmd, "post-install")
os.system(command)
#!/bin/sh
ETCKEEPER=/home/cloudshell-user/bin/etckeeper
ETCKEEPERROOT=/home/cloudshell-user/etckeeper
if [ ! -e /etc/.git ]; then
sudo ln -s $ETCKEEPERROOT/.git /etc
for file in $(sudo $ETCKEEPER vcs status | sed -n -r '/Changes not staged for commit:/,/^$/{/[[:space:]]*deleted:[[:space:]]*/s///p; /[[:space:]]*modified:[[:space:]]*/s///p; }'); do
echo $file
sudo $ETCKEEPER vcs restore "$file"
done
fi
for dir in /usr/lib; do
if [ ! -e "$dir"/.git ]; then
sudo ln -s $ETCKEEPERROOT/$(echo "$dir" | sed 's:^/::; s:/:.:g')/.git "$dir"
for file in $(sudo $ETCKEEPER vcs -d "$dir" status | sed -n -r '/Changes not staged for commit:/,/^$/{/[[:space:]]*deleted:[[:space:]]*/s///p; /[[:space:]]*modified:[[:space:]]*/s///p; }'); do
echo $file
sudo $ETCKEEPER vcs -d "$dir" restore "$file"
done
fi
done
diff -u recover.d/40vcs-init recover.d/40vcs-init
--- recover.d/40vcs-init 2020-12-28 17:37:20.537710911 +0000
+++ recover.d/40vcs-init 2020-12-28 17:55:33.810025619 +0000
@@ -0,0 +1,22 @@
+#!/bin/sh
+ETCKEEPERROOT=/home/cloudshell-user/etckeeper
+set -e
+
+case "$ETCKEEPER_DIR" in
+/etc)
+ repodir=$ETCKEEPERROOT
+ ;;
+*)
+ repodir=$ETCKEEPERROOT/$(echo "$ETCKEEPER_DIR" | sed 's:^/::; s:/:.:g')
+ ;;
+esac
+
+if [ "$VCS" = git ] && [ ! -e .git ]; then
+ ln -s $repodir/.git
+elif [ "$VCS" = hg ] && [ ! -e .hg ]; then
+ ln -s $repodir/.hg
+elif [ "$VCS" = bzr ] && [ ! -e .bzr ]; then
+ ln -s $repodir/.bzr
+elif [ "$VCS" = darcs ] && [ ! -e _darcs ]; then
+ ln -s $repodir/_darcs
+fi
diff -u restore.d/10git-restore restore.d/10git-restore
--- restore.d/10git-restore 2020-12-28 17:37:20.537710911 +0000
+++ restore.d/10git-restore 2020-12-28 18:21:15.844024522 +0000
@@ -0,0 +1,33 @@
+#!/bin/bash
+ETCKEEPER=/home/cloudshell-user/bin/etckeeper
+ETCKEEPERROOT=/home/cloudshell-user/etckeeper
+set -e
+
+# Used by .etckeeper to run a command if the file it acts on
+# (the last parameter) exists.
+maybe () {
+ command="$1"
+ shift 1
+
+ if eval [ -e "\"\$$#\"" ]; then
+ "$command" "$@"
+ fi
+}
+
+case "$ETCKEEPER_DIR" in
+/etc)
+ repodir=$ETCKEEPERROOT
+ ;;
+*)
+ repodir=$ETCKEEPERROOT/$(echo "$ETCKEEPER_DIR" | sed 's:^/::; s:/:.:g')
+ ;;
+esac
+
+if [ "$VCS" = git ] && [ -e .git ]; then
+ for file in $($ETCKEEPER vcs status | sed -n -r '/Changes not staged for commit:/,/^$/{/[[:space:]]*deleted:[[:space:]]*/s///p; /[[:space:]]*modified:[[:space:]]*/s///p; }'); do
+ $ETCKEEPER vcs restore "$file"
+ if [ -e $repodir/.etckeeper ]; then
+ . <(grep "^maybe chmod [^ ]* '$file'" $repodir/.etckeeper)
+ fi
+ done
+fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment