This commit is contained in:
Andras Bacsai 2023-10-02 15:51:06 +02:00
parent e18766ec21
commit 7d1a76570c
3 changed files with 54 additions and 7 deletions

View File

@ -10,6 +10,7 @@ class StartService
use AsAction; use AsAction;
public function handle(Service $service) public function handle(Service $service)
{ {
$network = $service->destination->network;
$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()}.'";
@ -21,6 +22,7 @@ public function handle(Service $service)
$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";
$commands[] = "docker network connect $service->uuid coolify-proxy 2>/dev/null || true"; $commands[] = "docker network connect $service->uuid coolify-proxy 2>/dev/null || true";
$commands[] = "docker network connect $network --alias $service->name-$service->uuid $service->name-$service->uuid 2>/dev/null || true";
$activity = remote_process($commands, $service->server); $activity = remote_process($commands, $service->server);
return $activity; return $activity;
} }

View File

@ -63,6 +63,10 @@ public function databases()
{ {
return $this->hasMany(ServiceDatabase::class); return $this->hasMany(ServiceDatabase::class);
} }
public function destination()
{
return $this->morphTo();
}
public function environment() public function environment()
{ {
return $this->belongsTo(Environment::class); return $this->belongsTo(Environment::class);
@ -124,9 +128,16 @@ public function parse(bool $isNew = false): Collection
$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');
$definedNetwork = $this->uuid;
$generatedServiceFQDNS = collect([]); $generatedServiceFQDNS = collect([]);
if (is_null($this->destination)) {
$destination = $this->server->destinations()->first();
if ($destination) {
$this->destination()->associate($destination);
$this->save();
}
}
$definedNetwork = collect([$this->uuid]);
$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) {
$serviceVolumes = collect(data_get($service, 'volumes', [])); $serviceVolumes = collect(data_get($service, 'volumes', []));
@ -237,13 +248,19 @@ public function parse(bool $isNew = false): Collection
return $value == $definedNetwork; return $value == $definedNetwork;
}); });
if (!$definedNetworkExists) { if (!$definedNetworkExists) {
$topLevelNetworks->put($definedNetwork, [ foreach ($definedNetwork as $network) {
'name' => $definedNetwork, $topLevelNetworks->put($network, [
'external' => true 'name' => $network,
]); 'external' => true
]);
}
} }
$networks = $serviceNetworks->toArray(); $networks = $serviceNetworks->toArray();
$networks = array_merge($networks, [$definedNetwork]); foreach ($definedNetwork as $key => $network) {
$networks = array_merge($networks, [
$network => null
]);
}
data_set($service, 'networks', $networks); data_set($service, 'networks', $networks);
// Collect/create/update volumes // Collect/create/update volumes
@ -308,7 +325,7 @@ public function parse(bool $isNew = false): Collection
$target = Str::of($volume)->after(':')->beforeLast(':'); $target = Str::of($volume)->after(':')->beforeLast(':');
$source = $name; $source = $name;
$volume = "$source:$target"; $volume = "$source:$target";
} else if(is_array($volume)) { } else if (is_array($volume)) {
data_set($volume, 'source', $name); data_set($volume, 'source', $name);
} }
$topLevelVolumes->put($name, null); $topLevelVolumes->put($name, null);

View File

@ -0,0 +1,28 @@
<?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');
});
}
};