From 607a12d9d7403160727fcd800eda4e25a23c273b Mon Sep 17 00:00:00 2001 From: Stephan Huber Date: Mon, 17 Aug 2020 17:23:06 +0200 Subject: [PATCH 1/2] 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 --- docs/scripts.md | 40 +++++++++++++++++++++++++++++++++++ src/Command/ScriptCommand.php | 3 +++ src/Method/K8sMethod.php | 6 ++++++ src/Method/ScriptMethod.php | 2 ++ 4 files changed, 51 insertions(+) diff --git a/docs/scripts.md b/docs/scripts.md index b5514555..71fb4085 100644 --- a/docs/scripts.md +++ b/docs/scripts.md @@ -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 script test +``` + will output `Hello World` + +``` +phab -c 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. diff --git a/src/Command/ScriptCommand.php b/src/Command/ScriptCommand.php index e3a5f88b..30970425 100644 --- a/src/Command/ScriptCommand.php +++ b/src/Command/ScriptCommand.php @@ -2,6 +2,7 @@ namespace Phabalicious\Command; +use Phabalicious\Method\ScriptMethod; use Phabalicious\Method\TaskContext; use Phabalicious\ShellCompletion\FishShellCompletionContext; use Phabalicious\Utilities\Utilities; @@ -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); } diff --git a/src/Method/K8sMethod.php b/src/Method/K8sMethod.php index 2c7413a4..bdabfe3a 100644 --- a/src/Method/K8sMethod.php +++ b/src/Method/K8sMethod.php @@ -20,6 +20,8 @@ class K8sMethod extends BaseMethod implements MethodInterface { + const KUBECTL_SCRIPT_CONTEXT = 'kubectl'; + const AVAILABLE_SUB_COMMANDS = [ 'delete', 'apply', @@ -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) diff --git a/src/Method/ScriptMethod.php b/src/Method/ScriptMethod.php index 45b3956b..e6e86113 100644 --- a/src/Method/ScriptMethod.php +++ b/src/Method/ScriptMethod.php @@ -14,6 +14,8 @@ class ScriptMethod extends BaseMethod implements MethodInterface { + const HOST_SCRIPT_CONTEXT = 'host'; + private $breakOnFirstError = true; private $callbacks = []; private $handledTaskSpecificScripts = []; From 74eb42cbab21eac0b3f83cd423208a43813411c7 Mon Sep 17 00:00:00 2001 From: Stephan Huber Date: Mon, 17 Aug 2020 17:26:38 +0200 Subject: [PATCH 2/2] Bump version number, update change log --- Changelog.md | 6 ++++++ src/Utilities/Utilities.php | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index 484d3253..790a3125 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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: diff --git a/src/Utilities/Utilities.php b/src/Utilities/Utilities.php index bacf5056..59d1b588 100644 --- a/src/Utilities/Utilities.php +++ b/src/Utilities/Utilities.php @@ -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';