Commit aa5bb75f authored by Jon Pugh's avatar Jon Pugh

Add "start" property and method to Provision Task, and use that to output when...

Add "start" property and method to Provision Task, and use that to output when a task starts. If Provision is being run interactively, erase the start message and display the success message. Implement this in the HTTP Docker tasks.
parent 3a0f1486
......@@ -511,6 +511,9 @@ class Context implements BuilderAwareInterface
$collection->getConfig()->set('failure', '');
if ($task instanceof Task) {
if (!empty($task->start)) {
$collection->getConfig()->set($title . '.start', $task->start);
}
if (!empty($task->success)) {
$collection->getConfig()->set($title . '.success', $task->success);
}
......
......@@ -41,12 +41,30 @@ class ProvisionCollection extends Collection {
if ($this->getProvision()->getOutput()->isVerbose()) {
$this->getProvision()->io()->customLite('STARTED ' . $name, '○');
}
// Show starting message.
$start_message = !empty($this->getConfig()->get($name . '.start'))? $this->getConfig()->get($name . '.start'): $name;
$this->getProvision()->io()->customLite($start_message , '☐');
// If being run interactively, pause momentarily to let user read start message, and replace start message with success or fail.
if ($this->getProvision()->getInput()->isInteractive()) {
sleep(1);
// Erase lines
// @TODO: Detect how many lines were output and erase that many.
$lines = 1;
$this->getProvision()->getOutput()->write(["\x0D"]);
$this->getProvision()->getOutput()->write(["\x1B[2K"]);
if ($lines > 0) {
$this->getProvision()->getOutput()->write(str_repeat("\x1B[1A\x1B[2K", $lines));
}
}
// ROBO
$taskList = $taskGroup->getTaskList();
$result = $this->runTaskList($name, $taskList, $result);
// END ROBO
if (!$result->wasSuccessful()) {
// Override output with failure() message.
......
......@@ -130,6 +130,7 @@ class HttpApacheDockerService extends HttpApacheService
// Build Docker image.
$tasks['http.docker.build'] = $this->getProvision()->newTask()
->start('Building new Docker image for Apache...')
->success('Built new Docker image for Apache: ' . $this->containerTag)
->failure('Unable to build docker container with tag: ' . $this->containerTag)
->execute(function () use ($provision, $build_dir) {
......@@ -151,8 +152,12 @@ class HttpApacheDockerService extends HttpApacheService
});
// Docker run
$tasks['Run docker image.'] = function () use ($provision,$provider) {
$tasks['Run docker image.'] = $this->getProvision()->newTask()
->start('Running Apache docker image...')
->success('Apache docker container is running: ' . $this->containerName)
->failure('Unable to run docker image with name: ' . $this->containerName)
->execute(function () use ($provision, $build_dir, $provider) {
// Check for existing container.
$containerExists = $provision->getTasks()
->taskExec("docker ps -a -q -f name={$this->containerName}")
......@@ -176,7 +181,9 @@ class HttpApacheDockerService extends HttpApacheService
function ($task, $state) use ($provision, $provider) {
if ($state['running'] == 'true') {
$provision->io()->successLite('Container is already running: ' . $this->containerName);
// $provision->io()->successLite('Container is already running: ' . $this->containerName);
// @TODO: Figure out how to change the "success" message of a task.
// $this->success('Container is already running: ' . $this->containerName);
}
// If not running, try to start it.
else {
......@@ -187,10 +194,10 @@ class HttpApacheDockerService extends HttpApacheService
;
if ($startResult->wasSuccessful()) {
$provision->io()->successLite('Existing container found. Restarted container ' . $this->containerName);
// $provision->io()->successLite('Existing container found. Restarted container ' . $this->containerName);
}
else {
$provision->io()->errorLite('Unable to restart docker container: ' . $this->containerName);
// $provision->io()->errorLite('Unable to restart docker container: ' . $this->containerName);
throw new \Exception('Unable to restart docker container: ' . $this->containerName);
}
}
......@@ -222,11 +229,7 @@ class HttpApacheDockerService extends HttpApacheService
$result = $container->run();
if ($result->wasSuccessful()) {
$provision->io()->successLite('Running Docker image ' . $this->containerTag);
}
else {
$provision->io()->errorLite('Unable to run docker container: ' . $this->containerName);
if (!$result->wasSuccessful()) {
throw new \Exception('Unable to run docker container: ' . $this->containerName);
}
}
......@@ -239,7 +242,7 @@ class HttpApacheDockerService extends HttpApacheService
else {
throw new \Exception('Something went wrong.');
}
};
});
$tasks['http.restart'] = $this->getProvision()->newTask()
->execute(function() {
......
......@@ -9,8 +9,13 @@ class Task {
$this->callable = $callable;
return $this;
}
function start($message) {
$this->start = $message;
return $this;
}
function success($message) {
$this->success = $message;
return $this;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment