Repository navigation
Expand file tree
/
Copy pathcloud-init.sh
More file actions
executable file
·330 lines (275 loc) · 10.5 KB
/
Copy pathcloud-init.sh
File metadata and controls
executable file
·330 lines (275 loc) · 10.5 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/bash
# dFlow Bootstrap Script
# Sets up Tailscale, Dokku, and dFlow environment
set -euo pipefail # Exit on error, undefined vars, pipe failures
# Configuration
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly LOG_FILE="/var/log/dflow-bootstrap.log"
readonly DOKKU_PLUGINS_DIR="/var/lib/dokku/plugins/available"
readonly MOTD_DIR="/etc/update-motd.d"
readonly MOTD_FILE="/etc/motd"
# Colors for output
readonly PRIMARY='\033[38;2;120;66;242m'
readonly NC='\033[0m' # No Color
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
# Functions
log_info() {
echo -e "${GREEN}[INFO]${NC} $1" | tee -a "$LOG_FILE"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1" | tee -a "$LOG_FILE"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1" | tee -a "$LOG_FILE" >&2
}
cleanup() {
local exit_code=$?
if [[ $exit_code -ne 0 ]]; then
log_error "Script failed with exit code $exit_code"
log_error "Check $LOG_FILE for details"
fi
exit $exit_code
}
check_root() {
if [[ $EUID -ne 0 ]]; then
log_error "This script must be run as root"
exit 1
fi
}
validate_args() {
if [[ $# -lt 2 ]]; then
log_error "Usage: $0 <TAILSCALE_AUTH_KEY> <DOKKU_VERSION> [STEP FLAGS]"
log_error "Example: $0 tskey-auth-xxxxx 0.34.6 --skip-motd"
exit 1
fi
local auth_key="$1"
local dokku_version="$2"
if [[ -z "$auth_key" ]]; then
log_error "Tailscale auth key cannot be empty"
exit 1
fi
if [[ -z "$dokku_version" ]]; then
log_error "Dokku version cannot be empty"
exit 1
fi
# Basic version format validation
if ! [[ "$dokku_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
log_error "Dokku version must be in format X.Y.Z (e.g., 0.34.6)"
exit 1
fi
}
# Step-skipping flags (#370). Each --skip-<step> omits one setup stage;
# combinations are allowed. Unknown flags are rejected.
parse_skip_flags() {
SKIP_TAILSCALE_INSTALL=0
SKIP_TAILSCALE_CONFIGURE=0
SKIP_DOKKU_INSTALL=0
SKIP_DOKKU_HOOKS=0
SKIP_DOKKU_CONFIGURE=0
SKIP_MOTD=0
shift 2 || true
for flag in "$@"; do
case "$flag" in
--skip-tailscale-install) SKIP_TAILSCALE_INSTALL=1 ;;
--skip-tailscale-configure) SKIP_TAILSCALE_CONFIGURE=1 ;;
--skip-dokku-install) SKIP_DOKKU_INSTALL=1 ;;
--skip-dokku-hooks) SKIP_DOKKU_HOOKS=1 ;;
--skip-dokku-configure) SKIP_DOKKU_CONFIGURE=1 ;;
--skip-motd) SKIP_MOTD=1 ;;
--skip-all)
SKIP_TAILSCALE_INSTALL=1
SKIP_TAILSCALE_CONFIGURE=1
SKIP_DOKKU_INSTALL=1
SKIP_DOKKU_HOOKS=1
SKIP_DOKKU_CONFIGURE=1
SKIP_MOTD=1
;;
-h|--help)
print_usage
exit 0
;;
*)
log_error "Unknown flag: $flag"
print_usage
exit 1
;;
esac
done
}
print_usage() {
cat <<'USAGE'
Usage: cloud-init.sh <TAILSCALE_AUTH_KEY> <DOKKU_VERSION> [STEP FLAGS]
Step flags (skippable independently, combinable):
--skip-tailscale-install Skip Tailscale installation
--skip-tailscale-configure Skip `tailscale up` configuration
--skip-dokku-install Skip Dokku installation
--skip-dokku-hooks Skip Dokku pre-deploy hook installation
--skip-dokku-configure Skip Dokku global-domain configuration
--skip-motd Skip MOTD banner setup
--skip-all Skip every setup step (dry validation only)
-h, --help Show this help
Example: cloud-init.sh tskey-auth-xxxxx 0.34.6 --skip-motd --skip-dokku-hooks
USAGE
}
install_tailscale() {
log_info "Installing Tailscale..."
if command -v tailscale &> /dev/null; then
log_warn "Tailscale is already installed"
return 0
fi
if ! curl -fsSL https://tailscale.com/install.sh | sh; then
log_error "Failed to install Tailscale"
exit 1
fi
log_info "Tailscale installed successfully"
}
configure_tailscale() {
local auth_key="$1"
log_info "Configuring Tailscale with SSH and customer tags..."
if ! tailscale up \
--authkey "$auth_key" \
--ssh \
--advertise-tags=tag:customer-machine; then
log_error "Failed to configure Tailscale"
exit 1
fi
log_info "Tailscale configured successfully"
}
install_dokku() {
local version="$1"
log_info "Installing Dokku v$version..."
if command -v dokku &> /dev/null; then
log_warn "Dokku is already installed"
return 0
fi
# Download bootstrap script
if ! wget -NP . https://dokku.com/bootstrap.sh --inet4-only; then
log_error "Failed to download Dokku bootstrap script"
exit 1
fi
# Install Dokku
if ! DOKKU_TAG="v$version" bash bootstrap.sh; then
log_error "Failed to install Dokku"
exit 1
fi
log_info "Dokku v$version installed successfully"
}
install_dokku_hooks() {
local hook_source="$SCRIPT_DIR/dokku-predeploy-resource-check.sh"
local hook_target="$DOKKU_PLUGINS_DIR/pre-deploy/check-resources"
log_info "Installing Dokku pre-deploy resource check hook..."
if [[ ! -f "$hook_source" ]]; then
log_error "Hook source file not found: $hook_source"
exit 1
fi
# Create plugin directory
mkdir -p "$DOKKU_PLUGINS_DIR/pre-deploy"
# Copy and set permissions
if ! cp "$hook_source" "$hook_target"; then
log_error "Failed to copy hook script"
exit 1
fi
chmod +x "$hook_target"
log_info "Dokku hooks installed successfully"
}
configure_dokku() {
log_info "Configuring Dokku..."
# Clear global domain
if ! dokku domains:clear-global; then
log_error "Failed to clear Dokku global domain"
exit 1
fi
log_info "Dokku configured successfully"
}
setup_motd() {
log_info "Setting up dFlow MOTD..."
# Disable existing MOTD scripts
if [[ -d "$MOTD_DIR" ]]; then
chmod -x "$MOTD_DIR"/* 2>/dev/null || true
fi
# Create new MOTD - redirect output to avoid verbose logging
{
printf '%b\n' \
' ' \
' **** ' \
' ******* ' \
' ******** ' \
' +++***** ' \
' ++++++++ ' \
' +++++++++ ++*** ' \
' +++++++ +++++** ' \
' =+++++++ ++++++++ ' \
' =====+++ ++++++++ ++ ' \
' ======== ++++++++ ++++ ' \
' ======== ===++++++ +++++++ ' \
' ========= =======++ +++++++++ ' \
' ======== ======== +++++++++++ ' \
' ----==== ======== ====++++++++++ ' \
' -------= ======== ========++++++++ ' \
' ------------====== ======== ==++++++ ' \
' -----------------==- ======== ======++++ ' \
' ------------------ ======= ==========++ ' \
' ---------------- -==- =========== ' \
' ' \
" ${PRIMARY}█████ ███████████ ████${NC} " \
" ${PRIMARY}░░███ ░░███░░░░░░█░░███${NC} " \
" ${PRIMARY}███████ ░███ █ ░ ░███ ██████ █████ ███ █████${NC}" \
" ${PRIMARY}███░░███ ░███████ ░███ ███░░███░░███ ░███░░███${NC} " \
"${PRIMARY}░███ ░███ ░███░░░█ ░███ ░███ ░███ ░███ ░███ ░███${NC} " \
"${PRIMARY}░███ ░███ ░███ ░ ░███ ░███ ░███ ░░███████████${NC} " \
"${PRIMARY}░░████████ █████ █████░░██████ ░░████░████${NC} " \
" ${PRIMARY}░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░ ░░░░ ░░░░${NC} " \
'' \
'=====================================================' \
' 🚀 Welcome to dFlow! 🚀' \
' A lightweight developer PaaS ' \
' powered by ⚙️ Dokku' \
'' \
' 🌐 GitHub: https://github.com/Dvorinka/dflow ' \
' ' \
'====================================================='
} > "$MOTD_FILE" 2>/dev/null
log_info "MOTD configured successfully"
}
main() {
local tailscale_auth_key="$1"
local dokku_version="$2"
log_info "Starting dFlow bootstrap process..."
log_info "Dokku version: $dokku_version"
log_info "Log file: $LOG_FILE"
# Setup logging
exec > >(tee -a "$LOG_FILE" | logger -t dflow-init -s) 2>&1
# Run setup steps (each skippable via flags, see print_usage)
if [[ "$SKIP_TAILSCALE_INSTALL" -eq 0 ]]; then
install_tailscale
else
log_info "Skipping Tailscale installation (--skip-tailscale-install)"
fi
if [[ "$SKIP_TAILSCALE_CONFIGURE" -eq 0 ]]; then
configure_tailscale "$tailscale_auth_key"
else
log_info "Skipping Tailscale configuration (--skip-tailscale-configure)"
fi
# install_dokku "$dokku_version"
# install_dokku_hooks
# configure_dokku
if [[ "$SKIP_MOTD" -eq 0 ]]; then
setup_motd
else
log_info "Skipping MOTD setup (--skip-motd)"
fi
log_info "✅ dFlow bootstrap completed successfully!"
log_info "System is ready for deployment"
}
# Script execution
trap cleanup EXIT
# Parse step flags first so --help works without root
parse_skip_flags "$@"
# Validate environment and arguments
check_root
validate_args "$@"
# Run main function
main "$@"