Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to restart stopped containers and stop running containers #71

Merged
merged 4 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions app/Commands/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +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 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',
];

Expand Down
63 changes: 63 additions & 0 deletions app/Commands/StartCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Commands;

use App\InitializesCommands;
use App\Services;
use App\Shell\Docker;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;
use PhpSchool\CliMenu\CliMenu;

class StartCommand extends Command
{
use InitializesCommands;

protected $signature = 'start {containerId?}';
protected $description = 'Start a stopped container.';

public function handle(): void
{
$this->initializeCommand();

$container = $this->argument('containerId');

if ($container) {
$this->start($container);

return;
}

$this->menu('Containers to start')
->addItems($this->startableContainers())
->open();
}

public function startableContainers(): array
{
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();
}];
}, collect())->toArray();
}

public function start(string $container): void
{
if(Str::contains($container, ' -')) {
$container = Str::before($container, ' -');
}

app(Docker::class)->startContainer($container);
}
}
63 changes: 63 additions & 0 deletions app/Commands/StopCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Commands;

use App\InitializesCommands;
use App\Services;
use App\Shell\Docker;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;
use PhpSchool\CliMenu\CliMenu;

class StopCommand extends Command
mattstauffer marked this conversation as resolved.
Show resolved Hide resolved
{
use InitializesCommands;

protected $signature = 'stop {containerId?}';
protected $description = 'Stop a service.';

public function handle(): void
{
$this->initializeCommand();

$container = $this->argument('containerId');

if ($container) {
$this->stop($container);

return;
}

$this->menu('Containers to stop')
->addItems($this->stoppableContainers())
->open();
}

public function stoppableContainers(): array
{
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();
}];
}, collect())->toArray();
}

public function stop(string $container): void
{
if(Str::contains($container, ' -')) {
$container = Str::before($container, ' -');
}

app(Docker::class)->stopContainer($container);
}
}
9 changes: 9 additions & 0 deletions app/Shell/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down