From b31fd58cc93d6bdfd6feb315fcce05a3539c8027 Mon Sep 17 00:00:00 2001 From: Michael Deck Date: Wed, 26 Aug 2020 23:56:28 -0400 Subject: [PATCH 1/4] Add command to restart and stop containers --- app/Commands/HelpCommand.php | 2 + app/Commands/RestartCommand.php | 72 +++++++++++++++++++++++++++++++++ app/Commands/StopCommand.php | 66 ++++++++++++++++++++++++++++++ app/Shell/Docker.php | 9 +++++ 4 files changed, 149 insertions(+) create mode 100644 app/Commands/RestartCommand.php create mode 100644 app/Commands/StopCommand.php diff --git a/app/Commands/HelpCommand.php b/app/Commands/HelpCommand.php index 023e99ec..22fe0615 100644 --- a/app/Commands/HelpCommand.php +++ b/app/Commands/HelpCommand.php @@ -14,6 +14,8 @@ class HelpCommand extends Command 'enable {service}' => 'Enable the provided service', 'disable' => 'Disable a service from a list of options', 'disable {service}' => 'Disable the provided service', + 'restart' => 'Restart a stopped container', + 'restart {container}' => 'Restart a stopped container', 'list' => 'List all enabled services', ]; diff --git a/app/Commands/RestartCommand.php b/app/Commands/RestartCommand.php new file mode 100644 index 00000000..843ecb18 --- /dev/null +++ b/app/Commands/RestartCommand.php @@ -0,0 +1,72 @@ +initializeCommand(); + + $container = $this->argument('container'); + + if ($container) { + $this->restart($container); + + return; + } + + $this->menu('Containers to restart') + ->addItems($this->restartableContainers()) + ->open(); + +// if (!$option) { +// return; +// } +// +// $this->restart($option); + } + + public function restartableContainers(): array + { + return collect(app(Docker::class)->takeoutContainers())->skip(1)->reduce(function ($containers, $container) { + if(!Str::contains($container[2], 'Up')) { + $containers->push(["$container[0] - $container[1]", function(CliMenu $menu) use ($container) { + $this->restart($menu->getSelectedItem()->getText()); + + foreach($menu->getItems() as $item) { + if($item->getText() === "$container[0] - $container[1]") { + $menu->removeItem($item); + } + } + + $menu->redraw(); + }]); + } + + return $containers; + }, collect())->toArray(); + } + + public function restart(string $container): void + { + if(Str::contains($container, ' -')) { + $container = Str::before($container, ' -'); + } + + app(Docker::class)->startContainer($container); + } +} diff --git a/app/Commands/StopCommand.php b/app/Commands/StopCommand.php new file mode 100644 index 00000000..3b094de5 --- /dev/null +++ b/app/Commands/StopCommand.php @@ -0,0 +1,66 @@ +initializeCommand(); + + $container = $this->argument('container'); + + if ($container) { + $this->stop($container); + + return; + } + + $this->menu('Containers to restart') + ->addItems($this->restartableContainers()) + ->open(); + } + + public function restartableContainers(): array + { + return collect(app(Docker::class)->takeoutContainers())->skip(1)->reduce(function ($containers, $container) { + if(!Str::contains($container[2], 'Up')) { + $containers->push(["$container[0] - $container[1]", function(CliMenu $menu) use ($container) { + $this->stop($menu->getSelectedItem()->getText()); + + foreach($menu->getItems() as $item) { + if($item->getText() === "$container[0] - $container[1]") { + $menu->removeItem($item); + } + } + + $menu->redraw(); + }]); + } + + return $containers; + }, collect())->toArray(); + } + + public function stop(string $container): void + { + if(Str::contains($container, ' -')) { + $container = Str::before($container, ' -'); + } + + app(Docker::class)->stopContainer($container); + } +} diff --git a/app/Shell/Docker.php b/app/Shell/Docker.php index f8bfaf8d..8bad846b 100644 --- a/app/Shell/Docker.php +++ b/app/Shell/Docker.php @@ -34,6 +34,15 @@ public function stopContainer(string $containerId): void } } + public function startContainer(string $containerId): void + { + $process = $this->shell->exec('docker start ' . $containerId); + + if (! $process->isSuccessful()) { + throw new Exception('Failed starting container ' . $containerId); + } + } + public function isInstalled(): bool { $process = $this->shell->execQuietly('docker --version 2>&1'); From 12fa2df4fd53235d5e5e0fa772c200aad2e9c388 Mon Sep 17 00:00:00 2001 From: Michael Deck Date: Fri, 28 Aug 2020 06:04:50 -0400 Subject: [PATCH 2/4] Rename restart command to start command. Add new commands to README. Clean up unneeded commands. --- README.md | 32 +++++++++++++++++++ .../{RestartCommand.php => StartCommand.php} | 24 ++++++-------- app/Commands/StopCommand.php | 8 ++--- 3 files changed, 45 insertions(+), 19 deletions(-) rename app/Commands/{RestartCommand.php => StartCommand.php} (70%) diff --git a/README.md b/README.md index 50f93f6b..3b3e842e 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,38 @@ Passed the short name of a service, disable the enabled service which matches it takeout disable mysql ``` +### Start a stopped container + +Show a list of all stopped containers you can start. + +```bash +takeout start +``` + +### Start a specific stopped container + +Passed the container id of stopped container, start the stopped container which matches it. + +```bash +takeout start {container_id} +``` + +### Stop a running container + +Show a list of all running containers you can stop. + +```bash +takeout stop +``` + +### Stop a specific running container + +Passed the container id of running container, stop the running container which matches it. + +```bash +takeout stop {container_id} +``` + ## Running multiple versions of a dependency Another of Takeout's benefits is that it allows you to have multiple versions of a dependency installed and running at the same time. That means, for example, that you can run both MySQL 5.7 and 8.0 at the same time, on different ports. diff --git a/app/Commands/RestartCommand.php b/app/Commands/StartCommand.php similarity index 70% rename from app/Commands/RestartCommand.php rename to app/Commands/StartCommand.php index 843ecb18..b6235d2a 100644 --- a/app/Commands/RestartCommand.php +++ b/app/Commands/StartCommand.php @@ -9,13 +9,13 @@ use LaravelZero\Framework\Commands\Command; use PhpSchool\CliMenu\CliMenu; -class RestartCommand extends Command +class StartCommand extends Command { use InitializesCommands; - protected $signature = 'restart {container?}'; - protected $description = 'Restart a service.'; + protected $signature = 'start {container?}'; + protected $description = 'Start a stopped container.'; public function handle(): void { @@ -24,28 +24,22 @@ public function handle(): void $container = $this->argument('container'); if ($container) { - $this->restart($container); + $this->start($container); return; } - $this->menu('Containers to restart') - ->addItems($this->restartableContainers()) + $this->menu('Containers to start') + ->addItems($this->startableContainers()) ->open(); - -// if (!$option) { -// return; -// } -// -// $this->restart($option); } - public function restartableContainers(): array + public function startableContainers(): array { return collect(app(Docker::class)->takeoutContainers())->skip(1)->reduce(function ($containers, $container) { if(!Str::contains($container[2], 'Up')) { $containers->push(["$container[0] - $container[1]", function(CliMenu $menu) use ($container) { - $this->restart($menu->getSelectedItem()->getText()); + $this->start($menu->getSelectedItem()->getText()); foreach($menu->getItems() as $item) { if($item->getText() === "$container[0] - $container[1]") { @@ -61,7 +55,7 @@ public function restartableContainers(): array }, collect())->toArray(); } - public function restart(string $container): void + public function start(string $container): void { if(Str::contains($container, ' -')) { $container = Str::before($container, ' -'); diff --git a/app/Commands/StopCommand.php b/app/Commands/StopCommand.php index 3b094de5..abe6b285 100644 --- a/app/Commands/StopCommand.php +++ b/app/Commands/StopCommand.php @@ -29,15 +29,15 @@ public function handle(): void return; } - $this->menu('Containers to restart') - ->addItems($this->restartableContainers()) + $this->menu('Containers to stop') + ->addItems($this->stoppableContainers()) ->open(); } - public function restartableContainers(): array + public function stoppableContainers(): array { return collect(app(Docker::class)->takeoutContainers())->skip(1)->reduce(function ($containers, $container) { - if(!Str::contains($container[2], 'Up')) { + if(Str::contains($container[2], 'Up')) { $containers->push(["$container[0] - $container[1]", function(CliMenu $menu) use ($container) { $this->stop($menu->getSelectedItem()->getText()); From d3559929fa9aa5e730b7980c4040caead4a9492a Mon Sep 17 00:00:00 2001 From: Michael Deck Date: Mon, 31 Aug 2020 10:48:10 -0400 Subject: [PATCH 3/4] Update help command with start and stop command signatures --- app/Commands/HelpCommand.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/Commands/HelpCommand.php b/app/Commands/HelpCommand.php index 22fe0615..e0c6f9be 100644 --- a/app/Commands/HelpCommand.php +++ b/app/Commands/HelpCommand.php @@ -14,8 +14,10 @@ class HelpCommand extends Command 'enable {service}' => 'Enable the provided service', 'disable' => 'Disable a service from a list of options', 'disable {service}' => 'Disable the provided service', - 'restart' => 'Restart a stopped container', - 'restart {container}' => 'Restart a stopped container', + 'start' => 'Start a stopped container', + 'start {container}' => 'Start a stopped container', + 'stop' => 'Stop a running container', + 'stop {container}' => 'Stop a running container', 'list' => 'List all enabled services', ]; From 4396aedd1e17bbb4c4006d2e84f89c1daedc48b1 Mon Sep 17 00:00:00 2001 From: Michael Deck Date: Mon, 31 Aug 2020 12:23:16 -0400 Subject: [PATCH 4/4] Make suggested tweaks to code to enhance readability --- README.md | 4 ++-- app/Commands/HelpCommand.php | 8 ++++---- app/Commands/StartCommand.php | 31 ++++++++++++++----------------- app/Commands/StopCommand.php | 31 ++++++++++++++----------------- 4 files changed, 34 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 3b3e842e..a7909880 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ takeout start ### Start a specific stopped container -Passed the container id of stopped container, start the stopped container which matches it. +Passed the container ID of stopped container, start the stopped container which matches it. ```bash takeout start {container_id} @@ -101,7 +101,7 @@ takeout stop ### Stop a specific running container -Passed the container id of running container, stop the running container which matches it. +Passed the container ID of running container, stop the running container which matches it. ```bash takeout stop {container_id} diff --git a/app/Commands/HelpCommand.php b/app/Commands/HelpCommand.php index e0c6f9be..b1a32d10 100644 --- a/app/Commands/HelpCommand.php +++ b/app/Commands/HelpCommand.php @@ -14,10 +14,10 @@ class HelpCommand extends Command 'enable {service}' => 'Enable the provided service', 'disable' => 'Disable a service from a list of options', 'disable {service}' => 'Disable the provided service', - 'start' => 'Start a stopped container', - 'start {container}' => 'Start a stopped container', - 'stop' => 'Stop a running container', - 'stop {container}' => 'Stop a running container', + 'start' => 'Start a stopped container from a list of options', + 'start {container}' => 'Start the provided stopped container (by ID)', + 'stop' => 'Stop a running container from a list of options', + 'stop {container}' => 'Stop the provided running container (by ID)', 'list' => 'List all enabled services', ]; diff --git a/app/Commands/StartCommand.php b/app/Commands/StartCommand.php index b6235d2a..3e2e5c55 100644 --- a/app/Commands/StartCommand.php +++ b/app/Commands/StartCommand.php @@ -11,17 +11,16 @@ class StartCommand extends Command { - use InitializesCommands; - protected $signature = 'start {container?}'; + protected $signature = 'start {containerId?}'; protected $description = 'Start a stopped container.'; public function handle(): void { $this->initializeCommand(); - $container = $this->argument('container'); + $container = $this->argument('containerId'); if ($container) { $this->start($container); @@ -36,22 +35,20 @@ public function handle(): void public function startableContainers(): array { - return collect(app(Docker::class)->takeoutContainers())->skip(1)->reduce(function ($containers, $container) { - if(!Str::contains($container[2], 'Up')) { - $containers->push(["$container[0] - $container[1]", function(CliMenu $menu) use ($container) { - $this->start($menu->getSelectedItem()->getText()); - - foreach($menu->getItems() as $item) { - if($item->getText() === "$container[0] - $container[1]") { - $menu->removeItem($item); - } + return collect(app(Docker::class)->takeoutContainers())->skip(1)->reject(function($container) { + return Str::contains($container[2], 'Up'); + })->map(function ($container) { + return ["$container[0] - $container[1]", function(CliMenu $menu) use ($container) { + $this->start($menu->getSelectedItem()->getText()); + + foreach($menu->getItems() as $item) { + if($item->getText() === "$container[0] - $container[1]") { + $menu->removeItem($item); } + } - $menu->redraw(); - }]); - } - - return $containers; + $menu->redraw(); + }]; }, collect())->toArray(); } diff --git a/app/Commands/StopCommand.php b/app/Commands/StopCommand.php index abe6b285..160656ab 100644 --- a/app/Commands/StopCommand.php +++ b/app/Commands/StopCommand.php @@ -11,17 +11,16 @@ class StopCommand extends Command { - use InitializesCommands; - protected $signature = 'stop {container?}'; + protected $signature = 'stop {containerId?}'; protected $description = 'Stop a service.'; public function handle(): void { $this->initializeCommand(); - $container = $this->argument('container'); + $container = $this->argument('containerId'); if ($container) { $this->stop($container); @@ -36,22 +35,20 @@ public function handle(): void public function stoppableContainers(): array { - return collect(app(Docker::class)->takeoutContainers())->skip(1)->reduce(function ($containers, $container) { - if(Str::contains($container[2], 'Up')) { - $containers->push(["$container[0] - $container[1]", function(CliMenu $menu) use ($container) { - $this->stop($menu->getSelectedItem()->getText()); - - foreach($menu->getItems() as $item) { - if($item->getText() === "$container[0] - $container[1]") { - $menu->removeItem($item); - } + return collect(app(Docker::class)->takeoutContainers())->skip(1)->filter(function($container) { + return Str::contains($container[2], 'Up'); + })->map(function ($container) { + return ["$container[0] - $container[1]", function(CliMenu $menu) use ($container) { + $this->stop($menu->getSelectedItem()->getText()); + + foreach($menu->getItems() as $item) { + if($item->getText() === "$container[0] - $container[1]") { + $menu->removeItem($item); } + } - $menu->redraw(); - }]); - } - - return $containers; + $menu->redraw(); + }]; }, collect())->toArray(); }