Refactor deploy-to-proxmox.sh to improve container existence check and streamline storage selection: implement logic to verify if the cm4-provisioning container exists, adjust storage selection process accordingly, and enhance logging for better user feedback. Update documentation for clarity on new behavior.

This commit is contained in:
nearxos
2026-02-19 14:52:13 +02:00
parent 2d3687fb7c
commit 987e71c36e

View File

@@ -3,11 +3,12 @@
# LXC container (dashboard), usbboot (rpiboot), and PiShrink. Uses hostname "cm4-provisioning" # LXC container (dashboard), usbboot (rpiboot), and PiShrink. Uses hostname "cm4-provisioning"
# to find the container on redeploy; creates with next available ID if not found. # to find the container on redeploy; creates with next available ID if not found.
# #
# Redeploy (re-run) behaviour: skips steps that are already configured so you can update # Redeploy (re-run) behaviour: checks first if the cm4-provisioning container exists;
# only what changed. Always updates: host scripts, dashboard files, env, systemd/udev. # if so, skips storage selection. Skips other steps that are already configured so you
# Skips when present: LXC creation, backups bind-mount (if same path), usbboot, PiShrink, # can update only what changed. Always updates: host scripts, dashboard files, env,
# LXC python3-flask and openssh-server apt installs. Set DEPLOY_ROOTFS_STORAGE to avoid # systemd/udev. Skips when present: LXC creation, backups bind-mount (if same path),
# storage prompt on redeploy. # usbboot, PiShrink, LXC python3-flask and openssh-server apt installs. Set
# DEPLOY_ROOTFS_STORAGE to avoid storage prompt on first deploy (when container is new).
# #
# With host internet: installs usbboot and PiShrink so USB flash/backup and dashboard # With host internet: installs usbboot and PiShrink so USB flash/backup and dashboard
# Shrink/Compress work. The only manual step left is to add a golden image for Deploy. # Shrink/Compress work. The only manual step left is to add a golden image for Deploy.
@@ -62,21 +63,45 @@ if [[ -n "$KEY_FILE" && -f "$KEY_FILE" ]]; then
fi fi
log "Deploying to $PROXMOX ..." log "Deploying to $PROXMOX ..."
log "[1/5] Cleaning remote staging dir ..." log "[1/5] Checking if cm4-provisioning container exists and listing storage ..."
ssh "$PROXMOX" "rm -rf /tmp/emmc-provisioning-deploy" REMOTE_CHECK=$(ssh "$PROXMOX" "bash -s" << 'REMOTECHECK'
log "[2/5] Rsync repo to $PROXMOX ..." LXC_HOSTNAME="cm4-provisioning"
rsync -a "$REPO_DIR/" "$PROXMOX:/tmp/emmc-provisioning-deploy/" --exclude='.git' --exclude='scripts/deploy-to-proxmox.sh' --exclude='scripts/deploy-*.log' LXC_EXISTS=0
for id in $(pct list 2>/dev/null | awk 'NR>1 {print $1}'); do
h=$(pct config "$id" 2>/dev/null | sed -n 's/^hostname: *//p')
if [[ "$h" == "$LXC_HOSTNAME" ]]; then LXC_EXISTS=1; break; fi
done
echo "LXC_EXISTS=$LXC_EXISTS"
pvesm status 2>/dev/null | awk 'NR>1 { sub(/^[ \t]+/, ""); if ($2!="pbs" && $3~/active/) print $1 }'
REMOTECHECK
)
# --- Ask user to select LXC rootfs storage (from host; PBS excluded) --- first_line=
# Normalize: trim leading spaces so first column is storage name (pvesm can align columns)
STORAGE_LIST=$(ssh "$PROXMOX" "pvesm status 2>/dev/null | awk 'NR>1 { sub(/^[ \t]+/, \"\"); if (\$2!=\"pbs\" && \$3~/active/) print \$1 }'" 2>/dev/null || true)
storages=() storages=()
while IFS= read -r line; do [[ -n "$line" ]] && storages+=("$line"); done <<< "$STORAGE_LIST" while IFS= read -r line; do
if [[ -z "$first_line" ]]; then
first_line="$line"
else
[[ -n "$line" ]] && storages+=("$line")
fi
done <<< "$REMOTE_CHECK"
if [[ "$first_line" == "LXC_EXISTS=1" ]]; then
LXC_ALREADY_EXISTS=1
log "Container cm4-provisioning already exists; no storage selection needed."
if [[ ${#storages[@]} -eq 0 ]]; then
log "Error: no Proxmox storage found on host (needed for validation). Run on host: pvesm status"
exit 1
fi
ROOTFS_STORAGE="${storages[0]}"
log "Using storage $ROOTFS_STORAGE for validation only."
else
LXC_ALREADY_EXISTS=0
if [[ ${#storages[@]} -eq 0 ]]; then if [[ ${#storages[@]} -eq 0 ]]; then
log "Error: no Proxmox storage found on host (PBS excluded). Run on host: pvesm status" log "Error: no Proxmox storage found on host (PBS excluded). Run on host: pvesm status"
exit 1 exit 1
fi fi
# If DEPLOY_ROOTFS_STORAGE is set and in the list, use it without prompting # --- Ask user to select LXC rootfs storage only when creating new container ---
if [[ -n "${DEPLOY_ROOTFS_STORAGE:-}" ]]; then if [[ -n "${DEPLOY_ROOTFS_STORAGE:-}" ]]; then
for s in "${storages[@]}"; do for s in "${storages[@]}"; do
if [[ "$s" == "$DEPLOY_ROOTFS_STORAGE" ]]; then if [[ "$s" == "$DEPLOY_ROOTFS_STORAGE" ]]; then
@@ -109,8 +134,14 @@ if [[ -z "${ROOTFS_STORAGE:-}" ]]; then
done done
fi fi
fi fi
fi
log "[3/5] Running remote install (host + LXC) ..." log "[2/5] Cleaning remote staging dir ..."
ssh "$PROXMOX" "rm -rf /tmp/emmc-provisioning-deploy"
log "[3/5] Rsync repo to $PROXMOX ..."
rsync -a "$REPO_DIR/" "$PROXMOX:/tmp/emmc-provisioning-deploy/" --exclude='.git' --exclude='scripts/deploy-to-proxmox.sh' --exclude='scripts/deploy-*.log'
log "[4/5] Running remote install (host + LXC) ..."
# Pass optional LXC SSH vars (base64) and selected storage # Pass optional LXC SSH vars (base64) and selected storage
ssh "$PROXMOX" "ROOTFS_STORAGE='$ROOTFS_STORAGE' CM4_BACKUPS_HOST_PATH='${CM4_BACKUPS_HOST_PATH:-}' DEPLOY_SSH_KEY_B64='${DEPLOY_SSH_KEY_B64:-}' DEPLOY_LXC_PWD_B64='${DEPLOY_LXC_PWD_B64:-}'" bash -s << 'REMOTE' ssh "$PROXMOX" "ROOTFS_STORAGE='$ROOTFS_STORAGE' CM4_BACKUPS_HOST_PATH='${CM4_BACKUPS_HOST_PATH:-}' DEPLOY_SSH_KEY_B64='${DEPLOY_SSH_KEY_B64:-}' DEPLOY_LXC_PWD_B64='${DEPLOY_LXC_PWD_B64:-}'" bash -s << 'REMOTE'
@@ -308,6 +339,7 @@ LXC_IP=$(pct exec "$CTID" -- hostname -I 2>/dev/null | awk '{print $1}')
echo "${LXC_IP:-}" > "$DEPLOY/lxc_ip.txt" echo "${LXC_IP:-}" > "$DEPLOY/lxc_ip.txt"
log "Deploy done on remote. LXC ID: $CTID" log "Deploy done on remote. LXC ID: $CTID"
# Heredoc terminator (must be at column 1, no leading space/tab)
REMOTE REMOTE
# Read LXC IP written by remote (container hostname -I) # Read LXC IP written by remote (container hostname -I)