Skip to content

Commit

Permalink
fix: #191 correct subsequent deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Sep 13, 2024
1 parent dce31ac commit 588417b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 50 deletions.
3 changes: 2 additions & 1 deletion app/Actions/Services/StartDeployment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\Service;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -19,7 +20,7 @@ class StartDeployment

public function rules(): array
{
return DeploymentData::getValidationRules([]);
return DeploymentData::getValidationRules(Request::all());
}

public function authorize(ActionRequest $request): bool
Expand Down
55 changes: 7 additions & 48 deletions app/Models/DeploymentData/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,15 @@ public function asNodeTasks(Deployment $deployment): array
// This code is duplicated in the next block
$actionUpdate = $deployment->service->tasks()->ofType(NodeTaskType::CreateService)->where('meta__docker_name', $this->dockerName)->completed()->exists();

$serviceSecretVars = $this->getSecretVars();

$tasks[] = [
'type' => $actionUpdate ? NodeTaskType::UpdateService : NodeTaskType::CreateService,
'meta' => $actionUpdate ? UpdateServiceMeta::from($serviceTaskMeta) : CreateServiceMeta::from($serviceTaskMeta),
'payload' => [
'AuthConfigName' => $authConfigName,
'ReleaseCommand' => $this->getReleaseCommandPayload($deployment, $labels),
'SecretVars' => (object) $this->getSecretVars($deployment, $previous, $labels),
'SecretVars' => $serviceSecretVars,
'SwarmServiceSpec' => [
'Name' => $this->dockerName,
'Labels' => $labels,
Expand Down Expand Up @@ -380,7 +382,7 @@ public function asNodeTasks(Deployment $deployment): array
'payload' => [
'AuthConfigName' => $authConfigName,
'ReleaseCommand' => (object) [],
'SecretVars' => (object) $this->getWorkerSecretVars($worker, $labels),
'SecretVars' => $serviceSecretVars,
'SwarmServiceSpec' => [
'Name' => $worker->dockerName,
'Labels' => $labels,
Expand Down Expand Up @@ -451,54 +453,11 @@ public function asNodeTasks(Deployment $deployment): array
return $tasks;
}

protected function getSecretVars(Deployment $deployment, ?Process $previous, $labels): array|object
protected function getSecretVars(): array
{
if (empty($this->secretVars->vars)) {
$this->secretVars->dockerName = null;

return (object) [];
}

$this->secretVars->dockerName = $this->makeResourceName('dpl_'.$deployment->id.'_secret_vars');

$data = [
'ConfigName' => $this->secretVars->dockerName,
'ConfigLabels' => dockerize_labels([
...$labels,
'kind' => 'secret-env-vars',
]),
'Values' => (object) collect($this->secretVars->vars)
->reject(fn (EnvVar $var) => $var->value === null)
->reduce(fn ($carry, EnvVar $var) => [...$carry, $var->name => $var->value], []),
];

if (! empty($previous?->secretVars->dockerName)) {
$data['Preserve'] = collect($this->secretVars->vars)
->filter(fn (EnvVar $var) => $var->value === null)
->map(fn (EnvVar $var) => $var->name)
->toArray();

$data['PreserveFromConfig'] = $previous->secretVars->dockerName;
}

return $data;
}

private function getWorkerSecretVars(Worker $worker, array $labels): array|object
{
if (empty($this->secretVars->vars)) {
return (object) [];
}

return [
'ConfigName' => $this->secretVars->dockerName.'_wkr_'.$worker->name,
'ConfigLabels' => dockerize_labels([
...$labels,
'kind' => 'secret-env-vars',
]),
'Values' => (object) [],
'Preserve' => collect($this->secretVars->vars)->map(fn (EnvVar $var) => $var->name)->toArray(),
'PreserveFromConfig' => $this->secretVars->dockerName,
'Values' => (object) collect($this->secretVars->vars)
->reduce(fn ($carry, EnvVar $var) => [...$carry, $var->name => $var->value ?? ''], []),
];
}

Expand Down
1 change: 0 additions & 1 deletion app/Models/DeploymentData/SecretVars.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ class SecretVars extends Data
public function __construct(
#[DataCollectionOf(EnvVar::class)]
public array $vars,
public ?string $dockerName
) {}
}

0 comments on commit 588417b

Please sign in to comment.