Skip to content

Commit ad2ef1e

Browse files
MAGECLOUD-4170: Add Support for Local Mounts to Production Mode in Cl… (#138)
1 parent e24b8ca commit ad2ef1e

File tree

7 files changed

+262
-121
lines changed

7 files changed

+262
-121
lines changed

src/Command/BuildCompose.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use Symfony\Component\Yaml\Yaml;
2424

2525
/**
26-
* Builds Docker configuration for Magento project.
26+
* Builds Docker configuration for Magento project
2727
*
2828
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2929
*/
@@ -55,7 +55,7 @@ class BuildCompose extends Command
5555
private const OPTION_WITH_SELENIUM = 'with-selenium';
5656

5757
/**
58-
* Option key to config name map.
58+
* Option key to config name map
5959
*
6060
* @var array
6161
*/
@@ -72,6 +72,16 @@ class BuildCompose extends Command
7272
self::OPTION_SELENIUM_IMAGE => ServiceFactory::SERVICE_SELENIUM_IMAGE
7373
];
7474

75+
/**
76+
* Available engines per mode
77+
*
78+
* @var array
79+
*/
80+
private static $enginesMap = [
81+
BuilderFactory::BUILDER_DEVELOPER => DeveloperBuilder::SYNC_ENGINES_LIST,
82+
BuilderFactory::BUILDER_PRODUCTION => ProductionBuilder::SYNC_ENGINES_LIST
83+
];
84+
7585
/**
7686
* @var BuilderFactory
7787
*/
@@ -205,9 +215,10 @@ protected function configure(): void
205215
InputOption::VALUE_REQUIRED,
206216
sprintf(
207217
'File sync engine. Works only with developer mode. Available: (%s)',
208-
implode(', ', DeveloperBuilder::SYNC_ENGINES_LIST)
209-
),
210-
DeveloperBuilder::SYNC_ENGINE_NATIVE
218+
implode(', ', array_unique(
219+
array_merge(DeveloperBuilder::SYNC_ENGINES_LIST, ProductionBuilder::SYNC_ENGINES_LIST)
220+
))
221+
)
211222
)
212223
->addOption(
213224
self::OPTION_NO_CRON,
@@ -237,22 +248,28 @@ protected function configure(): void
237248
*/
238249
public function execute(InputInterface $input, OutputInterface $output)
239250
{
240-
$type = $input->getOption(self::OPTION_MODE);
251+
$mode = $input->getOption(self::OPTION_MODE);
241252
$syncEngine = $input->getOption(self::OPTION_SYNC_ENGINE);
242253

243-
$builder = $this->builderFactory->create($type);
244-
$config = $this->configFactory->create();
254+
if ($mode === BuilderFactory::BUILDER_DEVELOPER && $syncEngine === null) {
255+
$syncEngine = DeveloperBuilder::DEFAULT_SYNC_ENGINE;
256+
} elseif ($mode === BuilderFactory::BUILDER_PRODUCTION && $syncEngine === null) {
257+
$syncEngine = ProductionBuilder::DEFAULT_SYNC_ENGINE;
258+
}
245259

246-
if (BuilderFactory::BUILDER_DEVELOPER === $type
247-
&& !in_array($syncEngine, DeveloperBuilder::SYNC_ENGINES_LIST, true)
260+
if (isset(self::$enginesMap[$mode])
261+
&& !in_array($syncEngine, self::$enginesMap[$mode], true)
248262
) {
249263
throw new GenericException(sprintf(
250264
"File sync engine '%s' is not supported. Available: %s",
251265
$syncEngine,
252-
implode(', ', DeveloperBuilder::SYNC_ENGINES_LIST)
266+
implode(', ', self::$enginesMap[$mode])
253267
));
254268
}
255269

270+
$builder = $this->builderFactory->create($mode);
271+
$config = $this->configFactory->create();
272+
256273
array_walk(self::$optionsMap, static function ($key, $option) use ($config, $input) {
257274
if ($value = $input->getOption($option)) {
258275
$config->set($key, $value);

src/Compose/BuilderInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface BuilderInterface
2424
public const KEY_EXPOSE_DB_PORT = 'expose-db-port';
2525
public const KEY_NO_TMP_MOUNTS = 'no-tmp-mounts';
2626
public const KEY_WITH_SELENIUM = 'with-selenium';
27+
public const KEY_SYNC_ENGINE = 'sync-engine';
2728

2829
public const SERVICE_GENERIC = 'generic';
2930
public const SERVICE_DB = 'db';
@@ -54,6 +55,9 @@ interface BuilderInterface
5455
public const VOLUME_MAGENTO_DB = 'magento-db';
5556
public const VOLUME_MAGENTO_DEV = 'magento-dev';
5657
public const VOLUME_DOCKER_MNT = 'docker-mnt';
58+
public const VOLUME_DOCKER_ETRYPOINT = 'docker-entrypoint';
59+
60+
public const SYNC_ENGINE_NATIVE = 'native';
5761

5862
/**
5963
* @param Repository $config

src/Compose/DeveloperBuilder.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\CloudDocker\Compose;
99

1010
use Illuminate\Contracts\Config\Repository;
11+
use Magento\CloudDocker\Compose\ProductionBuilder\VolumeResolver;
1112
use Magento\CloudDocker\Filesystem\FileList;
1213

1314
/**
@@ -19,9 +20,7 @@ class DeveloperBuilder implements BuilderInterface
1920
{
2021
public const SYNC_ENGINE_DOCKER_SYNC = 'docker-sync';
2122
public const SYNC_ENGINE_MUTAGEN = 'mutagen';
22-
public const SYNC_ENGINE_NATIVE = 'native';
23-
24-
public const KEY_SYNC_ENGINE = 'sync-engine';
23+
public const DEFAULT_SYNC_ENGINE = self::SYNC_ENGINE_NATIVE;
2524

2625
public const VOLUME_MAGENTO_SYNC = 'magento-sync';
2726

@@ -46,16 +45,27 @@ class DeveloperBuilder implements BuilderInterface
4645
*/
4746
private $resolver;
4847

48+
/**
49+
* @var VolumeResolver
50+
*/
51+
private $volumeResolver;
52+
4953
/**
5054
* @param BuilderFactory $builderFactory
5155
* @param FileList $fileList
5256
* @param Resolver $resolver
57+
* @param VolumeResolver $volumeResolver
5358
*/
54-
public function __construct(BuilderFactory $builderFactory, FileList $fileList, Resolver $resolver)
55-
{
59+
public function __construct(
60+
BuilderFactory $builderFactory,
61+
FileList $fileList,
62+
Resolver $resolver,
63+
VolumeResolver $volumeResolver
64+
) {
5665
$this->builderFactory = $builderFactory;
5766
$this->fileList = $fileList;
5867
$this->resolver = $resolver;
68+
$this->volumeResolver = $volumeResolver;
5969
}
6070

6171
/**
@@ -86,7 +96,14 @@ public function build(Repository $config): Manager
8696

8797
$manager->setVolumes([
8898
self::VOLUME_MAGENTO_SYNC => $syncConfig,
89-
self::VOLUME_MAGENTO_DB => []
99+
self::VOLUME_MAGENTO_DB => [],
100+
self::VOLUME_DOCKER_ETRYPOINT => [
101+
'driver_opts' => [
102+
'type' => 'none',
103+
'device' => $this->resolver->getRootPath('/.docker/mysql/docker-entrypoint-initdb.d'),
104+
'o' => 'bind'
105+
]
106+
]
90107
]);
91108

92109
/**
@@ -112,10 +129,18 @@ public function build(Repository $config): Manager
112129
$volumes,
113130
[
114131
self::VOLUME_MAGENTO_DB . ':/var/lib/mysql',
115-
'.docker/mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d'
132+
self::VOLUME_DOCKER_ETRYPOINT . ':/docker-entrypoint-initdb.d'
116133
]
117134
)
118135
]);
136+
$manager->updateService(self::SERVICE_BUILD, [
137+
'volumes' => array_merge(
138+
$volumes,
139+
$this->volumeResolver->normalize(
140+
$this->volumeResolver->getComposerVolumes()
141+
)
142+
)
143+
]);
119144

120145
return $manager;
121146
}
@@ -140,7 +165,7 @@ private function getMagentoVolumes(Repository $config): array
140165
}
141166

142167
return [
143-
self::VOLUME_MAGENTO_SYNC . ':' . $target
168+
self::VOLUME_MAGENTO_SYNC . ':' . $target . ':delegated',
144169
];
145170
}
146171
}

src/Compose/Manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function addService(string $name, array $extConfig, array $networks, arra
5858

5959
$this->services[$name] = [
6060
'config' => $config,
61-
'depends_on' => $depends
61+
'depends_on' => $depends,
6262
];
6363
}
6464

0 commit comments

Comments
 (0)