Skip to content

Commit

Permalink
feat: #36 support deployment from the private registreis
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Jul 2, 2024
1 parent ee7fda4 commit 6093084
Show file tree
Hide file tree
Showing 40 changed files with 630 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Events\NodeTasks\CheckRegistryAuth;

use App\Events\NodeTasks\BaseTaskEvent;

class CheckRegistryAuthCompleted extends BaseTaskEvent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Events\NodeTasks\CheckRegistryAuth;

use App\Events\NodeTasks\BaseTaskEvent;

class CheckRegistryAuthFailed extends BaseTaskEvent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Events\NodeTasks\CreateRegistryAuth;

use App\Events\NodeTasks\BaseTaskEvent;

class CreateRegistryAuthCompleted extends BaseTaskEvent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Events\NodeTasks\CreateRegistryAuth;

use App\Events\NodeTasks\BaseTaskEvent;

class CreateRegistryAuthFailed extends BaseTaskEvent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Events\NodeTasks\PullDockerImage;

use App\Events\NodeTasks\BaseTaskEvent;

class PullDockerImageCompleted extends BaseTaskEvent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Events\NodeTasks\PullDockerImage;

use App\Events\NodeTasks\BaseTaskEvent;

class PullDockerImageFailed extends BaseTaskEvent
{
}
8 changes: 8 additions & 0 deletions app/Http/Controllers/NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,19 @@ public function show(Node $node)

$taskGroup = $node->actualTaskGroup(NodeTaskGroupType::SelfUpgrade);

$node->load('swarm');

$registryTaskGroup = $node->actualTaskGroup(NodeTaskGroupType::UpdateDockerRegistries);
if ($registryTaskGroup?->is_completed) {
$registryTaskGroup = null;
}

return Inertia::render('Nodes/Show', [
'node' => $node,
'initTaskGroup' => $initTaskGroup,
'lastAgentVersion' => $lastAgentVersion,
'agentUpgradeTaskGroup' => $taskGroup?->is_completed ? null : $taskGroup,
'registryUpdateTaskGroup' => $registryTaskGroup?->is_completed ? null : $registryTaskGroup
]);
}

Expand Down
10 changes: 9 additions & 1 deletion app/Http/Controllers/ServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function create()

$networks = count($swarms) ? $swarms[0]->networks : [];
$nodes = count($swarms) ? $swarms[0]->nodes : [];
$dockerRegistries = count($swarms) ? $swarms[0]->data->registries : [];

$deploymentData = DeploymentData::make([
'networkName' => count($networks) ? $networks[0]->name : null,
Expand All @@ -49,6 +50,7 @@ public function create()
'networks' => $networks,
'nodes' => $nodes,
'deploymentData' => $deploymentData,
'dockerRegistries' => $dockerRegistries
]);
}

Expand Down Expand Up @@ -79,8 +81,14 @@ public function show(Service $service)

$networks = $service->swarm->networks;
$nodes = $service->swarm->nodes;
$dockerRegistries = $service->swarm->data->registries ;

return Inertia::render('Services/Show', ['service' => $service, 'networks' => $networks, 'nodes' => $nodes]);
return Inertia::render('Services/Show', [
'service' => $service,
'networks' => $networks,
'nodes' => $nodes,
'dockerRegistries' => $dockerRegistries
]);
}

public function deployments(Service $service)
Expand Down
83 changes: 83 additions & 0 deletions app/Http/Controllers/SwarmController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@

use App\Http\Requests\StoreSwarmRequest;
use App\Http\Requests\UpdateSwarmRequest;
use App\Models\DockerConfigKind;
use App\Models\NodeTaskGroup;
use App\Models\NodeTaskGroupType;
use App\Models\NodeTasks\CheckRegistryAuth\CheckRegistryAuthMeta;
use App\Models\NodeTasks\CreateConfig\CreateConfigMeta;
use App\Models\NodeTasks\CreateRegistryAuth\CreateRegistryAuthMeta;
use App\Models\NodeTaskType;
use App\Models\Swarm;
use App\Models\SwarmData;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SwarmController extends Controller
{
Expand Down Expand Up @@ -63,4 +73,77 @@ public function destroy(Swarm $swarm)
{
//
}

public function updateDockerRegistries(Swarm $swarm, Request $request)
{
// TODO: check if the registry is in use before deleting it
DB::transaction(function () use ($swarm, $request) {
$swarmData = SwarmData::validateAndCreate([
...$swarm->data->toArray(),
'registries' => $request->get('registries'),
]);

$swarmData->registriesRev += 1;

$tasks = [];

foreach ($swarmData->registries as $registry) {
$previous = $swarm->data->findRegistry($registry->dockerName);
if ($registry->sameAs($previous)) {
$registry->dockerName = $previous->dockerName;

continue;
}

$registry->dockerName = dockerize_name('registry_r' . $swarmData->registriesRev . '_' . $registry->name);

$taskMeta = [
'registryName' => $registry->name,
];

$tasks[] = [
'type' => NodeTaskType::CreateRegistryAuth,
'meta' => CreateRegistryAuthMeta::validateAndCreate($taskMeta),
'payload' => [
'PrevConfigName' => $previous?->dockerName,
'AuthConfigSpec' => [
'ServerAddress' => $registry->serverAddress,
'Username' => $registry->username,
'Password' => $registry->password,
],
'SwarmConfigSpec' => [
'Name' => $registry->dockerName,
'Labels' => dockerize_labels([
'kind' => DockerConfigKind::RegistryCredentials->value,
'revision' => $swarmData->registriesRev,
]),
],
]
];

$tasks[] = [
'type' => NodeTaskType::CheckRegistryAuth,
'meta' => CheckRegistryAuthMeta::validateAndCreate($taskMeta),
'payload' => [
'RegistryConfigName' => $registry->dockerName,
]
];
}

$swarm->data = $swarmData;
$swarm->save();

if (!empty($tasks)) {
$taskGroup = NodeTaskGroup::create([
'type' => NodeTaskGroupType::UpdateDockerRegistries,
'swarm_id' => $swarm->id,
'invoker_id' => auth()->user()->id,
]);

$taskGroup->tasks()->createMany($tasks);
}
});

// return back();
}
}
9 changes: 6 additions & 3 deletions app/Http/Controllers/SwarmTaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
use App\Models\Node;
use App\Models\NodeTaskGroup;
use App\Models\NodeTaskGroupType;
use App\Models\NodeTasks\CreateConfig\CreateConfigMeta;
use App\Models\NodeTasks\CreateNetwork\CreateNetworkMeta;
use App\Models\NodeTasks\InitSwarm\InitSwarmMeta;
use App\Models\NodeTasks\UpdateCurrentNode\UpdateCurrentNodeMeta;
use App\Models\NodeTaskType;
use App\Models\Swarm;
use Illuminate\Database\Eloquent\Casts\Json;
use App\Models\SwarmData;
use Illuminate\Support\Facades\DB;

class SwarmTaskController extends Controller
Expand All @@ -25,6 +24,10 @@ public function initCluster(InitClusterFormRequest $request)
DB::transaction(function () use ($request) {
$swarm = Swarm::create([
'name' => $request->name,
'data' => SwarmData::validateAndCreate([
'registriesRev' => 0,
'registries' => [],
]),
]);

$node = Node::find($request->node_id);
Expand Down Expand Up @@ -132,7 +135,7 @@ public function initCluster(InitClusterFormRequest $request)
],
[
'path' => '/start.sh',
'content' => file_get_contents(resource_path('support/caddy/start.sh')),
'content' => trim(file_get_contents(resource_path('support/caddy/start.sh'))),
]
],
'secretFiles' => [],
Expand Down
17 changes: 3 additions & 14 deletions app/Models/Deployment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,18 @@
namespace App\Models;

use App\Models\DeploymentData\Caddy;
use App\Models\DeploymentData\ConfigFile;
use App\Models\DeploymentData\EnvVar;
use App\Models\DeploymentData\NodePort;
use App\Models\DeploymentData\Process;
use App\Models\DeploymentData\Volume;
use App\Models\NodeTasks\CreateConfig\CreateConfigMeta;
use App\Models\NodeTasks\CreateSecret\CreateSecretMeta;
use App\Models\NodeTasks\CreateService\CreateServiceMeta;
use App\Models\NodeTasks\ApplyCaddyConfig\ApplyCaddyConfigMeta;
use App\Models\NodeTasks\DeleteService\DeleteServiceMeta;
use App\Traits\HasOwningTeam;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Eloquent\Casts\Json;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Query\Builder as QueryBuilder;
use InvalidArgumentException;

class Deployment extends Model
Expand Down
15 changes: 1 addition & 14 deletions app/Models/DeploymentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,13 @@

namespace App\Models;

use App\Models\DeploymentData\Caddy;
use App\Models\DeploymentData\ConfigFile;
use App\Models\DeploymentData\EnvVar;
use App\Models\DeploymentData\FastCgi;
use App\Models\DeploymentData\LaunchMode;
use App\Models\DeploymentData\NodePort;
use App\Models\DeploymentData\Process;
use App\Models\DeploymentData\SecretVars;
use App\Models\DeploymentData\Volume;
use App\Models\NodeTasks\CreateConfig\CreateConfigMeta;
use App\Models\NodeTasks\CreateSecret\CreateSecretMeta;
use App\Models\NodeTasks\CreateService\CreateServiceMeta;
use App\Rules\RequiredIfArrayHas;
use App\Util\Arrays;
use Illuminate\Validation\ValidationException;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Attributes\Validation\Exists;
use Spatie\LaravelData\Attributes\Validation\RequiredIf;
use Spatie\LaravelData\Attributes\Validation\RequiredUnless;
use Spatie\LaravelData\Attributes\Validation\Rule;
use Spatie\LaravelData\Data;

class DeploymentData extends Data
Expand All @@ -44,7 +31,7 @@ public static function make(array $attributes): static
{
$processDefaults = [
'name' => 'svc',
'dockerRegistryId' => null,
'dockerRegistry' => null,
'dockerImage' => '',
'command' => '',
'launchMode' => LaunchMode::Daemon->value,
Expand Down
22 changes: 19 additions & 3 deletions app/Models/DeploymentData/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace App\Models\DeploymentData;

use App\Models\Deployment;
use App\Models\DeploymentData;
use App\Models\NodeTasks\CreateConfig\CreateConfigMeta;
use App\Models\NodeTasks\CreateSecret\CreateSecretMeta;
use App\Models\NodeTasks\CreateService\CreateServiceMeta;
use App\Models\NodeTasks\PullDockerImage\PullDockerImageMeta;
use App\Models\NodeTaskType;
use App\Rules\RequiredIfArrayHas;
use Spatie\LaravelData\Attributes\DataCollectionOf;
Expand All @@ -19,7 +19,7 @@ class Process extends Data
public function __construct(
public string $name,
public ?string $dockerName,
public ?int $dockerRegistryId,
public ?string $dockerRegistry,
public string $dockerImage,
public ?string $command,
#[Enum(LaunchMode::class)]
Expand Down Expand Up @@ -153,6 +153,22 @@ public function asNodeTasks(Deployment $deployment): array
$args = array_slice($splitCmd, 1);
}

$tasks[] = [
'type' => NodeTaskType::PullDockerImage,
'meta' => PullDockerImageMeta::from([
'deploymentId' => $deployment->id,
'processName' => $this->dockerName,
'serviceId' => $deployment->service_id,
'serviceName' => $deployment->service->name,
'dockerImage' => $this->dockerImage,
]),
'payload' => [
'AuthConfigName' => $this->dockerRegistry,
'Image' => $this->dockerImage,
'PullOptions' => (object) [],
],
];

$tasks[] = [
'type' => $previous ? NodeTaskType::UpdateService : NodeTaskType::CreateService,
'meta' => CreateServiceMeta::from([
Expand All @@ -162,7 +178,7 @@ public function asNodeTasks(Deployment $deployment): array
'serviceName' => $deployment->service->name,
]),
'payload' => [
// 'AuthConfigName' => "",
'AuthConfigName' => $this->dockerRegistry,
'SecretVars' => (object) $this->getSecretVars($previous, $labels),
'SwarmServiceSpec' => [
'Name' => $this->dockerName,
Expand Down
8 changes: 8 additions & 0 deletions app/Models/DockerConfigKind.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Models;

enum DockerConfigKind: string
{
case RegistryCredentials = 'registry-credentials';
}
6 changes: 6 additions & 0 deletions app/Models/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Traits\HasOwningTeam;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -33,6 +34,11 @@ protected static function booted()
});
}

public function swarm(): BelongsTo
{
return $this->belongsTo(Swarm::class);
}

public function taskGroups(): HasMany
{
return $this->hasMany(NodeTaskGroup::class);
Expand Down
1 change: 1 addition & 0 deletions app/Models/NodeTaskGroupType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ enum NodeTaskGroupType: int
case UpdateService = 2;
case DeleteService = 3;
case SelfUpgrade = 4;
case UpdateDockerRegistries = 5;
}
Loading

0 comments on commit 6093084

Please sign in to comment.