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

84 lines
2.6 KiB
PHP
Raw Normal View History

2023-10-09 15:49:48 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\New;
2023-10-09 15:49:48 +02:00
use App\Models\Application;
use App\Models\Project;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
2024-06-10 22:43:34 +02:00
use Illuminate\Support\Str;
2023-10-09 15:49:48 +02:00
use Livewire\Component;
use Visus\Cuid2\Cuid2;
class DockerImage extends Component
{
public string $dockerImage = '';
2024-06-10 22:43:34 +02:00
2023-10-09 15:49:48 +02:00
public array $parameters;
2024-06-10 22:43:34 +02:00
2023-10-09 15:49:48 +02:00
public array $query;
2024-06-10 22:43:34 +02:00
2023-10-09 15:49:48 +02:00
public function mount()
{
$this->parameters = get_route_parameters();
$this->query = request()->query();
}
2024-06-10 22:43:34 +02:00
2023-10-09 15:49:48 +02:00
public function submit()
{
$this->validate([
2024-06-10 22:43:34 +02:00
'dockerImage' => 'required',
2023-10-09 15:49:48 +02:00
]);
$image = Str::of($this->dockerImage)->before(':');
2023-10-10 11:16:38 +02:00
if (Str::of($this->dockerImage)->contains(':')) {
$tag = Str::of($this->dockerImage)->after(':');
} else {
$tag = 'latest';
}
2023-10-09 15:49:48 +02:00
$destination_uuid = $this->query['destination'];
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
2024-06-10 22:43:34 +02:00
if (! $destination) {
2023-10-09 15:49:48 +02:00
$destination = SwarmDocker::where('uuid', $destination_uuid)->first();
}
2024-06-10 22:43:34 +02:00
if (! $destination) {
2023-10-09 15:49:48 +02:00
throw new \Exception('Destination not found. What?!');
}
$destination_class = $destination->getMorphClass();
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
2024-06-10 22:43:34 +02:00
ray($image, $tag);
2023-10-09 15:49:48 +02:00
$application = Application::create([
2024-06-10 22:43:34 +02:00
'name' => 'docker-image-'.new Cuid2(7),
2023-10-09 15:49:48 +02:00
'repository_project_id' => 0,
2024-06-10 22:43:34 +02:00
'git_repository' => 'coollabsio/coolify',
2023-10-09 15:49:48 +02:00
'git_branch' => 'main',
'build_pack' => 'dockerimage',
'ports_exposes' => 80,
'docker_registry_image_name' => $image,
'docker_registry_image_tag' => $tag,
'environment_id' => $environment->id,
'destination_id' => $destination->id,
'destination_type' => $destination_class,
'health_check_enabled' => false,
]);
$fqdn = generateFqdn($destination->server, $application->uuid);
$application->update([
2024-06-10 22:43:34 +02:00
'name' => 'docker-image-'.$application->uuid,
'fqdn' => $fqdn,
2023-10-09 15:49:48 +02:00
]);
2024-06-10 22:43:34 +02:00
2023-12-27 16:45:01 +01:00
return redirect()->route('project.application.configuration', [
2023-10-09 15:49:48 +02:00
'application_uuid' => $application->uuid,
'environment_name' => $environment->name,
'project_uuid' => $project->uuid,
2023-12-27 16:45:01 +01:00
]);
2023-10-09 15:49:48 +02:00
}
2024-06-10 22:43:34 +02:00
2023-10-09 15:49:48 +02:00
public function render()
{
return view('livewire.project.new.docker-image');
}
}