homelab infrastructure services
1#!/bin/bash
2
3# Configure boot ordering to ensure tinsnip services start first
4
5set -euo pipefail
6
7log() {
8 echo "[Boot Order] $*"
9}
10
11create_tinsnip_target() {
12 log "Creating tinsnip systemd target..."
13
14 # Create a systemd target that groups all tinsnip services
15 sudo tee /etc/systemd/system/tinsnip.target > /dev/null << 'EOF'
16[Unit]
17Description=tinsnip Infrastructure Services
18After=network-online.target
19Wants=network-online.target
20
21[Install]
22WantedBy=multi-user.target
23EOF
24
25 # Update tinsnip service files to be part of this target
26 for service in /etc/systemd/system/tinsnip-*.service; do
27 if [[ -f "$service" ]]; then
28 log "Updating $service to use tinsnip.target..."
29 sudo sed -i '/\[Install\]/,/WantedBy=/ s/WantedBy=.*/WantedBy=tinsnip.target/' "$service"
30 fi
31 done
32
33 sudo systemctl daemon-reload
34 sudo systemctl enable tinsnip.target
35}
36
37create_wait_for_tinsnip() {
38 log "Creating wait-for-tinsnip service..."
39
40 # Create a service that waits for tinsnip services to be ready
41 sudo tee /etc/systemd/system/wait-for-tinsnip.service > /dev/null << 'EOF'
42[Unit]
43Description=Wait for tinsnip services to be ready
44After=tinsnip.target
45Wants=tinsnip.target
46
47[Service]
48Type=oneshot
49RemainAfterExit=yes
50ExecStart=/bin/bash -c 'until nc -z localhost 3890; do echo "Waiting for LDAP..."; sleep 2; done; echo "LDAP is ready"'
51
52[Install]
53WantedBy=multi-user.target
54EOF
55
56 sudo systemctl daemon-reload
57 sudo systemctl enable wait-for-tinsnip.service
58}
59
60configure_user_dependencies() {
61 log "Configuring user session dependencies..."
62
63 # Create a drop-in for user@.service to depend on tinsnip
64 sudo mkdir -p /etc/systemd/system/user@.service.d
65
66 # This affects ALL user sessions
67 sudo tee /etc/systemd/system/user@.service.d/wait-for-tinsnip.conf > /dev/null << 'EOF'
68[Unit]
69After=wait-for-tinsnip.service
70Wants=wait-for-tinsnip.service
71EOF
72
73 # For more specific control, create a drop-in just for specific users
74 for uid in 1003 1004 1005; do # Add your regular user UIDs here
75 if id -u >/dev/null 2>&1; then
76 sudo mkdir -p "/etc/systemd/system/user@${uid}.service.d"
77 sudo tee "/etc/systemd/system/user@${uid}.service.d/wait-for-tinsnip.conf" > /dev/null << 'EOF'
78[Unit]
79After=wait-for-tinsnip.service
80Wants=wait-for-tinsnip.service
81EOF
82 fi
83 done
84
85 sudo systemctl daemon-reload
86}
87
88create_machine_dependencies() {
89 log "Creating machine service dependencies..."
90
91 # If machine repo has systemd services, update them
92 # This is a template - adjust based on actual machine services
93 cat > /tmp/machine-services-dependency.conf << 'EOF'
94[Unit]
95After=wait-for-tinsnip.service
96Wants=wait-for-tinsnip.service
97EOF
98
99 log "Template created at /tmp/machine-services-dependency.conf"
100 log "Apply this to any machine systemd services that need LDAP"
101}
102
103main() {
104 log "Configuring boot order for tinsnip priority..."
105
106 create_tinsnip_target
107 create_wait_for_tinsnip
108 configure_user_dependencies
109 create_machine_dependencies
110
111 log ""
112 log "Boot order configuration complete!"
113 log ""
114 log "Boot sequence will be:"
115 log "1. System boot"
116 log "2. tinsnip.target (all tinsnip services)"
117 log "3. wait-for-tinsnip.service (confirms LDAP is ready)"
118 log "4. Regular user sessions (including machine services)"
119 log ""
120 log "To test: sudo systemctl list-dependencies tinsnip.target"
121}
122
123main "$@"