Skip to content

feat(ssh): select an instance by number or with any - #1149

Open
davlgd wants to merge 2 commits into
masterfrom
davlgd-ssh-instance-selection
Open

feat(ssh): select an instance by number or with any#1149
davlgd wants to merge 2 commits into
masterfrom
davlgd-ssh-instance-selection

Conversation

@davlgd

@davlgd davlgd commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Builds on #1123 by @mehdi653, whose commit is included here — the --instance option and its
resolution by ID are theirs.

Context

As discussed, that option should accept instance numbers as well as IDs.

Numbers turn out not to be unique, though. During a redeploy the instance going away and the one
coming up carry the same number:

n°0  7e6f95ec-…  STOPPING
n°0  c320afba-…  UP

--instance 0 would then reach either, and the API listed the outgoing one first.

Changes

  • Accept an instance number, and any in any case, alongside the ID.
  • Prefer an UP instance: any looks for one before falling back to the lowest number, and a
    number prefers the UP instance among those sharing it. An ID still names one entry whatever its
    state, so a stopping instance stays reachable when asked for explicitly.
  • List the candidates when nothing matches, instead of only saying no:
[ERROR] No instance 7 on this application, pick one of:
          - 0 09d1615d-629c-4278-aad2-ab475f0a4be7 (UP)
          - 1 d13eaecf-66b7-4493-8fc0-b67a933e9c25 (UP)
  • Reject an empty value at parse time, so --instance --command "hostname" says what is wrong
    rather than reporting an instance named nothing.

How to review

selectInstance is the whole change; the rest is the option and its documentation.

Scale an application to two instances and check that --instance <number> reaches the one that
number names — the remote hostname is the instance ID, so the output says which one answered.
Then redeploy and, while both instances share number 0, check that --instance 0 and
--instance any reach the one that is UP.

When several instances are running, clever ssh currently requires an interactive
prompt (and fails in non-interactive mode). Accept --instance <instance-id> to
select the target directly, as requested in #1067.

Instance IDs are used rather than instance numbers, which are not stable across
deployments.

Fixes #1067
@davlgd
davlgd requested a review from a team as a code owner September 11, 2026 12:30
@github-actions

github-actions Bot commented Sep 11, 2026

Copy link
Copy Markdown

🔎 A preview has been automatically published!

If you created the alias to the preview script, you can run this command to download and install this preview:

clever-preview update davlgd-ssh-instance-selection

You can also run it from your local repository:

./scripts/preview.js update davlgd-ssh-instance-selection
OS SHA256 checksum
🐧 linux 706fe2badc9a2e53e153fb81947e9fd8e26be06735d1eab6c78d76b03afc9dc7
🍏 macos a651ba88fd46d95b749049c0668d789b1bf6ce983d409efeb4bd242f8d48e8ff

This preview will be deleted once this PR is closed.

@pdesoyres-cc pdesoyres-cc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few comments below. and I'm wondering if the private functions should be below the main command handler.

Comment thread src/commands/ssh/ssh.command.js Outdated
Comment on lines +35 to +37
// Every line carries its own indent: `Logger.error` indents continuation lines by the width of
// its prefix, so a list indented once by the template would come out with its first line two
// columns further right than the others.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unecessary comments

@davlgd davlgd Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

Comment thread src/commands/ssh/ssh.command.js Outdated
return readiest(byNumber);
}

const found = byNumber.find((inst) => inst.id === wanted) ?? readiest(byNumber.filter(hasNumber(wanted)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optimization and readability:

The wanted to valid number logic is made on every items of the array while filtering.
Prefer do the parsing/validation once, stop early and then filter with a valid wanted number.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now parsed once into a number, with an early return when it isn't one, so the filter only ever runs on a valid value.

Comment thread src/commands/ssh/ssh.command.js Outdated
Comment on lines +39 to +41
throw new Error(
`No instance ${styleText('red', wanted)} on this application, pick one of:\n${styleText('grey', available)}`,
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have make this function just select the right instance and return null if none is found.
Throwing and error with the right message is more a command concern (should be inside the command handler function)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. selectInstance returns null now and the handler raises the error, which also let the sort move up and be shared with the prompt.

@davlgd
davlgd force-pushed the davlgd-ssh-instance-selection branch from fa64e69 to addaf19 Compare September 11, 2026 14:31
@davlgd

davlgd commented Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

I'm wondering if the private functions should be below the main command handler.

Moved below the handler

@davlgd
davlgd requested a review from pdesoyres-cc September 11, 2026 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants