diff --git a/config/website.conf.example b/config/website.conf.example new file mode 100644 index 0000000..98a9f70 --- /dev/null +++ b/config/website.conf.example @@ -0,0 +1,8 @@ +# /etc/website/website.conf +# 書式は他のツールと同じ。[節] の下に 項目 = 値 を書く。行頭の # はコメント。 + +[server] +# 待ち受けるアドレス。外へ出さず、同じ機械の中だけにする。 +listen = 127.0.0.1:8201 +# 公開している名前。別の名前で来た要求は、この名前へ転送する。 +base_url = https://routersys.com diff --git a/deploy/website.service b/deploy/website.service new file mode 100644 index 0000000..12dc43c --- /dev/null +++ b/deploy/website.service @@ -0,0 +1,40 @@ +[Unit] +Description=routersys のウェブサイト +After=network-online.target +Wants=network-online.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/website -config /etc/website/website.conf +DynamicUser=yes +Restart=on-failure +RestartSec=5 +TimeoutStopSec=15 + +NoNewPrivileges=yes +CapabilityBoundingSet= +ProtectSystem=strict +ProtectHome=yes +PrivateTmp=yes +PrivateDevices=yes +ProtectKernelTunables=yes +ProtectKernelModules=yes +ProtectKernelLogs=yes +ProtectControlGroups=yes +ProtectClock=yes +ProtectHostname=yes +RestrictAddressFamilies=AF_INET AF_INET6 +RestrictNamespaces=yes +RestrictRealtime=yes +RestrictSUIDSGID=yes +LockPersonality=yes +MemoryDenyWriteExecute=yes +SystemCallArchitectures=native +SystemCallFilter=@system-service +SystemCallFilter=~@privileged @resources +UMask=0077 +MemoryMax=256M +TasksMax=64 + +[Install] +WantedBy=multi-user.target diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..3715353 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "$(dirname "$0")/.." +version=$(git describe --tags --always --dirty 2>/dev/null || echo dev) +mkdir -p dist +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 "${GO:-go}" build -trimpath -buildvcs=false \ + -ldflags "-s -w -X main.version=$version" -o dist/website ./cmd/website +printf 'built: dist/website (%s)\n' "$version" diff --git a/scripts/install.sh b/scripts/install.sh new file mode 100755 index 0000000..b355e91 --- /dev/null +++ b/scripts/install.sh @@ -0,0 +1,72 @@ +#!/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' diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 0000000..bcdaf24 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "$(dirname "$0")/.." +"${GO:-go}" vet ./... +exec "${GO:-go}" test -count=1 ./...