coolify/app/Livewire/Project/New/DockerCompose.php

84 lines
2.9 KiB
PHP
Raw Normal View History

2023-09-19 13:51:13 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\New;
2023-09-19 13:51:13 +00:00
2023-09-26 12:45:52 +00:00
use App\Models\EnvironmentVariable;
2023-09-19 13:51:13 +00:00
use App\Models\Project;
2023-09-20 13:42:41 +00:00
use App\Models\Service;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
2024-06-10 20:43:34 +00:00
use Livewire\Component;
use Symfony\Component\Yaml\Yaml;
2023-09-19 13:51:13 +00:00
class DockerCompose extends Component
{
2023-09-25 13:48:43 +00:00
public string $dockerComposeRaw = '';
2024-06-10 20:43:34 +00:00
2023-09-26 12:45:52 +00:00
public string $envFile = '';
2024-06-10 20:43:34 +00:00
2023-09-19 13:51:13 +00:00
public array $parameters;
2024-06-10 20:43:34 +00:00
2023-09-19 13:51:13 +00:00
public array $query;
2024-06-10 20:43:34 +00:00
2023-09-19 13:51:13 +00:00
public function mount()
{
$this->parameters = get_route_parameters();
$this->query = request()->query();
if (isDev()) {
$this->dockerComposeRaw = file_get_contents(base_path('templates/test-database-detection.yaml'));
2023-09-19 13:51:13 +00:00
}
}
2024-06-10 20:43:34 +00:00
2023-09-19 13:51:13 +00:00
public function submit()
{
2024-03-14 08:21:48 +00:00
$server_id = $this->query['server_id'];
try {
$this->validate([
2024-06-10 20:43:34 +00:00
'dockerComposeRaw' => 'required',
]);
2023-09-25 13:48:43 +00:00
$this->dockerComposeRaw = Yaml::dump(Yaml::parse($this->dockerComposeRaw), 10, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
2024-11-22 15:03:20 +00:00
$environment = $project->load(['environments'])->environments->where('uuid', $this->parameters['environment_uuid'])->first();
$destination_uuid = $this->query['destination'];
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
if (! $destination) {
$destination = SwarmDocker::where('uuid', $destination_uuid)->first();
}
if (! $destination) {
throw new \Exception('Destination not found. What?!');
}
$destination_class = $destination->getMorphClass();
$service = Service::create([
2023-09-25 13:48:43 +00:00
'docker_compose_raw' => $this->dockerComposeRaw,
'environment_id' => $environment->id,
'server_id' => (int) $server_id,
'destination_id' => $destination->id,
'destination_type' => $destination_class,
]);
2023-09-26 12:45:52 +00:00
$variables = parseEnvFormatToArray($this->envFile);
foreach ($variables as $key => $variable) {
EnvironmentVariable::create([
2023-09-26 12:45:52 +00:00
'key' => $key,
'value' => $variable,
'is_build_time' => false,
'is_preview' => false,
'resourceable_id' => $service->id,
'resourceable_type' => $service->getMorphClass(),
2023-09-26 12:45:52 +00:00
]);
}
$service->parse(isNew: true);
2023-09-21 15:48:31 +00:00
2023-12-27 15:45:01 +00:00
return redirect()->route('project.service.configuration', [
'service_uuid' => $service->uuid,
2024-11-22 15:03:20 +00:00
'environment_uuid' => $environment->uuid,
'project_uuid' => $project->uuid,
]);
} catch (\Throwable $e) {
return handleError($e, $this);
}
2023-09-19 13:51:13 +00:00
}
}