Files

158 lines
4.4 KiB
Bash

#!/usr/bin/env bash
# Download myoffice-publish.tar.gz from a Gitea release into API_DIR.
# Env:
# GITEA_URL, GITEA_OWNER, GITEA_REPO, RELEASE_TAG (latest|v1.2.3)
# API_DIR (default /opt/myoffice/api)
# RELEASE_ASSET_URL — optional direct override (skips API resolve)
# LOCAL_RELEASE_TAR — optional local .tar.gz (skips download; used when host pushes asset into CT)
# GITEA_TOKEN — optional for private release downloads
set -euo pipefail
API_DIR="${API_DIR:-/opt/myoffice/api}"
GITEA_URL="${GITEA_URL:-}"
GITEA_OWNER="${GITEA_OWNER:-}"
GITEA_REPO="${GITEA_REPO:-myoffice_public}"
RELEASE_TAG="${RELEASE_TAG:-latest}"
RELEASE_ASSET_URL="${RELEASE_ASSET_URL:-}"
LOCAL_RELEASE_TAR="${LOCAL_RELEASE_TAR:-}"
ASSET_NAME="${ASSET_NAME:-myoffice-publish.tar.gz}"
GITEA_TOKEN="${GITEA_TOKEN:-}"
msg() { echo -e " ==> $*"; }
curl_auth() {
if [[ -n "$GITEA_TOKEN" ]]; then
curl -fsSL -H "Authorization: token ${GITEA_TOKEN}" "$@"
else
curl -fsSL "$@"
fi
}
json_first_string() {
# Extract first "key":"value" for key=$1 from JSON on stdin (no jq/python required).
local key="$1"
tr -d '\n' | sed -n "s/.*\"${key}\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" | head -1
}
resolve_asset_url() {
if [[ -n "$RELEASE_ASSET_URL" ]]; then
echo "$RELEASE_ASSET_URL"
return 0
fi
if [[ -z "$GITEA_URL" || -z "$GITEA_OWNER" ]]; then
echo "Set GITEA_URL + GITEA_OWNER (and GITEA_REPO), or RELEASE_ASSET_URL." >&2
exit 1
fi
local base api_json tag browser_url
base="${GITEA_URL%/}"
if [[ "$RELEASE_TAG" == "latest" ]]; then
api_json=$(curl_auth "${base}/api/v1/repos/${GITEA_OWNER}/${GITEA_REPO}/releases/latest")
tag=$(printf '%s' "$api_json" | json_first_string tag_name)
if [[ -z "$tag" ]]; then
echo "Could not resolve latest release tag from Gitea API." >&2
exit 1
fi
else
tag="$RELEASE_TAG"
api_json=$(curl_auth "${base}/api/v1/repos/${GITEA_OWNER}/${GITEA_REPO}/releases/tags/${tag}" || true)
fi
# Prefer browser_download_url that ends with the asset name
browser_url=$(printf '%s' "${api_json:-}" | tr '"' '\n' | grep -F "/${ASSET_NAME}" | grep -E '^https?://' | head -1 || true)
if [[ -z "$browser_url" ]]; then
browser_url="${base}/${GITEA_OWNER}/${GITEA_REPO}/releases/download/${tag}/${ASSET_NAME}"
fi
echo "$browser_url"
}
# Download release asset to a file path (usable on Proxmox host).
download_asset_to() {
local dest="$1"
local url
url=$(resolve_asset_url)
msg "Downloading release asset: $url"
curl_auth -o "$dest" "$url"
msg "Saved $dest"
}
extract_tar_to_api() {
local tar_path="$1"
local staging prod shared_prod prod_bak="" shared_bak="" top_count top_dir only
mkdir -p "$API_DIR"
staging=$(mktemp -d /tmp/myoffice-extract.XXXXXX)
tar -xzf "$tar_path" -C "$staging"
prod="${API_DIR}/appsettings.Production.json"
shared_prod="${API_DIR}/appsettings.shared.Production.json"
if [[ -f "$prod" ]]; then
prod_bak=$(mktemp)
cp -a "$prod" "$prod_bak"
fi
if [[ -f "$shared_prod" ]]; then
shared_bak=$(mktemp)
cp -a "$shared_prod" "$shared_bak"
fi
top_count=$(find "$staging" -mindepth 1 -maxdepth 1 | wc -l)
top_dir=""
if [[ "$top_count" -eq 1 ]]; then
only=$(find "$staging" -mindepth 1 -maxdepth 1 | head -1)
if [[ -d "$only" ]]; then
top_dir="$only"
fi
fi
find "$API_DIR" -mindepth 1 -maxdepth 1 \
! -name 'appsettings.Production.json' \
! -name 'appsettings.shared.Production.json' \
-exec rm -rf {} +
if [[ -n "$top_dir" ]]; then
cp -a "$top_dir"/. "$API_DIR"/
else
cp -a "$staging"/. "$API_DIR"/
fi
rm -rf "$staging"
rm -f "$API_DIR/appsettings.Production.json" "$API_DIR/appsettings.shared.Production.json" 2>/dev/null || true
if [[ -n "$prod_bak" ]]; then
mv "$prod_bak" "$prod"
fi
if [[ -n "$shared_bak" ]]; then
mv "$shared_bak" "$shared_prod"
fi
if [[ ! -f "${API_DIR}/MyOffice.Web.dll" ]]; then
echo "Extracted archive but MyOffice.Web.dll is missing in ${API_DIR}" >&2
exit 1
fi
msg "App extracted to ${API_DIR}"
}
download_and_extract() {
local tar_path
if [[ -n "$LOCAL_RELEASE_TAR" && -f "$LOCAL_RELEASE_TAR" ]]; then
msg "Using local release tar: $LOCAL_RELEASE_TAR"
tar_path="$LOCAL_RELEASE_TAR"
extract_tar_to_api "$tar_path"
return 0
fi
tar_path="/tmp/${ASSET_NAME}"
download_asset_to "$tar_path"
extract_tar_to_api "$tar_path"
rm -f "$tar_path"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
download_and_extract
fi