-
Notifications
You must be signed in to change notification settings - Fork 7
Introduce task container selector #113
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
6 commits
Select commit
Hold shift + click to select a range
6a1ca94
feat: implement tasks container selector
vitolkachova 798524b
feat: use task container selector for ssh commands
vitolkachova 212400f
make tasks matcher truly prefix based
vitolkachova 029dc89
fix: minor improvements for task selector
vitolkachova 2258821
fix(selector): correct noRunningTaskMessage sprintf args and finished…
vitolkachova a193e4f
Merge branch 'main' into support-task-selector
bojanz 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
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,97 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Platformsh\Cli\Model\RemoteContainer; | ||
|
|
||
| use GuzzleHttp\ClientInterface; | ||
| use GuzzleHttp\Utils; | ||
| use Platformsh\Cli\Model\AppConfig; | ||
| use Platformsh\Client\Model\Environment; | ||
|
|
||
| /** | ||
| * Represents a running task container, which allows SSH access. | ||
| * | ||
| * Unlike apps and workers, a task container is ephemeral: it only exists while | ||
| * an `environment.task` activity is in progress. | ||
| */ | ||
| readonly class Task implements RemoteContainerInterface | ||
| { | ||
| private string $containerName; | ||
|
|
||
| public function __construct(private Environment $environment, private string $taskName, string $activityId, private ClientInterface $client) | ||
| { | ||
| // The SSH gateway addresses a task container as | ||
| // "<task-name>--task--<first 8 chars of the activity ID>". | ||
| $this->containerName = $taskName . '--task--' . substr($activityId, 0, 8); | ||
| } | ||
|
|
||
| public function getSshUrl($instance = ''): string | ||
| { | ||
| // A task is a single ephemeral container; it has no instances, so the | ||
| // instance argument (which may be null) is ignored. | ||
| return $this->constructTaskSshUrl($this->referenceSshUrl(), $this->containerName); | ||
| } | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return $this->containerName; | ||
| } | ||
|
|
||
| public function getConfig(): AppConfig | ||
| { | ||
| // The task's configuration (type, run, mounts, relationships, etc.) is | ||
| // exposed at /environments/<id>/tasks/<name>, independently of whether a | ||
| // run is in progress. Fetch it lazily, so callers that only need SSH | ||
| // access (ssh, log, scp) pay nothing. | ||
| $response = $this->client->request('GET', $this->environment->getUri() . '/tasks/' . rawurlencode($this->taskName)); | ||
| $data = (array) Utils::jsonDecode((string) $response->getBody(), true); | ||
|
|
||
| return new AppConfig($data); | ||
| } | ||
|
|
||
| public function getRuntimeOperations(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * Returns any of the environment's SSH URLs, to derive the host and user prefix from. | ||
| */ | ||
| private function referenceSshUrl(): string | ||
| { | ||
| $urls = $this->environment->getSshUrls(); | ||
| if ($urls === []) { | ||
| throw new \RuntimeException(sprintf( | ||
| 'Could not determine the SSH endpoint for the environment %s.', | ||
| $this->environment->id, | ||
| )); | ||
| } | ||
| return reset($urls); | ||
| } | ||
|
|
||
| /** | ||
| * Builds the task container's SSH URL from a reference URL. | ||
| * | ||
| * A container SSH URL has the form "<project>-<environment>--<container>@<host>". | ||
| * All containers in an environment share the same "<project>-<environment>" | ||
| * user prefix and host, so the task URL is the reference URL with its | ||
| * container suffix replaced by the task's container name. | ||
| */ | ||
| private function constructTaskSshUrl(string $referenceUrl, string $containerName): string | ||
| { | ||
| $atPos = strrpos($referenceUrl, '@'); | ||
| if ($atPos === false) { | ||
| throw new \RuntimeException(sprintf('Unexpected SSH URL format: %s', $referenceUrl)); | ||
| } | ||
| $user = substr($referenceUrl, 0, $atPos); | ||
| $hostPart = substr($referenceUrl, $atPos + 1); | ||
|
|
||
| // The "--" double-dash separates the shared user prefix from the | ||
| // container name. Project IDs and environment machine names only use | ||
| // single dashes, so the prefix is everything before the first "--". | ||
| $prefix = explode('--', $user, 2)[0]; | ||
|
|
||
| return $prefix . '--' . $containerName . '@' . $hostPart; | ||
| } | ||
| } |
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.