Created
June 25, 2015 09:37
-
-
Save katzefudder/fd700351fffc63d1bfb2 to your computer and use it in GitHub Desktop.
symfony command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
namespace DockerImages; | |
require_once __DIR__.'/vendor/autoload.php'; | |
use Symfony\Component\Console\Application; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Question\Question; | |
class ConnectCommand extends Command { | |
private $sshUser; | |
/** | |
* Configures the current command. | |
*/ | |
protected function configure() { | |
$this->setName('connect') | |
->setDescription('Connect to Docker Container') | |
->addArgument( | |
'id', | |
InputArgument::OPTIONAL, | |
'What Container would you like to connect to?' | |
)->addOption( | |
'user', | |
null, | |
InputOption::VALUE_OPTIONAL, | |
'What user would you like to connect via SSH?' | |
); | |
} | |
/** | |
* @param InputInterface $input | |
* @param OutputInterface $output | |
* @return int|null|void | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) { | |
$id = $input->getArgument('id'); | |
$this->sshUser = $input->getOption('user') ? $input->getOption('user') : 'qa'; | |
$processes = $this->getRunningProcesses(); | |
if (empty($processes)) { | |
$output->write("There are no Docker containers running on this machine\n"); | |
exit(1); | |
} | |
if ($id) { | |
$this->connectContainer($id, $this->sshUser); | |
exit(0); | |
} else { | |
$output->write($this->showRunningContainers($processes)); | |
$helper = $this->getHelper('question'); | |
$question = new Question("What Docker Container would you like to connect to?\n"); | |
$id = $helper->ask($input, $output, $question); | |
if (array_key_exists($id-1, $processes)) { | |
$this->connectContainer($processes[$id - 1], $this->sshUser); | |
} else { | |
$output->write("There's no Docker container with that ID\n"); | |
exit(1); | |
} | |
} | |
} | |
/** | |
* get the running Docker processes | |
* @return array | |
*/ | |
protected function getRunningProcesses() { | |
$command = 'sudo docker ps -q'; | |
$processes = null; | |
exec($command, $processes); | |
return $processes; | |
} | |
/** | |
* create some kind of a table showing the running Docker containers | |
* @param $processes | |
* @return string | |
*/ | |
protected function showRunningContainers($processes) { | |
$output = "Docker Containers running on this machine\n"; | |
$output .= "--------------------------------------------\n"; | |
if (is_array($processes) && !empty($processes)) { | |
$i = 1; | |
foreach ($processes as $process) { | |
$output .= "$i) $process with IP ".$this->getDockerContainerIp($process)." (".$this->getDockerContainerName($process).")\n"; | |
$i++; | |
} | |
} | |
$output .= "--------------------------------------------\n"; | |
return $output; | |
} | |
/** | |
* fetch the Docker Container name by id | |
* @param $id | |
* @return mixed | |
*/ | |
protected function getDockerContainerName($id) { | |
$command = "sudo docker inspect -f '{{ .Name }}' $id"; | |
$ip = null; | |
exec($command, $ip); | |
return $ip[0]; | |
} | |
/** | |
* fetch the Docker Container IP by it's ID | |
* @param $id | |
* @return mixed | |
*/ | |
protected function getDockerContainerIp($id) { | |
$command = "sudo docker inspect -f '{{ .NetworkSettings.IPAddress }}' $id"; | |
$ip = null; | |
exec($command, $ip); | |
return $ip[0]; | |
} | |
/** | |
* connect to the Docker Container via SSH | |
* @param $id | |
*/ | |
protected function connectContainer($id, $user) { | |
$command = "ssh $user@$(sudo docker inspect -f '{{ .NetworkSettings.IPAddress }}' $id)"; | |
passthru($command); | |
exit(0); | |
} | |
} | |
$app = new Application(); | |
$app->add(new ConnectCommand()); | |
$app->run(); |
./ConnectCommand.php connect
Docker Containers running on this machine
--------------------------------------------
1) af8ed09f3796 with IP 172.17.0.166 (/frontenddocker_frontend_1)
2) e7d101ed4f23 with IP 172.17.0.161 (/frontenddocker_database_1)
--------------------------------------------
What Docker Container would you like to connect to?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
composer require symfony/console