coolify/app/Models/ServerSetting.php

153 lines
5.3 KiB
PHP
Raw Normal View History

2023-03-30 13:52:19 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
2023-06-15 07:15:41 +00:00
use Illuminate\Database\Eloquent\Model;
2024-11-06 11:39:37 +00:00
use Illuminate\Support\Facades\Log;
2024-07-09 08:45:10 +00:00
use OpenApi\Attributes as OA;
2023-06-15 07:15:41 +00:00
2024-07-09 08:45:10 +00:00
#[OA\Schema(
description: 'Server Settings model',
type: 'object',
properties: [
'id' => ['type' => 'integer'],
'concurrent_builds' => ['type' => 'integer'],
'dynamic_timeout' => ['type' => 'integer'],
'force_disabled' => ['type' => 'boolean'],
2024-08-14 19:54:28 +00:00
'force_server_cleanup' => ['type' => 'boolean'],
2024-07-09 08:45:10 +00:00
'is_build_server' => ['type' => 'boolean'],
'is_cloudflare_tunnel' => ['type' => 'boolean'],
'is_jump_server' => ['type' => 'boolean'],
'is_logdrain_axiom_enabled' => ['type' => 'boolean'],
'is_logdrain_custom_enabled' => ['type' => 'boolean'],
'is_logdrain_highlight_enabled' => ['type' => 'boolean'],
'is_logdrain_newrelic_enabled' => ['type' => 'boolean'],
'is_metrics_enabled' => ['type' => 'boolean'],
'is_reachable' => ['type' => 'boolean'],
2024-10-15 11:39:19 +00:00
'is_sentinel_enabled' => ['type' => 'boolean'],
2024-07-09 08:45:10 +00:00
'is_swarm_manager' => ['type' => 'boolean'],
'is_swarm_worker' => ['type' => 'boolean'],
'is_usable' => ['type' => 'boolean'],
'logdrain_axiom_api_key' => ['type' => 'string'],
'logdrain_axiom_dataset_name' => ['type' => 'string'],
'logdrain_custom_config' => ['type' => 'string'],
'logdrain_custom_config_parser' => ['type' => 'string'],
'logdrain_highlight_project_id' => ['type' => 'string'],
'logdrain_newrelic_base_uri' => ['type' => 'string'],
'logdrain_newrelic_license_key' => ['type' => 'string'],
2024-10-14 10:07:37 +00:00
'sentinel_metrics_history_days' => ['type' => 'integer'],
'sentinel_metrics_refresh_rate_seconds' => ['type' => 'integer'],
'sentinel_token' => ['type' => 'string'],
'docker_cleanup_frequency' => ['type' => 'string'],
'docker_cleanup_threshold' => ['type' => 'integer'],
2024-07-09 08:45:10 +00:00
'server_id' => ['type' => 'integer'],
'wildcard_domain' => ['type' => 'string'],
'created_at' => ['type' => 'string'],
'updated_at' => ['type' => 'string'],
]
)]
2023-06-15 07:15:41 +00:00
class ServerSetting extends Model
2023-03-30 13:52:19 +00:00
{
2023-10-25 09:50:22 +00:00
protected $guarded = [];
2024-08-14 19:54:28 +00:00
protected $casts = [
'force_docker_cleanup' => 'boolean',
'docker_cleanup_threshold' => 'integer',
2024-10-14 10:07:37 +00:00
'sentinel_token' => 'encrypted',
'is_reachable' => 'boolean',
'is_usable' => 'boolean',
2024-08-14 19:54:28 +00:00
];
protected static function booted()
{
static::creating(function ($setting) {
2024-10-17 12:56:36 +00:00
try {
if (str($setting->sentinel_token)->isEmpty()) {
2024-11-08 09:39:02 +00:00
$setting->generateSentinelToken(save: false, ignoreEvent: true);
2024-10-17 12:56:36 +00:00
}
if (str($setting->sentinel_custom_url)->isEmpty()) {
2024-11-08 09:39:02 +00:00
$setting->generateSentinelUrl(save: false, ignoreEvent: true);
2024-10-17 12:56:36 +00:00
}
} catch (\Throwable $e) {
2024-11-06 11:39:37 +00:00
Log::error('Error creating server setting: '.$e->getMessage());
2024-10-17 12:56:36 +00:00
}
});
static::updated(function ($settings) {
if (
$settings->isDirty('sentinel_token') ||
$settings->isDirty('sentinel_custom_url') ||
$settings->isDirty('sentinel_metrics_refresh_rate_seconds') ||
$settings->isDirty('sentinel_metrics_history_days') ||
$settings->isDirty('sentinel_push_interval_seconds')
) {
$settings->server->restartSentinel();
}
if ($settings->isDirty('is_reachable')) {
$settings->server->isReachableChanged();
}
});
}
2024-11-08 09:39:02 +00:00
public function generateSentinelToken(bool $save = true, bool $ignoreEvent = false)
2024-10-17 12:56:36 +00:00
{
$data = [
'server_uuid' => $this->server->uuid,
];
$token = json_encode($data);
$encrypted = encrypt($token);
$this->sentinel_token = $encrypted;
if ($save) {
2024-11-08 09:39:02 +00:00
if ($ignoreEvent) {
2024-11-08 09:28:40 +00:00
$this->saveQuietly();
} else {
$this->save();
}
2024-10-17 12:56:36 +00:00
}
2024-10-22 06:31:53 +00:00
return $token;
2024-10-17 12:56:36 +00:00
}
2024-11-08 09:39:02 +00:00
public function generateSentinelUrl(bool $save = true, bool $ignoreEvent = false)
2024-10-17 12:56:36 +00:00
{
$domain = null;
$settings = InstanceSettings::get();
if ($this->server->isLocalhost()) {
$domain = 'http://host.docker.internal:8000';
2024-10-17 20:08:23 +00:00
} elseif ($settings->fqdn) {
2024-10-17 12:56:36 +00:00
$domain = $settings->fqdn;
2024-10-25 09:41:52 +00:00
} elseif ($settings->public_ipv4) {
$domain = 'http://'.$settings->public_ipv4.':8000';
} elseif ($settings->public_ipv6) {
$domain = 'http://'.$settings->public_ipv6.':8000';
2024-10-17 12:56:36 +00:00
}
$this->sentinel_custom_url = $domain;
if ($save) {
2024-11-08 09:39:02 +00:00
if ($ignoreEvent) {
2024-11-08 09:28:40 +00:00
$this->saveQuietly();
} else {
$this->save();
}
2024-10-17 12:56:36 +00:00
}
2024-10-17 20:08:23 +00:00
2024-10-17 12:56:36 +00:00
return $domain;
}
2023-03-30 13:52:19 +00:00
public function server()
{
return $this->belongsTo(Server::class);
}
public function dockerCleanupFrequency(): Attribute
{
return Attribute::make(
set: function ($value) {
return translate_cron_expression($value);
},
get: function ($value) {
return translate_cron_expression($value);
}
);
}
}