-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy-quadlet.sh
More file actions
executable file
·453 lines (388 loc) · 13.4 KB
/
deploy-quadlet.sh
File metadata and controls
executable file
·453 lines (388 loc) · 13.4 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
#!/bin/bash
# Everything OpenCode Quadlet Deployment Script
# Deploy Everything OpenCode as systemd services using Podman Quadlet
set -e
# Configuration
QUADLET_DIR="/etc/containers/systemd"
APP_DIR="/etc/everything-opencode"
CONTAINER_IMAGE="everything-opencode:latest"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Functions
print_header() {
echo -e "${BLUE}"
echo "=========================================="
echo "Everything OpenCode Quadlet Deployment"
echo "=========================================="
echo -e "${NC}"
}
print_step() {
echo -e "${YELLOW}[STEP] $1${NC}"
}
print_success() {
echo -e "${GREEN}✅ $1${NC}"
}
print_error() {
echo -e "${RED}❌ $1${NC}"
}
check_root() {
if [ "$EUID" -ne 0 ]; then
print_error "This script must be run as root"
echo "Please run: sudo $0"
exit 1
fi
}
check_podman_version() {
print_step "Checking Podman version..."
if ! podman --version &> /dev/null; then
print_error "Podman is not installed"
echo "Please install Podman 4.4+ first"
exit 1
fi
PODMAN_VERSION=$(podman --version | awk '{print $3}')
PODMAN_MAJOR=$(echo $PODMAN_VERSION | cut -d. -f1)
PODMAN_MINOR=$(echo $PODMAN_VERSION | cut -d. -f2)
if [ "$PODMAN_MAJOR" -lt 4 ] || ([ "$PODMAN_MAJOR" -eq 4 ] && [ "$PODMAN_MINOR" -lt 4 ]); then
print_error "Podman 4.4+ is required for Quadlet (found $PODMAN_VERSION)"
echo "Please upgrade Podman:"
echo " Ubuntu/Debian: sudo apt update && sudo apt upgrade podman"
echo " Fedora/RHEL: sudo dnf update podman"
exit 1
fi
print_success "Podman $PODMAN_VERSION is compatible with Quadlet"
}
check_quadlet_support() {
print_step "Checking Quadlet support..."
if [ ! -d "/etc/containers/systemd" ]; then
print_success "Creating Quadlet directory..."
mkdir -p /etc/containers/systemd
fi
# Check if systemd supports Quadlet
if ! systemctl --version | grep -q "systemd 24[0-9]\|systemd 25[0-9]"; then
echo "Note: Older systemd versions may have limited Quadlet support"
fi
}
build_image() {
print_step "Building container image..."
if [ -f "Dockerfile" ]; then
podman build -t $CONTAINER_IMAGE .
print_success "Image built successfully"
else
print_error "Dockerfile not found in current directory"
exit 1
fi
}
setup_directories() {
print_step "Setting up directories..."
# Create Quadlet directory
mkdir -p $QUADLET_DIR
print_success "Created $QUADLET_DIR"
# Create application directory
mkdir -p $APP_DIR
mkdir -p $APP_DIR/config
print_success "Created $APP_DIR"
# Create data directories
mkdir -p ~/.local/share/containers/everything-opencode/data
mkdir -p ~/.local/share/containers/postgres/data
mkdir -p ~/.local/share/containers/redis/data
print_success "Created data directories"
}
copy_quadlet_files() {
print_step "Copying Quadlet configuration files..."
# Check if quadlet directory exists
if [ ! -d "quadlet" ]; then
print_error "quadlet/ directory not found"
echo "Please run this script from the Everything OpenCode repository root"
exit 1
fi
# Copy Quadlet files
cp quadlet/*.container $QUADLET_DIR/
cp quadlet/*.network $QUADLET_DIR/
# Set correct permissions
chmod 644 $QUADLET_DIR/*.container
chmod 644 $QUADLET_DIR/*.network
print_success "Quadlet files copied to $QUADLET_DIR"
}
setup_environment() {
print_step "Setting up environment configuration..."
# Create environment file
cat > $APP_DIR/env << EOF
# Everything OpenCode Environment Configuration
NODE_ENV=production
DEBUG_ENABLED=true
PORT=3000
DEBUG_PORT=3005
LOG_LEVEL=info
CORS_ORIGIN=*
MAX_FILE_SIZE=10MB
DATABASE_URL=postgresql://opencode:\${POSTGRES_PASSWORD}@everything-opencode-postgres:5432/opencode
REDIS_URL=redis://everything-opencode-redis:6379
EOF
# Create PostgreSQL password file
if [ ! -f "$APP_DIR/postgres-password" ]; then
echo "opencode123" > $APP_DIR/postgres-password
chmod 600 $APP_DIR/postgres-password
print_success "Created PostgreSQL password file"
else
print_success "PostgreSQL password file already exists"
fi
# Create sample configuration
if [ ! -f "$APP_DIR/config/app.config" ]; then
cat > $APP_DIR/config/app.config << EOF
# Everything OpenCode Application Configuration
# Add your application-specific configuration here
EOF
print_success "Created sample configuration"
fi
print_success "Environment configuration complete"
}
reload_systemd() {
print_step "Reloading systemd..."
systemctl daemon-reload
print_success "Systemd reloaded"
}
enable_services() {
print_step "Enabling and starting services..."
# Enable network first
if [ -f "$QUADLET_DIR/everything-opencode.network" ]; then
systemctl enable --now everything-opencode.network
print_success "Network service enabled"
fi
# Enable database services
if [ -f "$QUADLET_DIR/postgres.container" ]; then
systemctl enable --now postgres.container
print_success "PostgreSQL service enabled"
fi
if [ -f "$QUADLET_DIR/redis.container" ]; then
systemctl enable --now redis.container
print_success "Redis service enabled"
fi
# Enable main application
if [ -f "$QUADLET_DIR/everything-opencode.container" ]; then
systemctl enable --now everything-opencode.container
print_success "Main application service enabled"
fi
# Wait a moment for services to start
sleep 3
}
check_service_status() {
print_step "Checking service status..."
SERVICES=("everything-opencode.network" "postgres.container" "redis.container" "everything-opencode.container")
for service in "${SERVICES[@]}"; do
if systemctl is-enabled $service >/dev/null 2>&1; then
STATUS=$(systemctl is-active $service)
if [ "$STATUS" = "active" ]; then
print_success "$service: active"
else
print_error "$service: $STATUS"
echo "View logs with: journalctl -u $service -xe"
fi
fi
done
}
check_application_health() {
print_step "Checking application health..."
# Wait for application to start
echo "Waiting for application to start (max 60 seconds)..."
local max_retries=20
local retry_count=0
while [ $retry_count -lt $max_retries ]; do
if curl -s -f http://localhost:3000/health > /dev/null 2>&1; then
print_success "Application health check passed"
return 0
fi
retry_count=$((retry_count + 1))
echo " Health check attempt $retry_count/$max_retries failed, retrying..."
sleep 3
done
print_error "Health check failed after $max_retries attempts"
echo "View application logs with: journalctl -u everything-opencode.container -f"
return 1
}
show_deployment_info() {
print_step "Deployment Information:"
echo ""
echo -e "${GREEN}✅ Quadlet Deployment Complete!${NC}"
echo ""
echo "Services deployed as systemd units:"
echo " Network: everything-opencode.network"
echo " PostgreSQL: postgres.container"
echo " Redis: redis.container"
echo " Main Application: everything-opencode.container"
echo ""
echo "Access URLs:"
echo " Main Application: http://localhost:3000"
echo " Debug Server: http://localhost:3005"
echo " Health Check: http://localhost:3000/health"
echo ""
echo "Management Commands:"
echo " View all services: systemctl list-units '*.container' '*.network'"
echo " View logs: journalctl -u everything-opencode.container -f"
echo " Restart service: systemctl restart everything-opencode.container"
echo " Stop service: systemctl stop everything-opencode.container"
echo " Check status: systemctl status everything-opencode.container"
echo ""
echo "Configuration Files:"
echo " Quadlet files: $QUADLET_DIR/"
echo " Environment: $APP_DIR/env"
echo " PostgreSQL password: $APP_DIR/postgres-password"
echo " Application config: $APP_DIR/config/"
echo ""
echo "Data Directories:"
echo " Application data: ~/.local/share/containers/everything-opencode/data"
echo " PostgreSQL data: ~/.local/share/containers/postgres/data"
echo " Redis data: ~/.local/share/containers/redis/data"
echo ""
echo "Next Steps:"
echo " 1. Configure your application in $APP_DIR/config/"
echo " 2. Update PostgreSQL password in $APP_DIR/postgres-password"
echo " 3. Review and customize Quadlet files in $QUADLET_DIR/"
echo " 4. Set up monitoring and backups"
echo ""
}
cleanup_old_services() {
print_step "Checking for old services..."
# Stop and disable any old systemd services
OLD_SERVICES=("everything-opencode.service" "everything-opencode-postgres.service" "everything-opencode-redis.service")
for service in "${OLD_SERVICES[@]}"; do
if systemctl is-active $service >/dev/null 2>&1; then
print_step "Stopping old service: $service"
systemctl stop $service
systemctl disable $service
print_success "Old service $service stopped and disabled"
fi
done
}
# Main deployment function
deploy() {
print_header
# Check prerequisites
check_root
check_podman_version
check_quadlet_support
# Build image
build_image
# Setup directories and files
setup_directories
copy_quadlet_files
setup_environment
# Clean up old services
cleanup_old_services
# Reload and enable services
reload_systemd
enable_services
# Verify deployment
check_service_status
check_application_health
# Show deployment information
show_deployment_info
echo -e "${GREEN}🎉 Everything OpenCode deployed successfully with Quadlet!${NC}"
}
# Management functions
manage_service() {
local service=$1
local action=$2
case $action in
"start")
systemctl start $service
;;
"stop")
systemctl stop $service
;;
"restart")
systemctl restart $service
;;
"status")
systemctl status $service
;;
"logs")
journalctl -u $service -f
;;
"enable")
systemctl enable $service
;;
"disable")
systemctl disable $service
;;
*)
echo "Unknown action: $action"
exit 1
;;
esac
}
# Handle command line arguments
case "$1" in
"deploy"|"")
deploy
;;
"service")
if [ -z "$2" ] || [ -z "$3" ]; then
echo "Usage: $0 service <service-name> <action>"
echo "Actions: start, stop, restart, status, logs, enable, disable"
echo ""
echo "Available services:"
echo " everything-opencode.container"
echo " postgres.container"
echo " redis.container"
echo " everything-opencode.network"
exit 1
fi
manage_service "$2" "$3"
;;
"status")
check_service_status
;;
"health")
check_application_health
;;
"logs")
journalctl -u everything-opencode.container -f
;;
"info")
show_deployment_info
;;
"clean")
print_step "Cleaning up deployment..."
# Stop and disable services
systemctl stop everything-opencode.container postgres.container redis.container everything-opencode.network 2>/dev/null || true
systemctl disable everything-opencode.container postgres.container redis.container everything-opencode.network 2>/dev/null || true
# Remove Quadlet files
rm -f $QUADLET_DIR/everything-opencode.container
rm -f $QUADLET_DIR/postgres.container
rm -f $QUADLET_DIR/redis.container
rm -f $QUADLET_DIR/everything-opencode.network
# Reload systemd
systemctl daemon-reload
print_success "Deployment cleaned up"
;;
"help"|"-h"|"--help")
echo "Usage: sudo $0 [command]"
echo ""
echo "Commands:"
echo " deploy Deploy Everything OpenCode with Quadlet (default)"
echo " service Manage a specific service"
echo " status Check service status"
echo " health Check application health"
echo " logs View application logs"
echo " info Show deployment information"
echo " clean Clean up deployment"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " sudo $0 deploy # Deploy everything"
echo " sudo $0 service everything-opencode.container restart"
echo " sudo $0 logs # View application logs"
echo " sudo $0 status # Check all services"
echo ""
exit 0
;;
*)
echo "Unknown command: $1"
echo "Use '$0 help' for usage information"
exit 1
;;
esac