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": {
"type": "array",
"items": {
"type": "string"
"type": "object",
"additionalProperties": false,
"minProperties": 3,
"properties": {
"ip_binding": {
"type": "string"
},
"port_number": {
"type": "string"
},
"protocol": {
"type": "string"
}
}
}
},
"restart": {

View file

@ -11,7 +11,11 @@
"display_name": "Apache",
"image": "nextcloud/aio-apache",
"ports": [
"%APACHE_PORT%/tcp"
{
"ip_binding": "%APACHE_IP_BINDING%",
"port_number": "%APACHE_PORT%",
"protocol": "tcp"
}
],
"internal_port": "%APACHE_PORT%",
"secrets": [],
@ -214,8 +218,16 @@
"display_name": "Talk",
"image": "nextcloud/aio-talk",
"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%",
"environment": [
@ -335,7 +347,11 @@
"display_name": "",
"image": "nextcloud/aio-domaincheck",
"ports": [
"%APACHE_PORT%/tcp"
{
"ip_binding": "%APACHE_IP_BINDING%",
"port_number": "%APACHE_PORT%",
"protocol": "tcp"
}
],
"internal_port": "",
"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,17 +3,17 @@
namespace AIO\Container;
class ContainerPorts {
/** @var string[] */
/** @var ContainerPort[] */
private array $ports = [];
public function AddPort(string $port) : void {
public function AddPort(ContainerPort $port) : void {
$this->ports[] = $port;
}
/**
* @return string[]
* @return ContainerPort[]
*/
public function GetPorts() : array {
return $this->ports;
}
}
}

View file

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

View file

@ -125,6 +125,12 @@ class DockerActionManager
$containerName = $container->GetIdentifier();
$internalPort = $container->GetInternalPort();
if($internalPort === '%APACHE_PORT%') {
$internalPort = $this->configurationManager->GetApachePort();
} elseif($internalPort === '%TALK_PORT%') {
$internalPort = $this->configurationManager->GetTalkPort();
}
if ($internalPort !== "" && $internalPort !== 'host') {
$connection = @fsockopen($containerName, (int)$internalPort, $errno, $errstr, 0.1);
if ($connection) {
@ -216,13 +222,6 @@ class DockerActionManager
$volumes[] = $volumeEntry;
}
$exposedPorts = [];
if ($container->GetInternalPort() !== 'host') {
foreach($container->GetPorts()->GetPorts() as $port) {
$exposedPorts[$port] = null;
}
}
$requestBody = [
'Image' => $this->BuildImageName($container),
];
@ -358,25 +357,40 @@ class DockerActionManager
}
$requestBody['HostConfig']['RestartPolicy']['Name'] = $container->GetRestartPolicy();
$exposedPorts = [];
if ($container->GetInternalPort() !== 'host') {
foreach($container->GetPorts()->GetPorts() as $value) {
$exposedPorts[$value->port] = null;
}
}
if(count($exposedPorts) > 0) {
$requestBody['ExposedPorts'] = $exposedPorts;
foreach ($container->GetPorts()->GetPorts() as $port) {
$portNumber = explode("/", $port);
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],
]
];
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,
]
];
}
}