web: Exclude web stack from Update All button (#142)

This commit is contained in:
Bas Nijholt
2026-01-04 19:56:41 +01:00
committed by GitHub
parent 8302f1d97a
commit 5e08f1d712
6 changed files with 17 additions and 99 deletions

View File

@@ -4,7 +4,6 @@ from __future__ import annotations
import asyncio
import contextlib
import os
from pathlib import Path
from typing import TYPE_CHECKING, Annotated, TypeVar
@@ -69,21 +68,6 @@ ServiceOption = Annotated[
_MISSING_PATH_PREVIEW_LIMIT = 2
_STATS_PREVIEW_LIMIT = 3 # Max number of pending migrations to show by name
# Environment variable to identify the web stack (for self-update ordering)
CF_WEB_STACK = os.environ.get("CF_WEB_STACK", "")
def sort_web_stack_last(stacks: list[str]) -> list[str]:
"""Move the web stack to the end of the list.
When updating all stacks, the web UI stack (compose-farm) should be updated
last. Otherwise, the container restarts mid-process and cancels remaining
updates. The CF_WEB_STACK env var identifies the web stack.
"""
if CF_WEB_STACK and CF_WEB_STACK in stacks:
return [s for s in stacks if s != CF_WEB_STACK] + [CF_WEB_STACK]
return stacks
def format_host(host: str | list[str]) -> str:
"""Format a host value for display."""

View File

@@ -23,7 +23,6 @@ from compose_farm.cli.common import (
maybe_regenerate_traefik,
report_results,
run_async,
sort_web_stack_last,
validate_host_for_stack,
validate_stacks,
)
@@ -172,8 +171,6 @@ def restart(
raw = True
results = run_async(run_on_stacks(cfg, stack_list, f"restart {service}", raw=raw))
else:
# Sort web stack last to avoid self-restart canceling remaining restarts
stack_list = sort_web_stack_last(stack_list)
raw = len(stack_list) == 1
results = run_async(run_sequential_on_stacks(cfg, stack_list, ["down", "up -d"], raw=raw))
maybe_regenerate_traefik(cfg, results)
@@ -209,8 +206,6 @@ def update(
)
)
else:
# Sort web stack last to avoid self-restart canceling remaining updates
stack_list = sort_web_stack_last(stack_list)
raw = len(stack_list) == 1
results = run_async(
run_sequential_on_stacks(
@@ -333,11 +328,6 @@ def apply( # noqa: C901, PLR0912, PLR0915 (multi-phase reconciliation needs the
console.print(f"\n{MSG_DRY_RUN}")
return
# Sort web stack last in each phase to avoid self-restart canceling remaining work
migrations = sort_web_stack_last(migrations)
missing = sort_web_stack_last(missing)
to_refresh = sort_web_stack_last(to_refresh)
# Execute changes
console.print()
all_results = []

View File

@@ -3,6 +3,7 @@
from __future__ import annotations
import asyncio
import os
import uuid
from typing import TYPE_CHECKING, Any
@@ -14,6 +15,9 @@ if TYPE_CHECKING:
from compose_farm.web.deps import get_config
from compose_farm.web.streaming import run_cli_streaming, run_compose_streaming, tasks
# Environment variable to identify the web stack (for exclusion from bulk updates)
CF_WEB_STACK = os.environ.get("CF_WEB_STACK", "")
router = APIRouter(tags=["actions"])
# Store task references to prevent garbage collection
@@ -96,7 +100,15 @@ async def pull_all() -> dict[str, Any]:
@router.post("/update-all")
async def update_all() -> dict[str, Any]:
"""Update all stacks (pull + build + down + up)."""
"""Update all stacks (pull + build + down + up), excluding the web stack.
The web stack is excluded to prevent the UI from shutting down mid-operation.
Use 'cf update <web-stack>' manually to update the web UI.
"""
config = get_config()
task_id = _start_task(lambda tid: run_cli_streaming(config, ["update", "--all"], tid))
return {"task_id": task_id, "command": "update --all"}
# Get all stacks except the web stack to avoid self-shutdown
stacks = [s for s in config.stacks if s != CF_WEB_STACK]
if not stacks:
return {"task_id": "", "command": "update (no stacks)", "skipped": True}
task_id = _start_task(lambda tid: run_cli_streaming(config, ["update", *stacks], tid))
return {"task_id": task_id, "command": f"update {' '.join(stacks)}"}

View File

@@ -608,7 +608,7 @@ function playFabIntro() {
cmd('action', 'Apply', 'Make reality match config', dashboardAction('apply'), icons.check),
cmd('action', 'Refresh', 'Update state from reality', dashboardAction('refresh'), icons.refresh_cw),
cmd('action', 'Pull All', 'Pull latest images for all stacks', dashboardAction('pull-all'), icons.cloud_download),
cmd('action', 'Update All', 'Update all stacks', dashboardAction('update-all'), icons.refresh_cw),
cmd('action', 'Update All', 'Update all stacks except web', dashboardAction('update-all'), icons.refresh_cw),
cmd('app', 'Theme', 'Change color theme', openThemePicker, icons.palette),
cmd('app', 'Dashboard', 'Go to dashboard', nav('/'), icons.home),
cmd('app', 'Live Stats', 'View all containers across hosts', nav('/live-stats'), icons.box),

View File

@@ -18,7 +18,7 @@
{{ action_btn("Apply", "/api/apply", "primary", "Make reality match config", check()) }}
{{ action_btn("Refresh", "/api/refresh", "outline", "Update state from reality", refresh_cw()) }}
{{ action_btn("Pull All", "/api/pull-all", "outline", "Pull latest images for all stacks", cloud_download()) }}
{{ action_btn("Update All", "/api/update-all", "outline", "Update all stacks (pull + build + down + up)", rotate_cw()) }}
{{ action_btn("Update All", "/api/update-all", "outline", "Update all stacks except web (pull + build + down + up)", rotate_cw()) }}
<div class="tooltip" data-tip="Save compose-farm.yaml config file"><button id="save-config-btn" class="btn btn-outline">{{ save() }} Save Config</button></div>
</div>

View File

@@ -437,71 +437,3 @@ class TestDownOrphaned:
)
assert exc_info.value.exit_code == 1
class TestSortWebStackLast:
"""Tests for the sort_web_stack_last helper."""
def test_no_web_stack_env(self) -> None:
"""When CF_WEB_STACK is not set, list is unchanged."""
from compose_farm.cli import common
original = common.CF_WEB_STACK
try:
common.CF_WEB_STACK = ""
stacks = ["a", "b", "c"]
result = common.sort_web_stack_last(stacks)
assert result == ["a", "b", "c"]
finally:
common.CF_WEB_STACK = original
def test_web_stack_not_in_list(self) -> None:
"""When web stack is not in list, list is unchanged."""
from compose_farm.cli import common
original = common.CF_WEB_STACK
try:
common.CF_WEB_STACK = "webstack"
stacks = ["a", "b", "c"]
result = common.sort_web_stack_last(stacks)
assert result == ["a", "b", "c"]
finally:
common.CF_WEB_STACK = original
def test_web_stack_moved_to_end(self) -> None:
"""When web stack is in list, it's moved to the end."""
from compose_farm.cli import common
original = common.CF_WEB_STACK
try:
common.CF_WEB_STACK = "webstack"
stacks = ["a", "webstack", "b", "c"]
result = common.sort_web_stack_last(stacks)
assert result == ["a", "b", "c", "webstack"]
finally:
common.CF_WEB_STACK = original
def test_web_stack_already_last(self) -> None:
"""When web stack is already last, list is unchanged."""
from compose_farm.cli import common
original = common.CF_WEB_STACK
try:
common.CF_WEB_STACK = "webstack"
stacks = ["a", "b", "webstack"]
result = common.sort_web_stack_last(stacks)
assert result == ["a", "b", "webstack"]
finally:
common.CF_WEB_STACK = original
def test_empty_list(self) -> None:
"""Empty list returns empty list."""
from compose_farm.cli import common
original = common.CF_WEB_STACK
try:
common.CF_WEB_STACK = "webstack"
result = common.sort_web_stack_last([])
assert result == []
finally:
common.CF_WEB_STACK = original