Skip to content

Commit

Permalink
Merge pull request coollabsio#3374 from Vahor/host-network
Browse files Browse the repository at this point in the history
fix: don't add `networks` key if `network_mode` is used
  • Loading branch information
andrasbacsai authored Sep 10, 2024
2 parents f8dfe64 + 66cf6df commit 1e68444
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 48 deletions.
6 changes: 4 additions & 2 deletions app/Actions/Service/StartService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public function handle(Service $service)
$service->saveComposeConfigs();
$commands[] = 'cd '.$service->workdir();
$commands[] = "echo 'Saved configuration files to {$service->workdir()}.'";
$commands[] = "echo 'Creating Docker network.'";
$commands[] = "docker network inspect $service->uuid >/dev/null 2>&1 || docker network create --attachable $service->uuid";
if($service->networks()->count() > 0){
$commands[] = "echo 'Creating Docker network.'";
$commands[] = "docker network inspect $service->uuid >/dev/null 2>&1 || docker network create --attachable $service->uuid";
}
$commands[] = 'echo Starting service.';
$commands[] = "echo 'Pulling images.'";
$commands[] = 'docker compose pull';
Expand Down
99 changes: 53 additions & 46 deletions bootstrap/helpers/shared.php
Original file line number Diff line number Diff line change
Expand Up @@ -3109,6 +3109,7 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
}
$volumes = collect(data_get($service, 'volumes', []));
$networks = collect(data_get($service, 'networks', []));
$use_network_mode = data_get($service, 'network_mode') !== null;
$depends_on = collect(data_get($service, 'depends_on', []));
$labels = collect(data_get($service, 'labels', []));
$environment = collect(data_get($service, 'environment', []));
Expand Down Expand Up @@ -3308,32 +3309,34 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
$depends_on = $newDependsOn;
}
}
if ($topLevel->get('networks')?->count() > 0) {
foreach ($topLevel->get('networks') as $networkName => $network) {
if ($networkName === 'default') {
continue;
}
// ignore aliases
if ($network['aliases'] ?? false) {
continue;
}
$networkExists = $networks->contains(function ($value, $key) use ($networkName) {
return $value == $networkName || $key == $networkName;
});
if (! $networkExists) {
$networks->put($networkName, null);
if(!$use_network_mode) {
if ($topLevel->get('networks')?->count() > 0) {
foreach ($topLevel->get('networks') as $networkName => $network) {
if ($networkName === 'default') {
continue;
}
// ignore aliases
if ($network['aliases'] ?? false) {
continue;
}
$networkExists = $networks->contains(function ($value, $key) use ($networkName) {
return $value == $networkName || $key == $networkName;
});
if (! $networkExists) {
$networks->put($networkName, null);
}
}
}
}
$baseNetworkExists = $networks->contains(function ($value, $_) use ($baseNetwork) {
return $value == $baseNetwork;
});
if (! $baseNetworkExists) {
foreach ($baseNetwork as $network) {
$topLevel->get('networks')->put($network, [
'name' => $network,
'external' => true,
]);
$baseNetworkExists = $networks->contains(function ($value, $_) use ($baseNetwork) {
return $value == $baseNetwork;
});
if (! $baseNetworkExists) {
foreach ($baseNetwork as $network) {
$topLevel->get('networks')->put($network, [
'name' => $network,
'external' => true,
]);
}
}
}

Expand All @@ -3359,30 +3362,32 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int

$networks_temp = collect();

foreach ($networks as $key => $network) {
if (gettype($network) === 'string') {
// networks:
// - appwrite
if(!$use_network_mode) {
foreach ($networks as $key => $network) {
if (gettype($network) === 'string') {
// networks:
// - appwrite
$networks_temp->put($network, null);
} elseif (gettype($network) === 'array') {
// networks:
// default:
// ipv4_address: 192.168.203.254
$networks_temp->put($key, $network);
}
}
foreach ($baseNetwork as $key => $network) {
$networks_temp->put($network, null);
} elseif (gettype($network) === 'array') {
// networks:
// default:
// ipv4_address: 192.168.203.254
$networks_temp->put($key, $network);
}
}
foreach ($baseNetwork as $key => $network) {
$networks_temp->put($network, null);
}

if ($isApplication) {
if (data_get($resource, 'settings.connect_to_docker_network')) {
$network = $resource->destination->network;
$networks_temp->put($network, null);
$topLevel->get('networks')->put($network, [
'name' => $network,
'external' => true,
]);
if ($isApplication) {
if (data_get($resource, 'settings.connect_to_docker_network')) {
$network = $resource->destination->network;
$networks_temp->put($network, null);
$topLevel->get('networks')->put($network, [
'name' => $network,
'external' => true,
]);
}
}
}

Expand Down Expand Up @@ -3623,9 +3628,11 @@ function newParser(Application|Service $resource, int $pull_request_id = 0, ?int
$payload = collect($service)->merge([
'container_name' => $containerName,
'restart' => $restart->value(),
'networks' => $networks_temp,
'labels' => $serviceLabels,
]);
if(!$use_network_mode){
$payload['networks'] = $networks_temp;
}
if ($ports->count() > 0) {
$payload['ports'] = $ports;
}
Expand Down

0 comments on commit 1e68444

Please sign in to comment.