101 lines
2.6 KiB
Bash
Executable File
101 lines
2.6 KiB
Bash
Executable File
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
# Parse arguments
|
|
enable=false
|
|
disable=false
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
--enable)
|
|
enable=true
|
|
shift
|
|
;;
|
|
--disable)
|
|
disable=true
|
|
shift
|
|
;;
|
|
*)
|
|
echo "Unknown option: $1"
|
|
echo "Usage: $0 [--enable | --disable]"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [[ "$enable" == "false" && "$disable" == "false" ]]; then
|
|
echo "Usage: $0 [--enable | --disable]"
|
|
exit 1
|
|
fi
|
|
|
|
if [[ "$enable" == "true" && "$disable" == "true" ]]; then
|
|
echo "Error: Cannot use both --enable and --disable"
|
|
exit 1
|
|
fi
|
|
|
|
REMOTE_HOST="vps.git"
|
|
REMOTE_USER="root"
|
|
GITEA_URL="https://git.hangman-lab.top/user/login"
|
|
MAX_RETRIES=10
|
|
RETRY_INTERVAL=3
|
|
TARGET_VALUE="true"
|
|
ACTION_WORD="disabled"
|
|
|
|
if [[ "$enable" == "true" ]]; then
|
|
TARGET_VALUE="false"
|
|
ACTION_WORD="enabled"
|
|
fi
|
|
|
|
wait_for_gitea() {
|
|
echo "[INFO] Waiting for Gitea to be ready..."
|
|
for i in $(seq 1 $MAX_RETRIES); do
|
|
STATUS=$(curl -s -o /dev/null -w '%{http_code}' "$GITEA_URL" 2>/dev/null || echo "000")
|
|
if [[ "$STATUS" == "200" ]]; then
|
|
echo "[INFO] Gitea is ready (HTTP 200)."
|
|
return 0
|
|
fi
|
|
echo "[INFO] Waiting for Gitea ($i/$MAX_RETRIES), current status: $STATUS..."
|
|
sleep $RETRY_INTERVAL
|
|
done
|
|
echo "[WARN] Gitea did not return 200 within expected time."
|
|
return 0
|
|
}
|
|
|
|
echo "[INFO] Setting REQUIRE_EXTERNAL_LOGIN = $TARGET_VALUE on Dockerized Gitea..."
|
|
ssh "$REMOTE_USER@$REMOTE_HOST" "
|
|
set -euo pipefail
|
|
python3 - <<'PY'
|
|
from pathlib import Path
|
|
p = Path('/root/git-kc/gitea/app.ini')
|
|
lines = p.read_text().splitlines()
|
|
out = []
|
|
in_auth = False
|
|
updated_in_auth = False
|
|
for line in lines:
|
|
stripped = line.strip()
|
|
if stripped.startswith('['):
|
|
if in_auth and not updated_in_auth:
|
|
out.append('REQUIRE_EXTERNAL_LOGIN = $TARGET_VALUE')
|
|
updated_in_auth = True
|
|
in_auth = (stripped == '[auth]')
|
|
out.append(line)
|
|
continue
|
|
if stripped.startswith('REQUIRE_EXTERNAL_LOGIN = '):
|
|
if in_auth and not updated_in_auth:
|
|
out.append('REQUIRE_EXTERNAL_LOGIN = $TARGET_VALUE')
|
|
updated_in_auth = True
|
|
# drop all duplicates outside [auth], and extra duplicates inside [auth]
|
|
continue
|
|
out.append(line)
|
|
if in_auth and not updated_in_auth:
|
|
out.append('REQUIRE_EXTERNAL_LOGIN = $TARGET_VALUE')
|
|
if not any(l.strip() == '[auth]' for l in lines):
|
|
out.extend(['', '[auth]', 'REQUIRE_EXTERNAL_LOGIN = $TARGET_VALUE'])
|
|
p.write_text('\n'.join(out) + '\n')
|
|
PY
|
|
docker restart git-kc-gitea >/dev/null
|
|
" <&0
|
|
|
|
wait_for_gitea
|
|
echo "[DONE] Local login $ACTION_WORD."
|