refactor: Remove unused server timezone seeder and related code

This commit is contained in:
Andras Bacsai
2024-08-26 15:26:08 +02:00
parent b8ff0540e2
commit 68169f75d1
12 changed files with 337 additions and 409 deletions

View File

@@ -35,6 +35,7 @@ class Kernel extends ConsoleKernel
// Instance Jobs
$schedule->command('horizon:snapshot')->everyMinute();
$schedule->job(new CleanupInstanceStuffsJob)->everyMinute()->onOneServer();
$schedule->job(new PullCoolifyImageJob)->cron($settings->update_check_frequency)->timezone($settings->instance_timezone)->onOneServer();
// Server Jobs
$this->check_scheduled_backups($schedule);
$this->check_resources($schedule);
@@ -134,6 +135,7 @@ class Kernel extends ConsoleKernel
if (is_null(data_get($scheduled_backup, 'database'))) {
ray('database not found');
$scheduled_backup->delete();
continue;
}
@@ -165,6 +167,7 @@ class Kernel extends ConsoleKernel
if (! $application && ! $service) {
ray('application/service attached to scheduled task does not exist');
$scheduled_task->delete();
continue;
}
if ($application) {
@@ -192,8 +195,8 @@ class Kernel extends ConsoleKernel
protected function commands(): void
{
$this->load(__DIR__ . '/Commands');
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
}

View File

@@ -5,8 +5,7 @@ namespace App\Livewire\Server;
use App\Actions\Server\StartSentinel;
use App\Actions\Server\StopSentinel;
use App\Jobs\PullSentinelImageJob;
use App\Models\Server;;
use App\Models\Server;
use Livewire\Component;
class Form extends Component
@@ -23,6 +22,8 @@ class Form extends Component
public bool $revalidate = false;
public $timezones;
protected $listeners = ['serverInstalled', 'revalidate' => '$refresh'];
protected $rules = [
@@ -71,8 +72,6 @@ class Form extends Component
'server.settings.server_timezone' => 'Server Timezone',
];
public $timezones;
public function mount(Server $server)
{
$this->server = $server;
@@ -174,7 +173,7 @@ class Form extends Component
$this->server->settings->save();
$this->dispatch('proxyStatusUpdated');
} else {
$this->dispatch('error', 'Server is not reachable.', 'Please validate your configuration and connection.<br><br>Check this <a target="_blank" class="underline" href="https://coolify.io/docs/knowledge-base/server/openssh">documentation</a> for further help. <br><br>Error: ' . $error);
$this->dispatch('error', 'Server is not reachable.', 'Please validate your configuration and connection.<br><br>Check this <a target="_blank" class="underline" href="https://coolify.io/docs/knowledge-base/server/openssh">documentation</a> for further help. <br><br>Error: '.$error);
return;
}
@@ -214,22 +213,12 @@ class Form extends Component
} else {
$this->server->settings->docker_cleanup_threshold = $this->server->settings->docker_cleanup_threshold;
}
$currentTimezone = $this->server->settings->getOriginal('server_timezone');
$newTimezone = $this->server->settings->server_timezone;
if ($currentTimezone !== $newTimezone || $currentTimezone === '') {
try {
$timezoneUpdated = $this->updateServerTimezone($newTimezone);
if ($timezoneUpdated) {
$this->server->settings->server_timezone = $newTimezone;
$this->server->settings->save();
} else {
return;
}
} catch (\Exception $e) {
$this->dispatch('error', 'Failed to update server timezone: ' . $e->getMessage());
return;
$currentTimezone = $this->server->settings->getOriginal('server_timezone');
$newTimezone = $this->server->settings->server_timezone;
if ($currentTimezone !== $newTimezone || $currentTimezone === '') {
$this->server->settings->server_timezone = $newTimezone;
$this->server->settings->save();
}
}
$this->server->settings->save();
$this->server->save();
@@ -239,89 +228,10 @@ class Form extends Component
}
}
public function updatedServerTimezone($value)
public function updatedServerSettingsServerTimezone($value)
{
if (!is_string($value) || !in_array($value, timezone_identifiers_list())) {
$this->addError('server.settings.server_timezone', 'Invalid timezone.');
return;
}
$this->server->settings->server_timezone = $value;
$this->updateServerTimezone($value);
}
private function updateServerTimezone($desired_timezone)
{
try {
$commands = [
"if command -v timedatectl > /dev/null 2>&1 && pidof systemd > /dev/null; then",
" timedatectl set-timezone " . escapeshellarg($desired_timezone),
"elif [ -f /etc/timezone ]; then",
" echo " . escapeshellarg($desired_timezone) . " > /etc/timezone",
" rm -f /etc/localtime",
" ln -sf /usr/share/zoneinfo/" . escapeshellarg($desired_timezone) . " /etc/localtime",
"elif [ -f /etc/localtime ]; then",
" rm -f /etc/localtime",
" ln -sf /usr/share/zoneinfo/" . escapeshellarg($desired_timezone) . " /etc/localtime",
"else",
" echo 'Unable to set timezone'",
" exit 1",
"fi",
"if command -v dpkg-reconfigure > /dev/null 2>&1; then",
" dpkg-reconfigure -f noninteractive tzdata",
"elif command -v tzdata-update > /dev/null 2>&1; then",
" tzdata-update",
"elif [ -f /etc/sysconfig/clock ]; then",
" sed -i 's/^ZONE=.*/ZONE=\"" . $desired_timezone . "\"/' /etc/sysconfig/clock",
" source /etc/sysconfig/clock",
"fi",
"if command -v systemctl > /dev/null 2>&1 && pidof systemd > /dev/null; then",
" systemctl try-restart systemd-timesyncd.service || true",
"elif command -v service > /dev/null 2>&1; then",
" service ntpd restart || service ntp restart || true",
"fi",
"echo \"Timezone updated to: $desired_timezone\"",
"date"
];
instant_remote_process($commands, $this->server);
$verificationCommands = [
"readlink /etc/localtime | sed 's#/usr/share/zoneinfo/##'",
"date +'%Z %:z'"
];
$verificationResult = instant_remote_process($verificationCommands, $this->server, false);
$verificationLines = explode("\n", trim($verificationResult));
if (count($verificationLines) !== 2) {
$this->dispatch('error', 'Failed to verify timezone update. Unexpected server response.');
return false;
}
$actualTimezone = trim($verificationLines[0]);
[$abbreviation, $offset] = explode(' ', trim($verificationLines[1]));
$desiredTz = new \DateTimeZone($desired_timezone);
$desiredAbbr = (new \DateTime('now', $desiredTz))->format('T');
$desiredOffset = $this->formatOffset($desiredTz->getOffset(new \DateTime('now', $desiredTz)));
if ($actualTimezone === $desired_timezone && $abbreviation === $desiredAbbr && $offset === $desiredOffset) {
$this->server->settings->server_timezone = $desired_timezone;
$this->server->settings->save();
return true;
} else {
$this->dispatch('error', 'Failed to update server timezone. The server reported a different timezone than requested.');
return false;
}
} catch (\Exception $e) {
$this->dispatch('error', 'Failed to update server timezone: ' . $e->getMessage());
return false;
}
}
private function formatOffset($offsetSeconds)
{
$hours = abs($offsetSeconds) / 3600;
$minutes = (abs($offsetSeconds) % 3600) / 60;
return sprintf('%s%02d:%02d', $offsetSeconds >= 0 ? '+' : '-', $hours, $minutes);
$this->server->settings->save();
$this->dispatch('success', 'Server timezone updated.');
}
}

View File

@@ -170,8 +170,15 @@ class Index extends Component
}
}
public function updatedSettingsInstanceTimezone($value)
{
$this->settings->instance_timezone = $value;
$this->settings->save();
$this->dispatch('success', 'Instance timezone updated.');
}
public function render()
{
return view('livewire.settings.index');
}
}
}

View File

@@ -37,6 +37,30 @@ class InstanceSettings extends Model implements SendsEmail
);
}
public function updateCheckFrequency(): Attribute
{
return Attribute::make(
set: function ($value) {
return translate_cron_expression($value);
},
get: function ($value) {
return translate_cron_expression($value);
}
);
}
public function autoUpdateFrequency(): Attribute
{
return Attribute::make(
set: function ($value) {
return translate_cron_expression($value);
},
get: function ($value) {
return translate_cron_expression($value);
}
);
}
public static function get()
{
return InstanceSettings::findOrFail(0);

View File

@@ -2,6 +2,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use OpenApi\Attributes as OA;
@@ -58,4 +59,18 @@ class ServerSetting extends Model
{
return $this->belongsTo(Server::class);
}
}
public function dockerCleanupFrequency(): Attribute
{
return Attribute::make(
set: function ($value) {
ray($value);
return translate_cron_expression($value);
},
get: function ($value) {
return translate_cron_expression($value);
}
);
}
}