Skip to content

Commit

Permalink
Merge pull request #85 from skrosoft/master
Browse files Browse the repository at this point in the history
Passing env vars from CLI to docker commands
  • Loading branch information
Plopix authored May 30, 2020
2 parents 467c1de + 44dcc80 commit c0107b6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion payload/dev/docker-compose-osx.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2.1'
version: '2.2'
services:
engine:
volumes:
Expand Down
2 changes: 1 addition & 1 deletion payload/dev/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2.1'
version: '2.2'
services:
nginx:
image: nginx:stable-alpine
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Docker/Initialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ protected function execute(InputInterface $input, OutputInterface $output): void

// eZ Platform <3 only support solr 6. Replace unsupported solr 7.7 by 6.6.2
if (
( (1 === (int) str_replace(['^', '~'], '', $normalizedVersion)) ||
(2 === (int) str_replace(['^', '~'], '', $normalizedVersion)) ) &&
((1 === (int) str_replace(['^', '~'], '', $normalizedVersion)) ||
(2 === (int) str_replace(['^', '~'], '', $normalizedVersion))) &&
$compose->hasService('solr')
) {
$composeFilePath = "{$provisioningFolder}/dev/{$composeFileName}";
Expand Down
12 changes: 10 additions & 2 deletions src/Core/DockerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ abstract class DockerCommand extends Command

protected function configure(): void
{
$this->addOption('env', 'env', InputOption::VALUE_REQUIRED, 'Docker Env', 'dev');
$this->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev');
$this->addOption(
'docker-env',
'd',
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Docker environment variables',
[]
);
}

public function getDockerClient(): Docker
Expand Down Expand Up @@ -73,7 +80,8 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
$this->taskExecutor = new TaskExecutor(
$this->dockerClient,
$this->projectConfiguration,
$this->requiredRecipes
$this->requiredRecipes,
$input->getOption('docker-env')
);
}
}
22 changes: 19 additions & 3 deletions src/Core/TaskExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ class TaskExecutor
*/
protected $recipes;

public function __construct(DockerClient $dockerClient, ProjectConfiguration $configuration, Collection $recipes)
{
/**
* @var array Docker environment variables
*/
protected $dockerEnvVars;

public function __construct(
DockerClient $dockerClient,
ProjectConfiguration $configuration,
Collection $recipes,
array $dockerEnvVars = []
) {
$this->dockerClient = $dockerClient;
$this->projectConfiguration = $configuration;
$this->recipes = $recipes;
$this->dockerEnvVars = $dockerEnvVars;
}

protected function checkRecipeAvailability(string $recipe): void
Expand Down Expand Up @@ -177,6 +187,12 @@ protected function execute(string $command, string $user = 'www-data', string $s

protected function globalExecute(string $command, string $user = 'www-data', string $service = 'engine')
{
return $this->dockerClient->exec($command, ['--user', $user], $service);
$args = ['--user', $user];

foreach ($this->dockerEnvVars as $envVar) {
$args = array_merge($args, ['--env', $envVar]);
}

return $this->dockerClient->exec($command, $args, $service);
}
}

0 comments on commit c0107b6

Please sign in to comment.