Skip to content

Commit

Permalink
Merge branch 'hotfix/3.5.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
stmh committed Aug 17, 2020
2 parents 18d8c4b + 74eb42c commit da3e53a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 3.5.4 / 2020-08-17

### New:

* Allow optional script context definition of a script, this will allow to execute the script in a different context, eg in the context of the kubectl shell

## 3.5.3 / 2020-08-11

## Fixed:
Expand Down
40 changes: 40 additions & 0 deletions docs/scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,46 @@ host:
These scripts in the above examples gets executed only for the host `test` and task `deploy`.


## Defaults

You an provide defaults for a script, which can be verridden via the `--arguments` commandline option

```yaml
scripts:
test:
defaults:
foo: World
script:
- echo "Hello %arguments.foo%"
```

Running
```
phab -c<config> script test
```
will output `Hello World`
```
phab -c<config> script test --arguments foo=bar
```
will output `Hello bar`
## Script contexts
Sometimes it makes sense to run a script in a different context, e.g. not on the host-config, but for example in the context of the kubectl application or the docker host. You can override the context via
```yaml
scripts:
test:
context: kubectl
script:
- kubectl apply -f whatever
```

The example above will run the script not in the context of the host, but in the context of the shell which also runs the kubectl command.

## Examples

A rather complex example scripting phabalicious.
Expand Down
3 changes: 3 additions & 0 deletions src/Command/ScriptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phabalicious\Command;

use Phabalicious\Method\ScriptMethod;
use Phabalicious\Method\TaskContext;
use Phabalicious\ShellCompletion\FishShellCompletionContext;
use Phabalicious\Utilities\Utilities;
Expand Down Expand Up @@ -81,12 +82,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}
$defaults = $script_data['defaults'] ?? [];
$script_context = $script_data['context'] ?? ScriptMethod::HOST_SCRIPT_CONTEXT;
if (!empty($script_data['script'])) {
$script_data = $script_data['script'];
}

$context = $this->createContext($input, $output, $defaults);
$context->set('scriptData', $script_data);
$context->set('scriptContext', $script_context);

$this->getMethods()->call('script', 'runScript', $this->getHostConfig(), $context);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Method/K8sMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

class K8sMethod extends BaseMethod implements MethodInterface
{
const KUBECTL_SCRIPT_CONTEXT = 'kubectl';

const AVAILABLE_SUB_COMMANDS = [
'delete',
'apply',
Expand Down Expand Up @@ -119,6 +121,10 @@ public function preflightTask(string $task, HostConfig $config, TaskContextInter
if (empty($config['kube']['podForCli'])) {
$config->setChild('kube', 'podForCli', $this->getPodNameFromSelector($config, $context));
}
if ($task == 'runScript' && $context->get('scriptContext', 'host') == self::KUBECTL_SCRIPT_CONTEXT) {
$this->ensureShell($config, $context);
$context->setShell($this->kubectlShell);
}
}

public function postflightTask(string $task, HostConfig $config, TaskContextInterface $context)
Expand Down
2 changes: 2 additions & 0 deletions src/Method/ScriptMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
class ScriptMethod extends BaseMethod implements MethodInterface
{

const HOST_SCRIPT_CONTEXT = 'host';

private $breakOnFirstError = true;
private $callbacks = [];
private $handledTaskSpecificScripts = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Utilities
{

const FALLBACK_VERSION = '3.5.3';
const FALLBACK_VERSION = '3.5.4';
const COMBINED_ARGUMENTS = 'combined';
const UNNAMED_ARGUMENTS = 'unnamedArguments';

Expand Down

0 comments on commit da3e53a

Please sign in to comment.