Files
blackbox/bin/_stack_lib.sh
tlimoncelli@stackexchange.com d74eeb33c7 * Initialization for new repos AUTOMATED.
* Adding new users AUTOMATED.
* Update docs for the new, more simplified installation processes.
* Remove dependency on any particular paths, etc.  Copy "bin" into a place along your path and everything should "just work".
* Add support for Mercurial (not tested).
* blackbox_addadmin now adds keys to the keyring for you.
* Unified #! lines to "#!/usr/bin/env bash" so it works better on FreeBSD.
* BUGFIX: (BugId#1) blackbox_update_all_files.sh expects hg, fails for git.
* BUGFIX: (BugId#2) blackbox_postdeploy.sh assumes certain directory layout.
* BUGFIX: Temporary files aren't deleted.
* NEW FILE: bin/blackbox_initialize: Automates enabling BB for a repo (creates directories, files, and updates .gitignore).
* NEW FILE: bin/blackbox_removeadmin: Automates removing an admit.
* NEW FILE: tools/confidence_test.sh: A battery of tests to verify operations.
* NEW FILE: bin/Makefile: Automate package creation.
* NEW FILE: bin/_stack_lib.sh: A library of shell routines from StackExchange.
2014-08-29 20:21:02 +00:00

141 lines
2.7 KiB
Bash
Executable File

# Library functions for bash scripts at Stack Exchange.
# Usage:
# . _stack_lib.sh
# ----- Utility Functions -----
function debugmsg() {
# Log to stderr.
echo 1>&2 LOG: """$@"""
:
}
function logit() {
# Log to stderr.
echo 1>&2 LOG: """$@"""
}
function fail_out() {
echo "FAILED:" "$*"
echo 'Exiting...'
exit 1
}
# on_exit and add_on_exit from http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
# Usage:
# add_on_exit rm -f /tmp/foo
# add_on_exit echo "I am exiting"
# tempfile=$(mktemp)
# add_on_exit rm -f "$tempfile"
function on_exit()
{
for i in "${on_exit_items[@]}"
do
eval $i
done
}
function add_on_exit()
{
local n=${#on_exit_items[*]}
on_exit_items[$n]="$*"
if [[ $n -eq 0 ]]; then
trap on_exit EXIT
fi
}
# Securely and portably create a temporary file that will be deleted
# on EXIT. $1 is the variable name to store the result.
function make_self_deleting_tempfile() {
local __resultvar="$1"
local name
case $(uname -s) in
Darwin )
: ${TMPDIR:=/tmp} ;
name=$(mktemp -t _stacklib_ )
;;
Linux )
name=$(mktemp)
;;
* )
echo 'ERROR: Unknown OS. Exiting.'
exit 1
;;
esac
add_on_exit rm -f "$name"
eval $__resultvar="$name"
}
function make_tempdir() {
local __resultvar="$1"
local name
case $(uname -s) in
Darwin )
: ${TMPDIR:=/tmp} ;
name=$(mktemp -d -t _stacklib_ )
;;
Linux )
name=$(mktemp -d)
;;
* )
echo 'ERROR: Unknown OS. Exiting.'
exit 1
;;
esac
eval $__resultvar="$name"
}
function make_self_deleting_tempdir() {
local __resultvar="$1"
local dirname
make_tempdir dirname
add_on_exit rm -rf "$dirname"
eval $__resultvar="$dirname"
}
function fail_if_not_running_as_root() {
if [[ $EUID -ne 0 ]]; then
echo 'ERROR: This command should only be run as root.'
echo 'Exiting...'
exit 1
fi
}
function fail_if_in_root_directory() {
# Verify nobody has tricked us into being in "/".
case $(uname -s) in
Darwin )
if [[ $(stat -f'%i' / ) == $(stat -f'%i' . ) ]] ; then
echo 'SECURITY ALERT: The current directory is the root directory.'
echo 'Exiting...'
exit 1
fi
;;
Linux )
if [[ $(stat -c'%i' / ) == $(stat -c'%i' . ) ]] ; then
echo 'SECURITY ALERT: The current directory is the root directory.'
echo 'Exiting...'
exit 1
fi
;;
CYGWIN* )
if [[ $(stat -c'%i' / ) == $(stat -c'%i' . ) ]] ; then
echo 'SECURITY ALERT: The current directory is the root directory.'
echo 'Exiting...'
exit 1
fi
;;
* )
echo 'ERROR: Unknown OS. Exiting.'
exit 1
;;
esac
}