-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·186 lines (168 loc) · 8.02 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·186 lines (168 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash
# run.sh — run one or more playbooks with local inventory
#
# Usage:
# ./run.sh prep # install Ansible and create config files (first time)
# ./run.sh homelab # full home lab setup
# ./run.sh garage # set up the Garage S3 node
# ./run.sh apt # refresh apt cache on all nodes (add --tags upgrade to upgrade)
# ./run.sh timezone # set timezone (run separately, can reboot)
# ./run.sh uninstall-k3s # cleanly remove k3s (server and/or agent)
# ./run.sh homelab apt # run multiple playbooks in sequence
# ./run.sh --help # show this help
set -euo pipefail
usage() {
echo ""
echo "Usage: ./run.sh <command> [<command> ...] [ansible-options]"
echo ""
echo "Commands:"
echo " prep Install Ansible and create vault.yml + prod.ini (run first)"
echo " prep --reset Regenerate vault.yml and .vault_pass (new secrets)"
echo " homelab Full home lab setup (packages, k3s)"
echo " garage Set up the Garage S3 node"
echo " apt Refresh apt cache on all nodes (--tags upgrade to also upgrade)"
echo " timezone Set timezone on all nodes (run separately, can reboot)"
echo " uninstall-k3s Cleanly remove k3s server/agent (use --limit to target a node)"
echo " kueue Install Kueue (GPU-priority job queue) on k3s server nodes"
echo " headlamp Install Headlamp (Kubernetes dashboard) on k3s server nodes"
echo ""
echo "Multiple commands run their playbooks in sequence, each as its own"
echo "ansible-playbook invocation — nothing is combined via ansible tags."
echo ""
echo "Any arguments after the command(s) are passed through to every"
echo "ansible-playbook invocation."
echo "Examples:"
echo " ./run.sh homelab --tags k3s"
echo " ./run.sh homelab --limit zyron --check"
echo " ./run.sh timezone --limit zyron --tags reboot"
echo " ./run.sh apt --tags upgrade"
echo " ./run.sh homelab apt --limit zyron"
echo " ./run.sh uninstall-k3s --limit zyron"
echo ""
}
if [[ $# -eq 0 || "$1" == "--help" || "$1" == "-h" ]]; then
usage
exit 0
fi
export ANSIBLE_CONFIG=$PWD/ansible.cfg
VAULT_FILE="$PWD/group_vars/all/vault.yml"
INVENTORY="$PWD/prod.ini"
playbook_file() {
case "$1" in
homelab) echo "playbooks/setup-homelab.yml" ;;
garage) echo "playbooks/setup-garage.yml" ;;
apt) echo "playbooks/apt.yml" ;;
timezone) echo "playbooks/timezone.yml" ;;
uninstall-k3s) echo "playbooks/revert-k3s.yml" ;;
kueue) echo "playbooks/kueue.yml" ;;
headlamp) echo "playbooks/headlamp.yml" ;;
*) return 1 ;;
esac
}
if [[ "$1" == "prep" ]]; then
RESET=false
if [[ "${2:-}" == "--reset" ]]; then
RESET=true
echo " --reset: existing vault.yml and .vault_pass will be replaced"
echo ""
rm -f "$VAULT_FILE" "$PWD/.vault_pass"
fi
# ── 1. Install Ansible if missing ────────────────────────────────────
if ! command -v ansible-playbook &>/dev/null; then
echo "Ansible not found — installing..."
sudo apt-get update -qq
sudo apt-get install -y ansible
echo " ✓ Ansible installed ($(ansible --version | head -1))"
else
echo " ✓ Ansible already installed ($(ansible --version | head -1))"
fi
echo ""
# ── 2. Create .vault_pass with generated password if missing ─────────
if [ ! -f "$PWD/.vault_pass" ]; then
VAULT_PASS=$(set +o pipefail; cat /dev/urandom | tr -dc 'a-zA-Z0-9!#$%&()*+,-./:<=>?@[\]^_{}~' | head -c 50)
echo "$VAULT_PASS" > "$PWD/.vault_pass"
chmod 600 "$PWD/.vault_pass"
echo " ✓ .vault_pass generated"
else
VAULT_PASS=$(cat "$PWD/.vault_pass")
echo " ✓ .vault_pass already exists — skipping"
fi
echo ""
# ── 4. Create vault.yml from example if missing ───────────────────────
if [ ! -f "$VAULT_FILE" ]; then
# Exclude \ from generated secrets — TOML double-quoted strings treat it as escape
GARAGE_RPC=$(set +o pipefail; cat /dev/urandom | tr -dc 'a-zA-Z0-9!#$%&()*+,-./:<=>?@[]^_{}~' | head -c 50)
K3S_TOKEN=$(set +o pipefail; cat /dev/urandom | tr -dc 'a-zA-Z0-9!#$%&()*+,-./:<=>?@[]^_{}~' | head -c 50)
cp "$PWD/group_vars/vault.example.yml" "$VAULT_FILE"
chmod 600 "$VAULT_FILE"
python3 - "$VAULT_FILE" "$GARAGE_RPC" "$K3S_TOKEN" <<'PYEOF'
import sys
path, garage_val, k3s_val = sys.argv[1], sys.argv[2], sys.argv[3]
content = open(path).read()
content = content.replace('vault_garage_rpc_secret: ""', f'vault_garage_rpc_secret: "{garage_val}"')
content = content.replace('vault_k3s_token: ""', f'vault_k3s_token: "{k3s_val}"')
open(path, 'w').write(content)
PYEOF
echo "┌────────────────────────────────────────────────────────────────────────────┐"
echo "│ Generated secrets — save these in Bitwarden before continuing │"
echo "├────────────────────────────────────────────────────────────────────────────┤"
printf "│ Vault password: %-54s │\n" "$VAULT_PASS"
printf "│ Garage RPC secret: %-54s │\n" "$GARAGE_RPC"
printf "│ K3s token: %-54s │\n" "$K3S_TOKEN"
echo "└────────────────────────────────────────────────────────────────────────────┘"
echo ""
echo "Opening vault.yml — fill in ansible_become_pass (sudo password),"
echo "then save and close."
echo ""
"${EDITOR:-nano}" "$VAULT_FILE"
ansible-vault encrypt "$VAULT_FILE"
echo " ✓ vault.yml encrypted"
else
echo " ✓ vault.yml already exists — skipping"
fi
echo ""
# ── 5. Create prod.ini from example if missing ───────────────────────
if [ ! -f "$INVENTORY" ]; then
cp "$PWD/inventory/hosts.example.ini" "$INVENTORY"
echo "Opening prod.ini — fill in your host IPs, then save and close."
echo ""
"${EDITOR:-nano}" "$INVENTORY"
echo " ✓ prod.ini created"
else
echo " ✓ prod.ini already exists — skipping"
fi
echo ""
echo "Setup complete. Run: ./run.sh homelab"
exit 0
fi
# ── Guard: require vault.yml and prod.ini ─────────────────────────────────
if [ ! -f "$VAULT_FILE" ]; then
echo "Error: group_vars/all/vault.yml not found."
echo "Run './run.sh prep' first."
exit 1
fi
if [ ! -f "$INVENTORY" ]; then
echo "Error: prod.ini not found."
echo "Run './run.sh prep' first."
exit 1
fi
# ── Collect one or more playbook names, then pass the rest through ────────
PLAYBOOKS=()
while [[ $# -gt 0 && "$1" != -* ]]; do
if ! FILE=$(playbook_file "$1"); then
echo "Unknown command: $1"
usage
exit 1
fi
PLAYBOOKS+=("$FILE")
shift
done
if [[ ${#PLAYBOOKS[@]} -eq 0 ]]; then
echo "No command given."
usage
exit 1
fi
for PB in "${PLAYBOOKS[@]}"; do
echo "── ansible-playbook $PB ──"
ansible-playbook "$PB" -i "$INVENTORY" "$@"
done