-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathstart-certmate.sh
More file actions
executable file
Β·634 lines (537 loc) Β· 26.7 KB
/
Copy pathstart-certmate.sh
File metadata and controls
executable file
Β·634 lines (537 loc) Β· 26.7 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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#!/bin/bash
# Complete script to build and run CertMate with existing certificates
# Respects existing settings and attempts to restore from backups when needed
set -e
echo "π Building and running CertMate with existing certificates..."
# Note: This script uses Docker to avoid local Python dependency conflicts
# If you see certbot/josepy errors, that's expected - we use Docker containers instead
# Function to ensure all necessary directories exist with proper permissions
ensure_directories() {
local dirs=("./certificates" "./data" "./backups" "./backups/settings" "./backups/certificates" "./logs" "./letsencrypt" "./letsencrypt/config" "./letsencrypt/logs" "./letsencrypt/work")
for dir in "${dirs[@]}"; do
if [ ! -d "$dir" ]; then
echo "π Creating directory: $dir"
mkdir -p "$dir"
fi
done
# Ensure proper permissions for data and backup directories
chmod 755 ./data ./backups 2>/dev/null || true
chmod 644 ./data/settings.json 2>/dev/null || true
}
# Ensure all necessary directories exist
ensure_directories
# Function to check if a certificate directory contains valid certificates
check_valid_certificate() {
local domain_dir="$1"
if [ -f "$domain_dir/fullchain.pem" ] && [ -f "$domain_dir/privkey.pem" ]; then
return 0
fi
return 1
}
# Function to find the best settings backup with comprehensive fallback strategy
find_best_settings_backup() {
local backup_dir="./backups/settings"
local backup_file=""
# Check multiple possible backup locations for backward compatibility
local backup_locations=(
"./backups/settings"
"./backups"
"./data/backups"
"./backup"
"./"
)
echo "π Searching for settings backups in multiple locations..."
for location in "${backup_locations[@]}"; do
if [ -d "$location" ]; then
echo " π Checking: $location"
# Look for various backup file patterns (backward compatibility)
local backup_patterns=(
"settings*.json"
"settings*.bak"
"config*.json"
"certmate*.json"
"*.settings.json"
"backup*.json"
)
for pattern in "${backup_patterns[@]}"; do
local found_files=$(find "$location" -maxdepth 1 -name "$pattern" -type f 2>/dev/null)
if [ -n "$found_files" ]; then
echo " β Found backup files matching: $pattern"
fi
done
fi
done
# Priority 1: Recent backups with domains configured
echo "π― Priority 1: Looking for backups with configured domains..."
backup_file=$(find "${backup_locations[@]}" -maxdepth 1 -name "*.json" -type f 2>/dev/null | \
xargs grep -l '"domains".*\[[^]]\+\]' 2>/dev/null | \
xargs ls -t 2>/dev/null | head -1)
if [ -n "$backup_file" ] && [ -f "$backup_file" ]; then
echo " β
Found backup with domains: $(basename "$backup_file")"
echo "$backup_file"
return 0
fi
# Priority 2: Backups with email configured (indicating setup completion)
echo "π― Priority 2: Looking for backups with email configuration..."
backup_file=$(find "${backup_locations[@]}" -maxdepth 1 -name "*.json" -type f 2>/dev/null | \
xargs grep -l '"email".*"[^"]\+@[^"]\+"' 2>/dev/null | \
xargs ls -t 2>/dev/null | head -1)
if [ -n "$backup_file" ] && [ -f "$backup_file" ]; then
local backup_age=$(stat -f "%m" "$backup_file" 2>/dev/null || stat -c "%Y" "$backup_file" 2>/dev/null || echo "0")
local current_time=$(date +%s)
local age_days=$(( (current_time - backup_age) / 86400 ))
echo " π
Found backup with email (age: $age_days days): $(basename "$backup_file")"
# Accept backups up to 30 days old if they have meaningful configuration
if [ $age_days -le 30 ]; then
echo " β
Backup is acceptable (within 30 days)"
echo "$backup_file"
return 0
fi
fi
# Priority 3: Any recent backup with DNS provider configuration
echo "π― Priority 3: Looking for backups with DNS provider settings..."
backup_file=$(find "${backup_locations[@]}" -maxdepth 1 -name "*.json" -type f 2>/dev/null | \
xargs grep -l '"dns_provider"' 2>/dev/null | \
xargs ls -t 2>/dev/null | head -1)
if [ -n "$backup_file" ] && [ -f "$backup_file" ]; then
local backup_age=$(stat -f "%m" "$backup_file" 2>/dev/null || stat -c "%Y" "$backup_file" 2>/dev/null || echo "0")
local current_time=$(date +%s)
local age_days=$(( (current_time - backup_age) / 86400 ))
echo " π
Found backup with DNS provider (age: $age_days days): $(basename "$backup_file")"
if [ $age_days -le 7 ]; then
echo " β
Recent DNS provider backup is acceptable"
echo "$backup_file"
return 0
fi
fi
# Priority 4: Any JSON file that looks like settings (most recent)
echo "π― Priority 4: Looking for any settings-like JSON files..."
backup_file=$(find "${backup_locations[@]}" -maxdepth 1 -name "*.json" -type f 2>/dev/null | \
xargs grep -l '"api_bearer_token"\|"setup_completed"\|"auto_renew"' 2>/dev/null | \
xargs ls -t 2>/dev/null | head -1)
if [ -n "$backup_file" ] && [ -f "$backup_file" ]; then
echo " π Found settings-like file: $(basename "$backup_file")"
echo "$backup_file"
return 0
fi
echo " β No suitable backup files found"
return 1
}
# Function to restore settings from backup with comprehensive fallback strategy
restore_settings_from_backup() {
local backup_file="$1"
local settings_file="$2"
local timestamp=$(date '+%Y%m%d_%H%M%S')
echo "π Restoring settings from backup: $(basename "$backup_file")"
# Create backup of current settings before restore
if [ -f "$settings_file" ]; then
cp "$settings_file" "./backups/settings/settings_${timestamp}_pre_restore.json"
echo "πΎ Created pre-restore backup"
fi
if command -v jq >/dev/null 2>&1; then
# Read backup data and handle both wrapped and direct formats
local backup_data=$(cat "$backup_file")
local settings_data=""
# Check if backup has wrapped format (with metadata)
if echo "$backup_data" | jq -e '.settings' >/dev/null 2>&1; then
echo "π Processing wrapped backup format"
settings_data=$(echo "$backup_data" | jq '.settings')
elif echo "$backup_data" | jq -e '.metadata' >/dev/null 2>&1; then
echo "π Processing metadata backup format"
settings_data=$(echo "$backup_data" | jq 'del(.metadata)')
else
echo "π Processing direct backup format"
settings_data="$backup_data"
fi
# Validate restored settings structure
if echo "$settings_data" | jq -e 'type == "object"' >/dev/null 2>&1; then
echo "$settings_data" | jq '.' > "$settings_file"
echo "β
Settings restored successfully from backup"
return 0
else
echo "β Invalid settings structure in backup"
return 1
fi
else
# Fallback without jq - direct copy
echo "β οΈ jq not available - attempting direct copy"
if [ -f "$backup_file" ]; then
cp "$backup_file" "$settings_file"
echo "β
Settings restored via direct copy"
return 0
fi
fi
return 1
}
# Function to migrate legacy settings formats and ensure compatibility
migrate_legacy_settings() {
local settings_file="$1"
local needs_migration=false
local timestamp=$(date '+%Y%m%d_%H%M%S')
echo "π Checking if settings migration is needed..."
# Create backup before any migration
cp "$settings_file" "./backups/settings/settings_${timestamp}_pre_migration.json"
# Check for various legacy format issues and fix them
if command -v jq >/dev/null 2>&1; then
local temp_file=$(mktemp)
# Ensure all required fields exist with default values
local required_fields=(
"domains:[]"
"email:\"\""
"auto_renew:true"
"setup_completed:false"
"dns_provider:\"cloudflare\""
"dns_providers:{}"
)
# Start with current settings
jq '.' "$settings_file" > "$temp_file"
# Add missing fields with defaults
for field_def in "${required_fields[@]}"; do
local field_name=$(echo "$field_def" | cut -d: -f1)
local field_default=$(echo "$field_def" | cut -d: -f2-)
if ! jq -e ".$field_name" "$temp_file" >/dev/null 2>&1; then
echo " π Adding missing field: $field_name"
jq --argjson value "$field_default" ". + {\"$field_name\": \$value}" "$temp_file" > "$temp_file.tmp"
mv "$temp_file.tmp" "$temp_file"
needs_migration=true
fi
done
# Ensure api_bearer_token exists
if ! jq -e '.api_bearer_token' "$temp_file" >/dev/null 2>&1 || [ "$(jq -r '.api_bearer_token' "$temp_file")" = "null" ] || [ "$(jq -r '.api_bearer_token' "$temp_file")" = "" ]; then
echo " π Generating missing API bearer token"
local new_token=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-32)
jq --arg token "$new_token" '.api_bearer_token = $token' "$temp_file" > "$temp_file.tmp"
mv "$temp_file.tmp" "$temp_file"
needs_migration=true
fi
# Ensure certificate_storage structure exists
if ! jq -e '.certificate_storage' "$temp_file" >/dev/null 2>&1; then
echo " π Adding certificate storage configuration"
local storage_config='{
"backend": "local_filesystem",
"cert_dir": "certificates",
"azure_keyvault": {
"vault_url": "",
"client_id": "",
"client_secret": "",
"tenant_id": ""
},
"aws_secrets_manager": {
"region": "us-east-1",
"access_key_id": "",
"secret_access_key": ""
},
"hashicorp_vault": {
"vault_url": "",
"vault_token": "",
"mount_point": "secret",
"engine_version": "v2"
},
"infisical": {
"site_url": "https://app.infisical.com",
"client_id": "",
"client_secret": "",
"project_id": "",
"environment": "prod"
}
}'
jq --argjson storage "$storage_config" '.certificate_storage = $storage' "$temp_file" > "$temp_file.tmp"
mv "$temp_file.tmp" "$temp_file"
needs_migration=true
fi
# Convert legacy cloudflare_token to dns_providers format if needed
local cf_token=$(jq -r '.cloudflare_token // ""' "$temp_file")
if [ -n "$cf_token" ] && [ "$cf_token" != "null" ] && [ "$cf_token" != "" ]; then
echo " π Migrating legacy Cloudflare token to new dns_providers format"
jq --arg token "$cf_token" '.dns_providers.cloudflare = {"api_token": $token}' "$temp_file" > "$temp_file.tmp"
mv "$temp_file.tmp" "$temp_file"
needs_migration=true
fi
# Apply migration if needed
if [ "$needs_migration" = true ]; then
mv "$temp_file" "$settings_file"
echo " β
Settings migrated successfully"
echo " πΎ Pre-migration backup: ./backups/settings/settings_${timestamp}_pre_migration.json"
else
rm -f "$temp_file"
echo " β
Settings already up to date"
fi
else
echo " β οΈ jq not available - skipping automatic migration"
fi
}
# Function to auto-populate domains from certificate folders while preserving other settings
auto_populate_domains() {
local settings_file="$1"
local domains_json="[]"
local domain_count=0
echo "π Auto-detecting domains from certificate folders..."
if [ -d "./certificates" ]; then
local temp_domains=()
for cert_dir in ./certificates/*/; do
if [ -d "$cert_dir" ]; then
local domain_name=$(basename "$cert_dir")
# Skip invalid domain names - comprehensive filtering for test/fake domains
if [[ "$domain_name" == "invalid.."* ]] || \
[[ "$domain_name" == "*invalid*" ]] || \
[[ "$domain_name" == "."* ]] || \
[[ "$domain_name" == ".." ]] || \
[[ "$domain_name" == *".."* ]] || \
[[ "$domain_name" == *"localhost"* ]] || \
[[ "$domain_name" == "example.com" ]] || \
[[ "$domain_name" == "test.example.com" ]] || \
[[ "$domain_name" == "staging.example.com" ]] || \
[[ "$domain_name" == "invalid..domain.com" ]] || \
[[ "$domain_name" == "test.com" ]] || \
[[ "$domain_name" == *"example.org"* ]] || \
[[ "$domain_name" == *"example.net"* ]] || \
[[ "$domain_name" == *"test."* ]] && [[ "$domain_name" != *".test.certmate.org" ]] || \
[[ "$domain_name" == *"staging."* ]] || \
[[ "$domain_name" == *"demo."* ]] || \
[[ "$domain_name" == *"temp."* ]] || \
[[ "$domain_name" == *".local"* ]] || \
[[ ${#domain_name} -lt 4 ]] || \
[[ "$domain_name" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo " β οΈ Skipping test/invalid/example domain: $domain_name"
continue
fi
# Check if it has valid certificates
if check_valid_certificate "$cert_dir"; then
temp_domains+=("$domain_name")
echo " β
Found valid certificates for: $domain_name"
((domain_count++))
else
echo " β οΈ Incomplete certificates for: $domain_name (missing fullchain.pem or privkey.pem)"
fi
fi
done
# Build JSON array
if [ ${#temp_domains[@]} -gt 0 ]; then
domains_json="["
for i in "${!temp_domains[@]}"; do
if [ $i -gt 0 ]; then
domains_json+=", "
fi
domains_json+="\"${temp_domains[$i]}\""
done
domains_json+="]"
fi
fi
echo "π Auto-detected $domain_count valid certificate domains"
# Update settings file with auto-detected domains (preserving other settings)
if [ $domain_count -gt 0 ] && [ -f "$settings_file" ]; then
echo "π§ Updating settings with auto-detected domains..."
# Create a backup of current settings
local timestamp=$(date '+%Y%m%d_%H%M%S')
cp "$settings_file" "./backups/settings/settings_${timestamp}_pre_auto_update.json"
# Use jq to update the domains array while preserving all other settings
if command -v jq >/dev/null 2>&1; then
# Check if domains already exist and decide what to do
local existing_domains_count=$(jq '.domains | length' "$settings_file" 2>/dev/null || echo "0")
if [ "$existing_domains_count" -eq 0 ]; then
echo " π No existing domains - adding auto-detected domains"
# Create the domains array as proper JSON
temp_json_file=$(mktemp)
printf '%s\n' "${temp_domains[@]}" | jq -R . | jq -s . > "$temp_json_file"
jq --argjson domains "$(cat "$temp_json_file")" '.domains = $domains' "$settings_file" > "$settings_file.tmp" && mv "$settings_file.tmp" "$settings_file"
rm -f "$temp_json_file"
echo " β
Updated settings with $domain_count domains"
else
echo " οΏ½ Found $existing_domains_count existing domains in settings"
# Check if auto-detected domains are different from existing ones
local existing_domains=($(jq -r '.domains[]' "$settings_file" 2>/dev/null))
local new_domains=()
for domain in "${temp_domains[@]}"; do
local found=false
for existing in "${existing_domains[@]}"; do
if [ "$domain" = "$existing" ]; then
found=true
break
fi
done
if [ "$found" = false ]; then
new_domains+=("$domain")
fi
done
if [ ${#new_domains[@]} -gt 0 ]; then
echo " π Found ${#new_domains[@]} new domains to add:"
printf ' - %s\n' "${new_domains[@]}"
# Merge new domains with existing ones
all_domains=("${existing_domains[@]}" "${new_domains[@]}")
temp_json_file=$(mktemp)
printf '%s\n' "${all_domains[@]}" | jq -R . | jq -s . > "$temp_json_file"
jq --argjson domains "$(cat "$temp_json_file")" '.domains = $domains' "$settings_file" > "$settings_file.tmp" && mv "$settings_file.tmp" "$settings_file"
rm -f "$temp_json_file"
echo " β
Merged ${#new_domains[@]} new domains with existing settings"
else
echo " β
All auto-detected domains already exist in settings"
fi
fi
else
echo " β οΈ jq not available - manual domain configuration may be needed"
fi
echo "πΎ Created backup: ./backups/settings/settings_${timestamp}_pre_auto_update.json"
elif [ $domain_count -eq 0 ]; then
echo "π No valid certificate domains found to auto-populate"
fi
return $domain_count
}
# Check if certificates exist
if [ ! -d "./certificates" ]; then
echo "β No certificates directory found. Creating empty one..."
mkdir -p certificates
fi
cert_count=$(find ./certificates -name "*.pem" 2>/dev/null | wc -l)
domain_count=$(find ./certificates -mindepth 1 -maxdepth 1 -type d 2>/dev/null | wc -l)
echo "π Found $domain_count certificate directories with $cert_count certificate files"
# List certificate domains
if [ $domain_count -gt 0 ]; then
echo "π Certificate domains found:"
ls -1 certificates/ | head -10
if [ $domain_count -gt 10 ]; then
echo " ... and $((domain_count - 10)) more"
fi
fi
# Check data directory and backups (already handled by ensure_directories)
echo "β
All necessary directories are ready"
# Handle settings restoration and domain auto-detection
settings_file="./data/settings.json"
if [ -f "$settings_file" ]; then
echo "β
Found existing settings file"
# Check if domains array is empty
current_domains_count=0
if command -v jq >/dev/null 2>&1; then
current_domains_count=$(jq '.domains | length' "$settings_file" 2>/dev/null || echo "0")
fi
if [ "$current_domains_count" -eq 0 ] && [ $domain_count -gt 0 ]; then
echo "π Settings has no domains but certificates exist - checking if auto-population is safe..."
# Check if this is a fresh installation or if user has meaningful settings
local has_email=$(jq -r '.email // ""' "$settings_file" 2>/dev/null | grep -E '.+@.+\..+' || echo "")
local has_dns_config=$(jq -r '.dns_providers | keys | length' "$settings_file" 2>/dev/null || echo "0")
local setup_completed=$(jq -r '.setup_completed // false' "$settings_file" 2>/dev/null)
if [ -n "$has_email" ] || [ "$has_dns_config" -gt 0 ] || [ "$setup_completed" = "true" ]; then
echo "β οΈ Detected existing user configuration - skipping automatic domain population"
echo "π‘ User has configured email: $([ -n "$has_email" ] && echo "Yes" || echo "No")"
echo "π‘ DNS providers configured: $has_dns_config"
echo "π‘ Setup completed: $setup_completed"
echo "π§ If you want to add certificate domains, please use the web interface"
return 0
fi
# Try to restore from backup first
best_backup=$(find_best_settings_backup)
if [ -n "$best_backup" ]; then
echo "πΎ Found suitable settings backup: $(basename "$best_backup")"
# Check backup age for automated decision
backup_age=$(stat -f "%m" "$best_backup" 2>/dev/null || stat -c "%Y" "$best_backup" 2>/dev/null || echo "0")
current_time=$(date +%s)
age_days=$(( (current_time - backup_age) / 86400 ))
echo "π
Backup is $age_days days old"
if [ $age_days -le 2 ]; then
echo "οΏ½ Auto-restoring from recent backup..."
# Create backup of current settings
timestamp=$(date '+%Y%m%d_%H%M%S')
cp "$settings_file" "./backups/settings/settings_${timestamp}_pre_restore.json"
# Extract settings from backup (handle both wrapped and direct formats)
if jq -e '.settings' "$best_backup" >/dev/null 2>&1; then
# Wrapped format (newer backups)
jq '.settings' "$best_backup" > "$settings_file.tmp"
echo "π Restored from wrapped backup format"
else
# Direct format (older backups)
cp "$best_backup" "$settings_file.tmp"
echo "π Restored from direct backup format"
fi
# Now auto-populate domains from certificates while preserving restored settings
if [ -f "$settings_file.tmp" ]; then
mv "$settings_file.tmp" "$settings_file"
echo "β
Restored settings from backup"
echo "π Now auto-detecting domains from certificates to update the restored settings..."
auto_populate_domains "$settings_file"
fi
echo "πΎ Created pre-restore backup: ./backups/settings/settings_${timestamp}_pre_restore.json"
else
echo "π
Backup is older than 2 days - using auto-detection instead"
auto_populate_domains "$settings_file"
fi
else
echo "π‘ No suitable backup found - auto-detecting domains from certificates..."
auto_populate_domains "$settings_file"
fi
elif [ "$current_domains_count" -eq 0 ] && [ $domain_count -eq 0 ]; then
echo "π No domains configured and no certificates found - fresh installation"
else
echo "β
Settings already contains $current_domains_count configured domains"
if [ $domain_count -gt 0 ] && [ "$current_domains_count" -ne $domain_count ]; then
echo "π Note: Found $domain_count certificate directories but settings shows $current_domains_count domains"
echo "π‘ You may want to verify domain configuration in the web interface"
fi
fi
else
echo "π No settings file found - will be created on first run"
fi
# Create .env file if it doesn't exist
if [ ! -f ".env" ]; then
echo "βοΈ Creating .env file..."
cat > .env << EOF
# CertMate Environment Configuration
SECRET_KEY=$(openssl rand -hex 32)
API_BEARER_TOKEN=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-32)
FLASK_ENV=production
# DNS Provider Tokens (set these for your provider)
# CLOUDFLARE_TOKEN=your_cloudflare_token_here
# AWS_ACCESS_KEY_ID=your_aws_key_here
# AWS_SECRET_ACCESS_KEY=your_aws_secret_here
EOF
echo "β
Created .env file with secure tokens"
else
echo "β
Using existing .env file"
fi
# Build and run with docker-compose
echo "π¨ Building Docker image..."
docker-compose build
echo "π Starting CertMate..."
docker-compose up -d
# Wait for container to start
echo "β³ Waiting for container to start..."
sleep 5
# Check if container is running
if docker-compose ps | grep -q "Up"; then
echo "β
CertMate is running!"
# Check if certificates are visible in container
echo "π Checking certificates in container..."
container_cert_count=$(docker-compose exec -T certmate find /app/certificates -name "*.pem" 2>/dev/null | wc -l || echo "0")
container_domain_count=$(docker-compose exec -T certmate find /app/certificates -mindepth 1 -maxdepth 1 -type d 2>/dev/null | wc -l || echo "0")
echo "π Container sees $container_domain_count domains with $container_cert_count certificate files"
# Check final settings state
if [ -f "./data/settings.json" ] && command -v jq >/dev/null 2>&1; then
final_domains_count=$(jq '.domains | length' "./data/settings.json" 2>/dev/null || echo "0")
echo "βοΈ Final settings configuration: $final_domains_count domains"
if [ "$final_domains_count" -gt 0 ]; then
echo "π Configured domains:"
jq -r '.domains[]' "./data/settings.json" 2>/dev/null | head -5 | sed 's/^/ - /'
if [ "$final_domains_count" -gt 5 ]; then
echo " ... and $((final_domains_count - 5)) more"
fi
fi
fi
if [ "$container_domain_count" -eq "$domain_count" ]; then
echo "β
All certificates are properly mounted!"
else
echo "β οΈ Certificate count mismatch. Checking permissions..."
# Fix permissions if needed
echo "π§ Fixing permissions..."
docker-compose exec -T certmate chown -R certmate:certmate /app/certificates /app/data /app/logs
fi
echo ""
echo "π Access URLs:"
echo " Web Interface: http://localhost:8000"
echo " API Documentation: http://localhost:8000/docs/"
echo " Health Check: http://localhost:8000/health"
echo ""
echo "π To debug further:"
echo " docker-compose logs -f certmate"
echo " docker-compose exec certmate ls -la /app/certificates"
else
echo "β Failed to start CertMate. Check logs:"
docker-compose logs certmate
fi