Initial commit.
This commit is contained in:
12
.editorconfig
Normal file
12
.editorconfig
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
|
|
||||||
|
[composer.json]
|
||||||
|
indent_size = 4
|
||||||
41
README.md
Normal file
41
README.md
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# Robo Docker Compose
|
||||||
|
|
||||||
|
Run docker compose commands from the Robo task runner.
|
||||||
|
|
||||||
|
### Getting Started
|
||||||
|
|
||||||
|
First, you'll need to download the robo docker compose library using composer:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
composer require droath/robo-docker-compose:~0.0.1
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use \Droath\RoboDockerCompose\Task\loadTasks;
|
||||||
|
|
||||||
|
// Command equivalent: `docker-composer up -d -remove-orphans`
|
||||||
|
$this->taskDockerComposeUp()
|
||||||
|
->detachedMode()
|
||||||
|
->removeOrphans()
|
||||||
|
->run();
|
||||||
|
|
||||||
|
// Command equivalent: `docker-composer down`
|
||||||
|
$this->taskDockerComposeDown()
|
||||||
|
->run();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Support
|
||||||
|
|
||||||
|
The following commands have been implemented:
|
||||||
|
|
||||||
|
- docker-compose up
|
||||||
|
- docker-compose down
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
19
composer.json
Normal file
19
composer.json
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "droath/robo-docker-compose",
|
||||||
|
"description": "Docker compose for Robo Task Runner",
|
||||||
|
"license": "MIT",
|
||||||
|
"keywords": ["docker", "compose", "robo", "task runner"],
|
||||||
|
"type": "robo-tasks",
|
||||||
|
"require": {
|
||||||
|
"php": ">=5.5.0",
|
||||||
|
"consolidation/robo": "^1.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^5.5"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Droath\\RoboDockerCompose\\": "./src"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
22
phpunit.xml.dist
Normal file
22
phpunit.xml.dist
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit backupGlobals="false"
|
||||||
|
backupStaticAttributes="false"
|
||||||
|
bootstrap="./tests/bootstrap.php"
|
||||||
|
colors="true"
|
||||||
|
convertErrorsToExceptions="true"
|
||||||
|
convertNoticesToExceptions="true"
|
||||||
|
convertWarningsToExceptions="true"
|
||||||
|
processIsolation="false"
|
||||||
|
stopOnFailure="false"
|
||||||
|
syntaxCheck="false">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="Test Suite">
|
||||||
|
<directory suffix="Test.php">./tests</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
<filter>
|
||||||
|
<whitelist>
|
||||||
|
<directory suffix=".php">./src</directory>
|
||||||
|
</whitelist>
|
||||||
|
</filter>
|
||||||
|
</phpunit>
|
||||||
61
src/Task/Base.php
Normal file
61
src/Task/Base.php
Normal 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
74
src/Task/Down.php
Normal 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
135
src/Task/Up.php
Normal 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
25
src/Task/loadTasks.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
3
tests/bootstrap.php
Normal file
3
tests/bootstrap.php
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
Reference in New Issue
Block a user