Files
reterminal-dm4/emmc-provisioning/scripts/sync-portal-files-to-lxc.sh
nearxos 0844adbcbe Update cloud-init scripts and documentation for enhanced DNS management and provisioning steps</message>
<message>Modify the first-boot.sh script to include an additional step for managing screen brightness during the provisioning process. Update user-data.bootstrap to improve DNS configuration by ensuring NetworkManager manages /etc/resolv.conf correctly, and remove obsolete scripts related to systemd-resolved. Enhance documentation to reflect these changes and clarify the setup process for users, improving overall network boot functionality and user experience.
2026-03-06 14:45:23 +02:00

162 lines
7.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Revision: 4
# Sync portal (file server) content from the repo to the LXC.
#
# All files that first-boot.sh downloads live in cloud-init/fileserver/.
# This script rsyncs that folder to the LXC's portal-files directory so
# the dashboard /files/ endpoint serves them.
#
# Expected layout on the LXC after sync:
# /var/lib/cm4-provisioning/portal-files/
# ├── first-boot.sh ← cloud-init runcmd downloads this
# ├── first-boot.conf ← cloud-init runcmd downloads this (required)
# ├── first-boot.conf.example ← reference
# ├── bootstrap.sh ← user-data.bootstrap downloads this (main path)
# ├── screen-brightness.py ← manual deploy: .../files/screen-brightness.py
# ├── screen-brightness.service
# ├── screen-brightness.conf
# └── first-boot/ ← FILE_SERVER points here (first-boot + step 14)
# ├── steps/
# │ ├── 01-hostname.sh … 14-screen_brightness.sh
# ├── start-chromium.sh
# ├── splash.png
# ├── screen-brightness.py (and .service, .conf)
# └── ...
#
# Usage: ./sync-portal-files-to-lxc.sh [user@lxc_ip]
# Example: ./sync-portal-files-to-lxc.sh root@10.130.60.141
set -e
LXC="${1:-root@10.130.60.141}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_DIR="$(dirname "$SCRIPT_DIR")"
CLOUDINIT_DIR="$REPO_DIR/cloud-init"
FILESERVER_DIR="$CLOUDINIT_DIR/fileserver"
REMOTE_PORTAL="/var/lib/cm4-provisioning/portal-files"
REMOTE_FIRST_BOOT="${REMOTE_PORTAL}/first-boot"
# Files we sync to the portal root (outside first-boot/)
PORTAL_ROOT_FILES=(first-boot.sh first-boot.conf first-boot.conf.example bootstrap.sh)
# Files from fileserver/ that we also copy to portal root so /files/<name> works
# (e.g. manual deploy: sudo ./deploy-screen-brightness-to-device.sh "http://HOST:5000/files")
FILESERVER_FILES_AT_PORTAL_ROOT=(screen-brightness.py screen-brightness.service screen-brightness.conf)
# ── Validate local files ────────────────────────────────────────────────
if [[ ! -d "$FILESERVER_DIR" ]]; then
echo "Error: fileserver dir not found: $FILESERVER_DIR"
exit 1
fi
if [[ ! -f "$CLOUDINIT_DIR/first-boot.sh" ]]; then
echo "Error: first-boot.sh not found in $CLOUDINIT_DIR"
exit 1
fi
if [[ ! -f "$CLOUDINIT_DIR/first-boot.conf" ]]; then
echo "Warning: first-boot.conf not found — first-boot.sh requires it!"
fi
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║ Sync portal files → $LXC"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""
# ── Ensure rsync on LXC ────────────────────────────────────────────────
ssh "$LXC" "command -v rsync >/dev/null 2>&1 || (apt-get update -qq && apt-get install -y rsync)"
ssh "$LXC" "mkdir -p $REMOTE_FIRST_BOOT"
# ── Dry-run: show what will change ──────────────────────────────────────
echo "── Preview (dry-run) ──────────────────────────────────────────"
echo ""
echo "Portal root ($REMOTE_PORTAL/):"
for f in "${PORTAL_ROOT_FILES[@]}"; do
if [[ -f "$CLOUDINIT_DIR/$f" ]]; then
echo "$f"
else
echo "$f (not found locally, will skip)"
fi
done
for f in "${FILESERVER_FILES_AT_PORTAL_ROOT[@]}"; do
if [[ -f "$FILESERVER_DIR/$f" ]]; then
echo "$f (from fileserver/)"
else
echo "$f (not in fileserver/, will skip portal root copy)"
fi
done
echo ""
echo "first-boot/ assets ($REMOTE_FIRST_BOOT/):"
CHANGES=$(rsync -avzn --delete "$FILESERVER_DIR/" "$LXC:$REMOTE_FIRST_BOOT/" 2>&1 | grep -v '^\(sending\|sent\|total\|$\|building\|\.d\.\.\.\.\.\.\.\.\.\)' | head -40)
if [[ -z "$CHANGES" ]]; then
echo " (no changes)"
else
echo "$CHANGES" | sed 's/^/ /'
fi
echo ""
read -rp "Proceed with sync? [Y/n] " CONFIRM
CONFIRM="${CONFIRM:-Y}"
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
echo "Aborted."
exit 0
fi
# ── Sync portal root files ──────────────────────────────────────────────
echo ""
echo "── Syncing portal root files ──────────────────────────────────"
for f in "${PORTAL_ROOT_FILES[@]}"; do
if [[ -f "$CLOUDINIT_DIR/$f" ]]; then
rsync -avz "$CLOUDINIT_DIR/$f" "$LXC:$REMOTE_PORTAL/"
fi
done
# ── Sync fileserver/ → first-boot/ (with --delete) ──────────────────────
echo ""
echo "── Syncing fileserver/ → first-boot/ ──────────────────────────"
rsync -avz --delete "$FILESERVER_DIR/" "$LXC:$REMOTE_FIRST_BOOT/"
# ── Copy screen-brightness (and similar) from fileserver to portal root ───
# So http://HOST:5000/files/screen-brightness.py works for manual deploy
echo ""
echo "── Copying fileserver files to portal root ───────────────────"
for f in "${FILESERVER_FILES_AT_PORTAL_ROOT[@]}"; do
if [[ -f "$FILESERVER_DIR/$f" ]]; then
rsync -avz "$FILESERVER_DIR/$f" "$LXC:$REMOTE_PORTAL/"
echo "$f"
fi
done
# ── Check for extra files in portal root ────────────────────────────────
echo ""
echo "── Checking for extra files in portal root ────────────────────"
REMOTE_FILES=$(ssh "$LXC" "find $REMOTE_PORTAL -maxdepth 1 -not -path $REMOTE_PORTAL -printf '%f\n' 2>/dev/null" | sort)
EXPECTED_FILES=$(printf '%s\n' "${PORTAL_ROOT_FILES[@]}" "${FILESERVER_FILES_AT_PORTAL_ROOT[@]}" "first-boot" | sort -u)
EXTRA_FILES=$(comm -23 <(echo "$REMOTE_FILES") <(echo "$EXPECTED_FILES"))
if [[ -z "$EXTRA_FILES" ]]; then
echo " No extra files found. Portal root is clean."
else
echo " Extra files/folders found in $REMOTE_PORTAL/:"
echo "$EXTRA_FILES" | while read -r f; do
SIZE=$(ssh "$LXC" "du -sh '$REMOTE_PORTAL/$f' 2>/dev/null | cut -f1" 2>/dev/null || echo "?")
TYPE="file"
ssh "$LXC" "[ -d '$REMOTE_PORTAL/$f' ]" 2>/dev/null && TYPE="dir"
echo " $f ($TYPE, $SIZE)"
done
echo ""
read -rp " Remove extra files? [y/N] " REMOVE
if [[ "$REMOVE" =~ ^[Yy]$ ]]; then
echo "$EXTRA_FILES" | while read -r f; do
echo " Removing: $f"
ssh "$LXC" "rm -rf '$REMOTE_PORTAL/$f'"
done
echo " Done."
else
echo " Kept extra files."
fi
fi
echo ""
echo "══════════════════════════════════════════════════════════════"
echo "Done. Portal files at http://$(echo "$LXC" | cut -d@ -f2):5000/files/"
echo "══════════════════════════════════════════════════════════════"