Initial commit.

This commit is contained in:
Travis Tomka
2017-03-13 12:52:47 -06:00
commit 75264c6c28
9 changed files with 392 additions and 0 deletions

61
src/Task/Base.php Normal file
View File

@@ -0,0 +1,61 @@
<?php
namespace Droath\RoboDockerCompose\Task;
use Robo\Common\ExecOneCommand;
use Robo\Exception\TaskException;
use Robo\Task\BaseTask;
/**
* Docker compose base class.
*/
abstract class Base extends BaseTask
{
use ExecOneCommand;
/**
* Executable.
*
* @var string
*/
protected $executable;
/**
* Executable action.
*
* @var string
*/
protected $action = '';
/**
* Docker compose constructor.
*
* @param string $pathToDockerCompose
* A custom path to the docker-compose executable binary.
*/
public function __construct($pathToDockerCompose = null)
{
$this->executable = $pathToDockerCompose;
if (!$this->executable) {
$this->executable = $this->findExecutablePhar('docker-compose');
}
if (!$this->executable) {
throw new TaskException(
__CLASS__,
'Unable to find the docker-compose executable.'
);
}
}
/**
* Get docker-compose command.
*
* @return string
*/
protected function getCommand()
{
return "{$this->executable} {$this->action} {$this->arguments}";
}
}

74
src/Task/Down.php Normal file
View File

@@ -0,0 +1,74 @@
<?php
namespace Droath\RoboDockerCompose\Task;
use Robo\Exception\TaskException;
/**
* Define docker compose down command.
*/
class Down extends Base
{
/**
* {@inheritdoc}
*/
protected $action = 'down';
/**
* Remove images.
*
* @param string $type The image type to remove.
* The following types are allowed:
* - all
* Remove all images used by any service
* - local
* Remove only images that don't have a custom tag.
*/
public function rmi($type)
{
$allowed = ['all', 'local'];
if (!in_array($type, $allowed)) {
throw new TaskException(
__CLASS__,
'Invalid type given to --rmi option.'
);
}
$this->option('rmi', $type);
return $this;
}
/**
* Remove named volumes declared in the volumes section of the Compose file
* and anonymous volumes.
*/
public function volumes()
{
$this->option('volumes');
return $this;
}
/**
* Remove containers for services not defined in the Compose file.
*/
public function removeOrphans()
{
$this->option('remove-orphans');
return $this;
}
/**
* {@inheritdoc}
*/
public function run()
{
$command = $this->getCommand();
$this->printTaskInfo('Docker Down: {command}', ['command' => $command]);
return $this->executeCommand($command);
}
}

135
src/Task/Up.php Normal file
View File

@@ -0,0 +1,135 @@
<?php
namespace Droath\RoboDockerCompose\Task;
use Robo\Exception\TaskException;
/**
* Define docker compose up command.
*/
class Up extends Base
{
/**
* {@inheritdoc}
*/
protected $action = 'up';
/**
* Command detached mode.
*
* @var bool
*/
protected $detachedMode = false;
/**
* Run containers in the background.
*/
public function detachedMode()
{
$this
->arg('-d')
->setDetachedMode();
return $this;
}
/**
* Produce monochrome output.
*/
public function noColor()
{
$this->option('no-color');
return $this;
}
/**
* Don't start linked services.
*/
public function noDeps()
{
$this->option('no-deps');
return $this;
}
/**
* Recreate containers even if their configuration and image haven't
* changed.
*/
public function forceRecreate()
{
$this->option('force-recreate');
return $this;
}
/**
* Don't build an image, even if it's missing.
*/
public function noBuild()
{
$this->option('no-build');
return $this;
}
/**
* Stops all containers if any container was stopped.
*/
public function abortOnContainerExit()
{
if ($this->detachedMode) {
throw new TaskException(
__CLASS__,
'Abort on container exit is not supported in detached mode.'
);
}
$this->option('abort-on-container-exit');
return $this;
}
/**
* Timeout in seconds for container shutdown when attached or when
* containers are already running.
*/
public function timeout($timeout = 10)
{
$this->option('timeout', $timeout);
return $this;
}
/**
* Remove containers for services not defined in the Compose file.
*/
public function removeOrphans()
{
$this->option('remove-orphans');
return $this;
}
/**
* {@inheritdoc}
*/
public function run()
{
$command = $this->getCommand();
$this->printTaskInfo('Docker Up: {command}', ['command' => $command]);
return $this->executeCommand($command);
}
/**
* Set command detached mode.
*/
protected function setDetachedMode()
{
$this->detachedMode = true;
return $this;
}
}

25
src/Task/loadTasks.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
namespace Droath\RoboDockerCompose\Task;
/**
* Load docker compose tasks.
*/
trait loadTasks
{
/**
* Docker compose up task.
*/
protected function taskDockerComposeUp($pathToDockerCompose = null)
{
return $this->task(Up::class, $pathToDockerCompose);
}
/**
* Docker compose up task.
*/
protected function taskDockerComposeDown($pathToDockerCompose = null)
{
return $this->task(Down::class, $pathToDockerCompose);
}
}