Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add registry auth to docker image apps #4187

Draft
wants to merge 15 commits into
base: next
Choose a base branch
from
Draft
152 changes: 105 additions & 47 deletions app/Jobs/ApplicationDeploymentJob.php

Large diffs are not rendered by default.

Empty file.
13 changes: 13 additions & 0 deletions app/Livewire/Images/Images/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Livewire\Images\Images;

use Livewire\Component;

class Index extends Component
{
public function render()
{
return view('livewire.images.images.index');
}
}
50 changes: 50 additions & 0 deletions app/Livewire/Images/Registry/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Livewire\Images\Registry;

use App\Models\DockerRegistry;
use Livewire\Component;

class Create extends Component
{
public string $name = '';
public string $type = 'docker_hub';
public ?string $url = null;
public ?string $username = null;
public ?string $token = null;

protected $rules = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the new validation attribute of Livewire as this syntax is deprecated. https://livewire.laravel.com/docs/validation#validate-attributes

'name' => 'required|string|max:255',
'type' => 'required|string',
'url' => 'nullable|string|max:255',
'username' => 'nullable|string|max:255',
'token' => 'nullable|string',
];

public function getRegistryTypesProperty()
{
return DockerRegistry::getTypes();
}

public function submit()
{
$this->validate();

DockerRegistry::create([
'name' => $this->name,
'type' => $this->type,
'url' => $this->type === 'custom' ? $this->url : 'docker.io',
'username' => $this->username,
'token' => $this->token,
]);

$this->dispatch('registry-added');
$this->dispatch('success', 'Registry added successfully.');
$this->dispatch('close-modal');
}

public function render()
{
return view('livewire.images.registry.create');
}
}
18 changes: 18 additions & 0 deletions app/Livewire/Images/Registry/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Livewire\Images\Registry;

use App\Models\DockerRegistry;
use Livewire\Component;

class Index extends Component
{
protected $listeners = ['registry-added' => '$refresh'];

public function render()
{
return view('livewire.images.registry.index', [
'registries' => DockerRegistry::all()
]);
}
}
82 changes: 82 additions & 0 deletions app/Livewire/Images/Registry/Show.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Livewire\Images\Registry;

use App\Models\DockerRegistry;
use Livewire\Component;

class Show extends Component
{
public DockerRegistry $registry;
public string $name = '';
public string $type = '';
public ?string $url = null;
public ?string $username = null;
public ?string $token = null;

protected $rules = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also use the new validate attribute syntax

'name' => 'required|string|max:255',
'type' => 'required|string',
'url' => 'nullable|string|max:255',
'username' => 'nullable|string|max:255',
'token' => 'nullable|string',
];

public function mount(DockerRegistry $registry)
{
$this->registry = $registry;
$this->name = $registry->name;
$this->type = $registry->type;
$this->url = $registry->url;
$this->username = $registry->username;
$this->token = $registry->token;
}

public function getRegistryTypesProperty()
{
return DockerRegistry::getTypes();
}

public function updateRegistry()
{
$this->validate();

$this->registry->update([
'name' => $this->name,
'type' => $this->type,
'url' => $this->type === 'custom' ? $this->url : 'docker.io',
'username' => $this->username,
'token' => $this->token,
]);

$this->dispatch('success', 'Registry updated successfully.');
}

public function delete()
{
// Update all applications using this registry
$this->registry->applications()
->update([
'docker_registry_id' => null,
'docker_use_custom_registry' => false
]);

$this->registry->delete();
$this->dispatch('registry-added');
$this->dispatch('success', 'Registry deleted successfully.');
}

public function render()
{
return view('livewire.images.registry.show');
}

public function getIsFormDirtyProperty(): bool
{
return $this->name !== $this->registry->name
|| $this->type !== $this->registry->type
|| $this->url !== $this->registry->url
|| $this->username !== $this->registry->username
|| $this->token !== $this->registry->token;
}
}
18 changes: 16 additions & 2 deletions app/Livewire/Project/Application/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Actions\Application\GenerateConfig;
use App\Models\Application;
use App\Models\DockerRegistry;
use Illuminate\Support\Collection;
use Livewire\Component;
use Spatie\Url\Url;
Expand Down Expand Up @@ -71,6 +72,8 @@ class General extends Component
'application.dockerfile' => 'nullable',
'application.docker_registry_image_name' => 'nullable',
'application.docker_registry_image_tag' => 'nullable',
'application.docker_use_custom_registry' => 'boolean',
'application.docker_registry_id' => 'nullable|required_if:application.docker_use_custom_registry,true',
'application.dockerfile_location' => 'nullable',
'application.docker_compose_location' => 'nullable',
'application.docker_compose' => 'nullable',
Expand Down Expand Up @@ -113,6 +116,8 @@ class General extends Component
'application.dockerfile' => 'Dockerfile',
'application.docker_registry_image_name' => 'Docker registry image name',
'application.docker_registry_image_tag' => 'Docker registry image tag',
'application.docker_use_custom_registry' => 'Use private registry',
'application.docker_registry_id' => 'Registry',
'application.dockerfile_location' => 'Dockerfile location',
'application.docker_compose_location' => 'Docker compose location',
'application.docker_compose' => 'Docker compose',
Expand Down Expand Up @@ -148,6 +153,7 @@ public function mount()
$this->application->fqdn = null;
$this->application->settings->save();
}

$this->parsedServiceDomains = $this->application->docker_compose_domains ? json_decode($this->application->docker_compose_domains, true) : [];
$this->ports_exposes = $this->application->ports_exposes;
$this->is_preserve_repository_enabled = $this->application->settings->is_preserve_repository_enabled;
Expand Down Expand Up @@ -330,7 +336,7 @@ public function checkFqdns($showToaster = true)
public function set_redirect()
{
try {
$has_www = collect($this->application->fqdns)->filter(fn ($fqdn) => str($fqdn)->contains('www.'))->count();
$has_www = collect($this->application->fqdns)->filter(fn($fqdn) => str($fqdn)->contains('www.'))->count();
if ($has_www === 0 && $this->application->redirect === 'www') {
$this->dispatch('error', 'You want to redirect to www, but you do not have a www domain set.<br><br>Please add www to your domain list and as an A DNS record (if applicable).');

Expand Down Expand Up @@ -389,6 +395,7 @@ public function submit($showToaster = true)
if (data_get($this->application, 'build_pack') === 'dockerimage') {
$this->validate([
'application.docker_registry_image_name' => 'required',
'application.docker_registry_id' => 'required_if:application.docker_use_custom_registry,true',
]);
}

Expand Down Expand Up @@ -447,7 +454,14 @@ public function downloadConfig()
echo $config;
}, $fileName, [
'Content-Type' => 'application/json',
'Content-Disposition' => 'attachment; filename='.$fileName,
'Content-Disposition' => 'attachment; filename=' . $fileName,
]);
}

public function render()
{
return view('livewire.project.application.general', [
'registries' => DockerRegistry::all(),
]);
}
}
35 changes: 22 additions & 13 deletions app/Livewire/Project/New/DockerImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Livewire\Project\New;

use App\Models\Application;
use App\Models\DockerRegistry;
use App\Models\Project;
use App\Models\StandaloneDocker;
use App\Models\SwarmDocker;
Expand All @@ -12,11 +13,16 @@
class DockerImage extends Component
{
public string $dockerImage = '';

public bool $useCustomRegistry = false;
public ?int $selectedRegistry = null;
public array $parameters;

public array $query;

protected $rules = [
'dockerImage' => 'required|string',
'selectedRegistry' => 'required_if:useCustomRegistry,true|nullable|exists:docker_registries,id'
];

public function mount()
{
$this->parameters = get_route_parameters();
Expand All @@ -25,15 +31,13 @@ public function mount()

public function submit()
{
$this->validate([
'dockerImage' => 'required',
]);
$this->validate(['dockerImage' => 'required',]);

$image = str($this->dockerImage)->before(':');
if (str($this->dockerImage)->contains(':')) {
$tag = str($this->dockerImage)->after(':');
} else {
$tag = 'latest';
}
$tag = str($this->dockerImage)->contains(':') ?
str($this->dockerImage)->after(':') :
'latest';

$destination_uuid = $this->query['destination'];
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
if (! $destination) {
Expand All @@ -46,15 +50,18 @@ public function submit()

$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();

$application = Application::create([
'name' => 'docker-image-'.new Cuid2,
'name' => 'docker-image-' . new Cuid2,
'repository_project_id' => 0,
'git_repository' => 'coollabsio/coolify',
'git_branch' => 'main',
'build_pack' => 'dockerimage',
'ports_exposes' => 80,
'docker_registry_image_name' => $image,
'docker_registry_image_tag' => $tag,
'docker_use_custom_registry' => $this->useCustomRegistry,
'docker_registry_id' => $this->selectedRegistry ?? null,
'environment_id' => $environment->id,
'destination_id' => $destination->id,
'destination_type' => $destination_class,
Expand All @@ -63,7 +70,7 @@ public function submit()

$fqdn = generateFqdn($destination->server, $application->uuid);
$application->update([
'name' => 'docker-image-'.$application->uuid,
'name' => 'docker-image-' . $application->uuid,
'fqdn' => $fqdn,
]);

Expand All @@ -76,6 +83,8 @@ public function submit()

public function render()
{
return view('livewire.project.new.docker-image');
return view('livewire.project.new.docker-image', [
'registries' => DockerRegistry::all()
]);
}
}
Loading