Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0bd195e5a9 | ||
|
|
0d799fca6c | ||
|
|
a5c405ec02 | ||
|
|
c3cc0c2e1f | ||
|
|
65893fc1a9 | ||
|
|
c2242c4b93 | ||
|
|
463daf3a2d | ||
|
|
761f956826 | ||
|
|
01740ee9e4 | ||
|
|
ce1360c733 | ||
|
|
4697d956fa | ||
|
|
e3ace8a3ea |
@@ -7,7 +7,7 @@ Run docker compose commands from the Robo task runner.
|
|||||||
First, you'll need to download the robo docker compose library using composer:
|
First, you'll need to download the robo docker compose library using composer:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
composer require droath/robo-docker-compose:~0.0.1
|
composer require --dev droath/robo-docker-compose
|
||||||
```
|
```
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
@@ -33,7 +33,11 @@ composer require droath/robo-docker-compose:~0.0.1
|
|||||||
The following commands have been implemented:
|
The following commands have been implemented:
|
||||||
|
|
||||||
- docker-compose up
|
- docker-compose up
|
||||||
|
- docker-compose exec
|
||||||
- docker-compose down
|
- docker-compose down
|
||||||
|
- docker-compose start
|
||||||
|
- docker-compose restart
|
||||||
|
- docker-compose pause
|
||||||
|
|
||||||
I'll be adding the rests of the docker-compose commands shortly. Or if you want
|
I'll be adding the rests of the docker-compose commands shortly. Or if you want
|
||||||
to create a PR with additional commands that would be much appreciated.
|
to create a PR with additional commands that would be much appreciated.
|
||||||
|
|||||||
53
src/DockerServicesTrait.php
Executable file
53
src/DockerServicesTrait.php
Executable file
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Droath\RoboDockerCompose;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define docker services trait.
|
||||||
|
*/
|
||||||
|
trait DockerServicesTrait
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Docker compose services.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $services = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add docker composer service.
|
||||||
|
*
|
||||||
|
* @param string $service
|
||||||
|
* The docker services.
|
||||||
|
*/
|
||||||
|
public function setService($service)
|
||||||
|
{
|
||||||
|
$this->services[] = $service;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add docker composer services.
|
||||||
|
*
|
||||||
|
* @param array $services
|
||||||
|
* An array of services.
|
||||||
|
*/
|
||||||
|
public function setServices(array $services)
|
||||||
|
{
|
||||||
|
foreach ($services as $service) {
|
||||||
|
$this->setService($service);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getCommand()
|
||||||
|
{
|
||||||
|
// Append the services to the end of the command.
|
||||||
|
return parent::getCommand() . ' ' . implode(' ', $this->services);
|
||||||
|
}
|
||||||
|
}
|
||||||
41
src/ExecutableArguments.php
Normal file
41
src/ExecutableArguments.php
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Droath\RoboDockerCompose;
|
||||||
|
|
||||||
|
use Robo\Common\CommandArguments;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define command executable arguments.
|
||||||
|
*/
|
||||||
|
trait ExecutableArguments
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Executable arguments.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $executableArgs = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add executable options.
|
||||||
|
*
|
||||||
|
* @param string $option
|
||||||
|
* The executable option name.
|
||||||
|
* @param string $value
|
||||||
|
* The executable option value.
|
||||||
|
*/
|
||||||
|
protected function execOption($option, $value = null)
|
||||||
|
{
|
||||||
|
if (isset($option)) {
|
||||||
|
if (strpos($option, '-') !== 0) {
|
||||||
|
$option = "--$option";
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->executableArgs .= null == $option ? '' : ' ' . $option;
|
||||||
|
$this->executableArgs .= null == $value ? '' : ' ' . CommandArguments::escape($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,16 +2,19 @@
|
|||||||
|
|
||||||
namespace Droath\RoboDockerCompose\Task;
|
namespace Droath\RoboDockerCompose\Task;
|
||||||
|
|
||||||
|
use Droath\RoboDockerCompose\ExecutableArguments;
|
||||||
use Robo\Common\ExecOneCommand;
|
use Robo\Common\ExecOneCommand;
|
||||||
|
use Robo\Contract\CommandInterface;
|
||||||
use Robo\Exception\TaskException;
|
use Robo\Exception\TaskException;
|
||||||
use Robo\Task\BaseTask;
|
use Robo\Task\BaseTask;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Docker compose base class.
|
* Docker compose base class.
|
||||||
*/
|
*/
|
||||||
abstract class Base extends BaseTask
|
abstract class Base extends BaseTask implements CommandInterface
|
||||||
{
|
{
|
||||||
use ExecOneCommand;
|
use ExecOneCommand;
|
||||||
|
use ExecutableArguments;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executable.
|
* Executable.
|
||||||
@@ -35,11 +38,9 @@ abstract class Base extends BaseTask
|
|||||||
*/
|
*/
|
||||||
public function __construct($pathToDockerCompose = null)
|
public function __construct($pathToDockerCompose = null)
|
||||||
{
|
{
|
||||||
$this->executable = $pathToDockerCompose;
|
$this->executable = isset($pathToDockerCompose)
|
||||||
|
? $pathToDockerCompose
|
||||||
if (!$this->executable) {
|
: $this->findExecutablePhar('docker-compose');
|
||||||
$this->executable = $this->findExecutablePhar('docker-compose');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->executable) {
|
if (!$this->executable) {
|
||||||
throw new TaskException(
|
throw new TaskException(
|
||||||
@@ -49,6 +50,66 @@ abstract class Base extends BaseTask
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specify an alternate compose file.
|
||||||
|
*
|
||||||
|
* @param string $filename
|
||||||
|
* An alternative docker composer file
|
||||||
|
*/
|
||||||
|
public function file($filename)
|
||||||
|
{
|
||||||
|
if (!file_exists($filename)) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf("File %s wasn't found on the filesystem", $filename)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->execOption('file', $filename);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specify multiple composer files.
|
||||||
|
*
|
||||||
|
* @param array $files
|
||||||
|
* An array of alternative composer files.
|
||||||
|
*/
|
||||||
|
public function files(array $files)
|
||||||
|
{
|
||||||
|
foreach ($files as $filename) {
|
||||||
|
$this->file($filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specify an alternate project name.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* An alternative docker compose project name.
|
||||||
|
*/
|
||||||
|
public function projectName($name)
|
||||||
|
{
|
||||||
|
$this->execOption('project-name', $name);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Daemon socket to connect to.
|
||||||
|
*
|
||||||
|
* @param string $hostname
|
||||||
|
* The name of the host to connect to.
|
||||||
|
*/
|
||||||
|
public function host($hostname)
|
||||||
|
{
|
||||||
|
$this->execOption('host', $hostname);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
@@ -68,8 +129,8 @@ abstract class Base extends BaseTask
|
|||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getCommand()
|
public function getCommand()
|
||||||
{
|
{
|
||||||
return "{$this->executable} {$this->action} {$this->arguments}";
|
return "{$this->executable} {$this->executableArgs} {$this->action} {$this->arguments}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
153
src/Task/Execute.php
Normal file
153
src/Task/Execute.php
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Droath\RoboDockerCompose\Task;
|
||||||
|
|
||||||
|
use Robo\Contract\CommandInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Docker compose execute command.
|
||||||
|
*/
|
||||||
|
class Execute extends Base
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Execute command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $command;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute container.
|
||||||
|
*
|
||||||
|
* @var string.
|
||||||
|
*/
|
||||||
|
protected $container;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected $action = 'exec';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set docker container.
|
||||||
|
*
|
||||||
|
* @param $container
|
||||||
|
* The container name.
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setContainer($container)
|
||||||
|
{
|
||||||
|
$this->container = $container;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set execute command.
|
||||||
|
*
|
||||||
|
* @param string|CommandInterface $command
|
||||||
|
* The command to execute.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function exec($command)
|
||||||
|
{
|
||||||
|
if ($command instanceof CommandInterface) {
|
||||||
|
$command = $command->getCommand();
|
||||||
|
}
|
||||||
|
$this->command = $command;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run command in the background.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function detachedMode()
|
||||||
|
{
|
||||||
|
$this->arg('-d');
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Give extended privileges to the process.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function privileged()
|
||||||
|
{
|
||||||
|
$this->option('privileged');
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the command as this user.
|
||||||
|
*
|
||||||
|
* @param $user
|
||||||
|
* The user on which to run the command under.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function user($user)
|
||||||
|
{
|
||||||
|
$this->option('user', $user);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable pseudo-tty allocation.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function disablePseudoTty()
|
||||||
|
{
|
||||||
|
$this->arg('-T');
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Index of the container.
|
||||||
|
*
|
||||||
|
* @param $index
|
||||||
|
* The container index.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function index($index)
|
||||||
|
{
|
||||||
|
$this->option('index', $index);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set environment variables
|
||||||
|
*
|
||||||
|
* @param $key
|
||||||
|
* The environment key.
|
||||||
|
* @param $value
|
||||||
|
* The environment value.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function envVariable($key, $value)
|
||||||
|
{
|
||||||
|
$this->option('env', "{$key}={$value}");
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getCommand()
|
||||||
|
{
|
||||||
|
return parent::getCommand() . "{$this->container} {$this->command}";
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/Task/Pause.php
Executable file
18
src/Task/Pause.php
Executable file
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Droath\RoboDockerCompose\Task;
|
||||||
|
|
||||||
|
use Droath\RoboDockerCompose\DockerServicesTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define docker compose pause command.
|
||||||
|
*/
|
||||||
|
class Pause extends Base
|
||||||
|
{
|
||||||
|
use DockerServicesTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected $action = 'pause';
|
||||||
|
}
|
||||||
33
src/Task/Restart.php
Executable file
33
src/Task/Restart.php
Executable file
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Droath\RoboDockerCompose\Task;
|
||||||
|
|
||||||
|
use Droath\RoboDockerCompose\DockerServicesTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define docker compose restart command.
|
||||||
|
*/
|
||||||
|
class Restart extends Base
|
||||||
|
{
|
||||||
|
use DockerServicesTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected $action = 'restart';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specify a shutdown timeout in seconds.
|
||||||
|
*
|
||||||
|
* @param int $timeout
|
||||||
|
* The timeout in seconds.
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function timeout($timeout = 10)
|
||||||
|
{
|
||||||
|
$this->option('timeout', $timeout);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/Task/Start.php
Executable file
18
src/Task/Start.php
Executable file
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Droath\RoboDockerCompose\Task;
|
||||||
|
|
||||||
|
use Droath\RoboDockerCompose\DockerServicesTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define docker compose start command.
|
||||||
|
*/
|
||||||
|
class Start extends Base
|
||||||
|
{
|
||||||
|
use DockerServicesTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected $action = 'start';
|
||||||
|
}
|
||||||
47
src/Task/Up.php
Normal file → Executable file
47
src/Task/Up.php
Normal file → Executable file
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Droath\RoboDockerCompose\Task;
|
namespace Droath\RoboDockerCompose\Task;
|
||||||
|
|
||||||
|
use Droath\RoboDockerCompose\DockerServicesTrait;
|
||||||
use Robo\Exception\TaskException;
|
use Robo\Exception\TaskException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -9,18 +10,13 @@ use Robo\Exception\TaskException;
|
|||||||
*/
|
*/
|
||||||
class Up extends Base
|
class Up extends Base
|
||||||
{
|
{
|
||||||
|
use DockerServicesTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
protected $action = 'up';
|
protected $action = 'up';
|
||||||
|
|
||||||
/**
|
|
||||||
* Docker compose services.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $services = [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command detached mode.
|
* Command detached mode.
|
||||||
*
|
*
|
||||||
@@ -28,34 +24,6 @@ class Up extends Base
|
|||||||
*/
|
*/
|
||||||
protected $detachedMode = false;
|
protected $detachedMode = false;
|
||||||
|
|
||||||
/**
|
|
||||||
* Add docker composer service.
|
|
||||||
*
|
|
||||||
* @param string $service
|
|
||||||
* The docker services.
|
|
||||||
*/
|
|
||||||
public function setService($service)
|
|
||||||
{
|
|
||||||
$this->services[] = $service;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add docker composer services.
|
|
||||||
*
|
|
||||||
* @param array $services
|
|
||||||
* An array of services.
|
|
||||||
*/
|
|
||||||
public function setServices(array $services)
|
|
||||||
{
|
|
||||||
foreach ($services as $service) {
|
|
||||||
$this->setService($service);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run containers in the background.
|
* Run containers in the background.
|
||||||
*/
|
*/
|
||||||
@@ -147,15 +115,6 @@ class Up extends Base
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
protected function getCommand()
|
|
||||||
{
|
|
||||||
// Append the services to the end of the command.
|
|
||||||
return parent::getCommand() . ' ' . implode(' ', $this->services);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set command detached mode.
|
* Set command detached mode.
|
||||||
*/
|
*/
|
||||||
|
|||||||
35
src/Task/loadTasks.php
Normal file → Executable file
35
src/Task/loadTasks.php
Normal file → Executable file
@@ -16,10 +16,43 @@ trait loadTasks
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Docker compose up task.
|
* Docker compose down task.
|
||||||
*/
|
*/
|
||||||
protected function taskDockerComposeDown($pathToDockerCompose = null)
|
protected function taskDockerComposeDown($pathToDockerCompose = null)
|
||||||
{
|
{
|
||||||
return $this->task(Down::class, $pathToDockerCompose);
|
return $this->task(Down::class, $pathToDockerCompose);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Docker compose pause task.
|
||||||
|
*/
|
||||||
|
protected function taskDockerComposePause($pathToDockerCompose = null)
|
||||||
|
{
|
||||||
|
return $this->task(Pause::class, $pathToDockerCompose);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Docker compose start task.
|
||||||
|
*/
|
||||||
|
protected function taskDockerComposeStart($pathToDockerCompose = null)
|
||||||
|
{
|
||||||
|
return $this->task(Start::class, $pathToDockerCompose);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Docker compose restart task.
|
||||||
|
*/
|
||||||
|
protected function taskDockerComposeRestart($pathToDockerCompose = null)
|
||||||
|
{
|
||||||
|
return $this->task(Restart::class, $pathToDockerCompose);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Docker compose execute task.
|
||||||
|
*/
|
||||||
|
protected function taskDockerComposeExecute($pathToDockerCompose = null)
|
||||||
|
{
|
||||||
|
return $this->task(Execute::class, $pathToDockerCompose);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user