Watch
1
0
Fork
You've already forked arc
0
forked from hesabix/arc
arc/scripts/hesabix
2026-05-07 22:21:45 +03:30

248 lines
6.5 KiB
Shell
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# Hesabix CLI – update from repo / control systemd units / reload itself.
set -euo pipefail
APP_ROOT="${APP_ROOT:-/opt/hesabix}"
if [[ ${EUID:-$(id -u)} -ne 0 ]]; then
echo "Run as root (e.g. sudo hesabix -update or sudo hesabix -services start)" >&2
exit 1
fi
usage() {
cat <<'USAGE'
Usage:
hesabix -update [-source REPO_URL] [-branch BRANCH]
hesabix -services {start|stop|restart|status}
hesabix -cli reload
-update Run update (pull, migrate, restart, rebuild frontend, reload nginx)
-source URL Override repo URL (default: from initial deploy)
-branch NAME Override branch (default: from initial deploy)
-services ACTION Control Hesabix systemd units (API, RQ worker, notification worker; pgadmin4 too if installed)
-cli reload Reload /usr/local/bin/hesabix from the deployed repo (APP_ROOT/app/scripts/hesabix)
USAGE
}
reload_hesabix_cli() {
local src="${APP_ROOT}/app/scripts/hesabix"
local target="/usr/local/bin/hesabix"
if [[ ! -f "${src}" ]]; then
echo "CLI source not found: ${src}" >&2
return 1
fi
local tmp
tmp="$(mktemp)"
cp -f "${src}" "${tmp}"
chmod 755 "${tmp}" 2>/dev/null || true
if [[ -f "${target}" ]]; then
if command -v sha256sum >/dev/null 2>&1; then
local new_sum old_sum
new_sum="$(sha256sum "${tmp}" | awk '{print $1}')"
old_sum="$(sha256sum "${target}" | awk '{print $1}')"
if [[ "${new_sum}" == "${old_sum}" ]]; then
rm -f "${tmp}" 2>/dev/null || true
echo "hesabix CLI is already up-to-date."
return 0
fi
else
if cmp -s "${tmp}" "${target}"; then
rm -f "${tmp}" 2>/dev/null || true
echo "hesabix CLI is already up-to-date."
return 0
fi
fi
fi
mv -f "${tmp}" "${target}"
chmod 755 "${target}" 2>/dev/null || true
echo "hesabix CLI reloaded from ${src}."
return 0
}
# Ensure API service gets journalctl sudo fallback env.
ensure_api_journalctl_env() {
local dropin_dir="/etc/systemd/system/hesabix-api.service.d"
local dropin_file="${dropin_dir}/10-journalctl-env.conf"
mkdir -p "${dropin_dir}"
cat > "${dropin_file}" <<'EOF'
[Service]
Environment=HESABIX_ALLOW_SUDO_JOURNALCTL=1
EOF
systemctl daemon-reload
}
# Core application units (order: start forward, stop reverse). pgadmin4 is optional if installed.
run_hesabix_services() {
local action="$1"
local -a core=( hesabix-api hesabix-rq-worker hesabix-notification-moderation )
local u
for u in "${core[@]}"; do
if [[ "$(systemctl show "${u}.service" -p LoadState --value 2>/dev/null)" != "loaded" ]]; then
echo "Expected systemd unit is missing or not loaded: ${u}.service (re-run deploy.sh backend step)" >&2
exit 1
fi
done
local -a extra=()
if [[ "$(systemctl show pgadmin4.service -p LoadState --value 2>/dev/null)" == "loaded" ]]; then
extra+=( pgadmin4 )
fi
case "${action}" in
start)
for u in "${core[@]}"; do
systemctl start "${u}.service"
done
for u in "${extra[@]}"; do
systemctl start "${u}.service"
done
echo "Started Hesabix services."
;;
stop)
for (( i = ${#extra[@]} - 1; i >= 0; i-- )); do
systemctl stop "${extra[i]}.service"
done
for (( i = ${#core[@]} - 1; i >= 0; i-- )); do
systemctl stop "${core[i]}.service"
done
echo "Stopped Hesabix services."
;;
restart)
ensure_api_journalctl_env
systemctl daemon-reload
for u in "${core[@]}"; do
systemctl restart "${u}.service"
done
for u in "${extra[@]}"; do
systemctl restart "${u}.service"
done
echo "Restarted Hesabix services."
;;
status)
for u in "${core[@]}"; do
echo "======== ${u}.service ========"
systemctl --no-pager -l status "${u}.service" || true
done
for u in "${extra[@]}"; do
echo "======== ${u}.service ========"
systemctl --no-pager -l status "${u}.service" || true
done
;;
*)
echo "Invalid action: ${action}. Use: start|stop|restart|status" >&2
exit 1
;;
esac
}
UPDATE_MODE=""
SERVICES_ACTION=""
CLI_ACTION=""
SOURCE_URL=""
BRANCH_OVERRIDE=""
while [[ $# -gt 0 ]]; do
case "$1" in
-update) UPDATE_MODE=1; shift ;;
-cli)
if [[ $# -lt 2 || -z "${2:-}" ]]; then
echo "hesabix -cli requires an action: reload" >&2
exit 1
fi
CLI_ACTION="$2"
shift 2
;;
-source)
if [[ $# -lt 2 || -z "${2:-}" ]]; then
echo "-source requires a repository URL." >&2
exit 1
fi
SOURCE_URL="$2"
shift 2
;;
-branch)
if [[ $# -lt 2 || -z "${2:-}" ]]; then
echo "-branch requires a branch name." >&2
exit 1
fi
BRANCH_OVERRIDE="$2"
shift 2
;;
-services)
if [[ $# -lt 2 || -z "${2:-}" ]]; then
echo "hesabix -services requires an action: start|stop|restart|status" >&2
exit 1
fi
SERVICES_ACTION="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1. Use -h for help." >&2
exit 1
;;
esac
done
if [[ -n "${CLI_ACTION}" ]]; then
if [[ -n "${UPDATE_MODE}" || -n "${SERVICES_ACTION}" ]]; then
echo "Use -cli alone (do not combine with -update or -services)." >&2
exit 1
fi
case "${CLI_ACTION}" in
reload) reload_hesabix_cli ;;
*) echo "Invalid -cli action: ${CLI_ACTION}. Use: reload" >&2; exit 1 ;;
esac
exit $?
fi
if [[ ! -f "${APP_ROOT}/.deploy_env" ]]; then
echo "Hesabix not deployed yet. Run deploy.sh first." >&2
exit 1
fi
if [[ -n "${UPDATE_MODE}" && -n "${SERVICES_ACTION}" ]]; then
echo "Use either -update or -services, not both." >&2
exit 1
fi
if [[ -n "${SERVICES_ACTION}" ]]; then
case "${SERVICES_ACTION}" in
start|stop|restart|status) ;;
*) echo "Invalid -services action: ${SERVICES_ACTION}. Use: start|stop|restart|status" >&2; exit 1 ;;
esac
run_hesabix_services "${SERVICES_ACTION}"
exit 0
fi
if [[ -z "${UPDATE_MODE}" ]]; then
usage
exit 0
fi
ensure_api_journalctl_env
set -a
# shellcheck source=/dev/null
source "${APP_ROOT}/.deploy_env"
set +a
[[ -n "${SOURCE_URL}" ]] && export REPO_URL="${SOURCE_URL}"
[[ -n "${BRANCH_OVERRIDE}" ]] && export BRANCH="${BRANCH_OVERRIDE}"
export APP_ROOT
if [[ ! -f "${APP_ROOT}/app/update.sh" ]]; then
echo "Update script not found: ${APP_ROOT}/app/update.sh. Pull the latest repo and run deploy again." >&2
exit 1
fi
exec bash "${APP_ROOT}/app/update.sh"