Skip to content

Commit 7c7bdea

Browse files
MAGECLOUD-5421: [GitHub] Introduce universal configuration file #160 (#162)
1 parent ae8adbf commit 7c7bdea

File tree

17 files changed

+256
-94
lines changed

17 files changed

+256
-94
lines changed

images/php/7.1-cli/bin/run-hooks

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import yaml
44
import os
55
import sys
66
import subprocess
7+
from os import path
78

89
# Disable output buffering.
910
os.environ['PYTHONUNBUFFERED'] = "1"
@@ -28,14 +29,19 @@ def get_magento_root():
2829

2930
# Gets set hooks by hook name.
3031
def get_hooks(hook_name):
31-
with open(get_magento_root() + "/.magento.app.yaml", 'r') as stream:
32+
config = get_magento_root() + "/.magento.docker.yaml"
33+
34+
if path.exists(get_magento_root() + "/.magento.app.yaml"):
35+
config = get_magento_root() + "/.magento.app.yaml"
36+
37+
with open(config, 'r') as stream:
3238
try:
3339
content = yaml.safe_load(stream)
3440
return content["hooks"][hook_name]
3541
except yaml.YAMLError as exc:
3642
error_exit(exc)
3743
except KeyError:
38-
error_exit("The hook \"" + hook_name + "\" is not in the .magento.app.yaml file. Skipped", 0)
44+
error_exit("The hook \"" + hook_name + "\" is not in the \"" + config + "\" file. Skipped", 0)
3945

4046

4147
# Main function.

images/php/7.2-cli/bin/run-hooks

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import yaml
44
import os
55
import sys
66
import subprocess
7+
from os import path
78

89
# Disable output buffering.
910
os.environ['PYTHONUNBUFFERED'] = "1"
@@ -28,14 +29,19 @@ def get_magento_root():
2829

2930
# Gets set hooks by hook name.
3031
def get_hooks(hook_name):
31-
with open(get_magento_root() + "/.magento.app.yaml", 'r') as stream:
32+
config = get_magento_root() + "/.magento.docker.yaml"
33+
34+
if path.exists(get_magento_root() + "/.magento.app.yaml"):
35+
config = get_magento_root() + "/.magento.app.yaml"
36+
37+
with open(config, 'r') as stream:
3238
try:
3339
content = yaml.safe_load(stream)
3440
return content["hooks"][hook_name]
3541
except yaml.YAMLError as exc:
3642
error_exit(exc)
3743
except KeyError:
38-
error_exit("The hook \"" + hook_name + "\" is not in the .magento.app.yaml file. Skipped", 0)
44+
error_exit("The hook \"" + hook_name + "\" is not in the \"" + config + "\" file. Skipped", 0)
3945

4046

4147
# Main function.

images/php/7.3-cli/bin/run-hooks

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import yaml
44
import os
55
import sys
66
import subprocess
7+
from os import path
78

89
# Disable output buffering.
910
os.environ['PYTHONUNBUFFERED'] = "1"
@@ -28,14 +29,19 @@ def get_magento_root():
2829

2930
# Gets set hooks by hook name.
3031
def get_hooks(hook_name):
31-
with open(get_magento_root() + "/.magento.app.yaml", 'r') as stream:
32+
config = get_magento_root() + "/.magento.docker.yaml"
33+
34+
if path.exists(get_magento_root() + "/.magento.app.yaml"):
35+
config = get_magento_root() + "/.magento.app.yaml"
36+
37+
with open(config, 'r') as stream:
3238
try:
3339
content = yaml.safe_load(stream)
3440
return content["hooks"][hook_name]
3541
except yaml.YAMLError as exc:
3642
error_exit(exc)
3743
except KeyError:
38-
error_exit("The hook \"" + hook_name + "\" is not in the .magento.app.yaml file. Skipped", 0)
44+
error_exit("The hook \"" + hook_name + "\" is not in the \"" + config + "\" file. Skipped", 0)
3945

4046

4147
# Main function.

images/php/cli/bin/run-hooks

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import yaml
44
import os
55
import sys
66
import subprocess
7+
from os import path
78

89
# Disable output buffering.
910
os.environ['PYTHONUNBUFFERED'] = "1"
@@ -28,14 +29,19 @@ def get_magento_root():
2829

2930
# Gets set hooks by hook name.
3031
def get_hooks(hook_name):
31-
with open(get_magento_root() + "/.magento.app.yaml", 'r') as stream:
32+
config = get_magento_root() + "/.magento.docker.yaml"
33+
34+
if path.exists(get_magento_root() + "/.magento.app.yaml"):
35+
config = get_magento_root() + "/.magento.app.yaml"
36+
37+
with open(config, 'r') as stream:
3238
try:
3339
content = yaml.safe_load(stream)
3440
return content["hooks"][hook_name]
3541
except yaml.YAMLError as exc:
3642
error_exit(exc)
3743
except KeyError:
38-
error_exit("The hook \"" + hook_name + "\" is not in the .magento.app.yaml file. Skipped", 0)
44+
error_exit("The hook \"" + hook_name + "\" is not in the \"" + config + "\" file. Skipped", 0)
3945

4046

4147
# Main function.

src/Command/BuildCompose.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ protected function configure(): void
160160
BuilderFactory::BUILDER_PRODUCTION,
161161
]
162162
)
163-
),
164-
BuilderFactory::BUILDER_PRODUCTION
163+
)
165164
)
166165
->addOption(
167166
Source\CliSource::OPTION_SYNC_ENGINE,
@@ -217,19 +216,19 @@ protected function configure(): void
217216
*/
218217
public function execute(InputInterface $input, OutputInterface $output)
219218
{
220-
$builder = $this->builderFactory->create(
221-
$input->getOption(Source\CliSource::OPTION_MODE)
222-
);
223219
$config = $this->configFactory->create([
224220
$this->sourceFactory->create(Source\BaseSource::class),
225221
$this->sourceFactory->create(Source\CloudBaseSource::class),
226222
$this->sourceFactory->create(Source\CloudSource::class),
223+
$this->sourceFactory->create(Source\ConfigSource::class),
227224
new Source\CliSource($input)
228225
]);
229226

230-
$this->distGenerator->generate($config);
227+
$builder = $this->builderFactory->create($config->getMode());
231228
$compose = $builder->build($config);
232229

230+
$this->distGenerator->generate($config);
231+
233232
$this->filesystem->put(
234233
$builder->getPath(),
235234
Yaml::dump([

src/Compose/BuilderFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function create(string $strategy): BuilderInterface
4949
{
5050
if (!array_key_exists($strategy, $this->strategies)) {
5151
throw new ConfigurationMismatchException(
52-
sprintf('Wrong strategy "%s" passed', $strategy)
52+
sprintf('Wrong mode "%s" passed', $strategy)
5353
);
5454
}
5555

src/Compose/ProductionBuilder.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ public function build(Config $config): Manager
416416
$manager->addService(
417417
self::SERVICE_CRON,
418418
array_merge(
419-
$this->getCronCliService($phpVersion, $config->getCronJobs()),
419+
$this->getCronCliService($config),
420420
['volumes' => $volumesRo]
421421
),
422422
[self::NETWORK_MAGENTO],
@@ -428,31 +428,34 @@ public function build(Config $config): Manager
428428
}
429429

430430
/**
431-
* @param string $version
432-
* @param array $cronConfig
431+
* @param Config $config
433432
* @return array
434433
* @throws ConfigurationMismatchException
435434
*/
436-
private function getCronCliService(string $version, array $cronConfig): array
435+
private function getCronCliService(Config $config): array
437436
{
438-
$config = $this->serviceFactory->create(ServiceInterface::SERVICE_PHP_CLI, $version, ['command' => 'run-cron']);
437+
$cron = $this->serviceFactory->create(
438+
ServiceInterface::SERVICE_PHP_CLI,
439+
$config->getServiceVersion(ServiceInterface::SERVICE_PHP),
440+
['command' => 'run-cron']
441+
);
439442
$preparedCronConfig = [];
440443

441-
foreach ($cronConfig as $job) {
444+
foreach ($config->getCronJobs() as $job) {
442445
$preparedCronConfig[] = sprintf(
443446
'%s root cd %s && %s >> %s/var/log/cron.log',
444-
$job['spec'],
447+
$job['schedule'],
445448
self::DIR_MAGENTO,
446-
str_replace('php ', '/usr/local/bin/php ', $job['cmd']),
449+
str_replace('php ', '/usr/local/bin/php ', $job['command']),
447450
self::DIR_MAGENTO
448451
);
449452
}
450453

451-
$config['environment'] = [
454+
$cron['environment'] = [
452455
'CRONTAB' => implode(PHP_EOL, $preparedCronConfig)
453456
];
454457

455-
return $config;
458+
return $cron;
456459
}
457460

458461
/**

src/Compose/ProductionBuilder/VolumeResolver.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ public function getMagentoVolumes(array $mounts, bool $isReadOnly, bool $hasSele
5151
{
5252
$volumes = $this->getDefaultMagentoVolumes($isReadOnly);
5353

54-
foreach (array_keys($mounts) as $volume) {
55-
$volumeName = 'magento-' . str_replace('/', '-', $volume);
54+
foreach ($mounts as $volumeData) {
55+
$path = $volumeData['path'];
56+
$volumeName = 'magento-' . str_replace('/', '-', $path);
5657

5758
$volumes[$volumeName] = [
58-
'path' => BuilderInterface::DIR_MAGENTO . '/' . $volume,
59-
'volume' => '/' . $volume,
59+
'path' => BuilderInterface::DIR_MAGENTO . '/' . $path,
60+
'volume' => '/' . $path,
6061
'mode' => 'rw'
6162
];
6263
}

src/Config/Config.php

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
use Illuminate\Config\Repository;
1111
use Magento\CloudDocker\App\ConfigurationMismatchException;
12+
use Magento\CloudDocker\Compose\BuilderFactory;
13+
use Magento\CloudDocker\Compose\DeveloperBuilder;
14+
use Magento\CloudDocker\Compose\ProductionBuilder;
1215
use Magento\CloudDocker\Config\Source\CliSource;
1316
use Magento\CloudDocker\Config\Source\SourceException;
1417
use Magento\CloudDocker\Config\Source\SourceInterface;
@@ -29,6 +32,16 @@ class Config
2932
*/
3033
private $data;
3134

35+
/**
36+
* Available engines per mode
37+
*
38+
* @var array
39+
*/
40+
private static $enginesMap = [
41+
BuilderFactory::BUILDER_DEVELOPER => DeveloperBuilder::SYNC_ENGINES_LIST,
42+
BuilderFactory::BUILDER_PRODUCTION => ProductionBuilder::SYNC_ENGINES_LIST
43+
];
44+
3245
/**
3346
* @param SourceInterface[] $sources
3447
*/
@@ -80,11 +93,27 @@ public function get(string $key)
8093
*/
8194
public function getSyncEngine(): string
8295
{
83-
if (!$this->all()->has(SourceInterface::CONFIG_SYNC_ENGINE)) {
84-
throw new ConfigurationMismatchException('Sync engine is not defined');
96+
$syncEngine = $this->all()->get(SourceInterface::SYSTEM_SYNC_ENGINE);
97+
$mode = $this->getMode();
98+
99+
if ($syncEngine === null) {
100+
if ($mode === BuilderFactory::BUILDER_DEVELOPER) {
101+
$syncEngine = DeveloperBuilder::DEFAULT_SYNC_ENGINE;
102+
} elseif ($mode === BuilderFactory::BUILDER_PRODUCTION) {
103+
$syncEngine = ProductionBuilder::DEFAULT_SYNC_ENGINE;
104+
}
85105
}
86106

87-
return $this->all()->get(SourceInterface::CONFIG_SYNC_ENGINE);
107+
if (isset(self::$enginesMap[$mode]) && !in_array($syncEngine, self::$enginesMap[$mode], true)) {
108+
throw new ConfigurationMismatchException(sprintf(
109+
'File sync engine "%s" is not supported in "%s" mode. Available: %s',
110+
$syncEngine,
111+
$mode,
112+
implode(', ', self::$enginesMap[$mode])
113+
));
114+
}
115+
116+
return $syncEngine;
88117
}
89118

90119
/**
@@ -93,11 +122,20 @@ public function getSyncEngine(): string
93122
*/
94123
public function getMode(): string
95124
{
96-
if (!$this->all()->has(SourceInterface::CONFIG_MODE)) {
125+
if (!$this->all()->get(SourceInterface::SYSTEM_MODE)) {
97126
throw new ConfigurationMismatchException('Mode is not defined');
98127
}
99128

100-
return $this->all()->get(SourceInterface::CONFIG_MODE);
129+
$mode = $this->all()->get(SourceInterface::SYSTEM_MODE);
130+
131+
if (!in_array($mode, [BuilderFactory::BUILDER_DEVELOPER, BuilderFactory::BUILDER_PRODUCTION], true)) {
132+
throw new ConfigurationMismatchException(sprintf(
133+
'Mode "%s" is not supported',
134+
$mode
135+
));
136+
}
137+
138+
return $this->all()->get(SourceInterface::SYSTEM_MODE);
101139
}
102140

103141
/**
@@ -106,6 +144,17 @@ public function getMode(): string
106144
*/
107145
public function getCronJobs(): array
108146
{
147+
$jobs = $this->all()->get(SourceInterface::CRON_JOBS, []);
148+
149+
foreach ($jobs as $job => $config) {
150+
if (!isset($config['schedule'], $config['command'])) {
151+
throw new ConfigurationMismatchException(sprintf(
152+
'One of required parameters is missing in "%s" job',
153+
$job
154+
));
155+
}
156+
}
157+
109158
return $this->all()->get(SourceInterface::CRON_JOBS, []);
110159
}
111160

@@ -156,7 +205,7 @@ public function getServiceVersion(string $name): string
156205
));
157206
}
158207

159-
return $this->all()->get($key);
208+
return (string)$this->all()->get($key);
160209
}
161210

162211
/**
@@ -190,7 +239,7 @@ public function getMounts(): array
190239
*/
191240
public function hasTmpMounts(): bool
192241
{
193-
return (bool)$this->all()->get(SourceInterface::CONFIG_TMP_MOUNTS);
242+
return (bool)$this->all()->get(SourceInterface::SYSTEM_TMP_MOUNTS);
194243
}
195244

196245
/**
@@ -208,7 +257,7 @@ public function getDbPortsExpose(): ?string
208257
*/
209258
public function getEnabledPhpExtensions(): array
210259
{
211-
return $this->all()->get(SourceInterface::PHP_EXTENSIONS, []);
260+
return $this->all()->get(SourceInterface::PHP_ENABLED_EXTENSIONS, []);
212261
}
213262

214263
/**

src/Config/Dist/Generator.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,9 @@ public function __construct(
107107
public function generate(Config $config): void
108108
{
109109
$dockerRootPath = $this->directoryList->getDockerRoot();
110-
111110
$configByServices = $this->generateByServices($config);
112-
$this->saveConfigDist(
113-
$dockerRootPath . '/config.php.dist',
114-
$configByServices
115-
);
116111

112+
$this->saveConfigDist($dockerRootPath . '/config.php.dist', $configByServices);
117113
$this->saveConfigEnv(
118114
$dockerRootPath . '/config.env',
119115
array_merge(

0 commit comments

Comments
 (0)