coolify/app/Http/Livewire/Settings/Configuration.php

150 lines
5.1 KiB
PHP
Raw Normal View History

2023-04-25 10:06:45 +02:00
<?php
2023-04-25 10:47:13 +02:00
namespace App\Http\Livewire\Settings;
2023-04-25 10:06:45 +02:00
2023-09-11 22:29:34 +02:00
use App\Jobs\ProxyContainerStatusJob;
2023-04-25 10:06:45 +02:00
use App\Models\InstanceSettings as ModelsInstanceSettings;
2023-05-25 12:00:09 +02:00
use App\Models\Server;
2023-04-25 10:06:45 +02:00
use Livewire\Component;
2023-05-25 12:00:09 +02:00
use Spatie\Url\Url;
use Symfony\Component\Yaml\Yaml;
2023-04-25 10:06:45 +02:00
2023-06-07 22:07:26 +02:00
class Configuration extends Component
2023-04-25 10:06:45 +02:00
{
public ModelsInstanceSettings $settings;
2023-06-23 13:13:02 +02:00
public bool $do_not_track;
public bool $is_auto_update_enabled;
public bool $is_registration_enabled;
public bool $next_channel;
2023-06-22 14:18:17 +02:00
protected string $dynamic_config_path = '/data/coolify/proxy/dynamic';
2023-05-31 09:22:08 +02:00
protected Server $server;
2023-04-25 10:06:45 +02:00
protected $rules = [
'settings.fqdn' => 'nullable',
2023-07-14 11:27:08 +02:00
'settings.resale_license' => 'nullable',
2023-04-25 10:06:45 +02:00
'settings.public_port_min' => 'required',
'settings.public_port_max' => 'required',
];
2023-06-16 12:35:40 +02:00
protected $validationAttributes = [
'settings.fqdn' => 'FQDN',
2023-07-14 11:27:08 +02:00
'settings.resale_license' => 'Resale License',
2023-06-16 12:35:40 +02:00
'settings.public_port_min' => 'Public port min',
'settings.public_port_max' => 'Public port max',
];
2023-04-25 10:06:45 +02:00
public function mount()
{
$this->do_not_track = $this->settings->do_not_track;
$this->is_auto_update_enabled = $this->settings->is_auto_update_enabled;
$this->is_registration_enabled = $this->settings->is_registration_enabled;
2023-06-23 13:13:02 +02:00
$this->next_channel = $this->settings->next_channel;
2023-04-25 10:06:45 +02:00
}
2023-04-25 10:06:45 +02:00
public function instantSave()
{
$this->settings->do_not_track = $this->do_not_track;
$this->settings->is_auto_update_enabled = $this->is_auto_update_enabled;
$this->settings->is_registration_enabled = $this->is_registration_enabled;
2023-06-23 13:13:02 +02:00
$this->settings->next_channel = $this->next_channel;
2023-04-25 10:06:45 +02:00
$this->settings->save();
2023-06-15 10:48:13 +02:00
$this->emit('success', 'Settings updated!');
2023-04-25 10:06:45 +02:00
}
public function submit()
{
$this->resetErrorBag();
if ($this->settings->public_port_min > $this->settings->public_port_max) {
$this->addError('settings.public_port_min', 'The minimum port must be lower than the maximum port.');
return;
}
$this->validate();
$this->settings->save();
$this->server = Server::findOrFail(0);
$this->setup_instance_fqdn();
$this->emit('success', 'Instance settings updated successfully!');
}
2023-05-31 09:22:08 +02:00
private function setup_instance_fqdn()
2023-04-25 10:06:45 +02:00
{
2023-05-31 09:22:08 +02:00
$file = "$this->dynamic_config_path/coolify.yaml";
2023-05-25 12:04:43 +02:00
if (empty($this->settings->fqdn)) {
remote_process([
2023-05-31 09:22:08 +02:00
"rm -f $file",
], $this->server);
2023-05-25 12:04:43 +02:00
} else {
2023-05-25 12:00:09 +02:00
$url = Url::fromString($this->settings->fqdn);
$host = $url->getHost();
$schema = $url->getScheme();
$traefik_dynamic_conf = [
'http' =>
2023-08-11 20:48:52 +02:00
[
'routers' =>
2023-05-25 12:00:09 +02:00
[
2023-08-11 20:48:52 +02:00
'coolify-http' =>
[
'entryPoints' => [
0 => 'http',
2023-05-26 11:01:50 +02:00
],
2023-08-11 20:48:52 +02:00
'service' => 'coolify',
'rule' => "Host(`{$host}`)",
],
],
'services' =>
[
'coolify' =>
[
'loadBalancer' =>
2023-05-25 12:00:09 +02:00
[
2023-08-11 20:48:52 +02:00
'servers' =>
[
0 =>
2023-05-25 12:00:09 +02:00
[
2023-08-11 20:48:52 +02:00
'url' => 'http://coolify:80',
2023-05-25 12:00:09 +02:00
],
2023-08-11 20:48:52 +02:00
],
2023-05-25 12:00:09 +02:00
],
2023-08-11 20:48:52 +02:00
],
2023-05-25 12:00:09 +02:00
],
2023-08-11 20:48:52 +02:00
],
2023-05-25 12:00:09 +02:00
];
2023-05-26 11:01:50 +02:00
2023-05-26 10:54:22 +02:00
if ($schema === 'https') {
2023-05-26 11:01:50 +02:00
$traefik_dynamic_conf['http']['routers']['coolify-http']['middlewares'] = [
2023-05-26 10:54:22 +02:00
0 => 'redirect-to-https@docker',
];
2023-05-26 11:01:50 +02:00
$traefik_dynamic_conf['http']['routers']['coolify-https'] = [
'entryPoints' => [
0 => 'https',
],
'service' => 'coolify',
'rule' => "Host(`{$host}`)",
'tls' => [
'certresolver' => 'letsencrypt',
],
2023-05-26 10:54:22 +02:00
];
}
2023-05-31 09:22:08 +02:00
$this->save_configuration_to_disk($traefik_dynamic_conf, $file);
2023-09-11 22:29:34 +02:00
dispatch(new ProxyContainerStatusJob($this->server));
2023-05-31 09:22:08 +02:00
}
}
2023-05-31 09:22:08 +02:00
private function save_configuration_to_disk(array $traefik_dynamic_conf, string $file)
{
$yaml = Yaml::dump($traefik_dynamic_conf, 12, 2);
$yaml =
"# This file is automatically generated by Coolify.\n" .
"# Do not edit it manually (only if you know what are you doing).\n\n" .
$yaml;
$base64 = base64_encode($yaml);
remote_process([
"mkdir -p $this->dynamic_config_path",
"echo '$base64' | base64 -d > $file",
], $this->server);
if (config('app.env') == 'local') {
ray($yaml);
}
}
2023-04-25 10:06:45 +02:00
}