Repository navigation
Expand file tree
/
Copy pathinstall-reminders-stack.sh
More file actions
executable file
·580 lines (484 loc) · 17.5 KB
/
Copy pathinstall-reminders-stack.sh
File metadata and controls
executable file
·580 lines (484 loc) · 17.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#!/bin/bash
# Complete install script for reminders-api + MCP with Caddy + Cloudflare Tunnel
# This should be contributed to cromulus/reminders-cli repository
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
echo ""
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║ Reminders Stack Installer (API + MCP + Caddy + Tunnel) ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""
# Check if running on macOS
if [[ "$OSTYPE" != "darwin"* ]]; then
echo -e "${RED}Error: This script requires macOS${NC}"
exit 1
fi
USER=$(whoami)
USER_HOME="$HOME"
# ============================================================================
# STEP 1: Configuration Questions
# ============================================================================
echo -e "${CYAN}═══ Step 1: Configuration ═══${NC}"
echo ""
echo "Service Configuration:"
read -p " Enable authentication? (Y/n): " ENABLE_AUTH
ENABLE_AUTH=${ENABLE_AUTH:-Y}
if [[ "$ENABLE_AUTH" =~ ^[Yy]$ ]]; then
read -p " Auth token (leave blank to auto-generate): " AUTH_TOKEN
if [ -z "$AUTH_TOKEN" ]; then
AUTH_TOKEN=$(openssl rand -hex 32)
echo -e " ${GREEN}Generated token: $AUTH_TOKEN${NC}"
fi
else
AUTH_TOKEN=""
fi
echo ""
echo "Exposure Method:"
echo " 1) Cloudflare Tunnel (recommended - reliable, free, custom domain)"
echo " 2) Tailscale Funnel (easy - uses tailscale hostname)"
echo " 3) Tailscale Serve (private - only your tailnet)"
echo " 4) Local only (no remote access)"
read -p " Choice (1-4): " EXPOSURE_METHOD
echo ""
if [ "$EXPOSURE_METHOD" = "1" ]; then
echo "Cloudflare Configuration:"
read -p " Domain (e.g., reminders.yourdomain.com): " CF_DOMAIN
read -p " Cloudflare API Token: " CF_API_TOKEN
read -p " Cloudflare Account ID: " CF_ACCOUNT_ID
read -p " Cloudflare Zone ID: " CF_ZONE_ID
echo ""
fi
echo "Ports Configuration:"
read -p " Caddy external port (default 443): " CADDY_PORT
CADDY_PORT=${CADDY_PORT:-443}
REMINDERS_API_PORT=8081
MCP_PORT=8082
echo ""
echo "Optional Services:"
read -p " Install MCP server? (Y/n): " INSTALL_MCP
INSTALL_MCP=${INSTALL_MCP:-Y}
echo ""
# ============================================================================
# STEP 2: Install Dependencies
# ============================================================================
echo -e "${CYAN}═══ Step 2: Installing Dependencies ═══${NC}"
echo ""
# Check for Homebrew
if ! command -v brew &> /dev/null; then
echo -e "${YELLOW}Homebrew not found. Installing...${NC}"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
# Install Caddy
if ! command -v caddy &> /dev/null; then
echo "Installing Caddy..."
brew install caddy
echo -e "${GREEN}✓ Caddy installed${NC}"
else
echo -e "${GREEN}✓ Caddy already installed${NC}"
fi
# Install Cloudflare CLI if needed
if [ "$EXPOSURE_METHOD" = "1" ]; then
if ! command -v cloudflared &> /dev/null; then
echo "Installing cloudflared..."
brew install cloudflare/cloudflare/cloudflared
echo -e "${GREEN}✓ cloudflared installed${NC}"
else
echo -e "${GREEN}✓ cloudflared already installed${NC}"
fi
fi
echo ""
# ============================================================================
# STEP 3: Install reminders-api
# ============================================================================
echo -e "${CYAN}═══ Step 3: Installing reminders-api ═══${NC}"
echo ""
# Check if reminders-api exists
if ! command -v reminders-api &> /dev/null; then
echo "reminders-api not found. Please install it first:"
echo " https://github.com/cromulus/reminders-cli"
exit 1
fi
# Find the binary
REMINDERS_API_PATH=$(which reminders-api)
echo -e "Found reminders-api at: ${GREEN}$REMINDERS_API_PATH${NC}"
# Link to standard location if not there
if [ "$REMINDERS_API_PATH" != "/usr/local/bin/reminders-api" ]; then
echo "Creating symlink in /usr/local/bin..."
sudo ln -sf "$REMINDERS_API_PATH" /usr/local/bin/reminders-api
fi
echo ""
# ============================================================================
# STEP 4: Create Service Directory Structure
# ============================================================================
echo -e "${CYAN}═══ Step 4: Creating Service Structure ═══${NC}"
echo ""
SERVICES_DIR="$USER_HOME/reminders-stack"
mkdir -p "$SERVICES_DIR"
mkdir -p "$SERVICES_DIR/logs"
mkdir -p "$SERVICES_DIR/config"
echo -e "Created: ${GREEN}$SERVICES_DIR${NC}"
echo ""
# ============================================================================
# STEP 5: Create Caddyfile
# ============================================================================
echo -e "${CYAN}═══ Step 5: Configuring Caddy ═══${NC}"
echo ""
cat > "$SERVICES_DIR/Caddyfile" << 'CADDYFILE_END'
# Caddy configuration for reminders stack
:CADDY_PORT {
# Health check endpoint
handle /health {
respond "OK" 200
}
# reminders-api at /api/*
handle_path /api/* {
reverse_proxy localhost:REMINDERS_API_PORT {
header_up X-Forwarded-Prefix /api
}
}
# MCP server at /mcp/*
handle_path /mcp/* {
reverse_proxy localhost:MCP_PORT {
header_up X-Forwarded-Prefix /mcp
}
}
# Root redirects to health
handle / {
redir /health
}
# Authentication (if enabled)
@authenticated {
header Authorization "Bearer AUTH_TOKEN"
}
# Logging
log {
output file SERVICES_DIR/logs/access.log {
roll_size 10MB
roll_keep 5
}
}
}
CADDYFILE_END
# Replace placeholders
sed -i '' "s|:CADDY_PORT|:$CADDY_PORT|g" "$SERVICES_DIR/Caddyfile"
sed -i '' "s|REMINDERS_API_PORT|$REMINDERS_API_PORT|g" "$SERVICES_DIR/Caddyfile"
sed -i '' "s|MCP_PORT|$MCP_PORT|g" "$SERVICES_DIR/Caddyfile"
sed -i '' "s|AUTH_TOKEN|$AUTH_TOKEN|g" "$SERVICES_DIR/Caddyfile"
sed -i '' "s|SERVICES_DIR|$SERVICES_DIR|g" "$SERVICES_DIR/Caddyfile"
echo -e "${GREEN}✓ Caddyfile created${NC}"
echo ""
# ============================================================================
# STEP 6: Create Service Scripts
# ============================================================================
echo -e "${CYAN}═══ Step 6: Creating Service Scripts ═══${NC}"
echo ""
# reminders-api script
cat > "$SERVICES_DIR/run-reminders-api.sh" << SCRIPT_END
#!/bin/bash
cd "$SERVICES_DIR"
if [ -n "$AUTH_TOKEN" ]; then
exec reminders-api \\
--auth-required \\
--token "$AUTH_TOKEN" \\
--host 127.0.0.1 \\
--port $REMINDERS_API_PORT
else
exec reminders-api \\
--host 127.0.0.1 \\
--port $REMINDERS_API_PORT
fi
SCRIPT_END
chmod +x "$SERVICES_DIR/run-reminders-api.sh"
echo -e "${GREEN}✓ reminders-api script created${NC}"
# MCP script (if requested)
if [[ "$INSTALL_MCP" =~ ^[Yy]$ ]]; then
cat > "$SERVICES_DIR/run-mcp.sh" << 'SCRIPT_END'
#!/bin/bash
cd "$SERVICES_DIR"
# TODO: Replace with actual MCP server command
# exec your-mcp-server --host 127.0.0.1 --port MCP_PORT
echo "MCP server not configured. Edit this script to add your MCP server."
sleep infinity
SCRIPT_END
sed -i '' "s|MCP_PORT|$MCP_PORT|g" "$SERVICES_DIR/run-mcp.sh"
chmod +x "$SERVICES_DIR/run-mcp.sh"
echo -e "${GREEN}✓ MCP script created (needs configuration)${NC}"
fi
# Caddy script
cat > "$SERVICES_DIR/run-caddy.sh" << SCRIPT_END
#!/bin/bash
cd "$SERVICES_DIR"
exec caddy run --config Caddyfile
SCRIPT_END
chmod +x "$SERVICES_DIR/run-caddy.sh"
echo -e "${GREEN}✓ Caddy script created${NC}"
echo ""
# ============================================================================
# STEP 7: Create LaunchAgents
# ============================================================================
echo -e "${CYAN}═══ Step 7: Creating LaunchAgents ═══${NC}"
echo ""
# reminders-api LaunchAgent
cat > "$USER_HOME/Library/LaunchAgents/com.reminders.api.plist" << PLIST_END
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.reminders.api</string>
<key>ProgramArguments</key>
<array>
<string>$SERVICES_DIR/run-reminders-api.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>$SERVICES_DIR/logs/reminders-api.log</string>
<key>StandardErrorPath</key>
<string>$SERVICES_DIR/logs/reminders-api.error.log</string>
<key>WorkingDirectory</key>
<string>$SERVICES_DIR</string>
</dict>
</plist>
PLIST_END
echo -e "${GREEN}✓ reminders-api LaunchAgent created${NC}"
# MCP LaunchAgent (if requested)
if [[ "$INSTALL_MCP" =~ ^[Yy]$ ]]; then
cat > "$USER_HOME/Library/LaunchAgents/com.reminders.mcp.plist" << PLIST_END
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.reminders.mcp</string>
<key>ProgramArguments</key>
<array>
<string>$SERVICES_DIR/run-mcp.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>$SERVICES_DIR/logs/mcp.log</string>
<key>StandardErrorPath</key>
<string>$SERVICES_DIR/logs/mcp.error.log</string>
<key>WorkingDirectory</key>
<string>$SERVICES_DIR</string>
</dict>
</plist>
PLIST_END
echo -e "${GREEN}✓ MCP LaunchAgent created${NC}"
fi
# Caddy LaunchAgent
cat > "$USER_HOME/Library/LaunchAgents/com.reminders.caddy.plist" << PLIST_END
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.reminders.caddy</string>
<key>ProgramArguments</key>
<array>
<string>$SERVICES_DIR/run-caddy.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>$SERVICES_DIR/logs/caddy.log</string>
<key>StandardErrorPath</key>
<string>$SERVICES_DIR/logs/caddy.error.log</string>
<key>WorkingDirectory</key>
<string>$SERVICES_DIR</string>
</dict>
</plist>
PLIST_END
echo -e "${GREEN}✓ Caddy LaunchAgent created${NC}"
echo ""
# ============================================================================
# STEP 8: Configure Cloudflare Tunnel (if selected)
# ============================================================================
if [ "$EXPOSURE_METHOD" = "1" ]; then
echo -e "${CYAN}═══ Step 8: Configuring Cloudflare Tunnel ═══${NC}"
echo ""
# Authenticate
echo "Authenticating with Cloudflare..."
export CLOUDFLARE_API_TOKEN="$CF_API_TOKEN"
cloudflared tunnel login --api-token "$CF_API_TOKEN" 2>/dev/null || true
# Create tunnel
TUNNEL_NAME="reminders-$(date +%s)"
echo "Creating tunnel: $TUNNEL_NAME"
cloudflared tunnel create "$TUNNEL_NAME"
# Get tunnel ID
TUNNEL_ID=$(cloudflared tunnel list | grep "$TUNNEL_NAME" | awk '{print $1}')
echo -e "Tunnel ID: ${GREEN}$TUNNEL_ID${NC}"
# Create tunnel config
mkdir -p "$USER_HOME/.cloudflared"
cat > "$USER_HOME/.cloudflared/config.yml" << CF_CONFIG_END
tunnel: $TUNNEL_ID
credentials-file: $USER_HOME/.cloudflared/$TUNNEL_ID.json
ingress:
- hostname: $CF_DOMAIN
service: http://localhost:$CADDY_PORT
- service: http_status:404
CF_CONFIG_END
# Create DNS record
echo "Creating DNS record..."
cloudflared tunnel route dns "$TUNNEL_ID" "$CF_DOMAIN"
# Create LaunchAgent for tunnel
cat > "$USER_HOME/Library/LaunchAgents/com.reminders.cloudflared.plist" << PLIST_END
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.reminders.cloudflared</string>
<key>ProgramArguments</key>
<array>
<string>/opt/homebrew/bin/cloudflared</string>
<string>tunnel</string>
<string>run</string>
<string>$TUNNEL_NAME</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>$SERVICES_DIR/logs/cloudflared.log</string>
<key>StandardErrorPath</key>
<string>$SERVICES_DIR/logs/cloudflared.error.log</string>
<key>WorkingDirectory</key>
<string>$USER_HOME/.cloudflared</string>
</dict>
</plist>
PLIST_END
echo -e "${GREEN}✓ Cloudflare Tunnel configured${NC}"
echo ""
fi
# ============================================================================
# STEP 9: Load Services
# ============================================================================
echo -e "${CYAN}═══ Step 9: Starting Services ═══${NC}"
echo ""
# Load LaunchAgents
echo "Loading reminders-api..."
launchctl load "$USER_HOME/Library/LaunchAgents/com.reminders.api.plist"
sleep 2
if [[ "$INSTALL_MCP" =~ ^[Yy]$ ]]; then
echo "Loading MCP server..."
launchctl load "$USER_HOME/Library/LaunchAgents/com.reminders.mcp.plist" 2>/dev/null || echo " (MCP needs configuration)"
sleep 1
fi
echo "Loading Caddy..."
launchctl load "$USER_HOME/Library/LaunchAgents/com.reminders.caddy.plist"
sleep 2
if [ "$EXPOSURE_METHOD" = "1" ]; then
echo "Loading Cloudflare Tunnel..."
launchctl load "$USER_HOME/Library/LaunchAgents/com.reminders.cloudflared.plist"
sleep 2
fi
echo ""
# ============================================================================
# STEP 10: Test Services
# ============================================================================
echo -e "${CYAN}═══ Step 10: Testing Services ═══${NC}"
echo ""
sleep 3 # Give services time to start
# Test reminders-api
if curl -s http://localhost:$REMINDERS_API_PORT/lists > /dev/null 2>&1; then
echo -e "${GREEN}✓ reminders-api is running${NC}"
else
echo -e "${RED}✗ reminders-api is not responding${NC}"
echo " Check: tail -f $SERVICES_DIR/logs/reminders-api.error.log"
fi
# Test Caddy
if curl -s http://localhost:$CADDY_PORT/health > /dev/null 2>&1; then
echo -e "${GREEN}✓ Caddy is running${NC}"
else
echo -e "${RED}✗ Caddy is not responding${NC}"
echo " Check: tail -f $SERVICES_DIR/logs/caddy.error.log"
fi
# Test API through Caddy
if curl -s http://localhost:$CADDY_PORT/api/lists > /dev/null 2>&1; then
echo -e "${GREEN}✓ Can reach API through Caddy at /api/*${NC}"
else
echo -e "${YELLOW}⚠ Cannot reach API through Caddy${NC}"
fi
echo ""
# ============================================================================
# STEP 11: Display Configuration Summary
# ============================================================================
echo ""
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║ Installation Complete! ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""
echo -e "${CYAN}Service URLs:${NC}"
echo " reminders-api: http://localhost:$REMINDERS_API_PORT/lists"
echo " Caddy: http://localhost:$CADDY_PORT/health"
echo " API via Caddy: http://localhost:$CADDY_PORT/api/lists"
if [[ "$INSTALL_MCP" =~ ^[Yy]$ ]]; then
echo " MCP via Caddy: http://localhost:$CADDY_PORT/mcp/"
fi
echo ""
if [ "$EXPOSURE_METHOD" = "1" ]; then
echo -e "${CYAN}Public URL:${NC}"
echo " https://$CF_DOMAIN/api/lists"
echo " https://$CF_DOMAIN/health"
echo ""
fi
if [ -n "$AUTH_TOKEN" ]; then
echo -e "${CYAN}Authentication:${NC}"
echo " Token: $AUTH_TOKEN"
echo " Header: Authorization: Bearer $AUTH_TOKEN"
echo ""
fi
echo -e "${CYAN}Home Assistant Configuration:${NC}"
if [ "$EXPOSURE_METHOD" = "1" ]; then
echo " URL: https://$CF_DOMAIN/api"
else
echo " URL: http://localhost:$CADDY_PORT/api"
fi
if [ -n "$AUTH_TOKEN" ]; then
echo " Token: $AUTH_TOKEN"
fi
echo ""
echo -e "${CYAN}Files Created:${NC}"
echo " $SERVICES_DIR/Caddyfile"
echo " $SERVICES_DIR/run-reminders-api.sh"
if [[ "$INSTALL_MCP" =~ ^[Yy]$ ]]; then
echo " $SERVICES_DIR/run-mcp.sh (needs configuration)"
fi
echo " $SERVICES_DIR/run-caddy.sh"
echo " ~/Library/LaunchAgents/com.reminders.*.plist"
echo ""
echo -e "${CYAN}Logs:${NC}"
echo " tail -f $SERVICES_DIR/logs/*.log"
echo ""
echo -e "${CYAN}Manage Services:${NC}"
echo " launchctl list | grep com.reminders"
echo " launchctl unload ~/Library/LaunchAgents/com.reminders.*.plist"
echo " launchctl load ~/Library/LaunchAgents/com.reminders.*.plist"
echo ""
if [ "$EXPOSURE_METHOD" = "2" ]; then
echo -e "${CYAN}Next Steps:${NC}"
echo " Run: tailscale funnel $CADDY_PORT"
echo ""
elif [ "$EXPOSURE_METHOD" = "3" ]; then
echo -e "${CYAN}Next Steps:${NC}"
echo " Run: tailscale serve https / http://127.0.0.1:$CADDY_PORT"
echo ""
fi
echo -e "${GREEN}Installation complete! Your services are running.${NC}"
echo ""