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

@@ -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.');
}
}