Revert "fix: services should have destination as well"

This reverts commit 9ab5a1f7bd.
This commit is contained in:
Andras Bacsai 2023-10-01 15:31:25 +02:00
parent 9ab5a1f7bd
commit b3c8c881b7
5 changed files with 9 additions and 78 deletions

View File

@ -13,20 +13,14 @@ public function handle(Service $service)
$service->saveComposeConfigs(); $service->saveComposeConfigs();
$commands[] = "cd " . $service->workdir(); $commands[] = "cd " . $service->workdir();
$commands[] = "echo '####### Saved configuration files to {$service->workdir()}.'"; $commands[] = "echo '####### Saved configuration files to {$service->workdir()}.'";
if (is_null($service->destination)) { $commands[] = "echo '####### Creating Docker network.'";
$dockerNetwork = $service->uuid; $commands[] = "docker network create --attachable {$service->uuid} >/dev/null 2>/dev/null || true";
$commands[] = "echo '####### Creating Docker network.'";
$commands[] = "docker network create --attachable {$dockerNetwork} >/dev/null 2>/dev/null || true";
}
$commands[] = "echo '####### Starting service {$service->name} on {$service->server->name}.'"; $commands[] = "echo '####### Starting service {$service->name} on {$service->server->name}.'";
$commands[] = "echo '####### Pulling images.'"; $commands[] = "echo '####### Pulling images.'";
$commands[] = "docker compose pull"; $commands[] = "docker compose pull";
$commands[] = "echo '####### Starting containers.'"; $commands[] = "echo '####### Starting containers.'";
$commands[] = "docker compose up -d --remove-orphans --force-recreate"; $commands[] = "docker compose up -d --remove-orphans --force-recreate";
if (is_null($service->destination)) { $commands[] = "docker network connect $service->uuid coolify-proxy 2>/dev/null || true";
$commands[] = "echo '####### Connecting to proxy network.'";
$commands[] = "docker network connect coolify-proxy {$dockerNetwork} 2>/dev/null || true";
}
$activity = remote_process($commands, $service->server); $activity = remote_process($commands, $service->server);
return $activity; return $activity;
} }

View File

@ -20,9 +20,7 @@ public function handle(Service $service)
instant_remote_process(["docker rm -f {$db->name}-{$service->uuid}"], $service->server); instant_remote_process(["docker rm -f {$db->name}-{$service->uuid}"], $service->server);
$db->update(['status' => 'exited']); $db->update(['status' => 'exited']);
} }
if (is_null($service->destination)) { instant_remote_process(["docker network disconnect {$service->uuid} coolify-proxy 2>/dev/null"], $service->server, false);
instant_remote_process(["docker network disconnect {$service->uuid} coolify-proxy 2>/dev/null"], $service->server, false); instant_remote_process(["docker network rm {$service->uuid} 2>/dev/null"], $service->server, false);
instant_remote_process(["docker network rm {$service->uuid} 2>/dev/null"], $service->server, false);
}
} }
} }

View File

@ -6,7 +6,7 @@
use App\Models\Project; use App\Models\Project;
use App\Models\Server; use App\Models\Server;
use App\Models\Service; use App\Models\Service;
use App\Models\StandaloneDocker; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class ProjectController extends Controller class ProjectController extends Controller
@ -66,22 +66,20 @@ public function new()
'database_uuid' => $standalone_postgresql->uuid, 'database_uuid' => $standalone_postgresql->uuid,
]); ]);
} }
if ($type->startsWith('one-click-service-') && !is_null((int)$server_id)) { if ($type->startsWith('one-click-service-') && !is_null( (int)$server_id)) {
$oneClickServiceName = $type->after('one-click-service-')->value(); $oneClickServiceName = $type->after('one-click-service-')->value();
$oneClickService = data_get($services, "$oneClickServiceName.compose"); $oneClickService = data_get($services, "$oneClickServiceName.compose");
ray($oneClickServiceName);
$oneClickDotEnvs = data_get($services, "$oneClickServiceName.envs", null); $oneClickDotEnvs = data_get($services, "$oneClickServiceName.envs", null);
if ($oneClickDotEnvs) { if ($oneClickDotEnvs) {
$oneClickDotEnvs = Str::of(base64_decode($oneClickDotEnvs))->split('/\r\n|\r|\n/'); $oneClickDotEnvs = Str::of(base64_decode($oneClickDotEnvs))->split('/\r\n|\r|\n/');
} }
if ($oneClickService) { if ($oneClickService) {
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
$service = Service::create([ $service = Service::create([
'name' => "$oneClickServiceName-" . Str::random(10), 'name' => "$oneClickServiceName-" . Str::random(10),
'docker_compose_raw' => base64_decode($oneClickService), 'docker_compose_raw' => base64_decode($oneClickService),
'environment_id' => $environment->id, 'environment_id' => $environment->id,
'server_id' => (int) $server_id, 'server_id' => (int) $server_id,
'destination_id' => (int) $destination->id,
'destination_type' => $destination->getMorphClass(),
]); ]);
$service->name = "$oneClickServiceName-" . $service->uuid; $service->name = "$oneClickServiceName-" . $service->uuid;
$service->save(); $service->save();

View File

@ -59,10 +59,6 @@ public function applications()
{ {
return $this->hasMany(ServiceApplication::class); return $this->hasMany(ServiceApplication::class);
} }
public function destination()
{
return $this->morphTo();
}
public function databases() public function databases()
{ {
return $this->hasMany(ServiceDatabase::class); return $this->hasMany(ServiceDatabase::class);
@ -124,23 +120,15 @@ public function parse(bool $isNew = false): Collection
throw new \Exception($e->getMessage()); throw new \Exception($e->getMessage());
} }
if ($this->server->destinations()->count() === 1) {
$this->destination()->associate($this->server->destinations()->first());
}
$topLevelVolumes = collect(data_get($yaml, 'volumes', [])); $topLevelVolumes = collect(data_get($yaml, 'volumes', []));
$topLevelNetworks = collect(data_get($yaml, 'networks', [])); $topLevelNetworks = collect(data_get($yaml, 'networks', []));
$dockerComposeVersion = data_get($yaml, 'version') ?? '3.8'; $dockerComposeVersion = data_get($yaml, 'version') ?? '3.8';
$services = data_get($yaml, 'services'); $services = data_get($yaml, 'services');
if ($this->destination) { $definedNetwork = $this->uuid;
$definedNetwork = $this->destination->network;
} else {
$definedNetwork = $this->uuid;
}
$generatedServiceFQDNS = collect([]); $generatedServiceFQDNS = collect([]);
$services = collect($services)->map(function ($service, $serviceName) use ($topLevelVolumes, $topLevelNetworks, $definedNetwork, $isNew, $generatedServiceFQDNS) { $services = collect($services)->map(function ($service, $serviceName) use ($topLevelVolumes, $topLevelNetworks, $definedNetwork, $isNew, $generatedServiceFQDNS) {
$serviceName = Str::of($serviceName)->before("-$this->uuid")->value;
$serviceVolumes = collect(data_get($service, 'volumes', [])); $serviceVolumes = collect(data_get($service, 'volumes', []));
$servicePorts = collect(data_get($service, 'ports', [])); $servicePorts = collect(data_get($service, 'ports', []));
$serviceNetworks = collect(data_get($service, 'networks', [])); $serviceNetworks = collect(data_get($service, 'networks', []));
@ -505,25 +493,6 @@ public function parse(bool $isNew = false): Collection
data_set($service, 'environment', $withoutServiceEnvs->toArray()); data_set($service, 'environment', $withoutServiceEnvs->toArray());
return $service; return $service;
}); });
$services = $services->mapWithKeys(function ($service, $serviceName) {
if (!Str::of($serviceName)->contains($this->uuid)) {
$newServiceName = $serviceName . '-' . $this->uuid;
return [$newServiceName => $service];
}
return [$serviceName => $service];
});
$yaml = collect($yaml);
$yamlServices = collect(data_get($yaml, 'services', []));
$yamlServices = $yamlServices->mapWithKeys(function ($service, $serviceName) {
if (!Str::of($serviceName)->contains($this->uuid)) {
$newServiceName = $serviceName . '-' . $this->uuid;
return [$newServiceName => $service];
}
return [$serviceName => $service];
});
$yaml->put('services', $yamlServices->toArray());
$yaml = $yaml->toArray();
$finalServices = [ $finalServices = [
'version' => $dockerComposeVersion, 'version' => $dockerComposeVersion,
'services' => $services->toArray(), 'services' => $services->toArray(),

View File

@ -1,28 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('services', function (Blueprint $table) {
$table->nullableMorphs('destination');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('services', function (Blueprint $table) {
$table->dropMorphs('destination');
});
}
};