#!/bin/bash # Configure boot ordering to ensure tinsnip services start first set -euo pipefail log() { echo "[Boot Order] $*" } create_tinsnip_target() { log "Creating tinsnip systemd target..." # Create a systemd target that groups all tinsnip services sudo tee /etc/systemd/system/tinsnip.target > /dev/null << 'EOF' [Unit] Description=tinsnip Infrastructure Services After=network-online.target Wants=network-online.target [Install] WantedBy=multi-user.target EOF # Update tinsnip service files to be part of this target for service in /etc/systemd/system/tinsnip-*.service; do if [[ -f "$service" ]]; then log "Updating $service to use tinsnip.target..." sudo sed -i '/\[Install\]/,/WantedBy=/ s/WantedBy=.*/WantedBy=tinsnip.target/' "$service" fi done sudo systemctl daemon-reload sudo systemctl enable tinsnip.target } create_wait_for_tinsnip() { log "Creating wait-for-tinsnip service..." # Create a service that waits for tinsnip services to be ready sudo tee /etc/systemd/system/wait-for-tinsnip.service > /dev/null << 'EOF' [Unit] Description=Wait for tinsnip services to be ready After=tinsnip.target Wants=tinsnip.target [Service] Type=oneshot RemainAfterExit=yes ExecStart=/bin/bash -c 'until nc -z localhost 3890; do echo "Waiting for LDAP..."; sleep 2; done; echo "LDAP is ready"' [Install] WantedBy=multi-user.target EOF sudo systemctl daemon-reload sudo systemctl enable wait-for-tinsnip.service } configure_user_dependencies() { log "Configuring user session dependencies..." # Create a drop-in for user@.service to depend on tinsnip sudo mkdir -p /etc/systemd/system/user@.service.d # This affects ALL user sessions sudo tee /etc/systemd/system/user@.service.d/wait-for-tinsnip.conf > /dev/null << 'EOF' [Unit] After=wait-for-tinsnip.service Wants=wait-for-tinsnip.service EOF # For more specific control, create a drop-in just for specific users for uid in 1003 1004 1005; do # Add your regular user UIDs here if id -u >/dev/null 2>&1; then sudo mkdir -p "/etc/systemd/system/user@${uid}.service.d" sudo tee "/etc/systemd/system/user@${uid}.service.d/wait-for-tinsnip.conf" > /dev/null << 'EOF' [Unit] After=wait-for-tinsnip.service Wants=wait-for-tinsnip.service EOF fi done sudo systemctl daemon-reload } create_machine_dependencies() { log "Creating machine service dependencies..." # If machine repo has systemd services, update them # This is a template - adjust based on actual machine services cat > /tmp/machine-services-dependency.conf << 'EOF' [Unit] After=wait-for-tinsnip.service Wants=wait-for-tinsnip.service EOF log "Template created at /tmp/machine-services-dependency.conf" log "Apply this to any machine systemd services that need LDAP" } main() { log "Configuring boot order for tinsnip priority..." create_tinsnip_target create_wait_for_tinsnip configure_user_dependencies create_machine_dependencies log "" log "Boot order configuration complete!" log "" log "Boot sequence will be:" log "1. System boot" log "2. tinsnip.target (all tinsnip services)" log "3. wait-for-tinsnip.service (confirms LDAP is ready)" log "4. Regular user sessions (including machine services)" log "" log "To test: sudo systemctl list-dependencies tinsnip.target" } main "$@"