255 lines
8.4 KiB
Bash
Executable File
255 lines
8.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Installs or removes docker-fence. Pure bash, coreutils and curl: no git,
|
|
# no make. Writes no firewall rule.
|
|
#
|
|
# From a clone: sudo ./install.sh [--prefix DIR] [--uninstall]
|
|
# Without git: curl -fsSL https://git.djeex.fr/Djeex/docker-fence/raw/branch/main/install.sh | sudo bash
|
|
# curl -fsSL .../install.sh | sudo bash -s -- --tag 1.0.0
|
|
#
|
|
# Layout: PREFIX/lib/docker-fence/{docker-fence,lib/*.sh} and the symlink
|
|
# PREFIX/bin/docker-fence. The configuration (/etc/docker-fence/fence.conf)
|
|
# is never created, changed nor removed, and no firewall rule is written:
|
|
# the tool only acts on "docker-fence apply".
|
|
set -euo pipefail
|
|
|
|
readonly CONFIG=/etc/docker-fence/fence.conf
|
|
readonly REPO_URL=https://git.djeex.fr/Djeex/docker-fence
|
|
|
|
# Files of the tool, relative to the repository root. A test checks this list
|
|
# against the repository.
|
|
readonly FILES=(
|
|
docker-fence
|
|
lib/messages.sh
|
|
lib/util.sh
|
|
lib/config.sh
|
|
lib/generate.sh
|
|
lib/rulesfile.sh
|
|
lib/envcheck.sh
|
|
lib/commands.sh
|
|
lib/report.sh
|
|
lib/template.sh
|
|
lib/listing.sh
|
|
)
|
|
|
|
usage() {
|
|
cat <<'EOF'
|
|
Usage: install.sh [--prefix DIR] [--uninstall] [--branch NAME | --tag NAME] [--source-url URL]
|
|
|
|
--prefix DIR installation prefix, absolute path (default /usr/local)
|
|
--uninstall remove docker-fence from the prefix
|
|
--branch NAME branch to download when no local copy is used (default main)
|
|
--tag NAME tag to download instead of a branch
|
|
--source-url URL base URL of the raw files (default: the branch or tag on
|
|
https://git.djeex.fr/Djeex/docker-fence)
|
|
--local use the files next to install.sh (default when they exist)
|
|
-h, --help this help
|
|
|
|
Installs PREFIX/lib/docker-fence and the command PREFIX/bin/docker-fence.
|
|
No firewall rule is written; /etc/docker-fence/fence.conf is never touched.
|
|
EOF
|
|
}
|
|
|
|
die() {
|
|
printf 'install.sh: %s\n' "$1" >&2
|
|
exit "${2:-1}"
|
|
}
|
|
|
|
prefix=/usr/local
|
|
uninstall=0
|
|
ref_kind=branch
|
|
ref=main
|
|
source_url=""
|
|
mode=auto
|
|
while (( $# > 0 )); do
|
|
case "$1" in
|
|
--prefix|--branch|--tag|--source-url)
|
|
(( $# >= 2 )) || die "$1 needs a value"
|
|
case "$1" in
|
|
--prefix) prefix="$2" ;;
|
|
--branch) ref_kind=branch; ref="$2" ;;
|
|
--tag) ref_kind=tag; ref="$2" ;;
|
|
--source-url) source_url="$2" ;;
|
|
esac
|
|
shift 2
|
|
continue
|
|
;;
|
|
--prefix=*) prefix="${1#--prefix=}" ;;
|
|
--branch=*) ref_kind=branch; ref="${1#--branch=}" ;;
|
|
--tag=*) ref_kind=tag; ref="${1#--tag=}" ;;
|
|
--source-url=*) source_url="${1#--source-url=}" ;;
|
|
--local) mode=local ;;
|
|
--uninstall) uninstall=1 ;;
|
|
-h|--help) usage; exit 0 ;;
|
|
*) die "unknown option: $1 (see --help)" ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
[[ "$prefix" == /* ]] || die "--prefix must be an absolute path: $prefix"
|
|
[[ "$prefix" =~ ^/[A-Za-z0-9_./-]*$ ]] || die "--prefix contains forbidden characters: $prefix"
|
|
prefix="${prefix%/}"
|
|
[[ -n "$prefix" ]] || die "--prefix cannot be /"
|
|
[[ "$ref" =~ ^[A-Za-z0-9_.-]+$ ]] || die "invalid branch or tag name: $ref"
|
|
if [[ -n "$source_url" && ! "$source_url" =~ ^(https|file)://[A-Za-z0-9_./:@%~+-]+$ ]]; then
|
|
die "--source-url must be an https:// or file:// URL: $source_url"
|
|
fi
|
|
(( EUID == 0 )) || die "must be run as root (sudo)"
|
|
|
|
libdir="$prefix/lib/docker-fence"
|
|
bindir="$prefix/bin"
|
|
link="$bindir/docker-fence"
|
|
tmpdir=""
|
|
cleanup() {
|
|
if [[ -n "$tmpdir" ]]; then
|
|
rm -rf -- "$tmpdir"
|
|
fi
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
# Directory of install.sh when it is a real file (not read from a pipe).
|
|
local_dir() {
|
|
local self="${BASH_SOURCE[0]:-}"
|
|
[[ -n "$self" && -f "$self" ]] || return 1
|
|
(cd "$(dirname -- "$(readlink -f -- "$self")")" && pwd -P)
|
|
}
|
|
|
|
# Checks that a downloaded file is the expected bash script, not an error or
|
|
# challenge page.
|
|
check_file() {
|
|
local f="$1" name="$2"
|
|
[[ -s "$f" ]] || die "empty download: $name"
|
|
if [[ "$name" == docker-fence ]]; then
|
|
[[ "$(head -n 1 -- "$f")" == '#!/usr/bin/env bash' ]] || die "unexpected content for $name (not a bash script)"
|
|
else
|
|
[[ "$(head -n 1 -- "$f")" == '# shellcheck shell=bash' ]] || die "unexpected content for $name (not a bash library)"
|
|
fi
|
|
bash -n -- "$f" || die "syntax error in downloaded $name"
|
|
}
|
|
|
|
# Downloads FILES into a temporary directory (tmpdir).
|
|
download() {
|
|
local base="$1" f
|
|
command -v curl >/dev/null || die "curl is needed to download docker-fence"
|
|
tmpdir="$(mktemp -d)"
|
|
mkdir -p -- "$tmpdir/lib"
|
|
for f in "${FILES[@]}"; do
|
|
curl -fsSL --proto '=https,file' --retry 2 -o "$tmpdir/$f" -- "$base/$f" \
|
|
|| die "download failed: $base/$f"
|
|
check_file "$tmpdir/$f" "$f"
|
|
done
|
|
}
|
|
|
|
do_install() {
|
|
local src f
|
|
if [[ "$mode" != local && -n "$source_url" ]]; then
|
|
src=""
|
|
elif src="$(local_dir)" && [[ -f "$src/docker-fence" && -d "$src/lib" ]]; then
|
|
:
|
|
elif [[ "$mode" == local ]]; then
|
|
die "no local copy next to install.sh"
|
|
else
|
|
src=""
|
|
fi
|
|
if [[ -z "$src" ]]; then
|
|
local base="${source_url:-$REPO_URL/raw/$ref_kind/$ref}"
|
|
echo "downloading docker-fence from $base"
|
|
download "$base"
|
|
src="$tmpdir"
|
|
else
|
|
echo "installing docker-fence from $src"
|
|
for f in "${FILES[@]}"; do
|
|
[[ -f "$src/$f" ]] || die "source not found: $src/$f"
|
|
done
|
|
fi
|
|
if [[ -e "$link" && ! -L "$link" ]]; then
|
|
die "$link exists and is not a symlink: remove it first"
|
|
fi
|
|
|
|
install -d -m 0755 -- "$libdir/lib" "$bindir"
|
|
for f in "${FILES[@]}"; do
|
|
if [[ "$f" == docker-fence ]]; then
|
|
install -m 0755 -- "$src/$f" "$libdir/$f"
|
|
else
|
|
install -m 0644 -- "$src/$f" "$libdir/$f"
|
|
fi
|
|
echo "installed $libdir/$f"
|
|
done
|
|
# Libraries that are no longer part of the tool are removed.
|
|
local known
|
|
for f in "$libdir"/lib/*.sh; do
|
|
[[ -e "$f" ]] || continue
|
|
known=0
|
|
local g
|
|
for g in "${FILES[@]}"; do
|
|
[[ "lib/$(basename -- "$f")" == "$g" ]] && known=1
|
|
done
|
|
(( known )) && continue
|
|
rm -f -- "$f"
|
|
echo "removed stale $f"
|
|
done
|
|
ln -sfn -- "$libdir/docker-fence" "$link"
|
|
echo "linked $link -> $libdir/docker-fence"
|
|
next_steps
|
|
}
|
|
|
|
next_steps() {
|
|
local cmd=docker-fence
|
|
[[ ":$PATH:" == *":$bindir:"* ]] || cmd="$link"
|
|
echo
|
|
echo "docker-fence $("$libdir/docker-fence" --version | cut -d' ' -f2) is installed. No firewall rule was written."
|
|
echo
|
|
echo "Next steps:"
|
|
if [[ -e "$CONFIG" ]]; then
|
|
echo " 1. configuration: $CONFIG already exists (kept as is)"
|
|
else
|
|
echo " 1. sudo $cmd init # writes $CONFIG (commented template)"
|
|
fi
|
|
echo " 2. sudo editor $CONFIG # admin addresses, allow, container_to_host"
|
|
echo " 3. sudo $cmd check # validates it and prints the block, changes nothing"
|
|
echo " 4. sudo $cmd apply # loads it (mode observe: logs only), rolls back unless confirmed"
|
|
echo " 5. sudo $cmd confirm # keeps it"
|
|
echo
|
|
echo " sudo $cmd rules # table of every rule (UFW and docker-fence)"
|
|
echo " sudo $cmd --help # every command"
|
|
}
|
|
|
|
do_uninstall() {
|
|
local f removed=0
|
|
if [[ -L "$link" ]]; then
|
|
if [[ "$(readlink -- "$link")" == "$libdir/docker-fence" ]]; then
|
|
rm -f -- "$link"
|
|
echo "removed $link"
|
|
removed=1
|
|
else
|
|
echo "kept $link: it points elsewhere ($(readlink -- "$link"))" >&2
|
|
fi
|
|
elif [[ -e "$link" ]]; then
|
|
echo "kept $link: not a symlink installed by install.sh" >&2
|
|
fi
|
|
if [[ -d "$libdir" ]]; then
|
|
for f in "$libdir/docker-fence" "$libdir"/lib/*.sh; do
|
|
[[ -e "$f" ]] || continue
|
|
rm -f -- "$f"
|
|
echo "removed $f"
|
|
removed=1
|
|
done
|
|
rmdir -- "$libdir/lib" 2>/dev/null || true
|
|
if rmdir -- "$libdir" 2>/dev/null; then
|
|
echo "removed $libdir"
|
|
else
|
|
echo "kept $libdir: it holds other files" >&2
|
|
fi
|
|
fi
|
|
(( removed )) || echo "nothing to remove in $prefix"
|
|
if [[ -e "$CONFIG" ]]; then
|
|
echo "kept $CONFIG (not touched)"
|
|
fi
|
|
echo "No firewall rule was changed: run 'docker-fence uninstall' before this script to remove the block from UFW."
|
|
}
|
|
|
|
if (( uninstall )); then
|
|
do_uninstall
|
|
else
|
|
do_install
|
|
fi
|