LibrePortal/scripts/cli/commands/app/cli_app_commands.sh
librelad e5273a482d feat(cli): route app install through the task processor + live follower
Spike — closes the gap where the CLI install bypassed the very task system
the WebUI uses. Now both surfaces hit the same path:

  user types `libreportal app install dashy`
    → CLI enqueues a task file in $TASK_DIR (identical shape to the
      WebUI's createTaskFile)
    → pokes $TASK_DIR/.queue.fifo so the processor dispatches in <100ms
      instead of waiting up to IDLE_POLL_SECS
    → CLI tails the task log + polls .status, exits with the task's
      exit_code on terminal state
    → Ctrl-C detaches the follower without killing the task — the
      WebUI's tasks panel keeps showing it

Bypass: the recursive command in the task file is prefixed
`LIBREPORTAL_TASK_EXEC=1 libreportal app install <name>`. The install
branch in cli_app_commands.sh honours that env var by running inline,
which is what the processor's eval invocation hits. No processor
changes — the bypass travels with the task.

Wins:
  - one log file per install, shared by CLI + WebUI (audit trail + replay)
  - locking serialises CLI + WebUI installs (no more two-frontend race)
  - WebUI's "current task" indicator now reflects CLI work too
  - free `--detach` for fire-and-forget queueing

New: scripts/cli/task/cli_task_run.sh
  cliTaskRun <cmd> [type] [app] [--detach]
    Enqueues + follows; --detach prints the task id and exits 0.
  cliTaskFollow <task_id>
    `tail -F` the log + jq-poll the status; returns the task's exit_code.
    Designed to be reused for `libreportal task log <id>` reattach later.

Trade-off: ~200-500ms latency before the first byte (write task file,
processor wakes, opens log, follower starts tailing). Negligible for
install/update/backup — fast commands (list/status/config get) still
run inline. The current branch only changes `app install`; uninstall +
update + backup can be moved on the same pattern once this lands clean.

Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-27 14:29:30 +01:00

154 lines
5.5 KiB
Bash
Executable File

#!/bin/bash
# App Commands Handler
# Handles all app subcommands by calling core functions
cliHandleAppCommands()
{
local action="$initial_command2"
local app_name="$initial_command3"
local config="$initial_command4"
local restore_arg2="$initial_command4"
local restore_arg3="$initial_command5"
local restore_arg4="$initial_command6"
local tool_name="$initial_command4"
local tool_args="$initial_command5"
local reset_network="false"
if [[ "$config" == "--reset-network" ]]; then
reset_network="true"
config=""
elif [[ "$initial_command5" == "--reset-network" ]]; then
reset_network="true"
fi
case "$action" in
"list")
if [[ -z "$app_name" ]]; then
cliShowAppHelp
elif [ "$app_name" = "available" ]; then
appScanAvailable
elif [ "$app_name" = "installed" ]; then
databaseListInstalledApps
else
isNotice "Invalid list type: $app_name"
cliShowAppHelp
fi
;;
"install")
# Two paths in / out:
# * The task processor's recursive invocation carries
# LIBREPORTAL_TASK_EXEC=1 — run the install inline so it
# actually does the work (otherwise we'd loop forever
# re-enqueueing).
# * The user typed `libreportal app install dashy` — enqueue
# a task and follow its log in the foreground, so the
# CLI + WebUI watch the same job and locking serialises
# parallel attempts. `--detach` queues and exits.
if [[ "$LIBREPORTAL_TASK_EXEC" == "1" ]]; then
dockerInstallApp "$app_name" "$config" "$reset_network"
else
local _mode=""
for _arg in "$config" "$initial_command5" "$initial_command6"; do
[[ "$_arg" == "--detach" ]] && _mode="--detach"
done
# config / --reset-network passthrough — strip the flags
# that are CLI-only, keep what dockerInstallApp expects.
local _passthrough_config="$config"
[[ "$_passthrough_config" == "--detach" || "$_passthrough_config" == "--reset-network" ]] && _passthrough_config=""
local _cmd="LIBREPORTAL_TASK_EXEC=1 libreportal app install $app_name"
[[ -n "$_passthrough_config" ]] && _cmd+=" '$_passthrough_config'"
[[ "$reset_network" == "true" ]] && _cmd+=" --reset-network"
cliTaskRun "$_cmd" "install" "$app_name" "$_mode"
fi
;;
"uninstall")
# Optional `--delete-images` flag (in any of the trailing
# positions) tells the uninstall to also remove the app's
# docker images. Default behaviour: keep them so a reinstall
# is fast and offline-friendly.
local _del_images="false"
local _del_tasks="false"
for _arg in "$config" "$initial_command5" "$initial_command6" "$initial_command7"; do
[[ "$_arg" == "--delete-images" ]] && _del_images="true"
[[ "$_arg" == "--delete-tasks" ]] && _del_tasks="true"
done
dockerUninstallApp "$app_name" "$_del_images" "$_del_tasks"
;;
"start")
dockerStartApp "$app_name"
;;
"stop")
dockerStopApp "$app_name"
;;
"restart")
dockerRestartApp "$app_name"
;;
"up")
dockerComposeUp "$app_name"
;;
"down")
dockerComposeDown "$app_name"
;;
"reload")
dockerRestartAppViaInstall "$app_name"
;;
"backup")
if [[ -z "$app_name" ]]; then
isNotice "No app provided."
cliShowAppHelp
else
backupAppStart "$app_name"
fi
;;
"restore")
if [[ -z "$app_name" ]]; then
isNotice "No app provided."
cliShowAppHelp
else
cliAppRestore "$app_name" "$restore_arg2" "$restore_arg3" "$restore_arg4"
fi
;;
"status")
if [[ -z "$app_name" ]]; then
isNotice "No app provided."
cliShowAppHelp
else
appStatus "$app_name"
fi
;;
"tool")
# `libreportal app tool list [<app>]` — discover available tools.
# When the second arg is the literal `list`, the third is treated
# as an optional app filter. Otherwise the standard run shape
# applies: `libreportal app tool <app> <tool_id> [args]`.
if [[ "$app_name" == "list" ]]; then
cliAppToolList "$tool_name"
elif [[ -z "$app_name" || -z "$tool_name" ]]; then
isNotice "Usage: libreportal app tool <app_name> <tool_name> [args]"
isNotice " libreportal app tool list [<app_name>]"
cliShowAppHelp
else
dockerAppRunTool "$app_name" "$tool_name" "$tool_args"
fi
;;
*)
isNotice "Invalid app command: $action"
cliShowAppHelp
;;
esac
}