Skip to content

Commit 7bfaa3e

Browse files
committed
Merge branch 'release/2.3.3'
2 parents 4f439e6 + 0549b23 commit 7bfaa3e

9 files changed

Lines changed: 130 additions & 27 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ CliTools Changelog
44
next - UPCOMING
55
------------------
66

7+
2.3.3 - 2016-03-03
8+
------------------
9+
- Added main and app containers (see `config.ini`) for `docker:exec`, `docker:shell` and `docker:cli`
10+
711
2.3.2 - 2016-02-18
812
------------------
913
- Fixed issue which prevented deletion of orpahend docker images

Documentation/INSTALL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ ignore[] = "CliTools\Console\Command\System\RebootCommand"
8888

8989
```bash
9090
# Stable channel
91-
ct self-udpate
91+
ct self-update
9292

9393
## Beta channel
9494
ct self-update --beta

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CliTools for Docker, PHP und MySQL development
22

3-
[![latest v2.3.2](https://img.shields.io/badge/latest-v2.3.2-green.svg?style=flat)](https://github.com/webdevops/clitools/releases/tag/2.3.2)
3+
[![latest v2.3.3](https://img.shields.io/badge/latest-v2.3.3-green.svg?style=flat)](https://github.com/webdevops/clitools/releases/tag/2.3.3)
44
[![License GPL3](https://img.shields.io/badge/license-GPL3-blue.svg?style=flat)](/LICENSE)
55
[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/webdevops/clitools.svg)](http://isitmaintained.com/project/webdevops/clitools "Average time to resolve an issue")
66
[![Percentage of issues still open](http://isitmaintained.com/badge/open/webdevops/clitools.svg)](http://isitmaintained.com/project/webdevops/clitools "Percentage of issues still open")

src/app/CliTools/Console/Command/Docker/AbstractCommand.php

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
use CliTools\Shell\CommandBuilder\CommandBuilder;
2525
use CliTools\Shell\CommandBuilder\CommandBuilderInterface;
26+
use CliTools\Utility\DockerUtility;
2627
use CliTools\Utility\PhpUtility;
2728

2829
abstract class AbstractCommand extends \CliTools\Console\Command\AbstractCommand
@@ -35,6 +36,11 @@ abstract class AbstractCommand extends \CliTools\Console\Command\AbstractCommand
3536
*/
3637
protected $dockerPath;
3738

39+
/**
40+
* @var array
41+
*/
42+
protected $runningContainerCache = array();
43+
3844
/**
3945
* Search and return for updir docker path
4046
*
@@ -105,7 +111,7 @@ protected function getDockerEnv($containerName, $envName)
105111

106112
if (!empty($path)) {
107113
// Genrate full docker container name
108-
$dockerContainerName = \CliTools\Utility\DockerUtility::getDockerInstanceName($containerName, 1, $path);
114+
$dockerContainerName = $this->findAndBuildContainerName($containerName);
109115

110116
// Switch to directory of docker-compose.yml
111117
PhpUtility::chdir($path);
@@ -127,6 +133,52 @@ protected function getDockerEnv($containerName, $envName)
127133
return $ret;
128134
}
129135

136+
/**
137+
* Check if docker container is available
138+
*
139+
* @param string $containerName Container name
140+
*
141+
* @return string|bool|null
142+
*/
143+
protected function checkIfDockerContainerIsAvailable($containerName)
144+
{
145+
$ret = null;
146+
147+
if (empty($containerName)) {
148+
$this->output->writeln('<p-error>No container specified</p-error>');
149+
150+
return false;
151+
}
152+
153+
try {
154+
// Search updir for docker-compose.yml
155+
$path = $this->getDockerPath();
156+
157+
if (!empty($path)) {
158+
// Genrate full docker container name
159+
$dockerContainerName = \CliTools\Utility\DockerUtility::getDockerInstanceName($containerName, 1, $path);
160+
161+
// Switch to directory of docker-compose.yml
162+
PhpUtility::chdir($path);
163+
164+
// Get docker confguration (fetched directly from docker)
165+
$conf = \CliTools\Utility\DockerUtility::getDockerConfiguration($dockerContainerName);
166+
167+
if (!empty($conf)
168+
&& !empty($conf->State)
169+
&& !empty($conf->State->Status)
170+
&& in_array(strtolower($conf->State->Status), ['running'], true)
171+
) {
172+
$ret = true;
173+
}
174+
}
175+
} catch (\Exception $e) {
176+
$ret = false;
177+
}
178+
179+
return $ret;
180+
}
181+
130182
/**
131183
* Execute docker command
132184
*
@@ -154,7 +206,7 @@ protected function executeDockerExec($containerName, CommandBuilderInterface $co
154206

155207
if (!empty($path)) {
156208
// Genrate full docker container name
157-
$dockerContainerName = \CliTools\Utility\DockerUtility::getDockerInstanceName($containerName, 1, $path);
209+
$dockerContainerName = $this->findAndBuildContainerName($containerName);
158210

159211
// Switch to directory of docker-compose.yml
160212
PhpUtility::chdir($path);
@@ -239,4 +291,37 @@ protected function executeDockerComposeRun($containerName, CommandBuilderInterfa
239291

240292
return 0;
241293
}
294+
295+
/**
296+
* @param null $containerName Poissible Container name (csv)
297+
* @return null|string
298+
*/
299+
protected function findAndBuildContainerName($containerName = null) {
300+
// Use cached container name
301+
if (isset($this->runningContainerCache[$containerName])) {
302+
return $this->runningContainerCache[$containerName];
303+
}
304+
305+
$fullContainerName = null;
306+
307+
$path = $this->getDockerPath();
308+
$instancePrefix = \CliTools\Utility\DockerUtility::getDockerInstancePrefix($path);
309+
310+
$containerNameList = PhpUtility::trimExplode(',', $containerName);
311+
foreach ($containerNameList as $containerNameToTry) {
312+
if ($this->checkIfDockerContainerIsAvailable($containerNameToTry, 'id')) {
313+
$fullContainerName = $instancePrefix . '_' . $containerNameToTry . '_1';
314+
break;
315+
}
316+
}
317+
318+
if (empty($fullContainerName)) {
319+
throw new \RuntimeException('No running docker container found, tried: ' . implode(', ', $containerNameList));
320+
}
321+
322+
// Cache running container
323+
$this->runningContainerCache[$containerName] = $fullContainerName;
324+
325+
return $fullContainerName;
326+
}
242327
}

src/app/CliTools/Utility/DockerUtility.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
class DockerUtility
2828
{
2929

30+
/**
31+
* Docker configuration cache
32+
*
33+
* @var array
34+
*/
35+
static protected $dockerConfigurationCache = array();
36+
3037
/**
3138
* Parse docker configuration (from docker inspect)
3239
*
@@ -36,9 +43,13 @@ class DockerUtility
3643
*/
3744
public static function getDockerConfiguration($container)
3845
{
46+
// Use from cache
47+
if (isset(self::$dockerConfigurationCache[$container])) {
48+
return self::$dockerConfigurationCache[$container];
49+
}
3950

4051
// Build command
41-
$command = new CommandBuilder('docker', 'inspect %s', array($container));
52+
$command = new CommandBuilder('docker', 'inspect %s 2> /dev/null', array($container));
4253

4354
// execute
4455
$executor = new Executor($command);
@@ -62,6 +73,9 @@ public static function getDockerConfiguration($container)
6273
$conf->Config->Env = $envList;
6374
}
6475

76+
// store cache
77+
self::$dockerConfigurationCache[$container] = $conf;
78+
6579
return $conf;
6680
}
6781

src/command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222

2323
error_reporting(E_ALL);
24-
define('CLITOOLS_COMMAND_VERSION', '2.3.2');
24+
define('CLITOOLS_COMMAND_VERSION', '2.3.3');
2525
define('CLITOOLS_ROOT_FS', __DIR__);
2626

2727
require __DIR__ . '/vendor/autoload.php';

src/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
{ "type": "vcs", "url": "https://github.com/jamiebicknell/Growl-GNTP.git" }
55
],
66
"require": {
7-
"symfony/console": "2.*",
7+
"symfony/console": "~2.8",
88
"jamiebicknell/Growl-GNTP": "dev-master",
9-
"symfony/yaml": "^2.6"
9+
"symfony/yaml": "~2.8"
1010
},
1111
"autoload": {
1212
"psr-0": {

src/composer.lock

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ growl = 1
2121
diskusage = 90
2222

2323
[docker]
24-
container = main
24+
container = main,app
2525
interface = docker0
2626
script_env_vars = CLI_SCRIPT
2727
user_env_vars = CLI_USER,APPLICATION_USER

0 commit comments

Comments
 (0)