deploy-site.sh 5.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env bash
  2. # Publishes website/ to https://lynx-share.com (Hostinger static deploy).
  3. # Needs the Hostinger API token in ~/.config/hostinger/token (never commit it).
  4. # The AppImage's update address (update/, see appimage-update-lib.sh) is never taken from this tree: the archive carries
  5. # the target that is live right now, and the deploy is refused when that can't be read, and reported when it changed.
  6. # --dry-run: everything up to the upload, then lists what the archive has under update/.
  7. set -euo pipefail
  8. DRY=0; [[ "${1:-}" == "--dry-run" ]] && DRY=1
  9. SCRIPTS="$(cd "$(dirname "$0")" && pwd)"
  10. # shellcheck source=scripts/appimage-update-lib.sh
  11. . "$SCRIPTS/appimage-update-lib.sh"
  12. "$SCRIPTS/site-media.sh" || { echo "deploy-site: the tutorial videos (media branch, website/_src/learn/media.json) are not in place: not deploying" >&2; exit 1; }
  13. cd "$SCRIPTS/../website"
  14. TARGET="$(update_target)" || { echo "deploy-site: can't read the update address $UPDATE_ADDRESS: not deploying, try again" >&2; exit 1; }
  15. TARGET_ARM="$(update_target_aarch64)" || { echo "deploy-site: can't read the update address $UPDATE_ADDRESS_AARCH64: not deploying, try again" >&2; exit 1; }
  16. [[ -d update ]] && echo "deploy-site: leaving website/update out of the archive (the live target stays)"
  17. USER_=u258086853; DOMAIN=lynx-share.com
  18. # the archive is about 450 MB: on disk (not /tmp, which can be RAM), removed however this script ends (round 190ra)
  19. BASE="${LYNX_SITE_WORK:-$HOME/.cache/lynx-site-deploy}"; mkdir -p "$BASE"
  20. WORK="$(mktemp -d "$BASE/XXXXXXXX")"; chmod 700 "$WORK"
  21. trap 'rm -r -- "${WORK:?}"' EXIT
  22. ZIP="$WORK/lynx-site.zip"
  23. HT="$WORK/htaccess"; [[ -n "$TARGET" ]] && update_htaccess "$TARGET" "$TARGET_ARM" > "$HT" # (round 190qp: the arm64 redirect kept too)
  24. # the archive's last file: this deploy's own line, so the live site can say that this very archive is in place
  25. MARK="$(date -u +%FT%TZ) $(git -C "$SCRIPTS" rev-parse --short HEAD 2> /dev/null || echo -) $RANDOM$RANDOM"
  26. ZIP="$ZIP" HT="$HT" TARGET="$TARGET" MARK="$MARK" python3 -c "
  27. import zipfile,os
  28. z=zipfile.ZipFile(os.environ['ZIP'],'w',zipfile.ZIP_DEFLATED)
  29. for r,d,f in os.walk('.'):
  30. if r=='.' and 'update' in d: d.remove('update')
  31. for x in f: z.write(os.path.join(r,x),os.path.relpath(os.path.join(r,x),'.'))
  32. if os.environ['TARGET']: z.write(os.environ['HT'],'update/.htaccess')
  33. z.writestr('deploy.txt',os.environ['MARK']+chr(10))
  34. z.close()"
  35. if [[ "$DRY" == 1 ]]; then
  36. echo "target: ${TARGET:-none}"
  37. python3 -c "import zipfile,sys;z=zipfile.ZipFile(sys.argv[1]);[print('archive:',n,'|',z.read(n).decode().splitlines()[1]) for n in z.namelist() if n.startswith('update/')];print('archive files:',len(z.namelist()))" "$ZIP"
  38. update_reminder "$TARGET"
  39. exit 0
  40. fi
  41. T=$(cat ~/.config/hostinger/token); B=https://developers.hostinger.com; SITE="$B/api/hosting/v1/accounts/$USER_/websites/$DOMAIN"
  42. # request <step> <curl arguments...>: the answer in $WORK/answer; an HTTP error names its step and code (round 190ra: a bare
  43. # "curl -sf" ended the script with 22 and no word about which request failed). The error text is printed only for the API's
  44. # own requests; the token and the upload keys never are.
  45. request()
  46. {
  47. local step="$1" code rc=0; shift
  48. : > "$WORK/answer"
  49. code="$(curl -sS -o "$WORK/answer" -w '%{http_code} %{time_total}' "$@")" || rc=$?
  50. echo "deploy-site: $step: HTTP ${code% *} after ${code#* } s$([[ $rc == 0 ]] || echo " (curl $rc)")" >&2
  51. code="${code% *}"
  52. [[ $rc == 0 && "$code" =~ ^2 ]] && return 0
  53. head -c 300 "$WORK/answer" | tr -d '\r\n' >&2; echo >&2
  54. return 1
  55. }
  56. AUTH=(-H "Authorization: Bearer $T"); JSON=(-H 'Content-Type: application/json')
  57. request "upload address" -X POST "${AUTH[@]}" "${JSON[@]}" -d "{\"username\":\"$USER_\",\"domain\":\"$DOMAIN\"}" "$B/api/hosting/v1/files/upload-urls"
  58. get() { python3 -c "import json,sys;print(json.load(open(sys.argv[2]))[sys.argv[1]])" "$1" "$WORK/answer"; }
  59. URL=$(get url); AK=$(get auth_key); RK=$(get rest_auth_key); SIZE=$(stat -c%s "$ZIP")
  60. H=(-H "X-Auth: $AK" -H "X-Auth-Rest: $RK" -H 'Tus-Resumable: 1.0.0')
  61. request "upload start ($SIZE bytes)" -X POST "$URL/lynx-site.zip?override=true" "${H[@]}" -H "Upload-Length: $SIZE" -H 'Upload-Offset: 0'
  62. request "upload" -X PATCH "$URL/lynx-site.zip?override=true" "${H[@]}" -H 'Upload-Offset: 0' -H 'Content-Type: application/offset+octet-stream' --data-binary @"$ZIP"
  63. # The API answers the deploy of this large archive with "500 [Hosting:9999] Request failed" and still carries it out (seen
  64. # 2026-11-25, 446 MB), so its answer proves nothing: what counts is the archive's deploy.txt on the live site.
  65. request "deploy" -X POST "${AUTH[@]}" "${JSON[@]}" -d '{"archive_path":"lynx-site.zip"}' "$SITE/deploy" \
  66. || echo "deploy-site: the deploy request failed; checking whether the site got the archive anyway" >&2
  67. LIVE=
  68. for _ in {1..60}; do
  69. LIVE="$(curl -s --max-time 20 "https://$DOMAIN/deploy.txt?check=$RANDOM" || true)"
  70. [[ "$LIVE" == "$MARK" ]] && break
  71. sleep 5
  72. done
  73. [[ "$LIVE" == "$MARK" ]] || { echo "deploy-site: NOT DEPLOYED: after 5 minutes https://$DOMAIN/deploy.txt is not this archive's ('$MARK')" >&2; exit 1; }
  74. echo "deploy-site: live: $MARK"
  75. # the CDN keeps assets for 30 days: purge it so the new files show up at once
  76. request "cache purge" -X DELETE "${AUTH[@]}" "$SITE/cache/clear"
  77. for _ in 1 2 3 4 5 6; do
  78. sleep 10
  79. AFTER="$(update_target || echo unreadable)"; AFTER_ARM="$(update_target_aarch64 || echo unreadable)"
  80. [[ "$AFTER" == "$TARGET" && "$AFTER_ARM" == "$TARGET_ARM" ]] && break
  81. done
  82. if [[ "$AFTER" != "$TARGET" || "$AFTER_ARM" != "$TARGET_ARM" ]]; then
  83. echo "deploy-site: THE UPDATE ADDRESS CHANGED: was '${TARGET:-none}' / arm64 '${TARGET_ARM:-none}', is now '${AFTER:-none}' / arm64 '${AFTER_ARM:-none}'. Run scripts/publish-appimage-update.sh for the newest release." >&2
  84. exit 1
  85. fi
  86. update_reminder "$TARGET"