2023-04-19 12:42:15 +02:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 19:06:32 +01:00
|
|
|
namespace App\Livewire\Project\Application;
|
2023-04-19 12:42:15 +02:00
|
|
|
|
|
|
|
use App\Models\Application;
|
2023-09-20 15:42:41 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2023-05-16 15:41:23 +02:00
|
|
|
use Illuminate\Support\Str;
|
2023-11-13 09:04:19 +01:00
|
|
|
use Livewire\Component;
|
2023-11-24 15:48:23 +01:00
|
|
|
use Visus\Cuid2\Cuid2;
|
2023-04-19 12:42:15 +02:00
|
|
|
|
|
|
|
class General extends Component
|
|
|
|
{
|
|
|
|
public string $applicationId;
|
|
|
|
|
|
|
|
public Application $application;
|
2023-09-20 15:42:41 +02:00
|
|
|
public Collection $services;
|
2023-04-19 12:42:15 +02:00
|
|
|
public string $name;
|
2023-10-09 11:10:04 +02:00
|
|
|
public ?string $fqdn = null;
|
2023-04-19 12:42:15 +02:00
|
|
|
public string $git_repository;
|
|
|
|
public string $git_branch;
|
2023-10-09 11:10:04 +02:00
|
|
|
public ?string $git_commit_sha = null;
|
2023-04-19 12:42:15 +02:00
|
|
|
public string $build_pack;
|
2023-10-20 12:34:25 +02:00
|
|
|
public ?string $ports_exposes = null;
|
2023-04-19 12:42:15 +02:00
|
|
|
|
2023-10-18 10:32:08 +02:00
|
|
|
public $customLabels;
|
|
|
|
public bool $labelsChanged = false;
|
2023-10-18 11:20:40 +02:00
|
|
|
public bool $isConfigurationChanged = false;
|
2023-10-18 10:32:08 +02:00
|
|
|
|
2023-11-27 11:54:55 +01:00
|
|
|
public ?string $initialDockerComposeLocation = null;
|
2023-11-27 15:50:22 +01:00
|
|
|
public ?string $initialDockerComposePrLocation = null;
|
2023-11-27 11:54:55 +01:00
|
|
|
|
2023-11-24 15:48:23 +01:00
|
|
|
public $parsedServices = [];
|
|
|
|
public $parsedServiceDomains = [];
|
|
|
|
|
2023-11-20 11:35:31 +01:00
|
|
|
protected $listeners = [
|
|
|
|
'resetDefaultLabels'
|
|
|
|
];
|
2023-04-19 12:42:15 +02:00
|
|
|
protected $rules = [
|
2023-07-06 14:37:14 +02:00
|
|
|
'application.name' => 'required',
|
2023-08-07 18:46:40 +02:00
|
|
|
'application.description' => 'nullable',
|
2023-04-19 12:42:15 +02:00
|
|
|
'application.fqdn' => 'nullable',
|
|
|
|
'application.git_repository' => 'required',
|
|
|
|
'application.git_branch' => 'required',
|
|
|
|
'application.git_commit_sha' => 'nullable',
|
2023-05-05 09:02:50 +02:00
|
|
|
'application.install_command' => 'nullable',
|
|
|
|
'application.build_command' => 'nullable',
|
|
|
|
'application.start_command' => 'nullable',
|
2023-04-19 12:42:15 +02:00
|
|
|
'application.build_pack' => 'required',
|
2023-04-25 15:48:45 +02:00
|
|
|
'application.static_image' => 'required',
|
2023-04-19 12:42:15 +02:00
|
|
|
'application.base_directory' => 'required',
|
|
|
|
'application.publish_directory' => 'nullable',
|
2023-04-25 14:43:35 +02:00
|
|
|
'application.ports_exposes' => 'required',
|
|
|
|
'application.ports_mappings' => 'nullable',
|
2023-08-11 22:41:47 +02:00
|
|
|
'application.dockerfile' => 'nullable',
|
2023-10-10 11:16:38 +02:00
|
|
|
'application.docker_registry_image_name' => 'nullable',
|
|
|
|
'application.docker_registry_image_tag' => 'nullable',
|
2023-10-10 14:02:43 +02:00
|
|
|
'application.dockerfile_location' => 'nullable',
|
2023-11-24 15:48:23 +01:00
|
|
|
'application.docker_compose_location' => 'nullable',
|
2023-11-27 15:50:22 +01:00
|
|
|
'application.docker_compose_pr_location' => 'nullable',
|
2023-11-24 15:48:23 +01:00
|
|
|
'application.docker_compose' => 'nullable',
|
2023-11-28 10:11:53 +01:00
|
|
|
'application.docker_compose_pr' => 'nullable',
|
2023-11-24 15:48:23 +01:00
|
|
|
'application.docker_compose_raw' => 'nullable',
|
2023-11-28 10:11:53 +01:00
|
|
|
'application.docker_compose_pr_raw' => 'nullable',
|
2023-10-18 10:32:08 +02:00
|
|
|
'application.custom_labels' => 'nullable',
|
2023-11-07 13:49:15 +01:00
|
|
|
'application.dockerfile_target_build' => 'nullable',
|
2023-11-20 11:35:31 +01:00
|
|
|
'application.settings.is_static' => 'boolean|required',
|
2023-12-17 20:56:12 +01:00
|
|
|
'application.docker_compose_custom_start_command' => 'nullable',
|
|
|
|
'application.docker_compose_custom_build_command' => 'nullable',
|
2023-04-19 12:42:15 +02:00
|
|
|
];
|
2023-06-16 12:35:40 +02:00
|
|
|
protected $validationAttributes = [
|
|
|
|
'application.name' => 'name',
|
2023-08-07 18:46:40 +02:00
|
|
|
'application.description' => 'description',
|
2023-06-16 12:35:40 +02:00
|
|
|
'application.fqdn' => 'FQDN',
|
|
|
|
'application.git_repository' => 'Git repository',
|
|
|
|
'application.git_branch' => 'Git branch',
|
|
|
|
'application.git_commit_sha' => 'Git commit SHA',
|
|
|
|
'application.install_command' => 'Install command',
|
|
|
|
'application.build_command' => 'Build command',
|
|
|
|
'application.start_command' => 'Start command',
|
|
|
|
'application.build_pack' => 'Build pack',
|
|
|
|
'application.static_image' => 'Static image',
|
|
|
|
'application.base_directory' => 'Base directory',
|
|
|
|
'application.publish_directory' => 'Publish directory',
|
|
|
|
'application.ports_exposes' => 'Ports exposes',
|
|
|
|
'application.ports_mappings' => 'Ports mappings',
|
2023-08-11 22:41:47 +02:00
|
|
|
'application.dockerfile' => 'Dockerfile',
|
2023-10-10 11:16:38 +02:00
|
|
|
'application.docker_registry_image_name' => 'Docker registry image name',
|
|
|
|
'application.docker_registry_image_tag' => 'Docker registry image tag',
|
2023-10-10 14:02:43 +02:00
|
|
|
'application.dockerfile_location' => 'Dockerfile location',
|
2023-11-24 15:48:23 +01:00
|
|
|
'application.docker_compose_location' => 'Docker compose location',
|
2023-11-27 15:50:22 +01:00
|
|
|
'application.docker_compose_pr_location' => 'Docker compose location',
|
2023-11-24 15:48:23 +01:00
|
|
|
'application.docker_compose' => 'Docker compose',
|
2023-11-28 10:11:53 +01:00
|
|
|
'application.docker_compose_pr' => 'Docker compose',
|
2023-11-24 15:48:23 +01:00
|
|
|
'application.docker_compose_raw' => 'Docker compose raw',
|
2023-11-28 10:11:53 +01:00
|
|
|
'application.docker_compose_pr_raw' => 'Docker compose raw',
|
2023-10-18 10:32:08 +02:00
|
|
|
'application.custom_labels' => 'Custom labels',
|
2023-11-07 13:49:15 +01:00
|
|
|
'application.dockerfile_target_build' => 'Dockerfile target build',
|
2023-11-20 11:35:31 +01:00
|
|
|
'application.settings.is_static' => 'Is static',
|
2023-12-17 20:56:12 +01:00
|
|
|
'application.docker_compose_custom_start_command' => 'Docker compose custom start command',
|
|
|
|
'application.docker_compose_custom_build_command' => 'Docker compose custom build command',
|
2023-06-16 12:35:40 +02:00
|
|
|
];
|
2023-10-18 10:32:08 +02:00
|
|
|
public function mount()
|
|
|
|
{
|
2023-11-24 15:48:23 +01:00
|
|
|
try {
|
|
|
|
$this->parsedServices = $this->application->parseCompose();
|
|
|
|
} catch (\Throwable $e) {
|
2023-12-07 19:06:32 +01:00
|
|
|
$this->dispatch('error', $e->getMessage());
|
2023-11-24 15:48:23 +01:00
|
|
|
}
|
|
|
|
$this->parsedServiceDomains = $this->application->docker_compose_domains ? json_decode($this->application->docker_compose_domains, true) : [];
|
|
|
|
|
2023-10-20 12:34:25 +02:00
|
|
|
$this->ports_exposes = $this->application->ports_exposes;
|
2023-10-18 11:26:01 +02:00
|
|
|
if (str($this->application->status)->startsWith('running') && is_null($this->application->config_hash)) {
|
|
|
|
$this->application->isConfigurationChanged(true);
|
|
|
|
}
|
2023-10-18 11:20:40 +02:00
|
|
|
$this->isConfigurationChanged = $this->application->isConfigurationChanged();
|
2023-12-13 09:23:27 +01:00
|
|
|
$this->customLabels = $this->application->parseContainerLabels();
|
2023-11-27 11:54:55 +01:00
|
|
|
$this->initialDockerComposeLocation = $this->application->docker_compose_location;
|
2023-10-18 10:32:08 +02:00
|
|
|
$this->checkLabelUpdates();
|
|
|
|
}
|
2023-11-20 11:35:31 +01:00
|
|
|
public function instantSave()
|
|
|
|
{
|
|
|
|
$this->application->settings->save();
|
2023-12-07 19:06:32 +01:00
|
|
|
$this->dispatch('success', 'Settings saved.');
|
2023-12-06 21:32:23 +01:00
|
|
|
$this->application->refresh();
|
|
|
|
if ($this->ports_exposes !== $this->application->ports_exposes) {
|
|
|
|
$this->resetDefaultLabels(false);
|
|
|
|
}
|
2023-11-20 11:35:31 +01:00
|
|
|
}
|
2023-11-24 15:48:23 +01:00
|
|
|
public function loadComposeFile($isInit = false)
|
|
|
|
{
|
2023-11-27 11:54:55 +01:00
|
|
|
try {
|
|
|
|
if ($isInit && $this->application->docker_compose_raw) {
|
|
|
|
return;
|
|
|
|
}
|
2023-11-27 15:50:22 +01:00
|
|
|
['parsedServices' => $this->parsedServices, 'initialDockerComposeLocation' => $this->initialDockerComposeLocation, 'initialDockerComposePrLocation' => $this->initialDockerComposePrLocation] = $this->application->loadComposeFile($isInit);
|
2023-12-07 19:06:32 +01:00
|
|
|
$this->dispatch('success', 'Docker compose file loaded.');
|
2023-11-27 11:54:55 +01:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
$this->application->docker_compose_location = $this->initialDockerComposeLocation;
|
2023-11-27 15:50:22 +01:00
|
|
|
$this->application->docker_compose_pr_location = $this->initialDockerComposePrLocation;
|
2023-11-27 11:54:55 +01:00
|
|
|
$this->application->save();
|
|
|
|
return handleError($e, $this);
|
2023-11-24 15:48:23 +01:00
|
|
|
}
|
2023-11-27 11:54:55 +01:00
|
|
|
}
|
|
|
|
public function generateDomain(string $serviceName)
|
|
|
|
{
|
|
|
|
$domain = $this->parsedServiceDomains[$serviceName]['domain'] ?? null;
|
|
|
|
if (!$domain) {
|
|
|
|
$uuid = new Cuid2(7);
|
|
|
|
$domain = generateFqdn($this->application->destination->server, $uuid);
|
|
|
|
$this->parsedServiceDomains[$serviceName]['domain'] = $domain;
|
|
|
|
$this->application->docker_compose_domains = json_encode($this->parsedServiceDomains);
|
2023-11-24 15:48:23 +01:00
|
|
|
$this->application->save();
|
|
|
|
}
|
2023-11-27 11:54:55 +01:00
|
|
|
return $domain;
|
2023-11-24 15:48:23 +01:00
|
|
|
}
|
2023-10-18 10:32:08 +02:00
|
|
|
public function updatedApplicationBuildPack()
|
|
|
|
{
|
2023-10-13 21:08:59 +02:00
|
|
|
if ($this->application->build_pack !== 'nixpacks') {
|
2023-12-06 21:32:23 +01:00
|
|
|
$this->application->settings->is_static = false;
|
2023-10-13 21:08:59 +02:00
|
|
|
$this->application->settings->save();
|
|
|
|
}
|
2023-11-27 15:25:15 +01:00
|
|
|
if ($this->application->build_pack === 'dockercompose') {
|
|
|
|
$this->application->fqdn = null;
|
|
|
|
$this->application->settings->save();
|
|
|
|
}
|
2023-10-11 12:12:25 +02:00
|
|
|
$this->submit();
|
|
|
|
}
|
2023-10-18 10:32:08 +02:00
|
|
|
public function checkLabelUpdates()
|
|
|
|
{
|
|
|
|
if (md5($this->application->custom_labels) !== md5(implode(",", generateLabelsApplication($this->application)))) {
|
|
|
|
$this->labelsChanged = true;
|
|
|
|
} else {
|
|
|
|
$this->labelsChanged = false;
|
|
|
|
}
|
|
|
|
}
|
2023-08-08 11:51:36 +02:00
|
|
|
|
2023-10-18 10:32:08 +02:00
|
|
|
public function getWildcardDomain()
|
|
|
|
{
|
2023-09-30 15:39:40 +02:00
|
|
|
$server = data_get($this->application, 'destination.server');
|
|
|
|
if ($server) {
|
|
|
|
$fqdn = generateFqdn($server, $this->application->uuid);
|
|
|
|
$this->application->fqdn = $fqdn;
|
|
|
|
$this->application->save();
|
2023-11-06 09:27:00 +01:00
|
|
|
$this->updatedApplicationFqdn();
|
2023-09-30 15:39:40 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-18 10:32:08 +02:00
|
|
|
public function resetDefaultLabels($showToaster = true)
|
2023-04-19 12:42:15 +02:00
|
|
|
{
|
2023-10-18 10:32:08 +02:00
|
|
|
$this->customLabels = str(implode(",", generateLabelsApplication($this->application)))->replace(',', "\n");
|
2023-10-20 12:34:25 +02:00
|
|
|
$this->ports_exposes = $this->application->ports_exposes;
|
2023-10-18 10:32:08 +02:00
|
|
|
$this->submit($showToaster);
|
2023-04-19 12:42:15 +02:00
|
|
|
}
|
2023-08-08 11:51:36 +02:00
|
|
|
|
2023-10-18 10:32:08 +02:00
|
|
|
public function updatedApplicationFqdn()
|
|
|
|
{
|
|
|
|
$this->resetDefaultLabels(false);
|
2023-12-07 19:06:32 +01:00
|
|
|
$this->dispatch('success', 'Labels reseted to default!');
|
2023-10-18 10:32:08 +02:00
|
|
|
}
|
|
|
|
public function submit($showToaster = true)
|
2023-04-19 12:42:15 +02:00
|
|
|
{
|
2023-05-16 15:27:47 +02:00
|
|
|
try {
|
2023-12-17 20:56:12 +01:00
|
|
|
ray($this->initialDockerComposeLocation, $this->application->docker_compose_location);
|
|
|
|
if ($this->application->build_pack === 'dockercompose' && $this->initialDockerComposeLocation !== $this->application->docker_compose_location) {
|
2023-11-27 11:54:55 +01:00
|
|
|
$this->loadComposeFile();
|
|
|
|
}
|
2023-09-20 15:42:41 +02:00
|
|
|
$this->validate();
|
2023-10-20 12:34:25 +02:00
|
|
|
if ($this->ports_exposes !== $this->application->ports_exposes) {
|
|
|
|
$this->resetDefaultLabels(false);
|
|
|
|
}
|
2023-10-18 10:32:08 +02:00
|
|
|
if (data_get($this->application, 'build_pack') === 'dockerimage') {
|
2023-10-11 12:10:40 +02:00
|
|
|
$this->validate([
|
|
|
|
'application.docker_registry_image_name' => 'required',
|
|
|
|
'application.docker_registry_image_tag' => 'required',
|
|
|
|
]);
|
|
|
|
}
|
2023-09-19 15:51:13 +02:00
|
|
|
if (data_get($this->application, 'fqdn')) {
|
2023-08-27 14:55:57 +02:00
|
|
|
$domains = Str::of($this->application->fqdn)->trim()->explode(',')->map(function ($domain) {
|
|
|
|
return Str::of($domain)->trim()->lower();
|
|
|
|
});
|
|
|
|
$this->application->fqdn = $domains->implode(',');
|
|
|
|
}
|
2023-12-07 19:06:32 +01:00
|
|
|
|
2023-09-19 15:51:13 +02:00
|
|
|
if (data_get($this->application, 'dockerfile')) {
|
2023-08-21 18:00:12 +02:00
|
|
|
$port = get_port_from_dockerfile($this->application->dockerfile);
|
2023-10-02 09:08:33 +02:00
|
|
|
if ($port && !$this->application->ports_exposes) {
|
2023-08-21 18:00:12 +02:00
|
|
|
$this->application->ports_exposes = $port;
|
|
|
|
}
|
2023-08-11 22:41:47 +02:00
|
|
|
}
|
2023-07-07 14:56:20 +02:00
|
|
|
if ($this->application->base_directory && $this->application->base_directory !== '/') {
|
|
|
|
$this->application->base_directory = rtrim($this->application->base_directory, '/');
|
|
|
|
}
|
|
|
|
if ($this->application->publish_directory && $this->application->publish_directory !== '/') {
|
|
|
|
$this->application->publish_directory = rtrim($this->application->publish_directory, '/');
|
|
|
|
}
|
2023-11-28 11:10:48 +01:00
|
|
|
if ($this->application->build_pack === 'dockercompose') {
|
|
|
|
$this->application->docker_compose_domains = json_encode($this->parsedServiceDomains);
|
|
|
|
$this->parsedServices = $this->application->parseCompose();
|
|
|
|
}
|
2023-12-12 16:34:05 +01:00
|
|
|
$this->application->custom_labels = base64_encode($this->customLabels);
|
2023-05-16 15:27:47 +02:00
|
|
|
$this->application->save();
|
2023-12-07 19:06:32 +01:00
|
|
|
$showToaster && $this->dispatch('success', 'Application settings updated!');
|
2023-09-11 17:36:30 +02:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 15:34:25 +02:00
|
|
|
return handleError($e, $this);
|
2023-10-18 10:32:08 +02:00
|
|
|
} finally {
|
|
|
|
$this->checkLabelUpdates();
|
2023-10-18 11:20:40 +02:00
|
|
|
$this->isConfigurationChanged = $this->application->isConfigurationChanged();
|
2023-05-16 15:27:47 +02:00
|
|
|
}
|
2023-04-19 12:42:15 +02:00
|
|
|
}
|
|
|
|
}
|