Skip to content

Commit

Permalink
Merge branch 'release/3.0.15'
Browse files Browse the repository at this point in the history
  • Loading branch information
stmh committed Jun 7, 2019
2 parents a2c2f91 + 96f972b commit 9fbe708
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 58 deletions.
17 changes: 17 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Changelog

## 3.0.15 / 2019-06-07

### Changed

* Enhance support for docker shells
* Rework shell execution for docker-exec
* Allow relative paths for docker rootFolder. Add sleep to reduce processor drain

### Fixed:
* Fix default values

## 3.0.14 / 2019-05-28

### Fixed

* Fix bug with variants overriding existing configuration

## 3.0.12 / 2019-05-27

### Fixed
Expand Down
14 changes: 8 additions & 6 deletions src/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,18 @@ public function runCommand(string $command, array $args, InputInterface $origina
*/
protected function startInteractiveShell(ShellProviderInterface $shell, array $command = [], $use_tty = true)
{
$options = ['tty' => true];
/** @var Process $process */
if (!empty($command)) {
$command = [
'bash',
$options['shell_provided'] = true;
array_unshift(
$command,
'/bin/bash',
'--login',
'-c',
'\'' . implode(' ', $command) .'\'',
];
'-c'
);
}
$process = $shell->createShellProcess($command, ['tty' => $use_tty]);
$process = $shell->createShellProcess($command, $options);
$stdin = fopen('php://stdin', 'r');
$process->setInput($stdin);
$process->setTimeout(0);
Expand Down
69 changes: 39 additions & 30 deletions src/Configuration/ConfigurationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,16 +377,17 @@ function ($severity, $message) {
return $contents;
}

/**
* @param string $config_name
*
* @return HostConfig
* @throws MismatchedVersionException
* @throws \Phabalicious\Exception\MissingHostConfigException
* @throws \Phabalicious\Exception\ValidationFailedException
* @throws \Phabalicious\Exception\ShellProviderNotFoundException
* @throws BlueprintTemplateNotFoundException
*/
/**
* @param string $config_name
*
* @return HostConfig
* @throws \Phabalicious\Exception\BlueprintTemplateNotFoundException
* @throws \Phabalicious\Exception\FabfileNotReadableException
* @throws \Phabalicious\Exception\MismatchedVersionException
* @throws \Phabalicious\Exception\MissingHostConfigException
* @throws \Phabalicious\Exception\ShellProviderNotFoundException
* @throws \Phabalicious\Exception\ValidationFailedException
*/
public function getHostConfig(string $config_name)
{
$cid = 'host:' . $config_name;
Expand All @@ -410,15 +411,17 @@ public function getHostConfig(string $config_name)
return $data;
}

/**
* @param string $blueprint
* @param string $identifier
* @return HostConfig
* @throws MismatchedVersionException
* @throws ShellProviderNotFoundException
* @throws ValidationFailedException
* @throws BlueprintTemplateNotFoundException
*/
/**
* @param string $blueprint
* @param string $identifier
*
* @return HostConfig
* @throws MismatchedVersionException
* @throws ShellProviderNotFoundException
* @throws ValidationFailedException
* @throws BlueprintTemplateNotFoundException
* @throws \Phabalicious\Exception\FabfileNotReadableException
*/
public function getHostConfigFromBlueprint(string $blueprint, string $identifier)
{
$cid = 'blueprint:' . $blueprint . ':' . $identifier;
Expand All @@ -443,14 +446,16 @@ public function getHostConfigFromBlueprint(string $blueprint, string $identifier
return $data;
}

/**
* @param $config_name
* @param $data
* @return HostConfig
* @throws MismatchedVersionException
* @throws ShellProviderNotFoundException
* @throws ValidationFailedException
*/
/**
* @param $config_name
* @param $data
*
* @return HostConfig
* @throws MismatchedVersionException
* @throws ShellProviderNotFoundException
* @throws ValidationFailedException
* @throws \Phabalicious\Exception\FabfileNotReadableException
*/
private function validateHostConfig($config_name, $data)
{
$data = $this->resolveInheritance($data, $this->hosts);
Expand Down Expand Up @@ -649,6 +654,9 @@ private function validateDockerConfig(array $data, $config_name)
$validation->hasKey('shellProvider', 'The name of the shell-provider to use');
$validation->hasKey('rootFolder', 'The rootFolder to start with');
$validation->hasKey('tmpFolder', 'The rootFolder to use');
if ($data['rootFolder'][0] === '.') {
$data['rootFolder'] = realpath($this->getFabfilePath() . '/' . $data['rootFolder']);
}

if ($errors->hasErrors()) {
throw new ValidationFailedException($errors);
Expand Down Expand Up @@ -711,7 +719,8 @@ protected function inheritFromBlueprint(string $config_name, $data): array
return $data;
}

public function hasHostConfig($configName) {
return !empty($this->hosts[$configName]);
}
public function hasHostConfig($configName)
{
return !empty($this->hosts[$configName]);
}
}
47 changes: 32 additions & 15 deletions src/Method/DockerMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ public function alterConfig(ConfigurationService $configuration_service, array &
* @throws MismatchedVersionException
* @throws MissingDockerHostConfigException
*/
public function getDockerConfig(HostConfig $host_config, TaskContextInterface $context)
public static function getDockerConfig(HostConfig $host_config, ConfigurationService $config)
{
$config = $context->getConfigurationService()->getDockerConfig($host_config['docker']['configuration']);
$config = $config->getDockerConfig($host_config['docker']['configuration']);
$config['executables'] = $host_config['executables'];
return $config;
}
Expand Down Expand Up @@ -143,7 +143,7 @@ private function runTaskImpl(HostConfig $host_config, TaskContextInterface $cont
}

/** @var DockerConfig $docker_config */
$docker_config = $this->getDockerConfig($host_config, $context);
$docker_config = $this->getDockerConfig($host_config, $context->getConfigurationService());
$tasks = $docker_config['tasks'];

if ($silent && empty($tasks[$task])) {
Expand Down Expand Up @@ -201,8 +201,8 @@ public function waitForServices(HostConfig $hostconfig, TaskContextInterface $co
}
$max_tries = 10;
$tries = 0;
$docker_config = $this->getDockerConfig($hostconfig, $context);
$container_name = $this->getDockerContainerName($hostconfig, $context);
$docker_config = $this->getDockerConfig($hostconfig, $context->getConfigurationService());
$container_name = $this->getDockerContainerName($hostconfig, $context->getConfigurationService());
$shell = $docker_config->shell();

if (!$this->isContainerRunning($docker_config, $container_name)) {
Expand Down Expand Up @@ -296,7 +296,7 @@ private function copySSHKeys(HostConfig $hostconfig, TaskContextInterface $conte
];
}

$docker_config = $this->getDockerConfig($hostconfig, $context);
$docker_config = $this->getDockerConfig($hostconfig, $context->getConfigurationService());
$root_folder = $docker_config['rootFolder'] . '/' . $hostconfig['docker']['projectFolder'];

/** @var ShellProviderInterface $shell */
Expand All @@ -323,7 +323,7 @@ private function copySSHKeys(HostConfig $hostconfig, TaskContextInterface $conte
}

if (count($files) > 0) {
$container_name = $this->getDockerContainerName($hostconfig, $context);
$container_name = $this->getDockerContainerName($hostconfig, $context->getConfigurationService());
if (!$this->isContainerRunning($docker_config, $container_name)) {
throw new \RuntimeException(sprintf(
'Docker container %s not running, check your `host.docker.name` configuration!',
Expand Down Expand Up @@ -408,11 +408,11 @@ public function getIpAddress(HostConfig $host_config, TaskContextInterface $cont
if (!empty($this->cache[$host_config['configName']])) {
return $this->cache[$host_config['configName']];
}
$docker_config = $this->getDockerConfig($host_config, $context);
$docker_config = $this->getDockerConfig($host_config, $context->getConfigurationService());
$shell = $docker_config->shell();
$scoped_loglevel = new ScopedLogLevel($shell, LogLevel::DEBUG);
try {
$container_name = $this->getDockerContainerName($host_config, $context);
$container_name = $this->getDockerContainerName($host_config, $context->getConfigurationService());
} catch (\RuntimeException $e) {
return false;
}
Expand Down Expand Up @@ -445,10 +445,10 @@ public function getIpAddress(HostConfig $host_config, TaskContextInterface $cont
*/
public function startRemoteAccess(HostConfig $host_config, TaskContextInterface $context)
{
$docker_config = $this->getDockerConfig($host_config, $context);
$docker_config = $this->getDockerConfig($host_config, $context->getConfigurationService());
$this->getIp($host_config, $context);
if (is_a($docker_config->shell(), 'SshShellProvider')) {
$context->setResult('config', $this->getDockerConfig($host_config, $context));
$context->setResult('config', $docker_config);
}
}

Expand All @@ -475,7 +475,7 @@ public function getIp(HostConfig $host_config, TaskContextInterface $context)
public function appCheckExisting(HostConfig $host_config, TaskContextInterface $context)
{
// Set outer-shell to the one provided by the docker-configuration.
$docker_config = $this->getDockerConfig($host_config, $context);
$docker_config = $this->getDockerConfig($host_config, $context->getConfigurationService());
$context->setResult('outerShell', $docker_config->shell());
$context->setResult('installDir', $docker_config['rootFolder'] .
'/' . $host_config['docker']['projectFolder']);
Expand Down Expand Up @@ -527,7 +527,7 @@ public function runAppSpecificTask(HostConfig $host_config, TaskContextInterface
throw new \InvalidArgumentException('Missing currentStage on context!');
}

$docker_config = $this->getDockerConfig($host_config, $context);
$docker_config = $this->getDockerConfig($host_config, $context->getConfigurationService());
$shell = $docker_config->shell();

if (isset($docker_config['tasks'][$current_stage['stage']]) ||
Expand All @@ -545,13 +545,13 @@ public function runAppSpecificTask(HostConfig $host_config, TaskContextInterface
* @throws MissingDockerHostConfigException
* @throws ValidationFailedException
*/
private function getDockerContainerName(HostConfig $host_config, TaskContextInterface $context)
public static function getDockerContainerName(HostConfig $host_config, ConfigurationService $config)
{
if (!empty($host_config['docker']['name'])) {
return $host_config['docker']['name'];
}
if ($composer_service = $host_config['docker']['service']) {
$docker_config = $this->getDockerConfig($host_config, $context);
$docker_config = self::getDockerConfig($host_config, $config);
$shell = $docker_config->shell();
$cwd = $shell->getWorkingDir();
$shell->cd($docker_config['rootFolder'] . '/' . $host_config['docker']['projectFolder']);
Expand All @@ -575,4 +575,21 @@ private function getDockerContainerName(HostConfig $host_config, TaskContextInte
));
}
}

public function shell(HostConfig $host_config, TaskContextInterface $context)
{
if (empty($host_config['docker']['name'])) {
try {
$config = $host_config['docker'];
$config['name'] = self::getDockerContainerName(
$host_config,
$context->getConfigurationService()
);
$host_config['docker'] = $config;
} catch (MismatchedVersionException $e) {
} catch (MissingDockerHostConfigException $e) {
} catch (ValidationFailedException $e) {
}
}
}
}
4 changes: 2 additions & 2 deletions src/Method/DrushMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public function getDefaultConfig(ConfigurationService $configuration_service, ar
: $configuration_service->getSetting('drushVersion', 8);

$config['supportsZippedBackups'] = true;
$config['siteFolder'] = 'sites/default';
$config['filesFolder'] = 'sites/default/files';
$config['siteFolder'] = '/sites/default';
$config['filesFolder'] = '/sites/default/files';

return $config;
}
Expand Down
12 changes: 8 additions & 4 deletions src/ShellProvider/DockerExecShellProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Phabalicious\ShellProvider;

use Phabalicious\Configuration\ConfigurationService;
use Phabalicious\Method\DockerMethod;
use Phabalicious\Method\TaskContextInterface;
use Phabalicious\Validation\ValidationErrorBagInterface;
use Phabalicious\Validation\ValidationService;
Expand Down Expand Up @@ -30,7 +31,11 @@ public function validateConfig(array $config, ValidationErrorBagInterface $error
]);
if (!$errors->hasErrors()) {
$validation = new ValidationService($config['docker'], $errors, 'host:docker');
$validation->hasKey('name', 'The name of the docker-container to use');
if (empty($config['docker']['service'])) {
$validation->hasKey('name', 'The name of the docker-container to use');
} else {
$validation->hasKey('service', 'The service of the docker-compose to use');
}
}
}

Expand All @@ -43,7 +48,7 @@ public function getShellCommand(array $options = []): array
(empty($options['tty']) ? '-i' : '-it'),
$this->hostConfig['docker']['name'],
];
if (!empty($options['tty'])) {
if (!empty($options['tty']) && empty($options['shell_provided'])) {
$command[] = $this->hostConfig['shellExecutable'];
}

Expand Down Expand Up @@ -85,5 +90,4 @@ public function getFile(string $source, string $dest, TaskContextInterface $cont

return $this->runProcess($command, $context, false, true);
}

}
}
4 changes: 3 additions & 1 deletion src/ShellProvider/LocalShellProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class LocalShellProvider extends BaseShellProvider implements ShellProviderInter

protected $captureOutput = false;


public function getName(): string
{
return 'local';
Expand Down Expand Up @@ -148,6 +147,9 @@ public function run(string $command, $capture_output = false, $throw_exception_o
$result = '';
while ((strpos($result, self::RESULT_IDENTIFIER) === false) && !$this->process->isTerminated()) {
$result .= $this->process->getIncrementalOutput();
if (!$this->process->isTerminated()) {
usleep(1000 * 100);
}
}
if ($this->process->isTerminated()) {
$this->logger->log($this->errorLogLevel->get(), 'Local shell terminated unexpected!');
Expand Down

0 comments on commit 9fbe708

Please sign in to comment.