lasthourcloud/app/Livewire/Server/LogDrains.php

158 lines
6.7 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;
public $parameters = [];
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',
];
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)) {
2023-12-07 21:56:55 +00:00
return $this->redirectRoute('server.all', navigate: true);
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);
}
}
public function configureLogDrain()
{
2023-11-16 23:37:09 +00:00
try {
2023-11-17 20:16:25 +00:00
InstallLogDrain::run($this->server);
if (!$this->server->isLogDrainEnabled()) {
2023-12-07 18:06:32 +00:00
$this->dispatch('serverRefresh');
$this->dispatch('success', 'Log drain service stopped.');
return;
}
2023-12-07 18:06:32 +00:00
$this->dispatch('serverRefresh');
$this->dispatch('success', 'Log drain service started successfully.');
2023-11-16 23:37:09 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function instantSave(string $type)
{
try {
$ok = $this->submit($type);
if (!$ok) {
return;
}
$this->configureLogDrain();
} catch (\Throwable $e) {
return handleError($e, $this);
}
2023-11-16 23:37:09 +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
]);
} else if ($type === 'highlight') {
$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,
]);
} else if ($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,
]);
} 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,
2023-11-16 23:37:09 +00:00
]);
}
$this->server->settings->save();
2023-12-07 18:06:32 +00:00
$this->dispatch('success', 'Settings saved successfully.');
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,
]);
} else if ($type === 'highlight') {
$this->server->settings->update([
'is_logdrain_highlight_enabled' => false,
]);
} else if ($type === 'axiom') {
$this->server->settings->update([
'is_logdrain_axiom_enabled' => false,
]);
2023-12-01 10:13:58 +00:00
} else if ($type === 'custom') {
$this->server->settings->update([
'is_logdrain_custom_enabled' => false,
]);
}
handleError($e, $this);
return false;
2023-11-16 23:37:09 +00:00
}
}
public function render()
{
return view('livewire.server.log-drains');
}
}