Skip to content

Commit b96356c

Browse files
committed
update
1 parent 092eef6 commit b96356c

File tree

12 files changed

+363
-63
lines changed

12 files changed

+363
-63
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace Modules\Caddy\App\Console;
4+
5+
use Illuminate\Console\Command;
6+
7+
class CaddyFormat extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'caddy:format {--validate : Validate after formatting}';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Format Caddy configuration file to fix inconsistencies';
22+
23+
/**
24+
* Execute the console command.
25+
*/
26+
public function handle(): void
27+
{
28+
$caddyConfigPath = '/etc/caddy/Caddyfile';
29+
$caddyBinary = '/usr/bin/caddy';
30+
31+
// Check if Caddyfile exists
32+
if (!file_exists($caddyConfigPath)) {
33+
$this->error('Caddyfile not found at: ' . $caddyConfigPath);
34+
return;
35+
}
36+
37+
// Check if Caddy binary is available
38+
if (!is_executable($caddyBinary)) {
39+
$this->error('Caddy binary not found at: ' . $caddyBinary);
40+
return;
41+
}
42+
43+
$this->info('Formatting Caddyfile...');
44+
45+
// Create backup before formatting
46+
$backupPath = $caddyConfigPath . '.backup.format.' . date('Y-m-d-H-i-s');
47+
if (!copy($caddyConfigPath, $backupPath)) {
48+
$this->error('Failed to create backup. Aborting format operation.');
49+
return;
50+
}
51+
$this->info('Backup created: ' . $backupPath);
52+
53+
// Format the Caddyfile
54+
$command = "{$caddyBinary} fmt --overwrite {$caddyConfigPath} 2>&1";
55+
$output = shell_exec($command);
56+
$exitCode = shell_exec("echo $?");
57+
58+
if (trim($exitCode) === '0') {
59+
$this->info('✓ Caddyfile formatted successfully');
60+
61+
// Validate if requested
62+
if ($this->option('validate')) {
63+
$this->validateCaddyfile();
64+
}
65+
} else {
66+
$this->error('✗ Failed to format Caddyfile: ' . $output);
67+
68+
// Restore backup on failure
69+
if (copy($backupPath, $caddyConfigPath)) {
70+
$this->info('Backup restored due to formatting failure');
71+
}
72+
}
73+
}
74+
75+
/**
76+
* Validate the Caddyfile
77+
*/
78+
protected function validateCaddyfile(): void
79+
{
80+
$caddyConfigPath = '/etc/caddy/Caddyfile';
81+
$caddyBinary = '/usr/bin/caddy';
82+
83+
$this->info('Validating Caddyfile...');
84+
85+
$command = "{$caddyBinary} validate --config {$caddyConfigPath} 2>&1";
86+
$output = shell_exec($command);
87+
$exitCode = shell_exec("echo $?");
88+
89+
if (trim($exitCode) === '0') {
90+
$this->info('✓ Caddyfile is valid');
91+
} else {
92+
$this->error('✗ Caddyfile validation failed:');
93+
$this->line($output);
94+
}
95+
}
96+
}

web/Modules/Caddy/App/Console/CaddyRebuild.php

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
use Modules\Caddy\App\Jobs\CaddyBuild;
77

88
class CaddyRebuild extends Command
9-
{
10-
/**
9+
{ /**
1110
* The name and signature of the console command.
1211
*
1312
* @var string
1413
*/
15-
protected $signature = 'caddy:rebuild';
14+
protected $signature = 'caddy:rebuild {--format : Format Caddyfile after rebuilding}';
1615

1716
/**
1817
* The console command description.
@@ -31,6 +30,61 @@ public function handle(): void
3130
$caddyBuild = new CaddyBuild(true);
3231
$caddyBuild->handle();
3332

33+
// Format Caddyfile if requested or if formatting is needed
34+
if ($this->option('format') || $this->shouldFormatCaddyfile()) {
35+
$this->formatCaddyfile();
36+
}
37+
3438
$this->info('Caddy configuration rebuilt successfully!');
3539
}
40+
41+
/**
42+
* Check if Caddyfile needs formatting
43+
*/
44+
protected function shouldFormatCaddyfile(): bool
45+
{
46+
$caddyConfigPath = '/etc/caddy/Caddyfile';
47+
$caddyBinary = '/usr/bin/caddy';
48+
49+
if (!file_exists($caddyConfigPath) || !is_executable($caddyBinary)) {
50+
return false;
51+
}
52+
53+
// Run caddy validate to check for formatting issues
54+
$output = shell_exec("{$caddyBinary} validate --config {$caddyConfigPath} 2>&1");
55+
56+
// Check if output contains formatting warning
57+
return strpos($output, 'not formatted') !== false;
58+
}
59+
60+
/**
61+
* Format the Caddyfile
62+
*/
63+
protected function formatCaddyfile(): void
64+
{
65+
$caddyConfigPath = '/etc/caddy/Caddyfile';
66+
$caddyBinary = '/usr/bin/caddy';
67+
68+
if (!file_exists($caddyConfigPath)) {
69+
$this->error('Caddyfile not found at: ' . $caddyConfigPath);
70+
return;
71+
}
72+
73+
if (!is_executable($caddyBinary)) {
74+
$this->error('Caddy binary not found at: ' . $caddyBinary);
75+
return;
76+
}
77+
78+
$this->info('Formatting Caddyfile...');
79+
80+
$command = "{$caddyBinary} fmt --overwrite {$caddyConfigPath} 2>&1";
81+
$output = shell_exec($command);
82+
$exitCode = shell_exec("echo $?");
83+
84+
if (trim($exitCode) === '0') {
85+
$this->info('Caddyfile formatted successfully');
86+
} else {
87+
$this->error('Failed to format Caddyfile: ' . $output);
88+
}
89+
}
3690
}

web/Modules/Caddy/App/Filament/Pages/CaddyManagement.php

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public function loadCaddyfile(): void
4242
} else {
4343
$this->caddyfileContent = 'Caddyfile not found';
4444
}
45-
}
46-
47-
protected function getHeaderActions(): array
45+
} protected function getHeaderActions(): array
4846
{
4947
return [
5048
Action::make('reloadConfig')
@@ -71,6 +69,14 @@ protected function getHeaderActions(): array
7169
->send();
7270
}),
7371

72+
Action::make('formatConfig')
73+
->label('Format Configuration')
74+
->icon('heroicon-o-document-text')
75+
->color('info')
76+
->action(function () {
77+
$this->formatCaddyfile();
78+
}),
79+
7480
Action::make('validateConfig')
7581
->label('Validate Configuration')
7682
->icon('heroicon-o-check-circle')
@@ -110,8 +116,7 @@ public function saveCaddyfile(): void
110116
$output = shell_exec('caddy validate --config /etc/caddy/Caddyfile 2>&1');
111117
$isValid = strpos($output, 'valid') !== false;
112118

113-
if ($isValid) {
114-
shell_exec('systemctl reload caddy');
119+
if ($isValid) { shell_exec('systemctl reload caddy');
115120
Notification::make()
116121
->title('Caddyfile saved and reloaded')
117122
->success()
@@ -125,6 +130,65 @@ public function saveCaddyfile(): void
125130
}
126131
}
127132

133+
public function formatCaddyfile(): void
134+
{
135+
$caddyConfigPath = '/etc/caddy/Caddyfile';
136+
$caddyBinary = '/usr/bin/caddy';
137+
138+
if (!file_exists($caddyConfigPath)) {
139+
Notification::make()
140+
->title('Error')
141+
->body('Caddyfile not found')
142+
->danger()
143+
->send();
144+
return;
145+
}
146+
147+
if (!is_executable($caddyBinary)) {
148+
Notification::make()
149+
->title('Error')
150+
->body('Caddy binary not found')
151+
->danger()
152+
->send();
153+
return;
154+
}
155+
156+
// Create backup before formatting
157+
$backupPath = $caddyConfigPath . '.backup.format.' . date('Y-m-d-H-i-s');
158+
if (!copy($caddyConfigPath, $backupPath)) {
159+
Notification::make()
160+
->title('Error')
161+
->body('Failed to create backup')
162+
->danger()
163+
->send();
164+
return;
165+
}
166+
167+
// Format the Caddyfile
168+
$command = "{$caddyBinary} fmt --overwrite {$caddyConfigPath} 2>&1";
169+
$output = shell_exec($command);
170+
$exitCode = shell_exec("echo $?");
171+
172+
if (trim($exitCode) === '0') {
173+
$this->loadCaddyfile(); // Reload the formatted content
174+
175+
Notification::make()
176+
->title('Caddyfile Formatted')
177+
->body('Configuration formatted successfully')
178+
->success()
179+
->send();
180+
} else {
181+
// Restore backup on failure
182+
copy($backupPath, $caddyConfigPath);
183+
184+
Notification::make()
185+
->title('Format Failed')
186+
->body('Failed to format Caddyfile: ' . $output)
187+
->danger()
188+
->send();
189+
}
190+
}
191+
128192
public function getTitle(): string
129193
{
130194
return 'Caddy Management';

web/Modules/Caddy/App/Jobs/CaddyBuild.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,13 @@ protected function buildCaddyConfiguration(): void
7171

7272
// Apply configuration
7373
$this->applyCaddyConfiguration();
74-
}
75-
76-
/**
74+
} /**
7775
* Validate prerequisites before building configuration
7876
*/
7977
protected function validatePrerequisites(): void
8078
{
81-
$caddyConfigPath = config('caddy.config_path', '/etc/caddy/Caddyfile');
82-
$caddyLogPath = config('caddy.log_path', '/var/log/caddy');
79+
$caddyConfigPath = '/etc/caddy/Caddyfile';
80+
$caddyLogPath = '/var/log/caddy';
8381

8482
// Check if Caddy config directory exists and is writable
8583
$configDir = dirname($caddyConfigPath);
@@ -232,22 +230,31 @@ private function createMasterDomainCaddyBlock(MasterDomain $masterDomain, $apach
232230
'enable_ssl' => true,
233231
'enable_www' => true,
234232
'is_master' => true, ];
235-
}
236-
237-
/**
233+
} /**
238234
* Validate generated configuration before applying
239235
*/
240236
protected function validateGeneratedConfig(): void
241237
{
242-
$caddyConfigPath = config('caddy.config_path', '/etc/caddy/Caddyfile');
243-
$caddyBinary = config('caddy.binary_path', '/usr/bin/caddy');
238+
$caddyConfigPath = '/etc/caddy/Caddyfile';
239+
$caddyBinary = '/usr/bin/caddy';
244240

245241
if (!file_exists($caddyConfigPath)) {
246242
throw new \Exception("Generated Caddyfile not found at: {$caddyConfigPath}");
247243
}
248244

249-
// Validate syntax using Caddy binary if available
245+
// Format Caddyfile to fix inconsistencies if Caddy binary is available
250246
if (is_executable($caddyBinary)) {
247+
$formatCommand = "{$caddyBinary} fmt --overwrite {$caddyConfigPath} 2>&1";
248+
$formatOutput = shell_exec($formatCommand);
249+
$formatExitCode = shell_exec("echo $?");
250+
251+
if (trim($formatExitCode) === '0') {
252+
\Log::info('Caddyfile formatted successfully');
253+
} else {
254+
\Log::warning('Caddyfile formatting failed: ' . $formatOutput);
255+
}
256+
257+
// Validate syntax using Caddy binary
251258
$command = "{$caddyBinary} validate --config {$caddyConfigPath} 2>&1";
252259
$output = shell_exec($command);
253260
$exitCode = shell_exec("echo $?");
@@ -258,7 +265,7 @@ protected function validateGeneratedConfig(): void
258265

259266
\Log::info('Caddyfile validation passed');
260267
} else {
261-
\Log::warning('Caddy binary not found, skipping syntax validation');
268+
\Log::warning('Caddy binary not found, skipping syntax validation and formatting');
262269
}
263270
}
264271

@@ -283,13 +290,12 @@ protected function applyCaddyConfiguration(): void
283290
throw $e;
284291
}
285292
}
286-
287-
/**
293+
/**
288294
* Create backup of current configuration
289295
*/
290296
protected function backupCurrentConfig(): void
291297
{
292-
$caddyConfigPath = config('caddy.config_path', '/etc/caddy/Caddyfile');
298+
$caddyConfigPath = '/etc/caddy/Caddyfile';
293299
$backupPath = $caddyConfigPath . '.backup.' . date('Y-m-d-H-i-s');
294300

295301
if (file_exists($caddyConfigPath)) {
@@ -300,13 +306,12 @@ protected function backupCurrentConfig(): void
300306
\Log::info("Configuration backup created: {$backupPath}");
301307
}
302308
}
303-
304-
/**
309+
/**
305310
* Restore configuration from backup
306311
*/
307312
protected function restoreConfigBackup(): void
308313
{
309-
$caddyConfigPath = config('caddy.config_path', '/etc/caddy/Caddyfile');
314+
$caddyConfigPath = '/etc/caddy/Caddyfile';
310315
$backupDir = dirname($caddyConfigPath);
311316

312317
// Find the most recent backup
@@ -372,15 +377,14 @@ protected function attemptRecovery(): void
372377
\Log::error('Recovery attempt failed: ' . $e->getMessage());
373378
}
374379
}
375-
376-
/**
380+
/**
377381
* Clean up old backup files
378382
*/
379383
protected function cleanupOldBackups(): void
380384
{
381-
$caddyConfigPath = config('caddy.config_path', '/etc/caddy/Caddyfile');
385+
$caddyConfigPath = '/etc/caddy/Caddyfile';
382386
$backupDir = dirname($caddyConfigPath);
383-
$maxBackups = config('caddy.max_backups', 10);
387+
$maxBackups = 10;
384388

385389
$backups = glob($backupDir . '/Caddyfile.backup.*');
386390
if (count($backups) > $maxBackups) {

0 commit comments

Comments
 (0)