Show target host in every step header
Each step now prints "--- [Label] on <host> ---". The host is threaded through the remote self-bootstrap as argv[2] so it shows the real server name when running over ssh. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+21
-8
@@ -49,6 +49,16 @@ print_banner() {
|
||||
echo ""
|
||||
}
|
||||
|
||||
# -----------------------------
|
||||
# Host đang chạy (đặt ở main; hiển thị lại trong mỗi header bước)
|
||||
# -----------------------------
|
||||
CURRENT_TARGET="local"
|
||||
|
||||
# In header bước kèm host: --- [Label] on <host> ---
|
||||
step_header() {
|
||||
echo "${C_TITLE}--- [$1] on ${CURRENT_TARGET} ---${C_RESET}"
|
||||
}
|
||||
|
||||
# -----------------------------
|
||||
# Phát hiện distro qua /etc/os-release → debian | arch | unknown
|
||||
# -----------------------------
|
||||
@@ -129,7 +139,7 @@ select_target() {
|
||||
# Bước 1: Nâng cấp hệ thống theo distro
|
||||
# -----------------------------
|
||||
step_upgrade() {
|
||||
echo "${C_TITLE}--- [System Upgrade] ---${C_RESET}"
|
||||
step_header "System Upgrade"
|
||||
local distro
|
||||
distro=$(detect_distro)
|
||||
case "$distro" in
|
||||
@@ -182,7 +192,7 @@ docker_cmd() {
|
||||
# Bước 2: Docker — df rồi prune từng loại
|
||||
# -----------------------------
|
||||
step_docker() {
|
||||
echo "${C_TITLE}--- [Docker] ---${C_RESET}"
|
||||
step_header "Docker"
|
||||
if ! command -v docker >/dev/null 2>&1; then
|
||||
echo "${C_WARN} ⚠ docker command not found.${C_RESET}"
|
||||
echo ""
|
||||
@@ -211,7 +221,7 @@ step_docker() {
|
||||
# Bước 3: Journal logs — vacuum log cũ hơn 7 ngày
|
||||
# -----------------------------
|
||||
step_journal() {
|
||||
echo "${C_TITLE}--- [Journal Logs] ---${C_RESET}"
|
||||
step_header "Journal Logs"
|
||||
if ! command -v journalctl >/dev/null 2>&1; then
|
||||
echo "${C_WARN} ⚠ journalctl not found.${C_RESET}"
|
||||
echo ""
|
||||
@@ -229,7 +239,7 @@ step_journal() {
|
||||
# Bước 4: Package cache — apt clean+autoremove hoặc paccache/pacman -Sc
|
||||
# -----------------------------
|
||||
step_pkg_cache() {
|
||||
echo "${C_TITLE}--- [Package Cache] ---${C_RESET}"
|
||||
step_header "Package Cache"
|
||||
local distro
|
||||
distro=$(detect_distro)
|
||||
case "$distro" in
|
||||
@@ -269,7 +279,7 @@ step_pkg_cache() {
|
||||
# Bước 5: Snap — forget từng snapshot
|
||||
# -----------------------------
|
||||
step_snap() {
|
||||
echo "${C_TITLE}--- [Snap] ---${C_RESET}"
|
||||
step_header "Snap"
|
||||
if ! command -v snap >/dev/null 2>&1; then
|
||||
echo "${C_WARN} ⚠ snap not found.${C_RESET}"
|
||||
echo ""
|
||||
@@ -297,7 +307,7 @@ step_snap() {
|
||||
# Bước 6: /tmp & cache cũ — liệt kê file >7 ngày, xác nhận rồi xóa
|
||||
# -----------------------------
|
||||
step_tmp_cache() {
|
||||
echo "${C_TITLE}--- [/tmp & Cache cũ] ---${C_RESET}"
|
||||
step_header "/tmp & Cache cũ"
|
||||
local days=7
|
||||
echo " Tìm file cũ hơn ${days} ngày trong /tmp /var/tmp:"
|
||||
local old_files
|
||||
@@ -342,6 +352,7 @@ run_all_steps() {
|
||||
run_on_target() {
|
||||
local target="$1"
|
||||
if [ "$target" = "local" ]; then
|
||||
CURRENT_TARGET="local"
|
||||
run_all_steps
|
||||
return $?
|
||||
fi
|
||||
@@ -351,8 +362,9 @@ run_on_target() {
|
||||
return 1
|
||||
}
|
||||
# Giải mã ra file tạm trên remote rồi chạy; stdin không bị redirect → read OK.
|
||||
# Truyền target làm argv[2] để remote biết host (hiển thị lại trong header).
|
||||
ssh -t "$target" \
|
||||
"umask 077; t=\$(mktemp); printf '%s' '$b64' | base64 -d > \"\$t\" && bash \"\$t\" --run-steps; r=\$?; rm -f \"\$t\"; exit \$r" \
|
||||
"umask 077; t=\$(mktemp); printf '%s' '$b64' | base64 -d > \"\$t\" && bash \"\$t\" --run-steps \"$target\"; r=\$?; rm -f \"\$t\"; exit \$r" \
|
||||
|| { echo "${C_DEL} ✗ ssh tới $target thất bại.${C_RESET}"; return 1; }
|
||||
}
|
||||
|
||||
@@ -361,7 +373,8 @@ run_on_target() {
|
||||
# -----------------------------
|
||||
main() {
|
||||
if [ "${1:-}" = "--run-steps" ]; then
|
||||
echo "${C_TITLE}===== 🧹 Cleanup-server (--run-steps) =====${C_RESET}"
|
||||
CURRENT_TARGET="${2:-local}"
|
||||
echo "${C_TITLE}===== 🧹 Cleanup-server on ${CURRENT_TARGET} =====${C_RESET}"
|
||||
echo ""
|
||||
run_all_steps
|
||||
echo ""
|
||||
|
||||
Reference in New Issue
Block a user