particle-os-modules/modules/apt-ostree/ostree-wrapper.sh
2025-08-27 19:42:18 -07:00

214 lines
6.9 KiB
Bash

#!/bin/bash
set -euo pipefail
# Log function
log() {
echo "[OSTREE-WRAPPER] $1"
}
# Function to add repository files
add_repos() {
local repos_config="$1"
if [ -n "$repos_config" ]; then
local repos=$(echo "$repos_config" | jq -r '.[]? // empty')
for repo in $repos; do
if [[ $repo == http* ]]; then
log "Downloading repository file: $repo"
curl -fsSL "$repo" -o "/etc/apt/sources.list.d/$(basename "$repo")"
else
log "Copying repository file: $repo"
cp "$repo" "/etc/apt/sources.list.d/"
fi
done
fi
}
# Function to add GPG keys
add_keys() {
local keys_config="$1"
if [ -n "$keys_config" ]; then
local keys=$(echo "$keys_config" | jq -r '.[]? // empty')
for key in $keys; do
if [[ $key == http* ]]; then
log "Downloading and adding GPG key: $key"
curl -fsSL "$key" | apt-key add -
else
log "Adding GPG key: $key"
apt-key add "$key"
fi
done
fi
}
# Function to handle /opt/ symlinking
handle_optfix() {
local optfix_config="$1"
if [ -n "$optfix_config" ]; then
local optfixes=$(echo "$optfix_config" | jq -r '.[]? // empty')
for optfix in $optfixes; do
log "Setting up /opt/ symlink for: $optfix"
mkdir -p "/opt/$optfix"
ln -sf "/opt/$optfix" "/usr/local/$optfix"
done
fi
}
# Function to install packages using ostree
install_packages() {
local install_config="$1"
if [ -n "$install_config" ]; then
local packages=$(echo "$install_config" | jq -r '.[]? // empty')
for package in $packages; do
if [[ $package == http* && $package == *.deb ]]; then
log "Downloading and installing .deb package: $package"
curl -fsSL "$package" -o /tmp/package.deb
# Use ostree to install the package
ostree admin install-pkg /tmp/package.deb || {
log "WARNING: Failed to install package via ostree: $package"
# Fallback to regular dpkg
dpkg -i /tmp/package.deb || apt-get install -f -y
}
rm -f /tmp/package.deb
else
log "Installing package via ostree: $package"
# Use ostree to install the package
ostree admin install-pkg "$package" || {
log "WARNING: Failed to install package via ostree: $package"
# Fallback to regular apt
apt-get install -y "$package" || {
log "ERROR: Failed to install package $package"
exit 1
}
}
fi
done
fi
}
# Function to remove packages using ostree
remove_packages() {
local remove_config="$1"
if [ -n "$remove_config" ]; then
local packages=$(echo "$remove_config" | jq -r '.[]? // empty')
for package in $packages; do
log "Removing package via ostree: $package"
# Use ostree to remove the package
ostree admin uninstall-pkg "$package" || {
log "WARNING: Failed to remove package via ostree: $package"
# Fallback to regular apt
apt-get remove -y "$package" || {
log "WARNING: Failed to remove package $package"
}
}
done
fi
}
# Function to replace packages using ostree
replace_packages() {
local replace_config="$1"
if [ -n "$replace_config" ]; then
local replacements=$(echo "$replace_config" | jq -c '.[]')
echo "$replacements" | while read -r replacement; do
local from_repo=$(echo "$replacement" | jq -r '.from-repo // empty')
local packages=$(echo "$replacement" | jq -r '.packages[]? // empty')
log "Replacing packages from repository: $from_repo"
for package in $packages; do
log "Replacing package via ostree: $package"
# Use ostree to replace the package
ostree admin replace-pkg "$package" || {
log "WARNING: Failed to replace package via ostree: $package"
# Fallback to regular apt
apt-get install -y "$package" || {
log "ERROR: Failed to replace package $package"
exit 1
}
}
done
done
fi
}
# Function to commit ostree changes
commit_changes() {
log "Committing ostree changes"
# Get current ostree ref
local current_ref=$(ostree refs --list | head -n1)
if [ -n "$current_ref" ]; then
log "Current ostree ref: $current_ref"
# Create new commit with changes
local new_ref="${current_ref}-$(date +%Y%m%d-%H%M%S)"
log "Creating new ostree ref: $new_ref"
# Commit the changes
ostree commit --branch="$new_ref" --tree=dir=/ || {
log "WARNING: Failed to commit ostree changes"
}
else
log "WARNING: No ostree refs found"
fi
}
# Main execution
main() {
local module_config="$1"
log "Starting apt-ostree module execution"
# Handle repositories
if echo "$module_config" | jq -e '.repos' >/dev/null 2>&1; then
log "Processing repositories configuration"
add_repos "$(echo "$module_config" | jq -c '.repos')"
fi
# Handle GPG keys
if echo "$module_config" | jq -e '.keys' >/dev/null 2>&1; then
log "Processing GPG keys configuration"
add_keys "$(echo "$module_config" | jq -c '.keys')"
fi
# Handle /opt/ symlinking
if echo "$module_config" | jq -e '.optfix' >/dev/null 2>&1; then
log "Processing optfix configuration"
handle_optfix "$(echo "$module_config" | jq -c '.optfix')"
fi
# Handle package installation
if echo "$module_config" | jq -e '.install' >/dev/null 2>&1; then
log "Processing package installation"
install_packages "$(echo "$module_config" | jq -c '.install')"
fi
# Handle package removal
if echo "$module_config" | jq -e '.remove' >/dev/null 2>&1; then
log "Processing package removal"
remove_packages "$(echo "$module_config" | jq -c '.remove')"
fi
# Handle package replacement
if echo "$module_config" | jq -e '.replace' >/dev/null 2>&1; then
log "Processing package replacement"
replace_packages "$(echo "$module_config" | jq -c '.replace')"
fi
# Commit ostree changes
commit_changes
# Clean up
log "Cleaning up package cache"
apt-get clean
apt-get autoremove -y
log "apt-ostree module execution completed successfully"
}
# Execute main function with the module configuration
main "$1"