diff options
| author | Hibby <hibby@Hibbys-MacBook-Air.local> | 2026-04-26 06:05:30 -0500 |
|---|---|---|
| committer | Hibby <hibby@Hibbys-MacBook-Air.local> | 2026-04-26 06:05:30 -0500 |
| commit | d0515abd618be298fb9f956b3dfcbb21e856d237 (patch) | |
| tree | b5f3b55ca90f189098f0916e4643d46cc7f9d608 | |
Initial import
| -rw-r--r-- | build.sh | 728 |
1 files changed, 728 insertions, 0 deletions
diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..b4c8b6d --- /dev/null +++ b/build.sh @@ -0,0 +1,728 @@ +#!/bin/bash + +set -euo pipefail +MODE="${1:-build}" +### CONFIG ### +TIMESTAMP=$(date +%Y-%m-%d) +REPO_URL="https://git.hibbian.org/Hibbian/hibbian-livedisk.git" +WORKDIR="/opt/livebuild/work" +OUTPUT_DIR="/opt/livebuild/output" +AMD64_DIR="$OUTPUT_DIR/amd64" +ARM64_DIR="$OUTPUT_DIR/arm64" +REMOTE_TARGET="hibby@hibbian.org:/var/www/download/" +LOGFILE="/opt/livebuild/build-${TIMESTAMP}.log" +FINAL_NAME="${TIMESTAMP}-Hibbian-amd64.iso" + +run_build() { + if has_today_build "$AMD64_DIR" "amd64.iso"; then + echo "✔ Build already exists for today — skipping build" + generate_links_notes + run_stats + return 0 + fi + + echo "No build for today — proceeding..." + + # Clean previous working dir + sudo rm -rf "$WORKDIR" + mkdir -p "$WORKDIR" + cd "$WORKDIR" + + # Clone repo + git clone "$REPO_URL" config >> "$LOGFILE" 2>&1 + cd config + + # Run live-build config + echo "Running lb config..." >> "$LOGFILE" + lb config >> "$LOGFILE" 2>&1 + + # Build (must be root) + echo "Running lb build..." >> "$LOGFILE" + sudo lb build >> "$LOGFILE" 2>&1 + + # Find ISO + ISO=$(find . -maxdepth 1 -name "*.iso" | head -n 1) + + if [[ -z "$ISO" ]]; then + echo "ERROR: No ISO found!" >> "$LOGFILE" + exit 1 + fi + + # Prepare output dir + mkdir -p "$OUTPUT_DIR" + + cp "$ISO" "$AMD64_DIR/$FINAL_NAME" + + # generate checksum + (cd "$AMD64_DIR" && sha256sum "$FINAL_NAME" > "$FINAL_NAME.sha256") + + generate_links_notes + + # I don't care about the old ones + prune_old_builds + + # Transfer to remote server + run_sync + + echo "==== $(date) Build complete ====" >> "$LOGFILE" +} + +generate_links_notes() { + + # Create/update "latest" symlink + ln -sfn "$FINAL_NAME" "$AMD64_DIR/hibbian-latest.iso" + ln -sfn "$FINAL_NAME.sha256" "$AMD64_DIR/hibbian-latest.iso.sha256" + + #Generate release notes + generate_release_notes + + ln -sfn "${FINAL_NAME%.iso}-packages.txt" "$AMD64_DIR/hibbian-latest-packages.txt" + ln -sfn "${FINAL_NAME%.iso}-release-notes.txt" "$AMD64_DIR/hibbian-latest-release-notes.txt" + +} + +run_stats() { + echo "Regenerating stats (best effort)..." + + ensure_all_diffs # only depends on packages.txt + rebuild_metrics # skips missing safely + generate_index # renders whatever exists +} + +ensure_all_diffs() { + echo "Checking diffs..." + + # Collect package files (oldest → newest) + mapfile -t FILES < <(ls -1 "$OUTPUT_DIR/amd64"/*-packages.txt 2>/dev/null | sort) + + # Need at least 2 files to compare + if (( ${#FILES[@]} < 2 )); then + echo "Not enough package lists to generate diffs" + return + fi + + # Iterate safely: previous → current + for ((i=1; i<${#FILES[@]}; i++)); do + PREVIOUS="${FILES[$((i-1))]}" + CURRENT="${FILES[$i]}" + + # Skip if either file is missing or empty + [[ ! -s "$PREVIOUS" || ! -s "$CURRENT" ]] && { + echo "Skipping empty/missing pair:" + echo " PREV: $PREVIOUS" + echo " CURR: $CURRENT" + continue + } + + base=$(basename "$CURRENT" -packages.txt) + DIFF_FILE="$AMD64_DIR/${base}-changes.txt" + + # Skip if already exists + [[ -f "$DIFF_FILE" ]] && continue + + echo "Generating diff for $base..." + + # Use temp files (safe + debuggable) + TMP_PREV=$(mktemp) + TMP_CURR=$(mktemp) + + sort "$PREVIOUS" > "$TMP_PREV" + sort "$CURRENT" > "$TMP_CURR" + + { + echo "Changes since last build" + echo "========================" + echo "" + + echo "Previous: $(basename "$PREVIOUS")" + echo "Current: $(basename "$CURRENT")" + echo "" + + echo "New packages:" + comm -13 "$TMP_PREV" "$TMP_CURR" || true + echo "" + + echo "Removed packages:" + comm -23 "$TMP_PREV" "$TMP_CURR" || true + echo "" + + echo "Version changes:" + join -j1 "$TMP_PREV" "$TMP_CURR" \ + | awk '$2 != $3 { printf "%-30s %s -> %s\n", $1, $2, $3 }' + + } > "$DIFF_FILE" + + rm -f "$TMP_PREV" "$TMP_CURR" + done +} + +rebuild_metrics() { + METRICS_FILE="$OUTPUT_DIR/build-history.csv" + > "$METRICS_FILE" + + for pkg in "$OUTPUT_DIR"/amd64/*-packages.txt; do + base=$(basename "$pkg" -packages.txt) + + DATE=$(date -r "$pkg" "+%Y-%m-%d %H:%M") + PKG_COUNT=$(wc -l < "$pkg" 2>/dev/null || echo 0) + + CHANGES_FILE="$OUTPUT_DIR/${base}-changes.txt" + UPDATED_COUNT=$(grep -c "->" "$CHANGES_FILE" 2>/dev/null || echo 0) + + echo "$DATE,$base,$PKG_COUNT,$UPDATED_COUNT" >> "$METRICS_FILE" + done +} + +generate_index() { + INDEX_FILE="$OUTPUT_DIR/index.html" + + echo "Generating HTML index..." + + cat > "$INDEX_FILE" <<EOF +<!DOCTYPE html> +<html> +<head> + <meta charset="UTF-8"> + <title>Hibbian Downloads</title> + <style> + body { font-family: Arial, sans-serif; margin: 40px; } + h1 { color: #333; } + table { border-collapse: collapse; width: 100%; } + th, td { padding: 10px; border-bottom: 1px solid #ccc; text-align: left; } + a { text-decoration: none; color: #0066cc; } + ul { list-style: none; padding: 0; } + li { margin-bottom: 10px; } + strong { color: #333; } + code { + font-family: monospace; + font-size: 0.9em; + background: #f4f4f4; + padding: 2px 4px; + } + </style> +</head> +<body> + <h1>Hibbian Weekly Builds</h1> +EOF + +echo "<h2>Latest Downloads</h2>" >> "$INDEX_FILE" +echo "<ul>" >> "$INDEX_FILE" + +# --- AMD64 --- +LATEST_AMD=$(readlink "$AMD64_DIR/hibbian-latest.iso" 2>/dev/null || true) +if [[ -n "$LATEST_AMD" ]]; then + size=$(get_size "$LATEST_AMD") + checksum_link=$( [[ -f "$AMD64_DIR/hibbian-latest.iso.sha256" ]] && echo "<a href=\"amd64/hibbian-latest.iso.sha256\">checksum</a>" || echo "-" ) + notes_link=$( [[ -f "$AMD64_DIR/hibbian-latest-release-notes.txt" ]] && echo "<a href=\"amd64/hibbian-latest-release-notes.txt\">notes</a>" || echo "-" ) + changes_link=$( [[ -f "$AMD64_DIR/hibbian-latest-changes.txt" ]] && echo "<a href=\"amd64/hibbian-latest-changes.txt\">changes</a>" || echo "-" ) + + echo "<li> + <strong>PC (amd64):</strong> + <a href=\"amd64/hibbian-latest.iso\">Download ($size)</a> | + $checksum_link | + $notes_link | + $changes_link + </li>" >> "$INDEX_FILE" +fi + +# --- ARM64 --- +LATEST_ARM=$(readlink "$ARM64_DIR/hibbian-latest.img.zip" 2>/dev/null || true) + +if [[ -n "$LATEST_ARM" ]]; then + arm_path="$ARM64_DIR/$LATEST_ARM" + arm_base="${LATEST_ARM%.img.zip}" + build_date=$(extract_date "$LATEST_ARM") + + size=$(get_size "$arm_path") + + checksum_link=$( [[ -f "$OUTPUT_DIR/arm64/${arm_base}.img.sha256" ]] && echo "<a href=\"arm64/${arm_base}.img.sha256\">checksum</a>" || echo "-" ) + + # Match amd64 metadata + amd64_iso=$(find_matching_amd64 "$build_date") + + if [[ -n "$amd64_iso" ]]; then + amd_base=$(basename "$amd64_iso" .iso) + + notes_link="<a href=\"amd64/${amd_base}-release-notes.txt\">notes</a>" + changes_link="<a href=\"amd64/${amd_base}-changes.txt\">changes</a>" + else + notes_link="-" + changes_link="-" + fi + + echo "<li> + <strong>Raspberry Pi (arm64):</strong> + <a href=\"arm64/$LATEST_ARM\">Download ($size, zipped)</a> | + $checksum_link | + $notes_link | + $changes_link + </li>" >> "$INDEX_FILE" +fi + + get_latest_summary + + cat >> "$INDEX_FILE" <<EOF + <div style="border:1px solid #ccc; padding:15px; margin-bottom:20px; border-radius:8px; background:#f9f9f9;"> + <h2>Latest Changes Summary</h2> + <p> + 🆕 New: $NEW_COUNT + 🔄 Updated: $UPDATED_COUNT + (Major: $MAJOR_COUNT, Minor: $MINOR_COUNT, Patch: $PATCH_COUNT) + + ❌ Removed: $REMOVED_COUNT +</p> + <pre style="background:#fff; padding:10px; border:1px solid #eee;"> +$TOP_CHANGES + </pre> + </div> + + <h2>PC (AMD64)</h2> + <table> + <tr> + <th>Filename</th> + <th>Checksum</th> + <th>Date</th> + <th>Release Notes</th> + <th>Changes</th> + </tr> +EOF + +LATEST_AMD=$(readlink "$OUTPUT_DIR/amd64/hibbian-latest.iso" 2>/dev/null || true) + +for iso in "$OUTPUT_DIR"/amd64/*.iso; do + [[ -e "$iso" ]] || continue + + fname=$(basename "$iso") + base="${fname%.iso}" + + checksum_file="$OUTPUT_DIR/amd64/$fname.sha256" + notes_file="$OUTPUT_DIR/amd64/${base}-release-notes.txt" + changes_file="$OUTPUT_DIR/amd64/${base}-changes.txt" + + mod_date=$(date -r "$iso" "+%Y-%m-%d") + + # Latest label + if [[ "$fname" == "$LATEST_AMD" ]]; then + label=" <span class=\"latest\">(latest)</span>" + else + label="" + fi + + # Checksum + if [[ -f "$checksum_file" ]]; then + checksum=$(cut -d ' ' -f1 "$checksum_file") + checksum_display="<code>$checksum</code><br><a href=\"amd64/$fname.sha256\">download</a>" + else + checksum_display="-" + fi + + # Links + notes_link=$( [[ -f "$notes_file" ]] && echo "<a href=\"amd64/${base}-release-notes.txt\">notes</a>" || echo "-" ) + changes_link=$( [[ -f "$changes_file" ]] && echo "<a href=\"amd64/${base}-changes.txt\">changes</a>" || echo "-" ) + + echo "<tr> + <td><a href=\"amd64/$fname\">$fname</a>$label</td> + <td>$checksum_display</td> + <td>$mod_date</td> + <td>$notes_link</td> + <td>$changes_link</td> + </tr>" >> "$INDEX_FILE" +done + +echo "</table>" >> "$INDEX_FILE" + +echo "<h2>arm64 (Raspberry Pi)</h2>" >> "$INDEX_FILE" + +echo "<table> +<tr> + <th>File</th> + <th>Checksum</th> + <th>Date</th> + <th>Notes</th> + <th>Changes</th> +</tr>" >> "$INDEX_FILE" + +LATEST_ARM=$(readlink "$OUTPUT_DIR/arm64/hibbian-latest.img.zip" 2>/dev/null || true) + +for img in "$OUTPUT_DIR"/arm64/*.img.zip; do + [[ -e "$img" ]] || continue + + fname=$(basename "$img") + base="${fname%.img.zip}" + + checksum_file="$OUTPUT_DIR/arm64/$base.img.sha256" + + mod_date=$(date -r "$img" "+%Y-%m-%d") + + # Latest label + if [[ "$fname" == "$LATEST_ARM" ]]; then + label=" <span class=\"latest\">(latest)</span>" + else + label="" + fi + + # Checksum + if [[ -f "$checksum_file" ]]; then + checksum=$(cut -d ' ' -f1 "$checksum_file") + checksum_display="<code>$checksum</code><br><a href=\"arm64/$base.img.sha256\">download</a>" + else + checksum_display="-" + fi + + # Find matching amd64 build + amd64_iso=$(find_matching_amd64 "$build_date") + + if [[ -n "$amd64_iso" ]]; then + amd64_base=$(basename "$amd64_iso" .iso) + + notes_link="<a href=\"amd64/${amd64_base}-release-notes.txt\">notes</a>" + changes_link="<a href=\"amd64/${amd64_base}-changes.txt\">changes</a>" + else + notes_link="-" + changes_link="-" + fi + + echo "<tr> + <td><a href=\"arm64/$fname\">$fname</a>$label</td> + <td>$checksum_display</td> + <td>$mod_date</td> + <td>$notes_link</td> + <td>$changes_link</td> + </tr>" >> "$INDEX_FILE" +done + +cat >> "$INDEX_FILE" <<EOF +</table> +<h2>Build Trends</h2> +<pre> +EOF + +tail -n 10 "$OUTPUT_DIR/build-history.csv" >> "$INDEX_FILE" + +cat >> "$INDEX_FILE" <<EOF +</pre> +</body> +</html> +EOF +} + +has_today_build() { + local dir="$1" + local suffix="$2" + + local today + today=$(date +%Y-%m-%d) + + # Use glob safely (no ls) + for f in "$dir"/"${today}"-*"$suffix"; do + [[ -e "$f" ]] && return 0 + done + + return 1 +} + +get_size() { + local file="$1" + du -h "$file" 2>/dev/null | cut -f1 +} + +find_matching_amd64() { + local date="$1" + + ls "$OUTPUT_DIR/amd64"/${date}-Hibbian-amd64.iso 2>/dev/null | head -n 1 +} + +generate_release_notes() { + NOTES_FILE="$AMD64_DIR/${FINAL_NAME%.iso}-release-notes.txt" + PKG_FILE="$AMD64_DIR/${FINAL_NAME%.iso}-packages.txt" + + echo "Generating release notes for $FINAL_NAME..." + + # Locate chroot dynamically + CHROOT_DIR="${CHROOT_DIR:-$WORKDIR/config/chroot}" + + if [[ ! -d "$CHROOT_DIR" ]]; then + echo "⚠️ No chroot found, cannot generate package list" + return + fi + + sudo chroot "$CHROOT_DIR" dpkg-query -W -f='${Package} ${Version} ${Section}\n' \ + | awk '$3 ~ /^hamradio/ { print $1, $2 }' \ + | sort > "$PKG_FILE" + + { + echo "Hibbian Live Build" + echo "==================" + echo "" + echo "ISO: $FINAL_NAME" + echo "Generated: $(date)" + echo "" + echo "Hamradio Packages" + echo "-----------------" + cat "$PKG_FILE" + } > "$NOTES_FILE" +} + +generate_diff() { + CURRENT="$OUTPUT_DIR/${FINAL_NAME%.iso}-packages.txt" + PREVIOUS=$(ls -t "$OUTPUT_DIR"/*-packages.txt 2>/dev/null | grep -v "$FINAL_NAME" | head -n 1) + + DIFF_FILE="$OUTPUT_DIR/${FINAL_NAME%.iso}-changes.txt" + + echo "Generating diff vs previous build..." + + if [[ -z "$PREVIOUS" ]]; then + echo "No previous build to compare." > "$DIFF_FILE" + return + fi + + { + echo "Changes since last build" + echo "========================" + echo "" + + echo "Previous: $(basename "$PREVIOUS")" + echo "Current: $(basename "$CURRENT")" + echo "" + + echo "New packages:" + comm -13 <(cut -d ' ' -f1 "$PREVIOUS" | sort) \ + <(cut -d ' ' -f1 "$CURRENT" | sort) \ + || true + echo "" + + echo "Removed packages:" + comm -23 <(cut -d ' ' -f1 "$PREVIOUS" | sort) \ + <(cut -d ' ' -f1 "$CURRENT" | sort) \ + || true + echo "" + + echo "Version changes:" + join -j1 <(sort "$PREVIOUS") <(sort "$CURRENT") | \ + awk '$2 != $3 { printf "%-30s %s -> %s\n", $1, $2, $3 }' + + } > "$DIFF_FILE" +} + +get_latest_summary() { + LATEST_CHANGES=$(ls -t "$OUTPUT_DIR"/amd64/*-changes.txt 2>/dev/null | head -n 1) + + if [[ -z "$LATEST_CHANGES" || ! -s "$LATEST_CHANGES" ]]; then + NEW_COUNT=0 + REMOVED_COUNT=0 + UPDATED_COUNT=0 + MAJOR_COUNT=0 + MINOR_COUNT=0 + PATCH_COUNT=0 + TOP_CHANGES="No data yet" + return + fi + + # Single-pass awk for all counts + classification + read -r NEW_COUNT REMOVED_COUNT UPDATED_COUNT MAJOR_COUNT MINOR_COUNT PATCH_COUNT < <( + awk ' + function splitver(v, a, tmp) { + # Remove epoch if present (e.g. 1:2.3.4) + split(v, tmp, ":") + v = tmp[length(tmp)] + + # Remove Debian revision (-1, -2, etc.) + split(v, tmp, "-") + v = tmp[1] + + # Split into version parts + split(v, a, ".") + } + + BEGIN { + section="" + new=0 + removed=0 + updated=0 + major=0 + minor=0 + patch=0 + } + + /^New packages:/ { section="new"; next } + /^Removed packages:/ { section="removed"; next } + /^Version changes:/ { section="updated"; next } + /^$/ { section=""; next } + + { + if (section == "new" && NF) { + new++ + } + else if (section == "removed" && NF) { + removed++ + } + else if (section == "updated" && /->/) { + updated++ + + oldv = $2 + newv = $4 + + splitver(oldv, o) + splitver(newv, n) + + if (o[1] != n[1]) major++ + else if (o[2] != n[2]) minor++ + else if (o[3] != n[3]) patch++ + } + } + + END { + print new, removed, updated, major, minor, patch + } + ' "$LATEST_CHANGES" + ) + + # Top changes (prioritise major/minor first, fallback to any) + TOP_CHANGES=$(awk ' + function splitver(v, a, tmp) { + split(v, tmp, ":"); v = tmp[length(tmp)] + split(v, tmp, "-"); v = tmp[1] + split(v, a, ".") + } + + /^Version changes:/ { flag=1; next } + /^$/ { flag=0 } + + flag && /->/ { + oldv = $2 + newv = $4 + + splitver(oldv, o) + splitver(newv, n) + + if (o[1] != n[1] || o[2] != n[2]) { + print + count++ + if (count == 5) exit + } + } + ' "$LATEST_CHANGES") + + # Fallback to any changes if no major/minor found + if [[ -z "$TOP_CHANGES" ]]; then + TOP_CHANGES=$(awk ' + /^Version changes:/ { flag=1; next } + /^$/ { flag=0 } + flag && /->/ { + print + count++ + if (count == 5) exit + } + ' "$LATEST_CHANGES") + fi + + [[ -z "$TOP_CHANGES" ]] && TOP_CHANGES="No version changes" +} + +record_metrics() { + METRICS_FILE="$OUTPUT_DIR/build-history.csv" + + DATE=$(date "+%Y-%m-%d %H:%M") + NEW_COUNT=$(grep -c '^' "$OUTPUT_DIR/${FINAL_NAME%.iso}-packages.txt" || true) + UPDATED_COUNT=$(grep -c "->" "$OUTPUT_DIR/${FINAL_NAME%.iso}-changes.txt" || true) + + echo "$DATE,$FINAL_NAME,$NEW_COUNT,$UPDATED_COUNT" >> "$METRICS_FILE" +} + +prune_old_builds() { + echo "Pruning old builds (keeping latest 2)..." + + # Get ISO list newest first + mapfile -t ISOS < <(ls -t "$OUTPUT_DIR"/*.iso 2>/dev/null) + + # Skip if <=3 + [[ ${#ISOS[@]} -le 3 ]] && return + + # Delete everything beyond first 3 + for iso in "${ISOS[@]:3}"; do + base=$(basename "$iso" .iso) + + echo "Removing old build: $base" + + rm -f "$OUTPUT_DIR/$base.iso" + rm -f "$OUTPUT_DIR/$base.iso.sha256" + done +} + +verify_remote() { + echo "Verifying remote artefacts..." + + LOCAL_FILE="$OUTPUT_DIR/hibbian-latest.iso" + REMOTE_FILE="$REMOTE_TARGET/hibbian-latest.iso" + + # Extract remote path (strip user@host:) + REMOTE_PATH=$(echo "$REMOTE_FILE" | cut -d':' -f2) + REMOTE_HOST=$(echo "$REMOTE_TARGET" | cut -d':' -f1) + + # Local checksum + LOCAL_SUM=$(sha256sum "$LOCAL_FILE" | awk '{print $1}') + + # Remote checksum + REMOTE_SUM=$(ssh "$REMOTE_HOST" "sha256sum '$REMOTE_PATH'" 2>/dev/null | awk '{print $1}') + + if [[ -z "$REMOTE_SUM" ]]; then + echo "❌ ERROR: Remote file not found or checksum failed" + return 1 + fi + + if [[ "$LOCAL_SUM" != "$REMOTE_SUM" ]]; then + echo "❌ ERROR: Checksum mismatch!" + echo "Local: $LOCAL_SUM" + echo "Remote: $REMOTE_SUM" + return 1 + fi + + echo "✅ Remote verification successful" +} + +run_sync() { + echo "Starting sync to remote..." + + # Safety check + if [[ -z "${REMOTE_TARGET:-}" ]]; then + echo "ERROR: REMOTE_TARGET not set" + return 1 + fi + + if [[ ! -d "$OUTPUT_DIR" ]]; then + echo "ERROR: OUTPUT_DIR does not exist" + return 1 + fi + + # Perform rsync (mirror mode) + rsync -avz --delete "$OUTPUT_DIR/" "$REMOTE_TARGET" + + # Optional: verify after sync + verify_remote || { + echo "ERROR: Remote verification failed" + return 1 + } + + echo "Sync complete" +} + +extract_date() { + local name="$1" + [[ "$name" =~ ^([0-9]{4}-[0-9]{2}-[0-9]{2}) ]] && echo "${BASH_REMATCH[1]}" +} + +case "$MODE" in + build) + run_build + ;; + stats) + run_stats + ;; + sync) + run_sync + ;; + *) + echo "Usage: $0 [build|stats]" + exit 1 + ;; +esac |
