deploy-site.sh 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. cd "$SCRIPTS/../website"
  13. TARGET="$(update_target)" || { echo "deploy-site: can't read the update address $UPDATE_ADDRESS: not deploying, try again" >&2; exit 1; }
  14. [[ -d update ]] && echo "deploy-site: leaving website/update out of the archive (the live target stays)"
  15. USER_=u258086853; DOMAIN=lynx-share.com
  16. ZIP=$(mktemp -d)/lynx-site.zip
  17. HT="$(mktemp)"; [[ -n "$TARGET" ]] && update_htaccess "$TARGET" > "$HT"
  18. ZIP="$ZIP" HT="$HT" TARGET="$TARGET" python3 -c "
  19. import zipfile,os
  20. z=zipfile.ZipFile(os.environ['ZIP'],'w',zipfile.ZIP_DEFLATED)
  21. for r,d,f in os.walk('.'):
  22. if r=='.' and 'update' in d: d.remove('update')
  23. for x in f: z.write(os.path.join(r,x),os.path.relpath(os.path.join(r,x),'.'))
  24. if os.environ['TARGET']: z.write(os.environ['HT'],'update/.htaccess')
  25. z.close()"
  26. if [[ "$DRY" == 1 ]]; then
  27. echo "target: ${TARGET:-none}"
  28. 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"
  29. update_reminder "$TARGET"
  30. exit 0
  31. fi
  32. T=$(cat ~/.config/hostinger/token); B=https://developers.hostinger.com
  33. J=$(curl -sf -X POST -H "Authorization: Bearer $T" -H 'Content-Type: application/json' -d "{\"username\":\"$USER_\",\"domain\":\"$DOMAIN\"}" $B/api/hosting/v1/files/upload-urls)
  34. get() { python3 -c "import json,sys;print(json.load(sys.stdin)['$1'])" <<<"$J"; }
  35. URL=$(get url); AK=$(get auth_key); RK=$(get rest_auth_key); SIZE=$(stat -c%s "$ZIP")
  36. H=(-H "X-Auth: $AK" -H "X-Auth-Rest: $RK" -H 'Tus-Resumable: 1.0.0')
  37. curl -sf -o /dev/null -X POST "$URL/lynx-site.zip?override=true" "${H[@]}" -H "Upload-Length: $SIZE" -H 'Upload-Offset: 0'
  38. curl -sf -o /dev/null -X PATCH "$URL/lynx-site.zip?override=true" "${H[@]}" -H 'Upload-Offset: 0' -H 'Content-Type: application/offset+octet-stream' --data-binary @"$ZIP"
  39. curl -sf -X POST -H "Authorization: Bearer $T" -H 'Content-Type: application/json' -d '{"archive_path":"lynx-site.zip"}' $B/api/hosting/v1/accounts/$USER_/websites/$DOMAIN/deploy; echo
  40. # the CDN keeps assets for 30 days: purge it so the new files show up at once
  41. curl -sf -X DELETE -H "Authorization: Bearer $T" "$B/api/hosting/v1/accounts/$USER_/websites/$DOMAIN/cache/clear"; echo
  42. # (the deploy is accepted first and runs after: give it a moment)
  43. for _ in 1 2 3 4 5 6; do
  44. sleep 10
  45. AFTER="$(update_target || echo unreadable)"
  46. [[ "$AFTER" == "$TARGET" ]] && break
  47. done
  48. if [[ "$AFTER" != "$TARGET" ]]; then
  49. echo "deploy-site: THE UPDATE ADDRESS CHANGED: was '${TARGET:-none}', is now '${AFTER:-none}'. Run scripts/publish-appimage-update.sh for the newest release." >&2
  50. exit 1
  51. fi
  52. update_reminder "$TARGET"