Skip to content

Commit 3189219

Browse files
author
Yevhen Miroshnychenko
authored
MAGECLOUD-4894 [MTS] Add support of split DB on docker (#161)
1 parent ac79bff commit 3189219

File tree

27 files changed

+520
-158
lines changed

27 files changed

+520
-158
lines changed

codeception.dist.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ modules:
2020
mcd_repo: "https://github.com/magento/magento-cloud-docker.git"
2121
mcc_repo: "https://github.com/magento/magento-cloud-components.git"
2222
mcp_repo: "https://github.com/magento/magento-cloud-patches.git"
23+
ece_tools_repo: "https://github.com/magento/ece-tools.git"
2324
composer_magento_username: "%REPO_USERNAME%"
2425
composer_magento_password: "%REPO_PASSWORD%"
2526
composer_github_token: "%GITHUB_TOKEN%"
@@ -35,3 +36,16 @@ modules:
3536
password: "%Magento.docker.settings.db.password%"
3637
exposed_port: "%Magento.docker.settings.db.port%"
3738
reconnect: true
39+
databases:
40+
db_quote:
41+
dsn: "mysql:host=%Magento.docker.settings.db.host%;port=%Magento.docker.settings.db_quote.port%;dbname=%Magento.docker.settings.db.path%"
42+
user: "%Magento.docker.settings.db.username%"
43+
password: "%Magento.docker.settings.db.password%"
44+
exposed_port: "%Magento.docker.settings.db_quote.port%"
45+
reconnect: true
46+
db_sales:
47+
dsn: "mysql:host=%Magento.docker.settings.db.host%;port=%Magento.docker.settings.db_sales.port%;dbname=%Magento.docker.settings.db.path%"
48+
user: "%Magento.docker.settings.db.username%"
49+
password: "%Magento.docker.settings.db.password%"
50+
exposed_port: "%Magento.docker.settings.db_sales.port%"
51+
reconnect: true

composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@
7676
"dist/mysql",
7777
".docker/mysql"
7878
],
79+
[
80+
"dist/mysql-sales",
81+
".docker/mysql-sales"
82+
],
83+
[
84+
"dist/mysql-quote",
85+
".docker/mysql-quote"
86+
],
7987
[
8088
"dist/bin/magento-docker",
8189
"bin/magento-docker"

dist/mysql-quote/docker-entrypoint-initdb.d/.gitignore

Whitespace-only changes.

dist/mysql-sales/docker-entrypoint-initdb.d/.gitignore

Whitespace-only changes.

src/Command/BuildCompose.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ protected function configure(): void
110110
InputOption::VALUE_REQUIRED,
111111
'Expose DB port'
112112
)
113+
->addOption(
114+
Source\CliSource::OPTION_EXPOSE_DB_QUOTE_PORT,
115+
null,
116+
InputOption::VALUE_REQUIRED,
117+
'Expose port for DB sales'
118+
)
119+
->addOption(
120+
Source\CliSource::OPTION_EXPOSE_DB_SALES_PORT,
121+
null,
122+
InputOption::VALUE_REQUIRED,
123+
'Expose port for DB quote'
124+
)
113125
->addOption(
114126
Source\CliSource::OPTION_REDIS,
115127
null,

src/Compose/BuilderInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ interface BuilderInterface
1717

1818
public const SERVICE_GENERIC = 'generic';
1919
public const SERVICE_DB = 'db';
20+
public const SERVICE_DB_QUOTE = 'db-quote';
21+
public const SERVICE_DB_SALES = 'db-sales';
2022
public const SERVICE_FPM = 'fpm';
2123
public const SERVICE_FPM_XDEBUG = 'fpm_xdebug';
2224
public const SERVICE_BUILD = 'build';
@@ -43,9 +45,13 @@ interface BuilderInterface
4345
public const VOLUME_MAGENTO_STATIC = 'magento-static';
4446
public const VOLUME_MAGENTO_MEDIA = 'magento-media';
4547
public const VOLUME_MAGENTO_DB = 'magento-db';
48+
public const VOLUME_MAGENTO_DB_QUOTE = 'magento-db-quote';
49+
public const VOLUME_MAGENTO_DB_SALES = 'magento-db-sales';
4650
public const VOLUME_MAGENTO_DEV = 'magento-dev';
4751
public const VOLUME_DOCKER_MNT = 'docker-mnt';
4852
public const VOLUME_DOCKER_ETRYPOINT = 'docker-entrypoint';
53+
public const VOLUME_DOCKER_ETRYPOINT_QUOTE = 'docker-entrypoint-quote';
54+
public const VOLUME_DOCKER_ETRYPOINT_SALES = 'docker-entrypoint-sales';
4955
public const VOLUME_MARIADB_CONF = 'mariadb-conf';
5056

5157
public const SYNC_ENGINE_NATIVE = 'native';

src/Compose/ProductionBuilder.php

Lines changed: 118 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\CloudDocker\Compose;
99

10+
use Magento\CloudDocker\App\GenericException;
1011
use Magento\CloudDocker\App\ConfigurationMismatchException;
1112
use Magento\CloudDocker\Compose\Php\ExtensionResolver;
1213
use Magento\CloudDocker\Compose\ProductionBuilder\VolumeResolver;
@@ -138,36 +139,12 @@ public function build(Config $config): Manager
138139

139140
$phpVersion = $config->getServiceVersion(ServiceInterface::SERVICE_PHP);
140141
$dbVersion = $config->getServiceVersion(ServiceInterface::SERVICE_DB);
141-
$hostPort = $config->getDbPortsExpose();
142-
$dbPorts = $hostPort ? "$hostPort:3306" : '3306';
142+
$cliDepends = self::$cliDepends;
143143

144144
$manager->addNetwork(self::NETWORK_MAGENTO, ['driver' => 'bridge']);
145145
$manager->addNetwork(self::NETWORK_MAGENTO_BUILD, ['driver' => 'bridge']);
146146

147-
$volumes = [
148-
self::VOLUME_MAGENTO => [
149-
'driver_opts' => [
150-
'type' => 'none',
151-
'device' => $this->resolver->getRootPath(),
152-
'o' => 'bind'
153-
]
154-
],
155-
self::VOLUME_MAGENTO_DB => [],
156-
self::VOLUME_MARIADB_CONF => [
157-
'driver_opts' => [
158-
'type' => 'none',
159-
'device' => $this->resolver->getRootPath('/.docker/mysql/mariadb.conf.d'),
160-
'o' => 'bind',
161-
],
162-
],
163-
self::VOLUME_DOCKER_ETRYPOINT => [
164-
'driver_opts' => [
165-
'type' => 'none',
166-
'device' => $this->resolver->getRootPath('/.docker/mysql/docker-entrypoint-initdb.d'),
167-
'o' => 'bind'
168-
]
169-
]
170-
];
147+
$volumes = [self::VOLUME_MAGENTO => $this->getVolumeConfig()];
171148

172149
if ($config->hasServiceEnabled(ServiceInterface::SERVICE_SELENIUM)) {
173150
$manager->addVolume(self::VOLUME_MAGENTO_DEV, []);
@@ -178,38 +155,20 @@ public function build(Config $config): Manager
178155
$hasTmpMounts = $config->hasTmpMounts();
179156

180157
if ($hasTmpMounts) {
181-
$volumes[self::VOLUME_DOCKER_MNT] = [
182-
'driver_opts' => [
183-
'type' => 'none',
184-
'device' => $this->resolver->getRootPath('/.docker/mnt'),
185-
'o' => 'bind'
186-
]
187-
];
158+
$volumes[self::VOLUME_DOCKER_MNT] = $this->getVolumeConfig('/.docker/mnt');
188159
}
189160

190161
foreach ($this->volumeResolver->getMagentoVolumes($mounts, false, $hasSelenium) as $volumeName => $volume) {
191162
$syncConfig = [];
192163

193164
if (!empty($volume['volume']) && $config->getSyncEngine() === self::SYNC_ENGINE_NATIVE) {
194-
$syncConfig = [
195-
'driver_opts' => [
196-
'type' => 'none',
197-
'device' => $this->resolver->getRootPath($volume['volume']),
198-
'o' => 'bind'
199-
]
200-
];
165+
$syncConfig = $this->getVolumeConfig($volume['volume']);
201166
}
202167
$volumes[$volumeName] = $syncConfig;
203168
}
204169

205170
if ($config->getSyncEngine() === self::SYNC_ENGINE_MOUNT) {
206-
$volumes[self::VOLUME_MAGENTO] = [
207-
'driver_opts' => [
208-
'type' => 'none',
209-
'device' => $this->resolver->getRootPath(),
210-
'o' => 'bind'
211-
]
212-
];
171+
$volumes[self::VOLUME_MAGENTO] = $this->getVolumeConfig();
213172
}
214173

215174
$manager->setVolumes($volumes);
@@ -231,27 +190,23 @@ public function build(Config $config): Manager
231190
$this->volumeResolver->getMountVolumes($hasTmpMounts)
232191
);
233192

234-
$manager->addService(
235-
self::SERVICE_DB,
236-
$this->serviceFactory->create(
237-
ServiceInterface::SERVICE_DB,
238-
$dbVersion,
239-
[
240-
'ports' => [$dbPorts],
241-
'volumes' => array_merge(
242-
[
243-
self::VOLUME_MAGENTO_DB . ':/var/lib/mysql',
244-
self::VOLUME_DOCKER_ETRYPOINT . ':/docker-entrypoint-initdb.d',
245-
self::VOLUME_MARIADB_CONF . ':/etc/mysql/mariadb.conf.d',
246-
],
247-
$volumesMount
248-
)
249-
]
250-
),
251-
[self::NETWORK_MAGENTO],
252-
[]
193+
$manager->addVolume(
194+
self::VOLUME_MARIADB_CONF,
195+
$this->getVolumeConfig('/.docker/mysql/mariadb.conf.d')
253196
);
254197

198+
$this->addDbService(self::SERVICE_DB, $manager, $dbVersion, $volumesMount, $config);
199+
200+
if ($config->hasServiceEnabled(ServiceInterface::SERVICE_DB_QUOTE)) {
201+
$cliDepends = array_merge($cliDepends, [self::SERVICE_DB_QUOTE => ['condition' => 'service_started']]);
202+
$this->addDbService(self::SERVICE_DB_QUOTE, $manager, $dbVersion, $volumesMount, $config);
203+
}
204+
205+
if ($config->hasServiceEnabled(ServiceInterface::SERVICE_DB_SALES)) {
206+
$cliDepends = array_merge($cliDepends, [self::SERVICE_DB_SALES => ['condition' => 'service_started']]);
207+
$this->addDbService(self::SERVICE_DB_SALES, $manager, $dbVersion, $volumesMount, $config);
208+
}
209+
255210
foreach (self::$standaloneServices as $service) {
256211
if (!$config->hasServiceEnabled($service)) {
257212
continue;
@@ -361,7 +316,7 @@ public function build(Config $config): Manager
361316
['volumes' => $volumesRw]
362317
),
363318
[self::NETWORK_MAGENTO],
364-
self::$cliDepends
319+
$cliDepends
365320
);
366321
}
367322

@@ -406,7 +361,7 @@ public function build(Config $config): Manager
406361
self::SERVICE_BUILD,
407362
$this->serviceFactory->create(ServiceInterface::SERVICE_PHP_CLI, $phpVersion, ['volumes' => $volumesBuild]),
408363
[self::NETWORK_MAGENTO_BUILD],
409-
self::$cliDepends
364+
$cliDepends
410365
);
411366
$manager->addService(
412367
self::SERVICE_DEPLOY,
@@ -423,7 +378,7 @@ public function build(Config $config): Manager
423378
['volumes' => $volumesRo]
424379
),
425380
[self::NETWORK_MAGENTO],
426-
self::$cliDepends
381+
$cliDepends
427382
);
428383
}
429384

@@ -468,4 +423,98 @@ public function getPath(): string
468423
{
469424
return $this->fileList->getMagentoDockerCompose();
470425
}
426+
427+
/**
428+
* @param string $service
429+
* @param Manager $manager
430+
* @param string $version
431+
* @param array $mounts
432+
* @param Config $config
433+
* @throws ConfigurationMismatchException
434+
* @throws GenericException
435+
*/
436+
private function addDbService(
437+
string $service,
438+
Manager $manager,
439+
string $version,
440+
array $mounts,
441+
Config $config
442+
) {
443+
$mounts[] = self::VOLUME_MARIADB_CONF . ':/etc/mysql/mariadb.conf.d';
444+
445+
switch ($service) {
446+
case self::SERVICE_DB:
447+
$port = $config->getDbPortsExpose();
448+
449+
$manager->addVolume(self::VOLUME_MAGENTO_DB, []);
450+
$manager->addVolume(
451+
self::VOLUME_DOCKER_ETRYPOINT,
452+
$this->getVolumeConfig('/.docker/mysql/docker-entrypoint-initdb.d')
453+
);
454+
455+
$mounts[] = self::VOLUME_MAGENTO_DB . ':/var/lib/mysql';
456+
$mounts[] = self::VOLUME_DOCKER_ETRYPOINT . ':/docker-entrypoint-initdb.d';
457+
$serviceType = ServiceInterface::SERVICE_DB;
458+
break;
459+
case self::SERVICE_DB_QUOTE:
460+
$port = $config->getDbQuotePortsExpose();
461+
462+
$manager->addVolume(self::VOLUME_MAGENTO_DB_QUOTE, []);
463+
$manager->addVolume(
464+
self::VOLUME_DOCKER_ETRYPOINT_QUOTE,
465+
$this->getVolumeConfig('/.docker/mysql-quote/docker-entrypoint-initdb.d')
466+
);
467+
468+
$mounts[] = self::VOLUME_MAGENTO_DB_QUOTE . ':/var/lib/mysql';
469+
$mounts[] = self::VOLUME_DOCKER_ETRYPOINT_QUOTE . ':/docker-entrypoint-initdb.d';
470+
$serviceType = ServiceInterface::SERVICE_DB_QUOTE;
471+
break;
472+
case self::SERVICE_DB_SALES:
473+
$port = $config->getDbSalesPortsExpose();
474+
475+
$manager->addVolume(self::VOLUME_MAGENTO_DB_SALES, []);
476+
$manager->addVolume(
477+
self::VOLUME_DOCKER_ETRYPOINT_SALES,
478+
$this->getVolumeConfig(
479+
'/.docker/mysql-sales/docker-entrypoint-initdb.d'
480+
)
481+
);
482+
483+
$mounts[] = self::VOLUME_MAGENTO_DB_SALES . ':/var/lib/mysql';
484+
$mounts[] = self::VOLUME_DOCKER_ETRYPOINT_SALES . ':/docker-entrypoint-initdb.d';
485+
$serviceType = ServiceInterface::SERVICE_DB_SALES;
486+
break;
487+
default:
488+
throw new GenericException(sprintf('Configuration for %s service not exist', $service));
489+
}
490+
491+
$manager->addService(
492+
$service,
493+
$this->serviceFactory->create(
494+
$serviceType,
495+
$version,
496+
[
497+
'ports' => [$port ? "$port:3306" : '3306'],
498+
'volumes' => $mounts,
499+
]
500+
),
501+
[self::NETWORK_MAGENTO],
502+
[]
503+
);
504+
}
505+
506+
/**
507+
* @param string $device
508+
* @return array
509+
*/
510+
private function getVolumeConfig(string $device = '/'): array
511+
{
512+
return [
513+
'driver_opts' => [
514+
'type' => 'none',
515+
'device' => $this->resolver->getRootPath($device),
516+
'o' => 'bind'
517+
]
518+
];
519+
}
471520
}

src/Config/Config.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,24 @@ public function getDbPortsExpose(): ?string
250250
return $this->all()->get(SourceInterface::SYSTEM_EXPOSE_DB_PORTS);
251251
}
252252

253+
/**
254+
* @return string|null
255+
* @throws ConfigurationMismatchException
256+
*/
257+
public function getDbQuotePortsExpose(): ?string
258+
{
259+
return $this->all()->get(SourceInterface::SYSTEM_EXPOSE_DB_QUOTE_PORTS);
260+
}
261+
262+
/**
263+
* @return string|null
264+
* @throws ConfigurationMismatchException
265+
*/
266+
public function getDbSalesPortsExpose(): ?string
267+
{
268+
return $this->all()->get(SourceInterface::SYSTEM_EXPOSE_DB_SALES_PORTS);
269+
}
270+
253271
/**
254272
* @return array
255273
* @throws ConfigurationMismatchException

0 commit comments

Comments
 (0)