Merge pull request #1635 from nextcloud/enh/noid/refactor-container-ports

refactor containerports
This commit is contained in:
Simon L 2022-12-30 23:15:35 +01:00 committed by GitHub
commit eaabc8e4b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 111 additions and 49 deletions

View file

@ -4,6 +4,10 @@ jq -c . ./php/containers.json > /tmp/containers.json
sed -i 's|","destination":"|:|g' /tmp/containers.json
sed -i 's|","writeable":false|:ro"|g' /tmp/containers.json
sed -i 's|","writeable":true|:rw"|g' /tmp/containers.json
sed -i 's|","port_number":"|:|g' /tmp/containers.json
sed -i 's|","protocol":"|/|g' /tmp/containers.json
sed -i 's|"ip_binding":":|"ip_binding":"|g' /tmp/containers.json
cat /tmp/containers.json
OUTPUT="$(cat /tmp/containers.json)"
OUTPUT="$(echo "$OUTPUT" | jq 'del(.services[].internal_port)')"
OUTPUT="$(echo "$OUTPUT" | jq 'del(.services[].secrets)')"
@ -21,13 +25,14 @@ sed -i '/display_name:/d' containers.yml
sed -i '/stop_grace_period:/s/$/s/' containers.yml
sed -i '/: \[\]/d' containers.yml
sed -i 's|- source: |- |' containers.yml
sed -i 's|- ip_binding: |- |' containers.yml
TCP="$(grep -oP '[%A-Z0-9_]+/tcp' containers.yml | sort -u)"
mapfile -t TCP <<< "$TCP"
for port in "${TCP[@]}"
do
solve_port="${port%%/tcp}"
sed -i "s|$port|$solve_port:$solve_port/tcp|" containers.yml
sed -i "s|$solve_port/tcp|$solve_port:$solve_port/tcp|" containers.yml
done
UDP="$(grep -oP '[%A-Z0-9_]+/udp' containers.yml | sort -u)"
@ -35,7 +40,7 @@ mapfile -t UDP <<< "$UDP"
for port in "${UDP[@]}"
do
solve_port="${port%%/udp}"
sed -i "s|$port|$solve_port:$solve_port/udp|" containers.yml
sed -i "s|$solve_port/udp|$solve_port:$solve_port/udp|" containers.yml
done
rm -f sample.conf
@ -64,6 +69,7 @@ sed -i 's|NEXTCLOUD_MAX_TIME=|NEXTCLOUD_MAX_TIME=3600 # This allows to
sed -i 's|NEXTCLOUD_TRUSTED_CACERTS_DIR=|NEXTCLOUD_TRUSTED_CACERTS_DIR=/usr/local/share/ca-certificates/my-custom-ca # Nextcloud container will trust all the Certification Authorities, whose certificates are included in the given directory.|' sample.conf
sed -i 's|UPDATE_NEXTCLOUD_APPS=|UPDATE_NEXTCLOUD_APPS=no # When setting to yes, it will automatically update all installed Nextcloud apps upon container startup on saturdays.|' sample.conf
sed -i 's|APACHE_PORT=|APACHE_PORT=443 # Changing this to a different value than 443 will allow you to run it behind a reverse proxy.|' sample.conf
sed -i 's|APACHE_IP_BINDING=|APACHE_IP_BINDING=0.0.0.0 # This can be changed to e.g. 127.0.0.1 if you want to run AIO behind a reverse proxy and if that is running on the same host and using localhost to connect|' sample.conf
sed -i 's|TALK_PORT=|TALK_PORT=3478 # This allows to adjust the port that the talk container is using.|' sample.conf
sed -i 's|AIO_TOKEN=|AIO_TOKEN=123456 # Has no function but needs to be set!|' sample.conf
sed -i 's|AIO_URL=|AIO_URL=localhost # Has no function but needs to be set!|' sample.conf

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,
]
];
}
}