From 75264c6c2800962f426fd97b59f1c4f323e7e984 Mon Sep 17 00:00:00 2001 From: Travis Tomka Date: Mon, 13 Mar 2017 12:52:47 -0600 Subject: [PATCH] Initial commit. --- .editorconfig | 12 ++++ README.md | 41 +++++++++++++ composer.json | 19 ++++++ phpunit.xml.dist | 22 +++++++ src/Task/Base.php | 61 +++++++++++++++++++ src/Task/Down.php | 74 ++++++++++++++++++++++ src/Task/Up.php | 135 +++++++++++++++++++++++++++++++++++++++++ src/Task/loadTasks.php | 25 ++++++++ tests/bootstrap.php | 3 + 9 files changed, 392 insertions(+) create mode 100644 .editorconfig create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/Task/Base.php create mode 100644 src/Task/Down.php create mode 100644 src/Task/Up.php create mode 100644 src/Task/loadTasks.php create mode 100644 tests/bootstrap.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..dc92228 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..ff6e5d2 --- /dev/null +++ b/README.md @@ -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 +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. + + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a52b1bd --- /dev/null +++ b/composer.json @@ -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" + } + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..ca14c2a --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,22 @@ + + + + + ./tests + + + + + ./src + + + diff --git a/src/Task/Base.php b/src/Task/Base.php new file mode 100644 index 0000000..d2e1b17 --- /dev/null +++ b/src/Task/Base.php @@ -0,0 +1,61 @@ +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}"; + } +} diff --git a/src/Task/Down.php b/src/Task/Down.php new file mode 100644 index 0000000..c670d39 --- /dev/null +++ b/src/Task/Down.php @@ -0,0 +1,74 @@ +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); + } +} diff --git a/src/Task/Up.php b/src/Task/Up.php new file mode 100644 index 0000000..3c54294 --- /dev/null +++ b/src/Task/Up.php @@ -0,0 +1,135 @@ +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; + } +} diff --git a/src/Task/loadTasks.php b/src/Task/loadTasks.php new file mode 100644 index 0000000..863de21 --- /dev/null +++ b/src/Task/loadTasks.php @@ -0,0 +1,25 @@ +task(Up::class, $pathToDockerCompose); + } + + /** + * Docker compose up task. + */ + protected function taskDockerComposeDown($pathToDockerCompose = null) + { + return $this->task(Down::class, $pathToDockerCompose); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..d21c14d --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,3 @@ +