From cc72f416e8eaafb994b01d1afc78588828855af3 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 1 Dec 2023 11:13:58 +0100 Subject: [PATCH] feat: custom log drain endpoints --- app/Actions/Server/InstallLogDrain.php | 24 +++++++++++--- app/Http/Livewire/Server/LogDrains.php | 24 ++++++++++++++ app/Models/Server.php | 2 +- ...dd_custom_fluentd_config_for_logdrains.php | 32 +++++++++++++++++++ .../livewire/server/log-drains.blade.php | 19 +++++++++++ 5 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 database/migrations/2023_12_01_095356_add_custom_fluentd_config_for_logdrains.php diff --git a/app/Actions/Server/InstallLogDrain.php b/app/Actions/Server/InstallLogDrain.php index 11487d633..e43642e7a 100644 --- a/app/Actions/Server/InstallLogDrain.php +++ b/app/Actions/Server/InstallLogDrain.php @@ -16,6 +16,8 @@ public function handle(Server $server) $type = 'highlight'; } else if ($server->settings->is_logdrain_axiom_enabled) { $type = 'axiom'; + } else if ($server->settings->is_logdrain_custom_enabled) { + $type = 'custom'; } else { $type = 'none'; } @@ -114,15 +116,23 @@ public function handle(Server $server) json_date_format iso8601 tls On "); + } else if ($type === 'custom') { + if (!$server->settings->is_logdrain_custom_enabled) { + throw new \Exception('Custom log drain is not enabled.'); + } + $config = base64_encode($server->settings->logdrain_custom_config); + $parsers = base64_encode($server->settings->logdrain_custom_config_parser); } else { throw new \Exception('Unknown log drain type.'); } - $parsers = base64_encode(" + if ($type !== 'custom') { + $parsers = base64_encode(" [PARSER] - Name empty_line_skipper - Format regex - Regex /^(?!\s*$).+/ +Name empty_line_skipper +Format regex +Regex /^(?!\s*$).+/ "); + } $compose = base64_encode(" services: coolify-log-drain: @@ -179,6 +189,12 @@ public function handle(Server $server) "echo AXIOM_DATASET_NAME={$server->settings->logdrain_axiom_dataset_name} >> $config_path/.env", "echo AXIOM_API_KEY={$server->settings->logdrain_axiom_api_key} >> $config_path/.env", ]; + } else if ($type === 'custom') { + $add_envs_command = [ + "touch $config_path/.env" + ]; + } else { + throw new \Exception('Unknown log drain type.'); } $restart_command = [ "echo 'Stopping old Fluent Bit'", diff --git a/app/Http/Livewire/Server/LogDrains.php b/app/Http/Livewire/Server/LogDrains.php index 056beb584..5afe540ca 100644 --- a/app/Http/Livewire/Server/LogDrains.php +++ b/app/Http/Livewire/Server/LogDrains.php @@ -19,6 +19,9 @@ class LogDrains extends Component 'server.settings.is_logdrain_axiom_enabled' => 'required|boolean', 'server.settings.logdrain_axiom_dataset_name' => 'required|string', 'server.settings.logdrain_axiom_api_key' => 'required|string', + 'server.settings.is_logdrain_custom_enabled' => 'required|boolean', + 'server.settings.logdrain_custom_config' => 'required|string', + 'server.settings.logdrain_custom_config_parser' => 'nullable', ]; protected $validationAttributes = [ 'server.settings.is_logdrain_newrelic_enabled' => 'New Relic log drain', @@ -29,6 +32,9 @@ class LogDrains extends Component 'server.settings.is_logdrain_axiom_enabled' => 'Axiom log drain', 'server.settings.logdrain_axiom_dataset_name' => 'Axiom dataset name', 'server.settings.logdrain_axiom_api_key' => 'Axiom API key', + 'server.settings.is_logdrain_custom_enabled' => 'Custom log drain', + 'server.settings.logdrain_custom_config' => 'Custom log drain configuration', + 'server.settings.logdrain_custom_config_parser' => 'Custom log drain configuration parser', ]; public function mount() @@ -84,6 +90,7 @@ public function submit(string $type) $this->server->settings->update([ 'is_logdrain_highlight_enabled' => false, 'is_logdrain_axiom_enabled' => false, + 'is_logdrain_custom_enabled' => false, ]); } else if ($type === 'highlight') { $this->validate([ @@ -93,6 +100,7 @@ public function submit(string $type) $this->server->settings->update([ 'is_logdrain_newrelic_enabled' => false, 'is_logdrain_axiom_enabled' => false, + 'is_logdrain_custom_enabled' => false, ]); } else if ($type === 'axiom') { $this->validate([ @@ -103,6 +111,18 @@ public function submit(string $type) $this->server->settings->update([ 'is_logdrain_newrelic_enabled' => false, 'is_logdrain_highlight_enabled' => false, + 'is_logdrain_custom_enabled' => false, + ]); + } else if ($type === 'custom') { + $this->validate([ + 'server.settings.is_logdrain_custom_enabled' => 'required|boolean', + 'server.settings.logdrain_custom_config' => 'required|string', + 'server.settings.logdrain_custom_config_parser' => 'nullable', + ]); + $this->server->settings->update([ + 'is_logdrain_newrelic_enabled' => false, + 'is_logdrain_highlight_enabled' => false, + 'is_logdrain_axiom_enabled' => false, ]); } $this->server->settings->save(); @@ -121,6 +141,10 @@ public function submit(string $type) $this->server->settings->update([ 'is_logdrain_axiom_enabled' => false, ]); + } else if ($type === 'custom') { + $this->server->settings->update([ + 'is_logdrain_custom_enabled' => false, + ]); } handleError($e, $this); return false; diff --git a/app/Models/Server.php b/app/Models/Server.php index 408106107..f65ecfb10 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -349,7 +349,7 @@ public function isFunctional() } public function isLogDrainEnabled() { - return $this->settings->is_logdrain_newrelic_enabled || $this->settings->is_logdrain_highlight_enabled || $this->settings->is_logdrain_axiom_enabled; + return $this->settings->is_logdrain_newrelic_enabled || $this->settings->is_logdrain_highlight_enabled || $this->settings->is_logdrain_axiom_enabled || $this->settings->is_logdrain_custom_enabled; } public function validateOS(): bool | Stringable { diff --git a/database/migrations/2023_12_01_095356_add_custom_fluentd_config_for_logdrains.php b/database/migrations/2023_12_01_095356_add_custom_fluentd_config_for_logdrains.php new file mode 100644 index 000000000..ff100b484 --- /dev/null +++ b/database/migrations/2023_12_01_095356_add_custom_fluentd_config_for_logdrains.php @@ -0,0 +1,32 @@ +boolean('is_logdrain_custom_enabled')->default(false); + $table->text('logdrain_custom_config')->nullable(); + $table->text('logdrain_custom_config_parser')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('server_settings', function (Blueprint $table) { + $table->dropColumn('is_logdrain_custom_enabled'); + $table->dropColumn('logdrain_custom_config'); + $table->dropColumn('logdrain_custom_config_parser'); + }); + } +}; diff --git a/resources/views/livewire/server/log-drains.blade.php b/resources/views/livewire/server/log-drains.blade.php index 465702053..c452b171b 100644 --- a/resources/views/livewire/server/log-drains.blade.php +++ b/resources/views/livewire/server/log-drains.blade.php @@ -62,6 +62,25 @@ --}} +

Custom FluentBit configuration

+
+ +
+
+
+ + +
+
+ + Save + +
+
+