apt-ostree/daemon-dbus-manager.sh
robojerk 97a9c40d7e docs: Add comprehensive documentation and update planning
- Add docs/README.md with project overview and current status
- Add docs/architecture.md with detailed architecture documentation
- Add docs/development.md with development guide for contributors
- Update .notes/todo.md to reflect architecture fix completion
- Update .notes/plan.md with completed phases and next priorities

Architecture fixes (daemon and dbus), bubblewrap integration are now complete.
Ready for OCI integration phase.
2025-07-18 23:38:57 +00:00

369 lines
No EOL
14 KiB
Bash
Executable file

#!/bin/bash
# Complete apt-ostree Management Script
# This script provides full lifecycle management for apt-ostree,
# including cleanup, building, installation, D-Bus access fixing,
# and service verification.
set -e
# --- Helper Functions ---
# Function to check if the script is run as root
check_root() {
if [[ $EUID -ne 0 ]]; then
echo "ERROR: This script must be run as root."
echo "Please run: sudo ./$(basename "$0")"
exit 1
fi
}
# Function to safely remove files or directories with status messages
safe_rm() {
local item="$1"
if [[ -e "$item" ]]; then # Check if the file/directory exists
echo " $item Found."
if rm -rf "$item"; then # -rf handles both files and directories, and forces removal
echo " $item successfully rm'd."
else
echo " Fail to rm $item."
# Optionally, you could exit here if removal failure is critical
# exit 1
fi
else
echo " $item not found, skipping removal."
fi
}
# --- Main Script Execution ---
echo "=== Complete apt-ostree Management Script ==="
echo
# Ensure the script is run as root
check_root
echo "Killing all apt-ostree related processes..."
pkill -f apt-ostree || true
pkill -f apt-ostreed || true
pkill -f apt-ostree-bootstatus || true
pkill -f apt-ostree-countme || true
echo "Processes killed or not found."
echo
# --- PHASE 1: STOPPING AND DISABLING ALL SERVICES ---
echo "=== PHASE 1: STOPPING AND DISABLING ALL SERVICES ==="
echo "1. Stopping all apt-ostree services..."
systemctl stop apt-ostreed.service 2>/dev/null || echo " apt-ostreed.service not running or found."
systemctl stop apt-ostree-bootstatus.service 2>/dev/null || echo " apt-ostree-bootstatus.service not running or found."
systemctl stop apt-ostree-countme.service 2>/dev/null || echo " apt-ostree-countme.service not running or found."
systemctl stop apt-ostreed-automatic.service 2>/dev/null || echo " apt-ostreed-automatic.service not running or found."
echo "Services stopped."
echo "2. Disabling all apt-ostree services..."
systemctl disable apt-ostreed.service 2>/dev/null || echo " apt-ostreed.service not enabled."
systemctl disable apt-ostree-bootstatus.service 2>/dev/null || echo " apt-ostree-bootstatus.service not enabled."
systemctl disable apt-ostree-countme.service 2>/dev/null || echo " apt-ostree-countme.service not enabled."
systemctl disable apt-ostree-countme.timer 2>/dev/null || echo " apt-ostree-countme.timer not enabled."
systemctl disable apt-ostreed-automatic.service 2>/dev/null || echo " apt-ostreed-automatic.service not enabled."
systemctl disable apt-ostreed-automatic.timer 2>/dev/null || echo " apt-ostreed-automatic.timer not enabled."
echo "Services disabled."
echo
# --- PHASE 2: REMOVING OLD SERVICE AND CONFIGURATION FILES ---
echo "=== PHASE 2: REMOVING OLD SERVICE AND CONFIGURATION FILES ==="
echo "3. Removing systemd service files..."
safe_rm /etc/systemd/system/apt-ostreed.service
safe_rm /etc/systemd/system/apt-ostree-bootstatus.service
safe_rm /etc/systemd/system/apt-ostree-countme.service
safe_rm /etc/systemd/system/apt-ostree-countme.timer
safe_rm /etc/systemd/system/apt-ostreed-automatic.service
safe_rm /etc/systemd/system/apt-ostreed-automatic.timer
echo "Systemd service files processed."
echo "4. Removing ALL D-Bus policy files (including old ones)..."
safe_rm /etc/dbus-1/system.d/org.aptostree.dev.conf
safe_rm /etc/dbus-1/system.d/org.aptostree*.conf # Catch-all for old/variant names
safe_rm /etc/dbus-1/system.d/org.debian.aptostree1.conf.old_python
safe_rm /etc/dbus-1/system.d/org.projectatomic.aptostree1.conf
echo "D-Bus policy files processed."
echo "5. Removing D-Bus service files..."
safe_rm /usr/share/dbus-1/system-services/org.aptostree.dev.service
safe_rm /usr/share/dbus-1/system-services/org.aptostree*.service # Catch-all
echo "D-Bus service files processed."
echo "6. Removing ALL Polkit policy files..."
safe_rm /usr/share/polkit-1/actions/org.aptostree.dev.policy
safe_rm /usr/share/polkit-1/actions/org.aptostree*.policy # Catch-all
echo "Polkit policy files processed."
echo "7. Removing ALL old configuration directory and files..."
safe_rm /etc/apt-ostree/ # This will remove the directory and its contents
echo "Old configuration processed."
echo
# --- PHASE 3: REMOVING OLD BINARIES ---
echo "=== PHASE 3: REMOVING OLD BINARIES ==="
echo "8. Removing old binaries..."
safe_rm /usr/libexec/apt-ostreed
safe_rm /usr/bin/apt-ostree
safe_rm /usr/bin/apt-ostreed
echo "Old binaries processed."
echo
# --- PHASE 4: RELOADING SYSTEMD AND D-BUS AFTER CLEANUP ---
echo "=== PHASE 4: RELOADING SYSTEMD AND D-BUS AFTER CLEANUP ==="
echo "9. Reloading systemd daemon..."
systemctl daemon-reload
echo "Systemd daemon reloaded."
echo "10. Reloading D-Bus daemon..."
systemctl reload dbus
echo "D-Bus daemon reloaded."
echo "11. Waiting for cleanup to complete and daemons to settle..."
sleep 2
echo "Wait complete."
echo
# --- PHASE 5: BUILDING PROJECT ---
echo "=== PHASE 5: BUILDING PROJECT ==="
echo "12. Building project (as current user if sudo, else trying common paths)..."
# Use the user's environment to find cargo
if [[ -n "$SUDO_USER" ]]; then
echo " Attempting build as user: $SUDO_USER"
if ! sudo -u "$SUDO_USER" cargo build --release; then
echo "ERROR: Build failed for user $SUDO_USER. Please check your Rust environment."
echo "Please run 'cargo build --release' manually as user, then re-run this script."
exit 1
fi
else
echo " Attempting build with current user's cargo in PATH or common locations..."
# Try common cargo locations
if command -v cargo >/dev/null 2>&1; then
if ! cargo build --release; then
echo "ERROR: Build failed. Please check your Rust environment."
echo "Please run 'cargo build --release' manually as user, then re-run this script."
exit 1
fi
elif [[ -f "/home/$USER/.cargo/bin/cargo" ]]; then
if ! "/home/$USER/.cargo/bin/cargo" build --release; then
echo "ERROR: Build failed using user's cargo path. Please check your Rust environment."
echo "Please run 'cargo build --release' manually as user, then re-run this script."
exit 1
fi
elif [[ -f "/usr/local/bin/cargo" ]]; then
if ! /usr/local/bin/cargo build --release; then
echo "ERROR: Build failed using /usr/local/bin/cargo. Please check your Rust environment."
echo "Please run 'cargo build --release' manually as user, then re-run this script."
exit 1
fi
else
echo "ERROR: cargo not found in PATH or common locations."
echo "Please run 'cargo build --release' manually as user, then re-run this script."
exit 1
fi
fi
echo "✓ Project built successfully."
echo
# --- PHASE 6: INSTALLING FRESH BINARIES AND SERVICE FILES ---
echo "=== PHASE 6: INSTALLING FRESH BINARIES AND SERVICE FILES ==="
echo "13. Installing fresh binaries..."
cp target/release/apt-ostreed /usr/libexec/
cp target/release/apt-ostree /usr/bin/
chmod +x /usr/libexec/apt-ostreed /usr/bin/apt-ostree
echo "Binaries installed and permissions set."
echo "14. Installing fresh systemd service files..."
cp src/daemon/apt-ostreed.service /etc/systemd/system/
cp src/daemon/apt-ostree-bootstatus.service /etc/systemd/system/
cp src/daemon/apt-ostree-countme.service /etc/systemd/system/
cp src/daemon/apt-ostree-countme.timer /etc/systemd/system/
cp src/daemon/apt-ostreed-automatic.service /etc/systemd/system/
cp src/daemon/apt-ostreed-automatic.timer /etc/systemd/system/
echo "Systemd service files installed."
echo "15. Installing and configuring fresh D-Bus service file..."
# Copy the file first
cp src/daemon/org.aptostree.dev.service /usr/share/dbus-1/system-services/
# Then use tee to ensure its content includes User=root and SystemdService=apt-ostreed.service
tee /usr/share/dbus-1/system-services/org.aptostree.dev.service > /dev/null << 'EOF'
[D-BUS Service]
Name=org.aptostree.dev
Exec=/usr/libexec/apt-ostreed
User=root
SystemdService=apt-ostreed.service
EOF
echo "D-Bus service file installed and configured to run as root."
echo "16. Installing fresh Polkit policy file..."
cp src/daemon/org.aptostree.dev.policy /usr/share/polkit-1/actions/
echo "Polkit policy file installed."
echo "17. Creating configuration directory and installing fresh configuration..."
mkdir -p /etc/apt-ostree
cp src/daemon/apt-ostreed.conf /etc/apt-ostree/
echo "Configuration installed."
echo
# --- PHASE 7: FIXING D-BUS POLICY FOR ACCESS ---
echo "=== PHASE 7: FIXING D-BUS POLICY FOR ACCESS ==="
echo "18. Updating D-Bus policy with more permissive settings for org.aptostree.dev..."
# This uses tee to write the multi-line XML content to the file
tee /etc/dbus-1/system.d/org.aptostree.dev.conf > /dev/null << 'EOF'
<!DOCTYPE busconfig PUBLIC
"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<!-- apt-ostree D-Bus Policy Configuration -->
<!-- Allow apt-ostreed to own the service name -->
<policy user="root">
<allow own="org.aptostree.dev"/>
<allow send_destination="org.aptostree.dev"/>
<allow receive_sender="org.aptostree.dev"/>
</policy>
<!-- Allow system users to call methods -->
<policy context="default">
<!-- Allow introspection for all users -->
<allow send_destination="org.aptostree.dev"
send_interface="org.freedesktop.DBus.Introspectable"
send_member="Introspect"/>
<!-- Read-only operations -->
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="ping"/>
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="status"/>
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="list_packages"/>
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="search_packages"/>
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="show_package_info"/>
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="show_history"/>
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="show_status"/>
<!-- Privileged operations require authentication (Polkit will handle this) -->
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="install_packages"/>
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="remove_packages"/>
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="upgrade_system"/>
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="rollback"/>
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="checkout"/>
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="prune_deployments"/>
<allow send_destination="org.aptostree.dev"
send_interface="org.aptostree.dev.Daemon"
send_member="initialize"/>
</policy>
<!-- Allow apt-ostreed to receive signals -->
<policy user="root">
<allow receive_sender="org.aptostree.dev"/>
</policy>
</busconfig>
EOF
echo "D-Bus policy updated."
echo "19. Setting correct permissions for D-Bus policy file..."
chmod 644 /etc/dbus-1/system.d/org.aptostree.dev.conf
chown root:root /etc/dbus-1/system.d/org.aptostree.dev.conf
echo "Permissions set for D-Bus policy."
echo
# --- PHASE 8: FINAL PERMISSIONS, ENABLING AND STARTING SERVICES ---
echo "=== PHASE 8: FINAL PERMISSIONS, ENABLING AND STARTING SERVICES ==="
echo "20. Setting correct permissions for all newly installed files..."
chmod 644 /usr/share/dbus-1/system-services/org.aptostree.dev.service
chmod 644 /usr/share/polkit-1/actions/org.aptostree.dev.policy
chmod 644 /etc/apt-ostree/apt-ostreed.conf
echo "Permissions set for installed files."
echo "21. Reloading systemd and D-Bus one more time to pick up new configurations..."
systemctl daemon-reload
systemctl reload dbus
echo "Systemd and D-Bus reloaded."
echo "22. Enabling services to start on boot..."
systemctl enable apt-ostreed.service
systemctl enable apt-ostree-bootstatus.service
systemctl enable apt-ostree-countme.timer
systemctl enable apt-ostreed-automatic.timer
echo "Services enabled."
echo "23. Starting main apt-ostree daemon..."
systemctl start apt-ostreed.service
echo "Daemon start command issued."
echo "24. Waiting for daemon to fully start..."
sleep 3
echo "Wait complete."
echo
# --- PHASE 9: VERIFICATION ---
echo "=== PHASE 9: VERIFICATION ==="
echo "25. Checking main daemon status..."
if systemctl is-active --quiet apt-ostreed.service; then
echo "✓ Daemon is running successfully!"
else
echo "✗ Daemon failed to start."
echo "Daemon status:"
systemctl status apt-ostreed.service --no-pager
echo
echo "Last 10 lines of daemon logs:"
journalctl -u apt-ostreed.service --no-pager -n 10
exit 1
fi
echo "26. Testing D-Bus communication (Introspection and Ping)..."
echo " Testing introspection:"
gdbus introspect --system --dest org.aptostree.dev --object-path /org/aptostree/dev/Daemon 2>&1 || echo " Introspection failed."
echo " Testing ping:"
gdbus call --system --dest org.aptostree.dev --object-path /org/aptostree/dev/Daemon --method org.aptostree.dev.Daemon.ping 2>&1 || echo " D-Bus ping failed."
echo "D-Bus communication tests complete."
echo "27. Testing client-daemon communication..."
echo " Testing client ping:"
apt-ostree daemon-ping || echo " Client ping failed."
echo " Testing client status:"
apt-ostree daemon-status || echo " Client status failed."
echo "Client-daemon communication tests complete."
echo
echo "=== APT-OSTREE FULL MANAGEMENT COMPLETE ==="
echo "All old services and policies have been removed, fresh ones installed, and D-Bus access fixed."
echo "apt-ostree should now be fully functional."