Skip to content

Instantly share code, notes, and snippets.

@cgthayer
Last active May 27, 2020 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgthayer/178c017f0e432a86cabd21edf317863d to your computer and use it in GitHub Desktop.
Save cgthayer/178c017f0e432a86cabd21edf317863d to your computer and use it in GitHub Desktop.
Env Pkg: Package your shell environment
#!/bin/bash -
#
# env-pkg: Environment Packager -- super simple, see Usage below
#
# by: Charles Thayer <thayer@mediabridge.com>
cd $HOME
ENVPKG_RSH=${ENVPKG_RSH:-ssh}
ENVDIR=env-pkg
TMPDIR=/tmp/env-pkg-$USER
OLDSUFFIX="-"
SENTFILE="$HOME/.env-pkg-sent"
FILELIST="$HOME/.env-pkg-list"
WEBCACHE="$HOME/public_html/src/env.tar"
PORT=7034
prog=${0##*/}
usage() {
sed -ne '/^Usage:/,/^For more/p' < $0
exit $1
}
cmd=$1; shift
case $cmd in
-h) usage 0;;
-init|-push|-pull|-t)
host=$1
if [ -z $host ]; then usage 1; fi
shift
;;
esac
pkgs=${*:-basic}
case $cmd in
-o) # stdout
for p in $pkgs; do pkglist="$pkglist $ENVDIR/$p"; done
tar cf - --ignore-failed-read $pkglist `cat $pkglist` | tee $WEBCACHE # main output
echo $host $pkgs >> $SENTFILE
exit 0
;;
-i) # stdin
if [ -d $TMPDIR ]; then rm -rf $TMPDIR; fi
mkdir -p $TMPDIR
cd $TMPDIR
tar xvfp - > $FILELIST # main listener
for file in `cat $FILELIST`; do
if [ -f $HOME/$file ]; then
if ! diff -q $HOME/$file $file > /dev/null; then
echo Differing versions $file
if [ -f $HOME/$file -a $file -nt $HOME/$file ]; then
rm -f $HOME/$file$OLDSUFFIX
mv $HOME/$file $HOME/$file$OLDSUFFIX
rm -f $HOME/$file
mv $file $HOME/$file
else
echo Warning: $file is not newer, skipping >&2
fi
fi
else
echo New version of $file
mv $file $HOME/$file
fi
done
exit 0
;;
-init) exec $prog -o $pkgs | $ENVPKG_RSH $host "tar xvfp -";;
-push) exec $prog -o $pkg | $ENVPKG_RSH $host "env-pkg -i";;
-pull) exec $ENVPKG_RSH $host "env-pkg -o $pkgs" | $prog -i;;
-t) exec $prog -o $* | ttcp -t -p$PORT $host;;
-r) exec ttcp -r -p$PORT | $prog -i;;
*) usage 1;;
esac
exit 0
# README for env-pkg
Usage: env-pkg -o [pkglist..]
env-pkg -i
env-pkg -init host [pkglist..]
env-pkg -push host [pkglist..]
env-pkg -pull host [pkglist..]
env-pkg -t host [pkglist..]
env-pkg -r
-i,-o Read package from stdin or write to stdout.
-init,-push,-pull Use ssh (or \$ENVPKG_RSH) to send or get files.
-t,-r Use ttcp (or another network endpoint) to transmit or receive.
For more details see end of shell script
Super simple shell-script for maintaining user environment across many
machines. A package is a file containing a list of files. In
~/env-pkg create package files, starting with "basic" which is the
default when no package names are provided on the command line.
env-pkg will transfer files with tar as per the package list provided.
If the file already exists a backup version is created with a '-'
suffix, the target file is newer than source files (in which case the
source can be found in /tmp/env-pkg-$USER). env-pkg is designed to be
short enough to cut and paste, but flexible enough to generally work
in hostile environments.
It keeps history files of what's been done when, and backups of
replaced files.
Useful tricks:
Don't have env-pkg yet:
ssh master env-pkg -o | tar xvf -
Tip toe through firewall:
lynx -dump http://master/~user/env.tar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment