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;
public function handle(Service $service)
{
$network = $service->destination->network;
$service->saveComposeConfigs();
$commands[] = "cd " . $service->workdir();
$commands[] = "echo '####### Saved configuration files to {$service->workdir()}.'";
@ -21,6 +22,7 @@ public function handle(Service $service)
$commands[] = "echo '####### Starting containers.'";
$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 $network --alias $service->name-$service->uuid $service->name-$service->uuid 2>/dev/null || true";
$activity = remote_process($commands, $service->server);
return $activity;
}

View File

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