refactor containerports

Signed-off-by: Simon L <szaimen@e.mail.de>
This commit is contained in:
Simon L 2022-12-25 17:08:41 +01:00
parent 1194b7a1ff
commit 5dc9fad2d6
6 changed files with 103 additions and 47 deletions

View file

@ -41,7 +41,20 @@
"ports": { "ports": {
"type": "array", "type": "array",
"items": { "items": {
"type": "object",
"additionalProperties": false,
"minProperties": 3,
"properties": {
"ip_binding": {
"type": "string" "type": "string"
},
"port_number": {
"type": "string"
},
"protocol": {
"type": "string"
}
}
} }
}, },
"restart": { "restart": {

View file

@ -11,7 +11,11 @@
"display_name": "Apache", "display_name": "Apache",
"image": "nextcloud/aio-apache", "image": "nextcloud/aio-apache",
"ports": [ "ports": [
"%APACHE_PORT%/tcp" {
"ip_binding": "%APACHE_IP_BINDING%",
"port_number": "%APACHE_PORT%",
"protocol": "tcp"
}
], ],
"internal_port": "%APACHE_PORT%", "internal_port": "%APACHE_PORT%",
"secrets": [], "secrets": [],
@ -214,8 +218,16 @@
"display_name": "Talk", "display_name": "Talk",
"image": "nextcloud/aio-talk", "image": "nextcloud/aio-talk",
"ports": [ "ports": [
"%TALK_PORT%/tcp", {
"%TALK_PORT%/udp" "ip_binding": "",
"port_number": "%TALK_PORT%",
"protocol": "tcp"
},
{
"ip_binding": "",
"port_number": "%TALK_PORT%",
"protocol": "udp"
}
], ],
"internal_port": "%TALK_PORT%", "internal_port": "%TALK_PORT%",
"environment": [ "environment": [
@ -335,7 +347,11 @@
"display_name": "", "display_name": "",
"image": "nextcloud/aio-domaincheck", "image": "nextcloud/aio-domaincheck",
"ports": [ "ports": [
"%APACHE_PORT%/tcp" {
"ip_binding": "%APACHE_IP_BINDING%",
"port_number": "%APACHE_PORT%",
"protocol": "tcp"
}
], ],
"internal_port": "", "internal_port": "",
"environment": [ "environment": [

View file

@ -0,0 +1,19 @@
<?php
namespace AIO\Container;
class ContainerPort {
public string $port;
public string $ipBinding;
public bool $protocol;
public function __construct(
string $port,
string $ipBinding,
bool $protocol
) {
$this->port = $port;
$this->ipBinding = $ipBinding;
$this->protocol = $protocol;
}
}

View file

@ -3,15 +3,15 @@
namespace AIO\Container; namespace AIO\Container;
class ContainerPorts { class ContainerPorts {
/** @var string[] */ /** @var ContainerPort[] */
private array $ports = []; private array $ports = [];
public function AddPort(string $port) : void { public function AddPort(ContainerPort $port) : void {
$this->ports[] = $port; $this->ports[] = $port;
} }
/** /**
* @return string[] * @return ContainerPort[]
*/ */
public function GetPorts() : array { public function GetPorts() : array {
return $this->ports; return $this->ports;

View file

@ -4,6 +4,7 @@ namespace AIO;
use AIO\Container\Container; use AIO\Container\Container;
use AIO\Container\ContainerEnvironmentVariables; use AIO\Container\ContainerEnvironmentVariables;
use AIO\Container\ContainerPort;
use AIO\Container\ContainerPorts; use AIO\Container\ContainerPorts;
use AIO\Container\ContainerVolume; use AIO\Container\ContainerVolume;
use AIO\Container\ContainerVolumes; use AIO\Container\ContainerVolumes;
@ -75,21 +76,14 @@ class ContainerDefinitionFetcher
} }
$ports = new ContainerPorts(); $ports = new ContainerPorts();
foreach ($entry['ports'] as $port) { foreach ($entry['ports'] as $value) {
if($port === '%APACHE_PORT%/tcp') { $ports->AddPort(
$port = $this->configurationManager->GetApachePort() . '/tcp'; new ContainerPort(
} elseif($port === '%TALK_PORT%/tcp') { $value['port_number'],
$port = $this->configurationManager->GetTalkPort() . '/tcp'; $value['ip_binding'],
} elseif($port === '%TALK_PORT%/udp') { $value['protocol']
$port = $this->configurationManager->GetTalkPort() . '/udp'; )
} );
$ports->AddPort($port);
}
if($entry['internal_port'] === '%APACHE_PORT%') {
$entry['internal_port'] = $this->configurationManager->GetApachePort();
} elseif($entry['internal_port'] === '%TALK_PORT%') {
$entry['internal_port'] = $this->configurationManager->GetTalkPort();
} }
$volumes = new ContainerVolumes(); $volumes = new ContainerVolumes();

View file

@ -125,6 +125,12 @@ class DockerActionManager
$containerName = $container->GetIdentifier(); $containerName = $container->GetIdentifier();
$internalPort = $container->GetInternalPort(); $internalPort = $container->GetInternalPort();
if($internalPort === '%APACHE_PORT%') {
$internalPort = $this->configurationManager->GetApachePort();
} elseif($internalPort === '%TALK_PORT%') {
$internalPort = $this->configurationManager->GetTalkPort();
}
if ($internalPort !== "" && $internalPort !== 'host') { if ($internalPort !== "" && $internalPort !== 'host') {
$connection = @fsockopen($containerName, (int)$internalPort, $errno, $errstr, 0.1); $connection = @fsockopen($containerName, (int)$internalPort, $errno, $errstr, 0.1);
if ($connection) { if ($connection) {
@ -216,13 +222,6 @@ class DockerActionManager
$volumes[] = $volumeEntry; $volumes[] = $volumeEntry;
} }
$exposedPorts = [];
if ($container->GetInternalPort() !== 'host') {
foreach($container->GetPorts()->GetPorts() as $port) {
$exposedPorts[$port] = null;
}
}
$requestBody = [ $requestBody = [
'Image' => $this->BuildImageName($container), 'Image' => $this->BuildImageName($container),
]; ];
@ -359,25 +358,40 @@ class DockerActionManager
$requestBody['HostConfig']['RestartPolicy']['Name'] = $container->GetRestartPolicy(); $requestBody['HostConfig']['RestartPolicy']['Name'] = $container->GetRestartPolicy();
if(count($exposedPorts) > 0) { $exposedPorts = [];
$requestBody['ExposedPorts'] = $exposedPorts; if ($container->GetInternalPort() !== 'host') {
foreach ($container->GetPorts()->GetPorts() as $port) { foreach($container->GetPorts()->GetPorts() as $value) {
$portNumber = explode("/", $port); $exposedPorts[$value->port] = null;
if ($this->configurationManager->GetApachePort() === $portNumber[0] && $this->configurationManager->GetApacheIPBinding() !== '') {
$requestBody['HostConfig']['PortBindings'][$port] = [
[
'HostPort' => $portNumber[0],
'HostIp' => $this->configurationManager->GetApacheIPBinding(),
]
];
} else {
$requestBody['HostConfig']['PortBindings'][$port] = [
[
'HostPort' => $portNumber[0],
]
];
} }
} }
if(count($exposedPorts) > 0) {
foreach ($container->GetPorts()->GetPorts() as $value) {
$port = $value->port;
if($port === '%APACHE_PORT%') {
$port = $this->configurationManager->GetApachePort();
} elseif($port === '%TALK_PORT%') {
$port = $this->configurationManager->GetTalkPort();
}
$ipBinding = $value->ipBinding;
if($ipBinding === '%APACHE_IP_BINDING%') {
$ipBinding = $this->configurationManager->GetApacheIPBinding();
}
if ($ipBinding === '') {
$ipBinding = '0.0.0.0';
}
$protocol = $value->protocol;
$portWithProtocol = $port . '/' . $protocol;
$requestBody['ExposedPorts'][$portWithProtocol] = null;
$requestBody['HostConfig']['PortBindings'][$port] = [
[
'HostPort' => $port,
'HostIp' => $ipBinding,
]
];
}
} }
// Special things for the backup container which should not be exposed in the containers.json // Special things for the backup container which should not be exposed in the containers.json