fix: delete preview deployments + cleanup stucked
fix: parser
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
use App\Models\Application;
|
use App\Models\Application;
|
||||||
|
use App\Models\ApplicationPreview;
|
||||||
use App\Models\ScheduledTask;
|
use App\Models\ScheduledTask;
|
||||||
use App\Models\Service;
|
use App\Models\Service;
|
||||||
use App\Models\ServiceApplication;
|
use App\Models\ServiceApplication;
|
||||||
@@ -42,6 +43,17 @@ class CleanupStuckedResources extends Command
|
|||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
echo "Error in cleaning stuck application: {$e->getMessage()}\n";
|
echo "Error in cleaning stuck application: {$e->getMessage()}\n";
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
$applicationsPreviews = ApplicationPreview::get();
|
||||||
|
foreach ($applicationsPreviews as $applicationPreview) {
|
||||||
|
if (! data_get($applicationPreview, 'application')) {
|
||||||
|
echo "Deleting stuck application preview: {$applicationPreview->uuid}\n";
|
||||||
|
$applicationPreview->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
echo "Error in cleaning stuck application: {$e->getMessage()}\n";
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
$postgresqls = StandalonePostgresql::withTrashed()->whereNotNull('deleted_at')->get();
|
$postgresqls = StandalonePostgresql::withTrashed()->whereNotNull('deleted_at')->get();
|
||||||
foreach ($postgresqls as $postgresql) {
|
foreach ($postgresqls as $postgresql) {
|
||||||
|
@@ -4,7 +4,6 @@ namespace App\Livewire\Project\Application;
|
|||||||
|
|
||||||
use App\Models\Application;
|
use App\Models\Application;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
use Visus\Cuid2\Cuid2;
|
use Visus\Cuid2\Cuid2;
|
||||||
|
|
||||||
|
@@ -142,6 +142,7 @@ class Application extends BaseModel
|
|||||||
$task->delete();
|
$task->delete();
|
||||||
}
|
}
|
||||||
$application->tags()->detach();
|
$application->tags()->detach();
|
||||||
|
$application->previews()->delete();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -12,9 +12,9 @@ class ApplicationPreview extends BaseModel
|
|||||||
protected static function booted()
|
protected static function booted()
|
||||||
{
|
{
|
||||||
static::deleting(function ($preview) {
|
static::deleting(function ($preview) {
|
||||||
if ($preview->application->build_pack === 'dockercompose') {
|
if (data_get($preview, 'application.build_pack') === 'dockercompose') {
|
||||||
$server = $preview->application->destination->server;
|
$server = $preview->application->destination->server;
|
||||||
$composeFile = $preview->application->oldParser(pull_request_id: $preview->pull_request_id);
|
$composeFile = newParser($preview->application, pull_request_id: $preview->pull_request_id);
|
||||||
$volumes = data_get($composeFile, 'volumes');
|
$volumes = data_get($composeFile, 'volumes');
|
||||||
$networks = data_get($composeFile, 'networks');
|
$networks = data_get($composeFile, 'networks');
|
||||||
$networkKeys = collect($networks)->keys();
|
$networkKeys = collect($networks)->keys();
|
||||||
|
@@ -3046,6 +3046,9 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
|
|||||||
} else {
|
} else {
|
||||||
$mainDirectory = str(base_configuration_dir().'/applications/'.$uuid);
|
$mainDirectory = str(base_configuration_dir().'/applications/'.$uuid);
|
||||||
$source = replaceLocalSource($source, $mainDirectory);
|
$source = replaceLocalSource($source, $mainDirectory);
|
||||||
|
if ($isApplication && $isPullRequest) {
|
||||||
|
$source = $source."-pr-$pullRequestId";
|
||||||
|
}
|
||||||
|
|
||||||
LocalFileVolume::updateOrCreate(
|
LocalFileVolume::updateOrCreate(
|
||||||
[
|
[
|
||||||
@@ -3076,6 +3079,10 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
|
|||||||
}
|
}
|
||||||
$slugWithoutUuid = Str::slug($source, '-');
|
$slugWithoutUuid = Str::slug($source, '-');
|
||||||
$name = "{$uuid}_{$slugWithoutUuid}";
|
$name = "{$uuid}_{$slugWithoutUuid}";
|
||||||
|
|
||||||
|
if ($isApplication && $isPullRequest) {
|
||||||
|
$name = "{$name}-pr-$pullRequestId";
|
||||||
|
}
|
||||||
if (is_string($volume)) {
|
if (is_string($volume)) {
|
||||||
$source = str($volume)->before(':');
|
$source = str($volume)->before(':');
|
||||||
$target = str($volume)->after(':')->beforeLast(':');
|
$target = str($volume)->after(':')->beforeLast(':');
|
||||||
@@ -3106,6 +3113,23 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
|
|||||||
$volumesParsed->put($index, $volume);
|
$volumesParsed->put($index, $volume);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($depends_on?->count() > 0) {
|
||||||
|
if ($isApplication && $isPullRequest) {
|
||||||
|
$newDependsOn = collect([]);
|
||||||
|
$depends_on->each(function ($dependency, $condition) use ($pullRequestId, $newDependsOn) {
|
||||||
|
if (is_numeric($condition)) {
|
||||||
|
$dependency = "$dependency-pr-$pullRequestId";
|
||||||
|
|
||||||
|
$newDependsOn->put($condition, $dependency);
|
||||||
|
} else {
|
||||||
|
$condition = "$condition-pr-$pullRequestId";
|
||||||
|
$newDependsOn->put($condition, $dependency);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
$depends_on = $newDependsOn;
|
||||||
|
}
|
||||||
|
}
|
||||||
if ($topLevel->get('networks')?->count() > 0) {
|
if ($topLevel->get('networks')?->count() > 0) {
|
||||||
foreach ($topLevel->get('networks') as $networkName => $network) {
|
foreach ($topLevel->get('networks') as $networkName => $network) {
|
||||||
if ($networkName === 'default') {
|
if ($networkName === 'default') {
|
||||||
@@ -3196,7 +3220,6 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
|
|||||||
// remove $environment from $allEnvironments
|
// remove $environment from $allEnvironments
|
||||||
$coolifyDefinedEnvironments = $allEnvironments->diffKeys($environment);
|
$coolifyDefinedEnvironments = $allEnvironments->diffKeys($environment);
|
||||||
|
|
||||||
|
|
||||||
// filter magic environments
|
// filter magic environments
|
||||||
$magicEnvironments = $environment->filter(function ($value, $key) {
|
$magicEnvironments = $environment->filter(function ($value, $key) {
|
||||||
$value = str(replaceVariables(str($value)));
|
$value = str(replaceVariables(str($value)));
|
||||||
@@ -3345,7 +3368,6 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
|
|||||||
$branch = "pull/{$pullRequestId}/head";
|
$branch = "pull/{$pullRequestId}/head";
|
||||||
}
|
}
|
||||||
if ($originalResource->environment_variables->where('key', 'COOLIFY_BRANCH')->isEmpty()) {
|
if ($originalResource->environment_variables->where('key', 'COOLIFY_BRANCH')->isEmpty()) {
|
||||||
ray($branch);
|
|
||||||
$environment->put('COOLIFY_BRANCH', $branch);
|
$environment->put('COOLIFY_BRANCH', $branch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3371,10 +3393,10 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
|
|||||||
$fqdns = collect([]);
|
$fqdns = collect([]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$fqdns = $fqdns->map(function ($fqdn) use ($pullRequestId) {
|
$fqdns = $fqdns->map(function ($fqdn) use ($pullRequestId, $resource) {
|
||||||
$preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->id, $pullRequestId);
|
$preview = ApplicationPreview::findPreviewByApplicationAndPullId($resource->id, $pullRequestId);
|
||||||
$url = Url::fromString($fqdn);
|
$url = Url::fromString($fqdn);
|
||||||
$template = $this->preview_url_template;
|
$template = $resource->preview_url_template;
|
||||||
$host = $url->getHost();
|
$host = $url->getHost();
|
||||||
$schema = $url->getScheme();
|
$schema = $url->getScheme();
|
||||||
$random = new Cuid2;
|
$random = new Cuid2;
|
||||||
@@ -3420,14 +3442,24 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
|
|||||||
if (! $isDatabase && $fqdns?->count() > 0) {
|
if (! $isDatabase && $fqdns?->count() > 0) {
|
||||||
if ($isApplication) {
|
if ($isApplication) {
|
||||||
$shouldGenerateLabelsExactly = $resource->destination->server->settings->generate_exact_labels;
|
$shouldGenerateLabelsExactly = $resource->destination->server->settings->generate_exact_labels;
|
||||||
|
$uuid = $resource->uuid;
|
||||||
|
$network = $resource->destination->network;
|
||||||
|
if ($isPullRequest) {
|
||||||
|
$uuid = "{$resource->uuid}-{$pullRequestId}";
|
||||||
|
}
|
||||||
|
if ($isPullRequest) {
|
||||||
|
$network = "{$resource->destination->network}-{$pullRequestId}";
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$shouldGenerateLabelsExactly = $resource->server->settings->generate_exact_labels;
|
$shouldGenerateLabelsExactly = $resource->server->settings->generate_exact_labels;
|
||||||
|
$uuid = $resource->uuid;
|
||||||
|
$network = $resource->server->destination->network;
|
||||||
}
|
}
|
||||||
if ($shouldGenerateLabelsExactly) {
|
if ($shouldGenerateLabelsExactly) {
|
||||||
switch ($server->proxyType()) {
|
switch ($server->proxyType()) {
|
||||||
case ProxyTypes::TRAEFIK->value:
|
case ProxyTypes::TRAEFIK->value:
|
||||||
$serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik(
|
$serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik(
|
||||||
uuid: $resource->uuid,
|
uuid: $uuid,
|
||||||
domains: $fqdns,
|
domains: $fqdns,
|
||||||
is_force_https_enabled: true,
|
is_force_https_enabled: true,
|
||||||
serviceLabels: $serviceLabels,
|
serviceLabels: $serviceLabels,
|
||||||
@@ -3439,8 +3471,8 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
|
|||||||
break;
|
break;
|
||||||
case ProxyTypes::CADDY->value:
|
case ProxyTypes::CADDY->value:
|
||||||
$serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy(
|
$serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy(
|
||||||
network: $resource->destination->network,
|
network: $network,
|
||||||
uuid: $resource->uuid,
|
uuid: $uuid,
|
||||||
domains: $fqdns,
|
domains: $fqdns,
|
||||||
is_force_https_enabled: true,
|
is_force_https_enabled: true,
|
||||||
serviceLabels: $serviceLabels,
|
serviceLabels: $serviceLabels,
|
||||||
@@ -3454,7 +3486,7 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik(
|
$serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik(
|
||||||
uuid: $resource->uuid,
|
uuid: $uuid,
|
||||||
domains: $fqdns,
|
domains: $fqdns,
|
||||||
is_force_https_enabled: true,
|
is_force_https_enabled: true,
|
||||||
serviceLabels: $serviceLabels,
|
serviceLabels: $serviceLabels,
|
||||||
@@ -3464,8 +3496,8 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
|
|||||||
image: $image
|
image: $image
|
||||||
));
|
));
|
||||||
$serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy(
|
$serviceLabels = $serviceLabels->merge(fqdnLabelsForCaddy(
|
||||||
network: $resource->destination->network,
|
network: $network,
|
||||||
uuid: $resource->uuid,
|
uuid: $uuid,
|
||||||
domains: $fqdns,
|
domains: $fqdns,
|
||||||
is_force_https_enabled: true,
|
is_force_https_enabled: true,
|
||||||
serviceLabels: $serviceLabels,
|
serviceLabels: $serviceLabels,
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Models\Application;
|
use App\Models\Application;
|
||||||
|
use App\Models\ApplicationPreview;
|
||||||
use App\Models\GithubApp;
|
use App\Models\GithubApp;
|
||||||
use App\Models\Server;
|
use App\Models\Server;
|
||||||
use App\Models\Service;
|
use App\Models\Service;
|
||||||
@@ -9,7 +10,6 @@ use Illuminate\Support\Collection;
|
|||||||
use Symfony\Component\Yaml\Yaml;
|
use Symfony\Component\Yaml\Yaml;
|
||||||
use Visus\Cuid2\Cuid2;
|
use Visus\Cuid2\Cuid2;
|
||||||
|
|
||||||
ray()->clearAll();
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
$this->applicationYaml = '
|
$this->applicationYaml = '
|
||||||
version: "3.8"
|
version: "3.8"
|
||||||
@@ -62,7 +62,8 @@ networks:
|
|||||||
'domain' => 'http://bcoowoookw0co4cok4sgc4k8.127.0.0.1.sslip.io',
|
'domain' => 'http://bcoowoookw0co4cok4sgc4k8.127.0.0.1.sslip.io',
|
||||||
],
|
],
|
||||||
]),
|
]),
|
||||||
'uuid' => 'bcoowoookw0co4cok4sgc4k8',
|
'preview_url_template' => '{{pr_id}}.{{domain}}',
|
||||||
|
'uuid' => 'bcoowoookw0co4cok4sgc4k8s',
|
||||||
'repository_project_id' => 603035348,
|
'repository_project_id' => 603035348,
|
||||||
'git_repository' => 'coollabsio/coolify-examples',
|
'git_repository' => 'coollabsio/coolify-examples',
|
||||||
'git_branch' => 'main',
|
'git_branch' => 'main',
|
||||||
@@ -77,6 +78,13 @@ networks:
|
|||||||
'source_id' => 1,
|
'source_id' => 1,
|
||||||
'source_type' => GithubApp::class,
|
'source_type' => GithubApp::class,
|
||||||
]);
|
]);
|
||||||
|
$this->application->environment_variables_preview()->where('key', 'APP_DEBUG')->update(['value' => 'true']);
|
||||||
|
$this->applicationPreview = ApplicationPreview::create([
|
||||||
|
'git_type' => 'github',
|
||||||
|
'application_id' => $this->application->id,
|
||||||
|
'pull_request_id' => 1,
|
||||||
|
'pull_request_html_url' => 'https://github.com/coollabsio/coolify-examples/pull/1',
|
||||||
|
]);
|
||||||
$this->serviceYaml = '
|
$this->serviceYaml = '
|
||||||
version: "3.8"
|
version: "3.8"
|
||||||
services:
|
services:
|
||||||
@@ -156,6 +164,7 @@ networks:
|
|||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
|
// $this->applicationPreview->forceDelete();
|
||||||
$this->application->forceDelete();
|
$this->application->forceDelete();
|
||||||
$this->service->forceDelete();
|
$this->service->forceDelete();
|
||||||
});
|
});
|
||||||
@@ -324,11 +333,10 @@ afterEach(function () {
|
|||||||
// });
|
// });
|
||||||
|
|
||||||
test('ServiceComposeParseNew', function () {
|
test('ServiceComposeParseNew', function () {
|
||||||
ray()->clearAll();
|
$output = newParser($this->application, pull_request_id: 1, preview_id: $this->applicationPreview->id);
|
||||||
$output = newParser($this->service);
|
|
||||||
// ray('New parser');
|
// ray('New parser');
|
||||||
// ray($output->toArray());
|
// ray($output->toArray());
|
||||||
ray($this->service->environment_variables->pluck('value', 'key')->toArray());
|
ray($this->service->environment_variables_preview->pluck('value', 'key')->toArray());
|
||||||
expect($output)->toBeInstanceOf(Collection::class);
|
expect($output)->toBeInstanceOf(Collection::class);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user