File: [local] / openbsd / sxxu / Attic / sxxu (download)
Revision 1.16, Tue Apr 20 17:25:49 2010 UTC (14 years, 1 month ago) by andrew
Branch: MAIN
Changes since 1.15: +104 -85 lines
some cleanup based on sysmerge, add usage
|
#!/bin/sh
# $Id: sxxu,v 1.16 2010/04/20 17:25:49 andrew Exp $
# Copyright (c) 2010 Andrew Fresh <andrew@afresh1.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# based on siteXXtools/generate
# Copyright (c) 2006 Alex Holst <a@mongers.org>
OSREV=$(uname -r| sed 's/\.//g')
SRCDIR=`pwd`
COMMONDIR=common
RELEASEDIR=/tmp
INSTALL_SITE=install.siteXX
do_pre() {
if [ -z "$WRKDIR" ]; then
WRKDIR=`mktemp -d -t sxxu.XXXXXXXXX` || exit 1
fi
set -A MACHINES -- "$@"
if [ ${#MACHINES[@]} -eq 0 ]; then
local _i=0
local _d
for _d in *; do
[ ! -d "$_d" ] && continue
[ -z "${NORECURSE}" -a "${_d#role-}" != "$_d" ] && continue
MACHINES[$_i]="${_d}"
_i=$((_i+1))
done
fi
echo WRKDIR: $WRKDIR
[ -z "$REGEN_MTREE" ] || echo Regenerate mtree
echo '#*#*#*#'
}
do_loop() {
local _machine
for _machine in "${MACHINES[@]}"; do
machine=`basename -- "$machine"`
if [ ! -d "${_machine}" ]; then
echo ${_machine} does not exist
continue
fi
local _site=site${OSREV}
if [ X"${_machine}" != X"${COMMONDIR}" ]; then
_site="${_site}-${_machine}"
fi
echo -n populating: $_site
WRKINST="${WRKDIR}/${_site}"
if [ -z "${REGEN_MTREE}" -o ! -e "${WRKINST}" ]; then
echo -n "\n including:"
include "${_machine}"
fi
if [ -z "$NORECURSE" ]; then
if [ X"${_machine}" != X"${COMMONDIR}" \
-a -d "${SRCDIR}/${COMMONDIR}" ]; then
include $COMMONDIR
fi
if [ -e "${INSTALL_SITE}" ]; then
cp ${INSTALL_SITE} "$WRKINST/install.site"
fi
fi
if [ ! -z "${REGEN_MTREE}" ]; then
local _mtree="${SRCDIR}/${_machine}/siteXX/mtree"
echo -n "\nregenerate: ${_mtree#${SRCDIR}/}"
mkdir -p "${SRCDIR}/${_machine}/siteXX" \
"${WRKINST}/var/siteXX/${_machine}"
mtree -cp "${WRKINST}" -k uid,gid,mode > "$_mtree"
fi
if [ -z "$NOGZIP" ]; then
local _tgz="${RELEASEDIR}/${_site}.tgz"
echo -n "\n creating: $_tgz"
tar -cz -C "${WRKINST}" -f "$_tgz" .
fi
echo ' . . . done'
done
}
do_post() {
if [ -z "$NOREMOVE" ]; then
rm -rf $WRKDIR
fi
}
copy_special() {
local _src="$1"
local _dst="$2"
if [ ! -d "$_src" ]; then
echo "$_src does not exist!"
return
fi
cd "$_src"
find . \( \
-path './siteXX' \
-o -name 'CVS' \
-o -name '.git' \
-o -name '.svn' \
\) -prune -o -print \
| {
local _file
while read _file ; do
if [ -d "$_file" ]; then
mkdir -p "${_dst}/${_file}"
else
cp "$_file" "${_dst}/${_file}"
fi
done
}
cd "${OLDPWD}"
}
include() {
local _src="$1"
if [ X"${_src}" == X"" ]; then
echo "include <dir>"
return
fi
if [ ! -d "${SRCDIR}/${_src}" ]; then
return
fi
local _siteXX_src="${SRCDIR}/${_src}/siteXX"
local _siteXX_dst="${WRKINST}/var/siteXX/${_src}"
if [ -d "$_siteXX_dst" ]; then
echo -n " !!!${_src}!!!"
return
fi
mkdir -p "$_siteXX_dst"
echo -n " $_src"
copy_special "${SRCDIR}/${_src}" "${WRKINST}"
if [ -d "$_siteXX_src" ]; then
copy_special "$_siteXX_src" "$_siteXX_dst"
if [ -z "${REGEN_MTREE}" -a -e "${_siteXX_src}/mtree" ]; then
mtree -Uep "${WRKINST}" < "${_siteXX_src}/mtree" > /dev/null
local _mtree_out=`mktemp`
mtree -ep "${WRKINST}" < "${_siteXX_src}/mtree" > $_mtree_out
if [ $? -ne 0 ]; then
echo "\nMTREE PROBLEMS"
cat $_mtree_out
fi
rm -f $_mtree_out
fi
[ -z "${NORECURSE}" ] || return
if [ -e "${_siteXX_src}/roles" ]; then
local _role
while read _role; do
include "$_role"
done < "${_siteXX_src}/roles"
fi
fi
}
usage() {
echo "usage: ${0##*/} [-kmR] [-w WRKDIR] [machine1 [machine2 [...]]]" >&2
}
cd ${SRCDIR}
for _INSTALL_SITE in \
install.site \
"${SAMPLE_DIR}/install.siteXX" \
"${SAMPLE_DIR}/install.site"
do
if [ -e "$_INSTALL_SITE" ]; then
INSTALL_SITE="$_INSTALL_SITE"
break
fi
done
local _cfg
for _cfg in .siteXXrc ~/.siteXXrc /etc/siteXX.conf; do
[ -e $_cfg ] && . $_cfg && break
done
while getopts :kmw:R arg; do
case ${arg} in
k)
NOREMOVE=1
;;
m)
REGEN_MTREE=1
NORECURSE=1
NOGZIP=1
;;
R)
NORECURSE=1
NOGZIP=1
;;
w)
WRKDIR=${OPTARG}
;;
*)
echo ${0##*/}: unknown option -- ${arg}
usage
exit 1
;;
esac
done
shift $(($OPTIND - 1))
do_pre
do_loop
do_post