apt-ostree/debian/apt-ostree.postinst
robojerk 2306ee2a72
Some checks failed
Comprehensive CI/CD Pipeline / Build and Test (push) Successful in 16m28s
Comprehensive CI/CD Pipeline / Security Audit (push) Failing after 7s
Comprehensive CI/CD Pipeline / Package Validation (push) Successful in 52s
Comprehensive CI/CD Pipeline / Status Report (push) Has been skipped
Fix apt-ostree container installation failure
- Enhanced check_systemd() function to properly detect container environments
- Added detection for Docker, Podman, and other container runtimes
- Verify systemd is running as PID 1 before attempting service operations
- Check systemd D-Bus availability before service configuration
- Gracefully skip systemd setup in containers with helpful messages
- Provide manual configuration instructions for real systems

Fixes container builds and CI/CD pipelines that install apt-ostree
while maintaining full functionality on real systems with systemd.
2025-09-08 10:41:11 -07:00

162 lines
4.4 KiB
Bash
Executable file

#!/bin/sh
set -e
# Source debconf library
. /usr/share/debconf/confmodule
# Define package name
PACKAGE="apt-ostree"
# Function to log messages
log() {
echo "$PACKAGE: $1" >&2
}
# Function to setup shell completions
setup_completions() {
log "Setting up shell completions..."
# Reload bash completion if available (skip if problematic)
# if [ -f /etc/bash_completion ]; then
# . /etc/bash_completion || true
# fi
# Reload zsh completion if available
if [ -d /usr/share/zsh/vendor-completions ]; then
# Zsh will automatically pick up completions from this directory
log "Zsh completions installed"
fi
}
# Function to check if systemd is available and running
check_systemd() {
# Check if systemctl command exists
if ! command -v systemctl >/dev/null 2>&1; then
log "Warning: systemd not available, skipping service setup"
return 1
fi
# Check if we're in a container environment
if [ -f /.dockerenv ] || [ -n "${container:-}" ] || [ -f /run/.containerenv ] || \
([ -f /proc/1/cgroup ] && grep -q docker /proc/1/cgroup 2>/dev/null); then
log "Warning: Running in container environment, skipping systemd service setup"
return 1
fi
# Check if systemd is actually running as PID 1
if ! ps -p 1 -o comm= | grep -q systemd; then
log "Warning: systemd not running as PID 1, skipping service setup"
return 1
fi
# Check if systemd D-Bus is available
if ! systemctl is-system-running >/dev/null 2>&1; then
log "Warning: systemd not running, skipping service setup"
return 1
fi
return 0
}
# Function to enable and start the service
setup_service() {
if ! check_systemd; then
log "Skipping systemd service configuration"
log "To configure services manually on a real system, run:"
log " systemctl enable apt-ostreed.service"
log " systemctl start apt-ostreed.service"
return 0
fi
log "Setting up apt-ostreed service..."
# Reload systemd daemon
systemctl daemon-reload
# Enable the service
if systemctl enable apt-ostreed.service; then
log "apt-ostreed service enabled"
else
log "Warning: Failed to enable apt-ostreed service"
fi
# Start the service if not running
if ! systemctl is-active --quiet apt-ostreed.service; then
if systemctl start apt-ostreed.service; then
log "apt-ostreed service started"
else
log "Warning: Failed to start apt-ostreed service"
fi
else
log "apt-ostreed service already running"
fi
}
# Function to setup directories and permissions
setup_directories() {
log "Setting up directories and permissions..."
# Create necessary directories with proper permissions
mkdir -p /var/log/apt-ostreed
mkdir -p /var/cache/apt-ostree
mkdir -p /var/lib/apt-ostree
mkdir -p /var/lib/apt-ostree/repo
# Set proper ownership (root:root)
chown root:root /var/log/apt-ostreed
chown root:root /var/cache/apt-ostree
chown root:root /var/lib/apt-ostree
chown root:root /var/lib/apt-ostree/repo
# Set proper permissions
chmod 755 /var/log/apt-ostreed
chmod 755 /var/cache/apt-ostree
chmod 755 /var/lib/apt-ostree
chmod 755 /var/lib/apt-ostree/repo
}
# Function to reload polkit rules
reload_polkit() {
if command -v pkaction >/dev/null 2>&1; then
log "Reloading polkit rules..."
# This will trigger polkit to reload its rules
pkaction --version >/dev/null 2>&1 || true
fi
}
# Function to check dependencies
check_dependencies() {
log "Checking dependencies..."
# Check if ostree is available
if ! command -v ostree >/dev/null 2>&1; then
log "Warning: ostree command not found. Please install ostree package."
fi
# Check if systemd is available
if ! command -v systemctl >/dev/null 2>&1; then
log "Warning: systemd not available. Some features may not work."
fi
}
# Main execution
case "$1" in
configure)
log "Configuring apt-ostree package..."
setup_completions
setup_directories
setup_service
reload_polkit
check_dependencies
log "Configuration completed successfully"
;;
abort-upgrade|abort-remove|abort-deconfigure)
# Do nothing on abort
;;
*)
log "Unknown action: $1"
exit 1
;;
esac
exit 0