Skip to content

Commit 9b5cfb4

Browse files
authored
MCLOUD-7259 Ability to disable TLS container from build:custom:compose (#18)
1 parent 1ee8870 commit 9b5cfb4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1567
-23
lines changed

images/nginx/1.19/Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ ENV MAGENTO_ROOT /app
1010
ENV MAGENTO_RUN_MODE production
1111
ENV MFTF_UTILS 0
1212
ENV DEBUG false
13+
ENV NGINX_WORKER_PROCESSES 1
14+
ENV NGINX_WORKER_CONNECTIONS 1024
1315

1416
COPY etc/nginx.conf /etc/nginx/
1517
COPY etc/vhost.conf /etc/nginx/conf.d/default.conf

images/nginx/1.19/docker-entrypoint.sh

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ XDEBUG_UPSTREAM_FILE="/etc/nginx/conf.d/xdebug/upstream.conf"
1818
[ ! -z "${UPLOAD_MAX_FILESIZE}" ] && sed -i "s/!UPLOAD_MAX_FILESIZE!/${UPLOAD_MAX_FILESIZE}/" $VHOST_FILE
1919
[ ! -z "${WITH_XDEBUG}" ] && sed -i "s/!WITH_XDEBUG!/${WITH_XDEBUG}/" $VHOST_FILE
2020
[ "${WITH_XDEBUG}" == "1" ] && sed -i "s/#include_xdebug_upstream/include/" $NGINX_FILE
21+
[ ! -z "${NGINX_WORKER_PROCESSES}" ] && sed -i "s/!NGINX_WORKER_PROCESSES!/${NGINX_WORKER_PROCESSES}/" $NGINX_FILE
22+
[ ! -z "${NGINX_WORKER_CONNECTIONS}" ] && sed -i "s/!NGINX_WORKER_CONNECTIONS!/${NGINX_WORKER_CONNECTIONS}/" $NGINX_FILE
2123

2224
# Check if the nginx syntax is fine, then launch.
2325
nginx -t

images/nginx/1.19/etc/nginx.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
worker_processes 2;
1+
worker_processes !NGINX_WORKER_PROCESSES!;
22

33
error_log /var/log/nginx/error.log debug;
44
pid /var/run/nginx.pid;
55

66
events {
77
# this should be equal to value of "ulimit -n"
88
# reference: https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration
9-
worker_connections 1048576;
9+
worker_connections !NGINX_WORKER_CONNECTIONS!;
1010
}
1111

1212
http {

src/Command/BuildCompose.php

+17
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ protected function configure(): void
186186
null,
187187
InputOption::VALUE_NONE,
188188
'Sets host.docker.internal for fpm_xdebug container to resolve debug issue for LINUX system'
189+
)->addOption(
190+
Source\CliSource::OPTION_NGINX_WORKER_PROCESSES,
191+
null,
192+
InputOption::VALUE_OPTIONAL,
193+
'The number of NGINX worker processes',
194+
Source\BaseSource::DEFAULT_NGINX_WORKER_PROCESSES
195+
)->addOption(
196+
Source\CliSource::OPTION_NGINX_WORKER_CONNECTIONS,
197+
null,
198+
InputOption::VALUE_OPTIONAL,
199+
'The maximum number of connections that each worker process can handle simultaneously',
200+
Source\BaseSource::DEFAULT_NGINX_WORKER_CONNECTIONS
189201
);
190202

191203
$this->addOption(
@@ -261,6 +273,11 @@ protected function configure(): void
261273
null,
262274
InputOption::VALUE_REQUIRED,
263275
'Port'
276+
)->addOption(
277+
Source\CliSource::OPTION_NO_TLS,
278+
null,
279+
InputOption::VALUE_NONE,
280+
'Disable TLS'
264281
)->addOption(
265282
Source\CliSource::OPTION_TLS_PORT,
266283
null,

src/Compose/ProductionBuilder.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\CloudDocker\Compose;
99

10+
use Magento\CloudDocker\App\ConfigurationMismatchException;
11+
use Magento\CloudDocker\Compose\ProductionBuilder\ServiceBuilderInterface;
1012
use Magento\CloudDocker\Compose\ProductionBuilder\ServicePool;
1113
use Magento\CloudDocker\Compose\ProductionBuilder\VolumeResolver;
1214
use Magento\CloudDocker\Config\Config;
@@ -35,7 +37,6 @@ class ProductionBuilder implements BuilderInterface
3537
self::SERVICE_GENERIC,
3638
self::SERVICE_DEPLOY,
3739
self::SERVICE_BUILD,
38-
self::SERVICE_TLS,
3940
self::SERVICE_WEB,
4041
self::SERVICE_FPM,
4142
self::SERVICE_DB,
@@ -91,7 +92,7 @@ public function build(Config $config): Manager
9192
$manager = $this->managerFactory->create($config);
9293

9394
foreach ($this->servicePool->getServices() as $service) {
94-
if ($config->hasServiceEnabled($service->getServiceName())
95+
if ($this->hasServiceEnabled($service, $config)
9596
|| in_array($service->getName(), self::$requiredServices)
9697
) {
9798
$manager->addService($service);
@@ -135,4 +136,24 @@ public function getPath(): string
135136
{
136137
return $this->fileList->getMagentoDockerCompose();
137138
}
139+
140+
/**
141+
* Checks the availability of the service
142+
*
143+
* @param ServiceBuilderInterface $service
144+
* @param Config $config
145+
* @return bool
146+
* @throws ConfigurationMismatchException
147+
*/
148+
private function hasServiceEnabled(ServiceBuilderInterface $service, Config $config): bool
149+
{
150+
$serviceNames = [
151+
BuilderInterface::SERVICE_FPM_XDEBUG,
152+
BuilderInterface::SERVICE_DB_QUOTE,
153+
BuilderInterface::SERVICE_DB_SALES,
154+
];
155+
$service = in_array($service->getName(), $serviceNames) ? $service->getServiceName() : $service->getName();
156+
157+
return $config->hasServiceEnabled($service);
158+
}
138159
}

src/Compose/ProductionBuilder/Service/Tls.php

+15-10
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function getConfig(Config $config): array
6262
'aliases' => [$config->getHost()]
6363
]
6464
],
65-
'environment' => $this->getBackendServiceUpstream($config),
65+
'environment' => $this->getEnvironmentVariables($config),
6666
'ports' => [
6767
$config->getPort() . ':80',
6868
$config->getTlsPort() . ':443'
@@ -106,18 +106,23 @@ private function getBackendService(Config $config): string
106106
* @return string[]
107107
* @throws ConfigurationMismatchException
108108
*/
109-
private function getBackendServiceUpstream(Config $config): array
109+
private function getEnvironmentVariables(Config $config): array
110110
{
111+
$envVariables = [
112+
'NGINX_WORKER_PROCESSES=' . $config->getNginxWorkerProcesses(),
113+
'NGINX_WORKER_CONNECTIONS=' . $config->getNginxWorkerConnections(),
114+
];
115+
111116
if ($config->hasServiceEnabled(ServiceInterface::SERVICE_VARNISH)) {
112-
return [
113-
'UPSTREAM_HOST=' . BuilderInterface::SERVICE_VARNISH,
114-
'UPSTREAM_PORT=80'
115-
];
117+
$envVariables[] = 'UPSTREAM_HOST=' . BuilderInterface::SERVICE_VARNISH;
118+
$envVariables[] = 'UPSTREAM_PORT=80';
119+
120+
return $envVariables;
116121
}
117122

118-
return [
119-
'UPSTREAM_HOST=' . BuilderInterface::SERVICE_WEB,
120-
'UPSTREAM_PORT=8080'
121-
];
123+
$envVariables[] = 'UPSTREAM_HOST=' . BuilderInterface::SERVICE_WEB;
124+
$envVariables[] = 'UPSTREAM_PORT=8080';
125+
126+
return $envVariables;
122127
}
123128
}

src/Compose/ProductionBuilder/Service/Varnish.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\CloudDocker\Compose\ProductionBuilder\ServiceBuilderInterface;
1212
use Magento\CloudDocker\Config\Config;
1313
use Magento\CloudDocker\Service\ServiceFactory;
14+
use Magento\CloudDocker\Service\ServiceInterface;
1415

1516
/**
1617
* Returns Varnish service configuration
@@ -51,13 +52,24 @@ public function getServiceName(): string
5152
*/
5253
public function getConfig(Config $config): array
5354
{
54-
return $this->serviceFactory->create(
55+
$result = $this->serviceFactory->create(
5556
$this->getServiceName(),
5657
$config->getServiceVersion($this->getServiceName()),
5758
[],
5859
$config->getServiceImage($this->getServiceName()),
5960
$config->getServiceImagePattern($this->getServiceName())
6061
);
62+
63+
if (!$config->hasServiceEnabled(ServiceInterface::SERVICE_TLS)) {
64+
$result['ports'] = [$config->getPort() . ':80'];
65+
$result['networks'] = [
66+
BuilderInterface::NETWORK_MAGENTO => [
67+
'aliases' => [$config->getHost()]
68+
]
69+
];
70+
}
71+
72+
return $result;
6173
}
6274

6375
/**

src/Compose/ProductionBuilder/Service/Web.php

+18-3
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,33 @@ public function getServiceName(): string
6060
*/
6161
public function getConfig(Config $config): array
6262
{
63-
return $this->serviceFactory->create(
63+
$result = $this->serviceFactory->create(
6464
$this->getServiceName(),
6565
$config->getServiceVersion(ServiceInterface::SERVICE_NGINX),
6666
[
6767
'volumes' => $this->volume->getRo($config),
6868
'environment' => [
69-
'WITH_XDEBUG=' . (int)$config->hasServiceEnabled(ServiceInterface::SERVICE_FPM_XDEBUG)
70-
]
69+
'WITH_XDEBUG=' . (int)$config->hasServiceEnabled(ServiceInterface::SERVICE_FPM_XDEBUG),
70+
'NGINX_WORKER_PROCESSES=' . $config->getNginxWorkerProcesses(),
71+
'NGINX_WORKER_CONNECTIONS=' . $config->getNginxWorkerConnections(),
72+
],
7173
],
7274
$config->getServiceImage(ServiceInterface::SERVICE_NGINX),
7375
$config->getServiceImagePattern(ServiceInterface::SERVICE_NGINX)
7476
);
77+
78+
if (!$config->hasServiceEnabled(ServiceInterface::SERVICE_TLS)
79+
&& !$config->hasServiceEnabled(ServiceInterface::SERVICE_VARNISH)
80+
) {
81+
$result['ports'] = [$config->getPort() . ':8080'];
82+
$result['networks'] = [
83+
BuilderInterface::NETWORK_MAGENTO => [
84+
'aliases' => [$config->getHost()]
85+
]
86+
];
87+
}
88+
89+
return $result;
7590
}
7691

7792
/**

src/Config/Config.php

+24
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,28 @@ public function getMailHogHttpPort(): ?string
467467
{
468468
return $this->all()->get(SourceInterface::SYSTEM_MAILHOG_HTTP_PORT);
469469
}
470+
471+
/**
472+
* Returns the value of the `worker_processes` parameter of the Nginx service
473+
*
474+
* @return string
475+
* @throws ConfigurationMismatchException
476+
*/
477+
public function getNginxWorkerProcesses(): string
478+
{
479+
$value = $this->all()->get(SourceInterface::SYSTEM_NGINX_WORKER_PROCESSES);
480+
if (SourceInterface::NGINX_WORKER_PROCESSES_AUTO === $value) {
481+
return $value;
482+
}
483+
return (string)$value;
484+
}
485+
486+
/**
487+
* @return int
488+
* @throws ConfigurationMismatchException
489+
*/
490+
public function getNginxWorkerConnections()
491+
{
492+
return (int)$this->all()->get(SourceInterface::SYSTEM_NGINX_WORKER_CONNECTIONS);
493+
}
470494
}

src/Config/Source/BaseSource.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class BaseSource implements SourceInterface
2929
public const DEFAULT_TLS_PORT = '443';
3030
public const DEFAULT_MAILHOG_SMTP_PORT = '1025';
3131
public const DEFAULT_MAILHOG_HTTP_PORT = '8025';
32+
public const DEFAULT_NGINX_WORKER_PROCESSES = '1';
33+
public const DEFAULT_NGINX_WORKER_CONNECTIONS = '1024';
3234

3335
/**
3436
* @var EnvReader
@@ -77,7 +79,9 @@ public function read(): Repository
7779
self::INSTALLATION_TYPE => self::INSTALLATION_TYPE_COMPOSER,
7880
self::MAGENTO_VERSION => $this->getMagentoVersion(),
7981
self::SYSTEM_MAILHOG_SMTP_PORT => self::DEFAULT_MAILHOG_SMTP_PORT,
80-
self::SYSTEM_MAILHOG_HTTP_PORT => self::DEFAULT_MAILHOG_HTTP_PORT
82+
self::SYSTEM_MAILHOG_HTTP_PORT => self::DEFAULT_MAILHOG_HTTP_PORT,
83+
self::SYSTEM_NGINX_WORKER_PROCESSES => self::DEFAULT_NGINX_WORKER_PROCESSES,
84+
self::SYSTEM_NGINX_WORKER_CONNECTIONS => self::DEFAULT_NGINX_WORKER_CONNECTIONS,
8185
]);
8286

8387
try {

src/Config/Source/CliSource.php

+12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class CliSource implements SourceInterface
3333
public const OPTION_INSTALLATION_TYPE = 'installation-type';
3434
public const OPTION_NO_ES = 'no-es';
3535
public const OPTION_NO_MAILHOG = 'no-mailhog';
36+
public const OPTION_NO_TLS = 'no-tls';
3637

3738
/**
3839
* MailHog configuration
@@ -67,6 +68,8 @@ class CliSource implements SourceInterface
6768
public const OPTION_HOST = 'host';
6869
public const OPTION_PORT = 'port';
6970
public const OPTION_TLS_PORT = 'tls-port';
71+
public const OPTION_NGINX_WORKER_PROCESSES = 'nginx-worker-processes';
72+
public const OPTION_NGINX_WORKER_CONNECTIONS = 'nginx-worker-connections';
7073

7174
public const OPTION_DB_INCREMENT_INCREMENT = 'db-increment-increment';
7275
public const OPTION_DB_INCREMENT_OFFSET = 'db-increment-offset';
@@ -115,6 +118,7 @@ class CliSource implements SourceInterface
115118
private static $disableOptionsMap = [
116119
self::OPTION_NO_ES => self::SERVICES_ES,
117120
self::OPTION_NO_MAILHOG => self::SERVICES_MAILHOG,
121+
self::OPTION_NO_TLS => self::SERVICES_TLS,
118122
];
119123

120124
/**
@@ -298,6 +302,14 @@ public function read(): Repository
298302
$repository->set(self::SYSTEM_MAILHOG_HTTP_PORT, $port);
299303
}
300304

305+
if ($nginxWorkerProcesses = $this->input->getOption(self::OPTION_NGINX_WORKER_PROCESSES)) {
306+
$repository->set(self::SYSTEM_NGINX_WORKER_PROCESSES, $nginxWorkerProcesses);
307+
}
308+
309+
if ($nginxWorkerConnections = $this->input->getOption(self::OPTION_NGINX_WORKER_CONNECTIONS)) {
310+
$repository->set(self::SYSTEM_NGINX_WORKER_CONNECTIONS, $nginxWorkerConnections);
311+
}
312+
301313
return $repository;
302314
}
303315
}

src/Config/Source/SourceInterface.php

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ interface SourceInterface
2020
public const MOUNTS = 'mounts';
2121
public const NAME = 'name';
2222

23+
public const NGINX_WORKER_PROCESSES_AUTO = 'auto';
24+
2325
/**
2426
* Services
2527
*/
@@ -125,6 +127,11 @@ interface SourceInterface
125127

126128
public const MAGENTO_VERSION = 'magento.version';
127129

130+
/**
131+
* TLS
132+
*/
133+
public const SERVICES_TLS = self::SERVICES . '.' . ServiceInterface::SERVICE_TLS;
134+
128135
/**
129136
* Config
130137
*/
@@ -142,6 +149,8 @@ interface SourceInterface
142149
public const SYSTEM_SET_DOCKER_HOST = 'system.set_docker_host';
143150
public const SYSTEM_MAILHOG_SMTP_PORT = 'system.mailhog.smtp_port';
144151
public const SYSTEM_MAILHOG_HTTP_PORT = 'system.mailhog.http_port';
152+
public const SYSTEM_NGINX_WORKER_PROCESSES = 'system.nginx.worker_processes';
153+
public const SYSTEM_NGINX_WORKER_CONNECTIONS = 'system.nginx.worker_connections';
145154

146155
public const SYSTEM_DB_INCREMENT_INCREMENT = 'system.db.increment_increment';
147156
public const SYSTEM_DB_INCREMENT_OFFSET = 'system.db.increment_offset';

src/Test/Integration/BuildComposeTest.php

+28-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ public function buildDataProvider(): array
9494
[CliSource::OPTION_WITH_ENTRYPOINT, true],
9595
[CliSource::OPTION_WITH_MARIADB_CONF, true],
9696
[CliSource::OPTION_TLS_PORT, '4443'],
97-
[CliSource::OPTION_NO_MAILHOG, true]
97+
[CliSource::OPTION_NO_MAILHOG, true],
98+
[CliSource::OPTION_NGINX_WORKER_PROCESSES, '8'],
99+
[CliSource::OPTION_NGINX_WORKER_CONNECTIONS, '4096'],
98100
]
99101
],
100102
'cloud-base-test' => [
@@ -111,9 +113,32 @@ public function buildDataProvider(): array
111113
[CliSource::OPTION_WITH_ENTRYPOINT, true],
112114
[CliSource::OPTION_WITH_MARIADB_CONF, true],
113115
[CliSource::OPTION_TLS_PORT, '4443'],
114-
[CliSource::OPTION_NO_MAILHOG, true]
116+
[CliSource::OPTION_NO_MAILHOG, true],
117+
[CliSource::OPTION_NGINX_WORKER_PROCESSES, 'auto'],
115118
]
116-
]
119+
],
120+
'without TLS service' => [
121+
__DIR__ . '/_files/cloud_no_tls_service',
122+
[
123+
[CliSource::OPTION_MODE, BuilderFactory::BUILDER_PRODUCTION],
124+
[CliSource::OPTION_NO_TLS, true],
125+
]
126+
],
127+
'without Varnish service' => [
128+
__DIR__ . '/_files/cloud_no_varnish_service',
129+
[
130+
[CliSource::OPTION_MODE, BuilderFactory::BUILDER_PRODUCTION],
131+
[CliSource::OPTION_NO_VARNISH, true],
132+
]
133+
],
134+
'without Varnish and TLS services' => [
135+
__DIR__ . '/_files/cloud_no_varnish_and_tls_service',
136+
[
137+
[CliSource::OPTION_MODE, BuilderFactory::BUILDER_PRODUCTION],
138+
[CliSource::OPTION_NO_VARNISH, true],
139+
[CliSource::OPTION_NO_TLS, true],
140+
]
141+
],
117142
];
118143
}
119144
}

0 commit comments

Comments
 (0)