lasthourcloud/app/Livewire/Server/LogDrains.php

168 lines
6.6 KiB
PHP
Raw Normal View History

2023-11-16 23:37:09 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Server;
2023-11-16 23:37:09 +00:00
2023-11-17 20:16:25 +00:00
use App\Actions\Server\InstallLogDrain;
2023-11-16 23:37:09 +00:00
use App\Models\Server;
use Livewire\Component;
class LogDrains extends Component
{
public Server $server;
2024-06-10 20:43:34 +00:00
2023-11-16 23:37:09 +00:00
public $parameters = [];
2024-06-10 20:43:34 +00:00
2023-11-16 23:37:09 +00:00
protected $rules = [
'server.settings.is_logdrain_newrelic_enabled' => 'required|boolean',
'server.settings.logdrain_newrelic_license_key' => 'required|string',
'server.settings.logdrain_newrelic_base_uri' => 'required|string',
'server.settings.is_logdrain_highlight_enabled' => 'required|boolean',
'server.settings.logdrain_highlight_project_id' => 'required|string',
'server.settings.is_logdrain_axiom_enabled' => 'required|boolean',
'server.settings.logdrain_axiom_dataset_name' => 'required|string',
'server.settings.logdrain_axiom_api_key' => 'required|string',
2023-12-01 10:13:58 +00:00
'server.settings.is_logdrain_custom_enabled' => 'required|boolean',
'server.settings.logdrain_custom_config' => 'required|string',
'server.settings.logdrain_custom_config_parser' => 'nullable',
];
2024-06-10 20:43:34 +00:00
protected $validationAttributes = [
'server.settings.is_logdrain_newrelic_enabled' => 'New Relic log drain',
'server.settings.logdrain_newrelic_license_key' => 'New Relic license key',
'server.settings.logdrain_newrelic_base_uri' => 'New Relic base URI',
'server.settings.is_logdrain_highlight_enabled' => 'Highlight log drain',
'server.settings.logdrain_highlight_project_id' => 'Highlight project ID',
'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',
2023-12-01 10:13:58 +00:00
'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',
2023-11-16 23:37:09 +00:00
];
public function mount()
{
2023-11-16 23:37:09 +00:00
$this->parameters = get_route_parameters();
try {
2023-12-07 21:56:55 +00:00
$server = Server::ownedByCurrentTeam()->whereUuid(request()->server_uuid)->first();
if (is_null($server)) {
2024-01-07 15:23:41 +00:00
return redirect()->route('server.index');
2023-11-16 23:37:09 +00:00
}
$this->server = $server;
2023-11-16 23:37:09 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
public function configureLogDrain()
{
2023-11-16 23:37:09 +00:00
try {
2023-11-17 20:16:25 +00:00
InstallLogDrain::run($this->server);
2024-06-10 20:43:34 +00:00
if (! $this->server->isLogDrainEnabled()) {
2023-12-07 18:06:32 +00:00
$this->dispatch('serverRefresh');
$this->dispatch('success', 'Log drain service stopped.');
2024-06-10 20:43:34 +00:00
return;
}
2023-12-07 18:06:32 +00:00
$this->dispatch('serverRefresh');
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Log drain service started.');
2023-11-16 23:37:09 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
public function instantSave(string $type)
{
try {
$ok = $this->submit($type);
2024-06-10 20:43:34 +00:00
if (! $ok) {
return;
}
$this->configureLogDrain();
} catch (\Throwable $e) {
return handleError($e, $this);
}
2023-11-16 23:37:09 +00:00
}
2024-06-10 20:43:34 +00:00
public function submit(string $type)
{
2023-11-16 23:37:09 +00:00
try {
$this->resetErrorBag();
if ($type === 'newrelic') {
$this->validate([
'server.settings.is_logdrain_newrelic_enabled' => 'required|boolean',
'server.settings.logdrain_newrelic_license_key' => 'required|string',
'server.settings.logdrain_newrelic_base_uri' => 'required|string',
]);
$this->server->settings->update([
'is_logdrain_highlight_enabled' => false,
'is_logdrain_axiom_enabled' => false,
2023-12-01 10:13:58 +00:00
'is_logdrain_custom_enabled' => false,
2023-11-16 23:37:09 +00:00
]);
2024-06-10 20:43:34 +00:00
} elseif ($type === 'highlight') {
2023-11-16 23:37:09 +00:00
$this->validate([
'server.settings.is_logdrain_highlight_enabled' => 'required|boolean',
'server.settings.logdrain_highlight_project_id' => 'required|string',
]);
$this->server->settings->update([
'is_logdrain_newrelic_enabled' => false,
'is_logdrain_axiom_enabled' => false,
2023-12-01 10:13:58 +00:00
'is_logdrain_custom_enabled' => false,
]);
2024-06-10 20:43:34 +00:00
} elseif ($type === 'axiom') {
$this->validate([
'server.settings.is_logdrain_axiom_enabled' => 'required|boolean',
'server.settings.logdrain_axiom_dataset_name' => 'required|string',
'server.settings.logdrain_axiom_api_key' => 'required|string',
]);
$this->server->settings->update([
'is_logdrain_newrelic_enabled' => false,
'is_logdrain_highlight_enabled' => false,
2023-12-01 10:13:58 +00:00
'is_logdrain_custom_enabled' => false,
]);
2024-06-10 20:43:34 +00:00
} elseif ($type === 'custom') {
2023-12-01 10:13:58 +00:00
$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,
2023-11-16 23:37:09 +00:00
]);
}
$this->server->settings->save();
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Settings saved.');
2024-06-10 20:43:34 +00:00
return true;
2023-11-16 23:37:09 +00:00
} catch (\Throwable $e) {
if ($type === 'newrelic') {
$this->server->settings->update([
'is_logdrain_newrelic_enabled' => false,
]);
2024-06-10 20:43:34 +00:00
} elseif ($type === 'highlight') {
$this->server->settings->update([
'is_logdrain_highlight_enabled' => false,
]);
2024-06-10 20:43:34 +00:00
} elseif ($type === 'axiom') {
$this->server->settings->update([
'is_logdrain_axiom_enabled' => false,
]);
2024-06-10 20:43:34 +00:00
} elseif ($type === 'custom') {
2023-12-01 10:13:58 +00:00
$this->server->settings->update([
'is_logdrain_custom_enabled' => false,
]);
}
handleError($e, $this);
2024-06-10 20:43:34 +00:00
return false;
2023-11-16 23:37:09 +00:00
}
}
2024-06-10 20:43:34 +00:00
2023-11-16 23:37:09 +00:00
public function render()
{
return view('livewire.server.log-drains');
}
}