Skip to content

Commit

Permalink
Get container-name from running service
Browse files Browse the repository at this point in the history
Needed because of change in recent versions of docker-compose, more info here: docker/compose#6316
  • Loading branch information
stmh committed Nov 26, 2018
1 parent ad67bad commit 383afe1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ Most notably the handling of arguments and options has changed a lot. Fabric gav
mbb:
shellProvider: docker-exec
* You can get help for a specific task via `phab help <task>`. It will show all possible options and some help.
* docker-compose version 23 changes the schema how names of docker-containers are constructed. To support this change we can now declare the needed service to compute the correct container-name from.

hosts:
testHost:
docker:
service: web


### Changed

* `docker:startRemoteAccess` is now the task `start-remote-access` as it makes more sense.
Expand Down
59 changes: 47 additions & 12 deletions src/Method/DockerMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function getDefaultConfig(ConfigurationService $configuration_service, ar
{
$config = parent::getDefaultConfig($configuration_service, $host_config); // TODO: Change the autogenerated stub
$config['executables']['supervisorctl'] = 'supervisorctl';
$config['executables']['docker-compose'] = 'docker-compose';
$config['executables']['docker'] = 'docker';
$config['executables']['chmod'] = 'chmod';
$config['executables']['chown'] = 'chown';
if (!empty($host_config['sshTunnel']) &&
Expand All @@ -56,7 +58,10 @@ public function validateConfig(array $config, ValidationErrorBagInterface $error
$errors,
sprintf('host.docker: `%s`', $config['configName'])
);
$validation->hasKey('name', 'name of the docker-container to inspect');
if (empty($config['docker']['service'])) {
$validation->hasKey('name', 'name of the docker-container to inspect');
}

$validation->hasKey(
'projectFolder',
'projectFolder where the project is stored, relative to the rootFolder'
Expand Down Expand Up @@ -169,12 +174,12 @@ public function waitForServices(HostConfig $hostconfig, TaskContextInterface $co
$max_tries = 10;
$tries = 0;
$docker_config = $this->getDockerConfig($hostconfig, $context);
$container_name = $hostconfig['docker']['name'];
$container_name = $this->getDockerContainerName($hostconfig, $context);
$shell = $docker_config->shell();

while ($tries < $max_tries) {
$error_log_level = new ScopedErrorLogLevel($shell, LogLevel::NOTICE);
$result = $shell->run(sprintf('docker exec %s #!supervisorctl status', $container_name), true, false);
$result = $shell->run(sprintf('#!docker exec %s #!supervisorctl status', $container_name), true, false);
$error_log_level = null;

$count_running = 0;
Expand Down Expand Up @@ -244,11 +249,11 @@ private function copySSHKeys(HostConfig $hostconfig, TaskContextInterface $conte
$docker_config = $this->getDockerConfig($hostconfig, $context);
/** @var ShellProviderInterface $shell */
$shell = $docker_config->shell();
$container_name = $hostconfig['docker']['name'];
$container_name = $this->getDockerContainerName($hostconfig, $context);
if (!$this->isContainerRunning($docker_config, $container_name)) {
throw new \RuntimeException(sprintf('Docker container %s not running', $container_name));
}
$shell->run(sprintf('docker exec %s mkdir -p /root/.ssh', $container_name));
$shell->run(sprintf('#!docker exec %s mkdir -p /root/.ssh', $container_name));

foreach ($files as $dest => $data) {
if ((substr($data['source'], 0, 7) == 'http://') ||
Expand All @@ -266,11 +271,11 @@ private function copySSHKeys(HostConfig $hostconfig, TaskContextInterface $conte
);
}

$shell->run(sprintf('docker cp %s %s:%s', $data['source'], $container_name, $dest));
$shell->run(sprintf('docker exec %s #!chmod %s %s', $container_name, $data['permissions'], $dest));
$shell->run(sprintf('#!docker cp %s %s:%s', $data['source'], $container_name, $dest));
$shell->run(sprintf('#!docker exec %s #!chmod %s %s', $container_name, $data['permissions'], $dest));
}
$shell->run(sprintf('docker exec %s #!chmod 700 /root/.ssh', $container_name));
$shell->run(sprintf('docker exec %s #!chown -R root /root/.ssh', $container_name));
$shell->run(sprintf('#!docker exec %s #!chmod 700 /root/.ssh', $container_name));
$shell->run(sprintf('#!docker exec %s #!chown -R root /root/.ssh', $container_name));
}

foreach ($temp_files as $temp_file) {
Expand All @@ -283,7 +288,7 @@ public function isContainerRunning(HostConfig $docker_config, $container_name)
$shell = $docker_config->shell();
$scoped_loglevel = new ScopedLogLevel($shell, LogLevel::DEBUG);
$result = $shell->run(sprintf(
'docker inspect -f {{.State.Running}} %s',
'#!docker inspect -f {{.State.Running}} %s',
$container_name
), true);

Expand Down Expand Up @@ -312,14 +317,14 @@ public function getIpAddress(HostConfig $host_config, TaskContextInterface $cont
$docker_config = $this->getDockerConfig($host_config, $context);
$shell = $docker_config->shell();
$scoped_loglevel = new ScopedLogLevel($shell, LogLevel::DEBUG);
$container_name = $host_config['docker']['name'];
$container_name = $this->getDockerContainerName($host_config, $context);

if (!$this->isContainerRunning($docker_config, $container_name)) {
return false;
}

$result = $shell->run(sprintf(
'docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}}\n{{end}}" %s',
'#!docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}}\n{{end}}" %s',
$container_name
), true);

Expand Down Expand Up @@ -429,4 +434,34 @@ public function runAppSpecificTask(HostConfig $host_config, TaskContextInterface
}
}

private function getDockerContainerName(HostConfig $host_config, TaskContextInterface $context)
{
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);
$shell = $docker_config->shell();
$cwd = $shell->getWorkingDir();
$shell->cd($docker_config['rootFolder'] . '/' . $host_config['docker']['projectFolder']);
$result = $shell->run('#!docker-compose ps', true);
$shell->cd($cwd);
$docker_name = false;

foreach ($result->getOutput() as $line) {
if (strpos($line, '_' . $composer_service . '_') !== false) {
list($docker_name) = explode(' ', $line);
$cfg = $host_config['docker'];
$cfg['name'] = $docker_name;
$host_config['docker'] = $cfg;

return $docker_name;
}
}
throw new \RuntimeException(sprintf(
'Could not get the name of the docker container running the service `s`'
));
}
}

}

0 comments on commit 383afe1

Please sign in to comment.