73 行
2.6 KiB
Bash
実行ファイル
73 行
2.6 KiB
Bash
実行ファイル
#!/usr/bin/env bash
|
|
# 使い方: scripts/install.sh [--apply [--binary ファイル]]
|
|
# 引数なしは、何を置くかを表示するだけで、何も変更しない。
|
|
set -euo pipefail
|
|
|
|
readonly prefix=${PREFIX:-/usr/local}
|
|
readonly root=${DESTDIR:-}
|
|
readonly source_directory=$(cd "$(dirname "$0")/.." && pwd)
|
|
readonly unit_directory=$prefix/lib/systemd/system
|
|
|
|
apply=false
|
|
binary=
|
|
while [ $# -gt 0 ]; do
|
|
case $1 in
|
|
--apply) apply=true ;;
|
|
--binary) binary=${2:?--binary にはファイルを指定します}; shift ;;
|
|
*) echo "未知の引数: $1" >&2; exit 3 ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
if ! $apply; then
|
|
printf 'would install: %s/bin/website\n' "$prefix"
|
|
printf 'would install: %s/website.service\n' "$unit_directory"
|
|
printf 'would create if absent: /etc/website/website.conf\n'
|
|
printf 'would enable and start: website.service\n'
|
|
exit 0
|
|
fi
|
|
|
|
if [ -z "$root" ] && [ "$(id -u)" -ne 0 ]; then
|
|
echo "root で実行してください。" >&2
|
|
exit 3
|
|
fi
|
|
|
|
if [ -z "$binary" ]; then
|
|
work=$(mktemp -d)
|
|
trap 'rm -rf "$work"' EXIT
|
|
(cd "$source_directory" && CGO_ENABLED=0 "${GO:-go}" build -trimpath -buildvcs=false \
|
|
-ldflags "-s -w -X main.version=$(git describe --tags --always 2>/dev/null || echo dev)" -o "$work/website" ./cmd/website)
|
|
binary=$work/website
|
|
fi
|
|
[ -f "$binary" ] || { echo "バイナリがありません: $binary" >&2; exit 3; }
|
|
|
|
changed=false
|
|
place() { # place 元 先 モード
|
|
if [ -f "$root$2" ] && cmp -s "$1" "$root$2"; then return; fi
|
|
install -D -m "$3" "$1" "$root$2"
|
|
changed=true
|
|
printf 'installed: %s\n' "$2"
|
|
}
|
|
place "$binary" "$prefix/bin/website" 0755
|
|
place "$source_directory/deploy/website.service" "$unit_directory/website.service" 0644
|
|
if [ ! -f "$root/etc/website/website.conf" ]; then
|
|
install -D -m 0644 "$source_directory/config/website.conf.example" "$root/etc/website/website.conf"
|
|
printf 'installed: /etc/website/website.conf (例のまま。内容を確かめてください)\n'
|
|
fi
|
|
|
|
[ -n "$root" ] && exit 0
|
|
|
|
"$prefix/bin/website" -check -config /etc/website/website.conf
|
|
listen=$(sed -n 's/^listen *= *//p' /etc/website/website.conf | head -1)
|
|
port=${listen##*:}
|
|
if ! systemctl is-active --quiet website.service && ss -ltn "sport = :$port" | grep -q LISTEN; then
|
|
echo "ポート $port は、すでに別のものが使っています。docs/ports.md を確かめて、設定を直してください。" >&2
|
|
exit 1
|
|
fi
|
|
systemctl daemon-reload
|
|
systemctl enable website.service
|
|
if $changed || ! systemctl is-active --quiet website.service; then
|
|
systemctl restart website.service
|
|
fi
|
|
printf 'enabled and running: website.service\n'
|