refactor: unify stack management output

This commit is contained in:
lingkai5wu committed 2026-10-10 16:09:37 +08:00
1 parent c76b2dfb9d
commit 50d9fc69ad
1 file changed
+36 -17
+36 -17
View File
@@ -9,6 +9,11 @@ CONFIG_FILE="${STACKS_CONFIG:-$STACKS_DIR/manage.conf}"
LOCK_FILE="${STACKS_LOCK_FILE:-/run/lock/stacks-manage.lock}"
DEFAULT_MAX_PARALLEL=2
OUTPUT_IS_TTY=0
if [[ -t 1 && -z "${NO_COLOR+x}" ]]; then
OUTPUT_IS_TTY=1
fi
CONFIG_GROUP_NAMES=()
CONFIG_GROUP_LIMITS=()
@@ -26,6 +31,7 @@ UNASSIGNED_STACKS=()
RUN_SUCCESS=0
RUN_FAILED=0
RUN_FAILED_STACKS=()
RUN_SKIPPED_GROUPS=()
die() {
echo "ERROR: $*" >&2
@@ -297,7 +303,7 @@ run_stack_prefixed() {
local action="$2"
local line status
STACKS_FORCE_ANSI=1 run_stack "$stack" "$action" 0 2>&1 |
STACKS_FORCE_ANSI="$OUTPUT_IS_TTY" run_stack "$stack" "$action" 0 2>&1 |
while IFS= read -r line || [[ -n "$line" ]]; do
[[ -n "$line" ]] || continue
printf '[%s] %s\n' "$stack" "$line"
@@ -313,7 +319,7 @@ run_group() {
local group_name="${GROUP_NAMES[group_index]}"
local limit="${GROUP_LIMITS[group_index]}"
local semaphore_dir semaphore_fifo semaphore_fd
local stack pid i status
local stack pid i status details
local failed=0
local -a stacks=() pids=() pid_stacks=()
@@ -324,8 +330,16 @@ run_group() {
limit="${#stacks[@]}"
fi
if ((${#stacks[@]} == 1)); then
details="1 stack"
elif ((limit == ${#stacks[@]})); then
details="${#stacks[@]} stacks in parallel"
else
details="${#stacks[@]} stacks, max $limit in parallel"
fi
echo
echo "==> Group $group_name: $action (${#stacks[@]} stacks, parallel=$limit)"
echo "==> Group $group_name: $action ($details)"
semaphore_dir="$(mktemp -d "${TMPDIR:-/tmp}/stacks-manage.XXXXXX")" ||
die "cannot create semaphore directory"
@@ -373,33 +387,33 @@ run_group() {
print_run_summary() {
echo
echo "Succeeded: $RUN_SUCCESS"
echo "Failed: $RUN_FAILED"
echo "Result: $RUN_SUCCESS succeeded, $RUN_FAILED failed"
if ((${#RUN_FAILED_STACKS[@]} > 0)); then
echo "Failures: ${RUN_FAILED_STACKS[*]}"
echo "Failed stacks: ${RUN_FAILED_STACKS[*]}"
fi
if ((${#RUN_SKIPPED_GROUPS[@]} > 0)); then
echo "Skipped groups: ${RUN_SKIPPED_GROUPS[*]}"
fi
}
print_skipped_groups() {
collect_skipped_groups() {
local failed_index="$1"
local direction="$2"
local i
local -a skipped=()
RUN_SKIPPED_GROUPS=()
if [[ "$direction" == "reverse" ]]; then
for ((i = failed_index - 1; i >= 0; i--)); do
skipped+=("${GROUP_NAMES[i]}")
RUN_SKIPPED_GROUPS+=("${GROUP_NAMES[i]}")
done
else
for ((i = failed_index + 1; i < ${#GROUP_NAMES[@]}; i++)); do
skipped+=("${GROUP_NAMES[i]}")
RUN_SKIPPED_GROUPS+=("${GROUP_NAMES[i]}")
done
fi
if ((${#skipped[@]} > 0)); then
echo "Skipped groups after failure: ${skipped[*]}" >&2
fi
}
run_all_grouped() {
@@ -410,13 +424,14 @@ run_all_grouped() {
RUN_SUCCESS=0
RUN_FAILED=0
RUN_FAILED_STACKS=()
RUN_SKIPPED_GROUPS=()
warn_unassigned
if [[ "$action" == "stop" || "$action" == "down" ]]; then
direction="reverse"
for ((i = ${#GROUP_NAMES[@]} - 1; i >= 0; i--)); do
if ! run_group "$i" "$action"; then
print_skipped_groups "$i" "$direction"
collect_skipped_groups "$i" "$direction"
print_run_summary
return 1
fi
@@ -424,7 +439,7 @@ run_all_grouped() {
else
for ((i = 0; i < ${#GROUP_NAMES[@]}; i++)); do
if ! run_group "$i" "$action"; then
print_skipped_groups "$i" "$direction"
collect_skipped_groups "$i" "$direction"
print_run_summary
return 1
fi
@@ -441,6 +456,7 @@ run_all_sequential() {
RUN_SUCCESS=0
RUN_FAILED=0
RUN_FAILED_STACKS=()
RUN_SKIPPED_GROUPS=()
warn_unassigned
for stack in "${MANAGED_STACKS[@]}"; do
@@ -452,7 +468,10 @@ run_all_sequential() {
fi
done
print_run_summary
if ((RUN_FAILED > 0)); then
echo
echo "Failed stacks: ${RUN_FAILED_STACKS[*]}" >&2
fi
((RUN_FAILED == 0))
}