Skip to content

Commit 8a5b49c

Browse files
committed
Added possibility to set multiple container names
1 parent 60f2406 commit 8a5b49c

3 files changed

Lines changed: 103 additions & 4 deletions

File tree

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/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)