LibrePortal/scripts/cli/commands/app/cli_app_commands.sh
librelad 3f582120ba feat(cli): route all long-running app + update commands through tasks
Extends the install-routing spike (e5273a4) to every long-running CLI
command, so CLI and WebUI now share one execution path everywhere:

  app install      ← already done
  app uninstall
  app start / stop / restart / up / down / reload
  app backup
  app restore
  update apply
  backup app create   (matches `app backup` — same end target)

Each handler now has the same shape:
  if [[ "$LIBREPORTAL_TASK_EXEC" == "1" ]]; then
      <inline call>            # processor's recursive invocation
  else
      cliTaskRun "<cmd>" <type> <app>   # user invocation: enqueue + follow
  fi

Processor change — crontab_task_processor.sh:
  Adds `export LIBREPORTAL_TASK_EXEC=1` next to LIBREPORTAL_NONINTERACTIVE.
  Universal bypass: every task command the processor runs (CLI-queued OR
  pre-existing WebUI-queued like `libreportal app install adguard`)
  inherits the env var, so the inline branch fires and we never
  re-enqueue. This also lets us drop the env-var prefix the install spike
  was baking into the command string (e5273a4) — cleaner task files +
  one place to think about the bypass.

`backup app schedule` (the cron-driven path that already enqueues via
createTaskFile in backup_app_schedule.sh) is left alone — different
entry point, different runtime context, already correctly task-routed.

Why route the fast ones too (start/stop/restart/up/down):
  Consistency beats the ~1s task-roundtrip latency for a CLI button.
  Locking now serialises a CLI `app stop foo` against a WebUI restart of
  the same app; the audit trail covers every state change. Cheap to
  revert any individually if the latency turns out to bother someone.

Validated live earlier with `libreportal app install dashy` — task file
written, processor dispatched, follower streamed install live, exit 0
propagated. Same machinery now powers the other 9 handlers.

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

189 lines
7.0 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")
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 CLI-only
# flags, keep what dockerInstallApp expects.
local _passthrough_config="$config"
[[ "$_passthrough_config" == "--detach" || "$_passthrough_config" == "--reset-network" ]] && _passthrough_config=""
local _cmd="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"
local _u_mode=""
for _arg in "$config" "$initial_command5" "$initial_command6" "$initial_command7"; do
[[ "$_arg" == "--delete-images" ]] && _del_images="true"
[[ "$_arg" == "--delete-tasks" ]] && _del_tasks="true"
[[ "$_arg" == "--detach" ]] && _u_mode="--detach"
done
if [[ "$LIBREPORTAL_TASK_EXEC" == "1" ]]; then
dockerUninstallApp "$app_name" "$_del_images" "$_del_tasks"
else
local _cmd="libreportal app uninstall $app_name"
[[ "$_del_images" == "true" ]] && _cmd+=" --delete-images"
[[ "$_del_tasks" == "true" ]] && _cmd+=" --delete-tasks"
cliTaskRun "$_cmd" "uninstall" "$app_name" "$_u_mode"
fi
;;
"start")
if [[ "$LIBREPORTAL_TASK_EXEC" == "1" ]]; then
dockerStartApp "$app_name"
else
cliTaskRun "libreportal app start $app_name" "start" "$app_name"
fi
;;
"stop")
if [[ "$LIBREPORTAL_TASK_EXEC" == "1" ]]; then
dockerStopApp "$app_name"
else
cliTaskRun "libreportal app stop $app_name" "stop" "$app_name"
fi
;;
"restart")
if [[ "$LIBREPORTAL_TASK_EXEC" == "1" ]]; then
dockerRestartApp "$app_name"
else
cliTaskRun "libreportal app restart $app_name" "restart" "$app_name"
fi
;;
"up")
if [[ "$LIBREPORTAL_TASK_EXEC" == "1" ]]; then
dockerComposeUp "$app_name"
else
cliTaskRun "libreportal app up $app_name" "up" "$app_name"
fi
;;
"down")
if [[ "$LIBREPORTAL_TASK_EXEC" == "1" ]]; then
dockerComposeDown "$app_name"
else
cliTaskRun "libreportal app down $app_name" "down" "$app_name"
fi
;;
"reload")
if [[ "$LIBREPORTAL_TASK_EXEC" == "1" ]]; then
dockerRestartAppViaInstall "$app_name"
else
cliTaskRun "libreportal app reload $app_name" "reload" "$app_name"
fi
;;
"backup")
if [[ -z "$app_name" ]]; then
isNotice "No app provided."
cliShowAppHelp
elif [[ "$LIBREPORTAL_TASK_EXEC" == "1" ]]; then
backupAppStart "$app_name"
else
cliTaskRun "libreportal app backup $app_name" "backup" "$app_name"
fi
;;
"restore")
if [[ -z "$app_name" ]]; then
isNotice "No app provided."
cliShowAppHelp
elif [[ "$LIBREPORTAL_TASK_EXEC" == "1" ]]; then
cliAppRestore "$app_name" "$restore_arg2" "$restore_arg3" "$restore_arg4"
else
# Pass the positional args through verbatim. They may
# include local|remote1|… selector, filename, password —
# quote each to survive shell re-parse in the processor.
local _cmd="libreportal app restore $app_name"
for _a in "$restore_arg2" "$restore_arg3" "$restore_arg4"; do
[[ -n "$_a" ]] && _cmd+=" '$_a'"
done
cliTaskRun "$_cmd" "restore" "$app_name"
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
}