Here is a first attempt at automating the GhostBSD Ports Tree Maintenance support. Removing humans from the equation is the goal. It will take many iterations to get right. Feel free to review and improve it.
#!/bin/sh
# GhostBSD Automated Ports Tree Maintenance Script
# Author: Vester Thacker
# Description: Automates syncing, building, signing, publishing GhostBSD packages with monitoring and rollback
set -eu
PORTS_DIR="/usr/ghostbsd-src/ghostbsd-ports"
POUDRIERE_JAIL="ghostbsd"
PKGLIST="/usr/local/etc/poudriere.d/pkglist.txt"
REPO_DIR="/usr/local/poudriere/data/packages/${POUDRIERE_JAIL}-default"
WWW_DIR="/usr/local/www/packages"
LOG_FILE="/var/log/ghostbsd-ports-auto.log"
MAIL_RECIPIENT="root"
ZFS_DATASET="zroot/ghostbsd-ports"
SNAP_NAME="prebuild_$(date +%Y%m%d_%H%M%S)"
MONITOR_FILE="/var/log/ghostbsd-ports-monitor.log"
exec > "$LOG_FILE" 2>&1
log_monitor() {
echo "[$(date)] $1" >> "$MONITOR_FILE"
}
log_monitor "===== Starting automated ports maintenance ====="
echo "[$(date)] Starting automated ports maintenance..."
# Step 0: ZFS Snapshot for rollback
if zfs list "$ZFS_DATASET" >/dev/null 2>&1; then
echo "Creating ZFS snapshot: ${ZFS_DATASET}@${SNAP_NAME}"
zfs snapshot "${ZFS_DATASET}@${SNAP_NAME}"
log_monitor "Snapshot created: ${ZFS_DATASET}@${SNAP_NAME}"
else
echo "ZFS dataset $ZFS_DATASET not found. Skipping snapshot."
log_monitor "No snapshot created: ZFS dataset not found."
fi
# Step 1: Sync Ports Tree by force-resetting to upstream
cd "$PORTS_DIR"
echo "Force-resetting ports tree to upstream master..."
git fetch origin
git reset --hard origin/master || {
log_monitor "Git reset failed. Attempting rollback."
if zfs list "${ZFS_DATASET}@${SNAP_NAME}" >/dev/null 2>&1; then
echo "Restoring snapshot..."
zfs rollback -r "${ZFS_DATASET}@${SNAP_NAME}"
log_monitor "Rollback completed."
fi
echo "Git operation failed. Aborting."
exit 1
}
log_monitor "Ports tree synced successfully."
# Step 2: Update poudriere ports database
echo "Updating poudriere ports database..."
poudriere ports -u -p "$POUDRIERE_JAIL"
log_monitor "Poudriere ports database updated."
# Step 3: Build Updated Packages
echo "Starting poudriere bulk build..."
poudriere bulk -j "$POUDRIERE_JAIL" -f "$PKGLIST" || {
log_monitor "Build failed. Rolling back to previous snapshot."
if zfs list "${ZFS_DATASET}@${SNAP_NAME}" >/dev/null 2>&1; then
echo "Restoring snapshot..."
zfs rollback -r "${ZFS_DATASET}@${SNAP_NAME}"
log_monitor "Rollback completed."
fi
echo "Build failed. Aborting."
exit 1
}
log_monitor "Build completed successfully."
# Step 4: Generate and Sign Repo
echo "Generating and signing pkg repo..."
cd "$REPO_DIR"
pkg repo . # Add signing options if needed
log_monitor "Repository signed."
# Step 5: Sync to Public Package Server
echo "Syncing built packages to web server directory..."
rsync -a --delete "$REPO_DIR/" "$WWW_DIR/"
log_monitor "Repository synced to web directory."
# Step 6: Cleanup or Notify
echo "[$(date)] Ports tree maintenance complete."
echo "Build and sync complete at $(date)" | mail -s "GhostBSD Ports Auto Build Complete" "$MAIL_RECIPIENT"
log_monitor "Maintenance cycle complete."
exit 0
GhostBSD Automated Ports Maintenance Pipeline
This script automates the entire lifecycle of updating, building, signing, and deploying the GhostBSD ports tree and binary package repository. It is designed to run unattended and includes ZFS snapshot rollback and monitoring support.
Features
- Force-resets ports tree from upstream to avoid merge conflicts
- Takes ZFS snapshot before build for safe rollback on failure
- Uses poudriere
to rebuild packages from a predefined list
- Signs the repository using pkg repo
- Publishes packages to a web-accessible directory
- Logs activity and status to a separate monitoring file
- Sends completion or failure notifications via email
Prerequisites
- FreeBSD or GhostBSD with ZFS
- Git installed and configured
poudriere
properly configured with a jail named ghostbsd
- A populated ports tree at
/usr/ghostbsd-src/ghostbsd-ports
- ZFS dataset defined as
zroot/ghostbsd-ports
(customisable)
- Mail service configured (for notifications)
Configuration
Adjust these variables in the script if your environment differs:
PORTS_DIR="/usr/ghostbsd-src/ghostbsd-ports"
POUDRIERE_JAIL="ghostbsd"
PKGLIST="/usr/local/etc/poudriere.d/pkglist.txt"
REPO_DIR="/usr/local/poudriere/data/packages/${POUDRIERE_JAIL}-default"
WWW_DIR="/usr/local/www/packages"
ZFS_DATASET="zroot/ghostbsd-ports"
MAIL_RECIPIENT="root"
Installation
- Save the script as
ghostbsd-ports-auto.sh
.
- Make it executable:
chmod +x ghostbsd-ports-auto.sh
- (Optional) Add to
cron
for scheduled execution: 0 3 * * * /path/to/ghostbsd-ports-auto.sh
Logs
- Primary Log:
/var/log/ghostbsd-ports-auto.log
- Monitoring Log:
/var/log/ghostbsd-ports-monitor.log
Rollback
If a build fails or the Git reset encounters issues, the script will automatically roll back to the latest ZFS snapshot taken before execution.