-
Notifications
You must be signed in to change notification settings - Fork 7
Support for Tasks functionality #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8048bcd
feat: implement task execute command
vitolkachova 1e96d75
feat: add tasks list command
vitolkachova ea2f837
refactor: add task activity to the const list
vitolkachova 9f4b408
refactor: reword from execute to run
vitolkachova a603dab
refactor: add exception wrapper
vitolkachova 7064791
feat(task:run): add --wait, production confirmation, shared variable …
vitolkachova 3ec44d8
test: cover Variable::parseMultiple()
vitolkachova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Platformsh\Cli\Command\Task; | ||
|
|
||
| use GuzzleHttp\Exception\BadResponseException; | ||
| use GuzzleHttp\Utils; | ||
| use Platformsh\Cli\Command\CommandBase; | ||
| use Platformsh\Cli\Selector\Selector; | ||
| use Platformsh\Cli\Service\Api; | ||
| use Platformsh\Cli\Service\Table; | ||
| use Platformsh\Client\Exception\ApiResponseException; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| #[AsCommand(name: 'task:list', description: 'Get a list of tasks on an environment', aliases: ['tasks'])] | ||
| class TaskListCommand extends CommandBase | ||
| { | ||
| /** @var array<string, string> */ | ||
| private array $tableHeader = [ | ||
| 'name' => 'Name', | ||
| 'type' => 'Type', | ||
| 'command' => 'Command', | ||
| 'timeout' => 'Timeout (s)', | ||
| ]; | ||
|
|
||
| public function __construct(private readonly Api $api, private readonly Selector $selector, private readonly Table $table) | ||
| { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure(): void | ||
| { | ||
| Table::configureInput($this->getDefinition(), $this->tableHeader); | ||
| $this->selector->addProjectOption($this->getDefinition()); | ||
| $this->selector->addEnvironmentOption($this->getDefinition()); | ||
| $this->addCompleter($this->selector); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| $selection = $this->selector->getSelection($input); | ||
| $environment = $selection->getEnvironment(); | ||
|
|
||
| try { | ||
| $response = $this->api->getHttpClient()->request('GET', $environment->getUri() . '/tasks'); | ||
| } catch (BadResponseException $e) { | ||
| throw ApiResponseException::create($e->getRequest(), $e->getResponse(), $e); | ||
| } | ||
| $tasks = (array) Utils::jsonDecode((string) $response->getBody(), true); | ||
|
|
||
| if ($tasks === []) { | ||
| $this->stdErr->writeln(sprintf( | ||
| 'No tasks were found on the environment %s.', | ||
| $this->api->getEnvironmentLabel($environment), | ||
| )); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| $rows = []; | ||
| foreach ($tasks as $task) { | ||
| $rows[] = [ | ||
| 'name' => $task['name'] ?? '', | ||
| 'type' => $task['type'] ?? '', | ||
| 'command' => isset($task['run']['command']) ? trim((string) $task['run']['command']) : '', | ||
| 'timeout' => $task['run']['timeout'] ?? '', | ||
| ]; | ||
| } | ||
|
|
||
| if (!$this->table->formatIsMachineReadable()) { | ||
| $this->stdErr->writeln(sprintf( | ||
| 'Tasks on the environment %s:', | ||
| $this->api->getEnvironmentLabel($environment), | ||
| )); | ||
| } | ||
|
|
||
| $this->table->render($rows, $this->tableHeader); | ||
|
|
||
| return 0; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Platformsh\Cli\Command\Task; | ||
|
|
||
| use GuzzleHttp\Exception\BadResponseException; | ||
| use GuzzleHttp\Utils; | ||
| use Platformsh\Cli\Command\CommandBase; | ||
| use Platformsh\Cli\Model\Variable; | ||
| use Platformsh\Cli\Selector\Selector; | ||
| use Platformsh\Cli\Service\ActivityMonitor; | ||
| use Platformsh\Cli\Service\Api; | ||
| use Platformsh\Cli\Service\Config; | ||
| use Platformsh\Cli\Service\QuestionHelper; | ||
| use Platformsh\Client\Exception\ApiResponseException; | ||
| use Platformsh\Client\Model\Activity; | ||
| use Platformsh\Client\Model\Result; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| #[AsCommand(name: 'task:run', description: 'Execute a task on an environment')] | ||
| class TaskRunCommand extends CommandBase | ||
| { | ||
| public function __construct(private readonly ActivityMonitor $activityMonitor, private readonly Api $api, private readonly Config $config, private readonly QuestionHelper $questionHelper, private readonly Selector $selector) | ||
| { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure(): void | ||
| { | ||
| $this | ||
| ->addArgument('task', InputArgument::REQUIRED, 'The name of the task to execute') | ||
| ->addOption('variable', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A variable to set when running the task, in the format <info>type:name=value</info>') | ||
| // Tasks can run for a long time, so waiting is opt-in rather than the default. | ||
| ->addOption('wait', null, InputOption::VALUE_NONE, 'Wait for the task to complete'); | ||
|
|
||
| $this->selector->addProjectOption($this->getDefinition()); | ||
| $this->selector->addEnvironmentOption($this->getDefinition()); | ||
| $this->addCompleter($this->selector); | ||
|
|
||
| $this->addExample('Run the "migrate" task on the environment "main"', 'migrate --environment main'); | ||
| $this->addExample('Run the "migrate" task, setting environment variable FOO=bar', 'migrate -e main --variable env:FOO=bar'); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| $selection = $this->selector->getSelection($input); | ||
| $environment = $selection->getEnvironment(); | ||
|
|
||
| $taskName = $input->getArgument('task'); | ||
| $variables = (new Variable())->parseMultiple($input->getOption('variable')); | ||
|
|
||
| if ($environment->type === 'production' && !$this->questionHelper->confirm(sprintf( | ||
| 'Are you sure you want to run the task <comment>%s</comment> on the production environment %s?', | ||
| $taskName, | ||
| $this->api->getEnvironmentLabel($environment, 'comment'), | ||
| ))) { | ||
| return 1; | ||
| } | ||
|
|
||
| $this->stdErr->writeln(sprintf( | ||
| 'Executing task <info>%s</info> on the environment %s', | ||
| $taskName, | ||
| $this->api->getEnvironmentLabel($environment), | ||
| )); | ||
|
|
||
| $url = $environment->getUri() . '/tasks/' . rawurlencode($taskName) . '/run'; | ||
| try { | ||
| $response = $this->api->getHttpClient()->request('POST', $url, ['json' => ['variables' => (object) $variables]]); | ||
| } catch (BadResponseException $e) { | ||
| throw ApiResponseException::create($e->getRequest(), $e->getResponse(), $e); | ||
| } | ||
|
|
||
| $result = new Result( | ||
| (array) Utils::jsonDecode((string) $response->getBody(), true), | ||
| $environment->getUri(), | ||
| $this->api->getHttpClient(), | ||
| Activity::class, | ||
| ); | ||
| $activities = $result->getActivities(); | ||
|
|
||
| $this->stdErr->writeln(''); | ||
| $this->stdErr->writeln('The task has been triggered.'); | ||
|
|
||
| // Waiting is opt-in so the exit code can reflect a failed activity, e.g. in CI. | ||
| if ($input->getOption('wait') && $activities !== []) { | ||
| $success = $this->activityMonitor->waitMultiple($activities, $selection->getProject()); | ||
| return $success ? 0 : 1; | ||
| } | ||
|
|
||
| $executable = $this->config->getStr('application.executable'); | ||
| if ($activities !== []) { | ||
| // Reference the exact activity ID so the log can be followed even | ||
| // when several activities are running in parallel. | ||
| $activity = reset($activities); | ||
| $this->stdErr->writeln(sprintf( | ||
| 'To follow its log, run: <info>%s activity:log %s</info>', | ||
| $executable, | ||
| $activity->id, | ||
| )); | ||
| } else { | ||
| $this->stdErr->writeln(sprintf( | ||
| 'To follow its log, run: <info>%s activity:log --type environment.task -e %s</info>', | ||
| $executable, | ||
| $environment->id, | ||
| )); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.