Skip to content

Commit 8b55d96

Browse files
committed
Merge branch 'release/2.3.4'
2 parents 7bfaa3e + b163dfe commit 8b55d96

6 files changed

Lines changed: 48 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
CliTools Changelog
22
==================
33

4-
next - UPCOMING
4+
2.3.4 - 2016-03-31
55
------------------
6+
- Implement docker container name fetching by using docker-compose
7+
- Implement docker exec without needing sudo
68

79
2.3.3 - 2016-03-03
810
------------------

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.3](https://img.shields.io/badge/latest-v2.3.3-green.svg?style=flat)](https://github.com/webdevops/clitools/releases/tag/2.3.3)
3+
[![latest v2.3.4](https://img.shields.io/badge/latest-v2.3.4-green.svg?style=flat)](https://github.com/webdevops/clitools/releases/tag/2.3.4)
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: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,11 @@ protected function checkIfDockerContainerIsAvailable($containerName)
184184
*
185185
* @param string $containerName Container name
186186
* @param CommandBuilderInterface $comamnd Command
187+
* @param callback|null $dockerCommandCallback Docker command callback
187188
*
188189
* @return int|null|void
189190
*/
190-
protected function executeDockerExec($containerName, CommandBuilderInterface $command)
191+
protected function executeDockerExec($containerName, CommandBuilderInterface $command, callable $dockerCommandCallback = null)
191192
{
192193
if (empty($containerName)) {
193194
$this->output->writeln('<p-error>No container specified</p-error>');
@@ -216,7 +217,12 @@ protected function executeDockerExec($containerName, CommandBuilderInterface $co
216217
) . '" in docker container "' . $dockerContainerName . '" ...</info>'
217218
);
218219

219-
$dockerCommand = new CommandBuilder('docker', 'exec -ti %s', array($dockerContainerName));
220+
$dockerCommand = new CommandBuilder('docker', 'exec -ti');
221+
if ($dockerCommandCallback) {
222+
$dockerCommandCallback($dockerCommand);
223+
}
224+
$dockerCommand->addArgument($dockerContainerName);
225+
220226
$dockerCommand->append($command, false);
221227
$dockerCommand->executeInteractive();
222228
} else {
@@ -296,7 +302,8 @@ protected function executeDockerComposeRun($containerName, CommandBuilderInterfa
296302
* @param null $containerName Poissible Container name (csv)
297303
* @return null|string
298304
*/
299-
protected function findAndBuildContainerName($containerName = null) {
305+
protected function findAndBuildContainerName($containerName = null)
306+
{
300307
// Use cached container name
301308
if (isset($this->runningContainerCache[$containerName])) {
302309
return $this->runningContainerCache[$containerName];
@@ -305,16 +312,27 @@ protected function findAndBuildContainerName($containerName = null) {
305312
$fullContainerName = null;
306313

307314
$path = $this->getDockerPath();
308-
$instancePrefix = \CliTools\Utility\DockerUtility::getDockerInstancePrefix($path);
315+
$oldPath = getcwd();
316+
317+
chdir($path);
309318

310319
$containerNameList = PhpUtility::trimExplode(',', $containerName);
311320
foreach ($containerNameList as $containerNameToTry) {
312-
if ($this->checkIfDockerContainerIsAvailable($containerNameToTry, 'id')) {
313-
$fullContainerName = $instancePrefix . '_' . $containerNameToTry . '_1';
321+
try {
322+
$command = new CommandBuilder('docker-compose');
323+
$command
324+
->addArgumentTemplate('ps -q %s', $containerNameToTry)
325+
->setOutputRedirect(CommandBuilder::OUTPUT_REDIRECT_NO_STDERR);
326+
$fullContainerName = $command->execute()->getOutputString();
314327
break;
328+
} catch (\Exception $e) {
329+
// container not running
330+
continue;
315331
}
316332
}
317333

334+
chdir($oldPath);
335+
318336
if (empty($fullContainerName)) {
319337
throw new \RuntimeException('No running docker container found, tried: ' . implode(', ', $containerNameList));
320338
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
*/
2323

24+
use CliTools\Shell\CommandBuilder\CommandBuilder;
2425
use CliTools\Shell\CommandBuilder\RemoteCommandBuilder;
2526
use CliTools\Utility\PhpUtility;
2627
use Symfony\Component\Console\Input\InputInterface;
@@ -86,14 +87,16 @@ public function execute(InputInterface $input, OutputInterface $output)
8687
$command->parse($cliScript)
8788
->addArgumentList($paramList);
8889

90+
$dockerCommandCallback = null;
91+
8992
if (!empty($cliUser)) {
90-
// sudo wrapping as cli user
91-
$commandSudo = new RemoteCommandBuilder('sudo', '-H -E -u %s', array($cliUser));
92-
$commandSudo->append($command, false);
93-
$command = $commandSudo;
93+
// Run script as specific user
94+
$dockerCommandCallback = function(CommandBuilder $command) use ($cliUser) {
95+
$command->addArgumentTemplate('-u %s', $cliUser);
96+
};
9497
}
9598

96-
$this->executeDockerExec($container, $command);
99+
$this->executeDockerExec($container, $command, $dockerCommandCallback);
97100
break;
98101

99102
###########################

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.3');
24+
define('CLITOOLS_COMMAND_VERSION', '2.3.4');
2525
define('CLITOOLS_ROOT_FS', __DIR__);
2626

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

src/composer.lock

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

0 commit comments

Comments
 (0)