homelab infrastructure services
1#!/bin/bash
2
3set -euo pipefail
4
5SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6LOG_FILE="/tmp/tinsnip-setup-$(date +%Y%m%d-%H%M%S).log"
7
8# Sheet configuration
9SHEET_FILE="/etc/tinsnip-sheet"
10
11log() {
12 echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
13}
14
15error() {
16 log "ERROR: $*" >&2
17 exit 1
18}
19
20setup_sheet() {
21 # Check if TIN_SHEET is already set in environment
22 if [[ -z "${TIN_SHEET:-}" ]]; then
23 # Check if sheet file exists
24 if [[ -f "$SHEET_FILE" ]]; then
25 TIN_SHEET=$(cat "$SHEET_FILE")
26 log "Found existing sheet: $TIN_SHEET"
27 else
28 # Prompt user for sheet name
29 echo
30 read -p "Enter sheet [topsheet]: " sheet_input
31 TIN_SHEET="${sheet_input:-topsheet}"
32
33 # Save sheet system-wide
34 log "Setting sheet to: $TIN_SHEET"
35 echo "$TIN_SHEET" | sudo tee "$SHEET_FILE" > /dev/null
36 sudo chmod 644 "$SHEET_FILE"
37 fi
38 else
39 log "Using TIN_SHEET from environment: $TIN_SHEET"
40 # Ensure it's saved system-wide
41 if [[ ! -f "$SHEET_FILE" ]] || [[ "$(cat "$SHEET_FILE")" != "$TIN_SHEET" ]]; then
42 echo "$TIN_SHEET" | sudo tee "$SHEET_FILE" > /dev/null
43 sudo chmod 644 "$SHEET_FILE"
44 fi
45 fi
46
47 # Export for use by other scripts
48 export TIN_SHEET
49
50 # Create profile script to load sheet for all users
51 local profile_script="/etc/profile.d/tinsnip-sheet.sh"
52 if [[ ! -f "$profile_script" ]]; then
53 log "Creating system-wide sheet profile script..."
54 sudo tee "$profile_script" > /dev/null << EOF
55#!/bin/bash
56# tinsnip sheet configuration
57if [[ -f "$SHEET_FILE" ]]; then
58 export TIN_SHEET=\$(cat "$SHEET_FILE")
59fi
60EOF
61 sudo chmod 644 "$profile_script"
62 fi
63}
64
65check_ubuntu() {
66 if [[ ! -f /etc/os-release ]] || ! grep -q "Ubuntu" /etc/os-release; then
67 error "This script requires Ubuntu"
68 fi
69}
70
71check_current_user() {
72 if [[ "$USER" == "tinsnip" ]]; then
73 error "Do not run this script as the tinsnip user. Run as a regular admin user."
74 fi
75
76 if [[ $EUID -eq 0 ]]; then
77 error "Do not run this script as root. Run as a regular user with sudo access."
78 fi
79
80 if ! groups | grep -q sudo; then
81 error "Current user must have sudo access"
82 fi
83}
84
85main() {
86 log "tinsnip Infrastructure Setup"
87 log "============================"
88 log "Log file: $LOG_FILE"
89
90 check_ubuntu
91 check_current_user
92
93 # Setup sheet first
94 setup_sheet
95 log "Using sheet: $TIN_SHEET"
96
97 log "This will set up tinsnip infrastructure services on this host."
98 log "A dedicated 'tinsnip' user will be created to run all services."
99 echo
100 read -p "Continue? (y/N): " response
101 if [[ ! "$response" =~ ^[Yy]$ ]]; then
102 log "Setup cancelled by user"
103 exit 0
104 fi
105
106 log ""
107 log "DEPRECATED: This legacy setup.sh is deprecated."
108 log "Use the modern CLI instead:"
109 log ""
110 log " # Set up topsheet (required first):"
111 log " TIN_SHEET=topsheet tin machine station prod <nas-server>"
112 log ""
113 log " # Create machine environments:"
114 log " tin machine <service> <environment>"
115 log ""
116 log " # Deploy services:"
117 log " tin service deploy <machine-env> <service-name>"
118 log ""
119 log "For detailed instructions, see the README.md"
120
121 log ""
122 log "Setup completed successfully!"
123 log ""
124 log "Services deployed:"
125 log " - LLDAP: http://$(hostname):17170 (Web UI)"
126 log " - LLDAP: ldap://$(hostname):3890 (LDAP endpoint)"
127 log ""
128 log "Default credentials:"
129 log " - Username: admin"
130 log " - Password: (set during LLDAP setup)"
131 log ""
132 log "To manage services:"
133 log " sudo -u tinsnip -i"
134 log " cd ~/service/lldap && docker compose ps"
135}
136
137main "$@"