Skip to content
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

SSH: use the new container name format #2648

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/utils/device/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,21 @@ export async function getContainerIdForService(
.filter((l) => l);

const { escapeRegExp } = await import('lodash');
const regex = new RegExp(`(?:^|\\/)${escapeRegExp(opts.service)}_\\d+_\\d+`);
const regex = new RegExp(
`(?:^|\\/)${escapeRegExp(
opts.service,
)}_(?:\\d+_)?(?:\\d+_)?(?:[a-f0-9]*)?$`,
Copy link
Member

Choose a reason for hiding this comment

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

How about v ?

Suggested change
)}_(?:\\d+_)?(?:\\d+_)?(?:[a-f0-9]*)?$`,
)}_(?:(?:\\d+_\\d+)|(?:[a-f0-9]+)$)`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is not really an OR, it can have numeric ids, numeric ids with commit or just commit. I think I can merge the numeric matching though

);
// Old balenaOS container name pattern:
// main_1234567_2345678
// New balenaOS container name patterns:
// main_1234567_2345678_a000b111c222d333e444f555a666b777
// main_1_1_localrelease
const nameRegex = /(?:^|\/)([a-zA-Z0-9_-]+)_\d+_\d+(?:_.+)?$/;
// Newer balenaOS container names just use commit
// main_a000b111c222d333e444f555a666b777
// main_10ca12e1ea5e
const nameRegex =
/(?:^|\/)([a-zA-Z0-9_-]+?)_(?:\d+_)?(?:\d+_)?(?:[a-f0-9]*)?$/;
Copy link
Member

Choose a reason for hiding this comment

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

How about v which is slightly stricter?

Suggested change
/(?:^|\/)([a-zA-Z0-9_-]+?)_(?:\d+_)?(?:\d+_)?(?:[a-f0-9]*)?$/;
/(?:^|\/)([a-zA-Z0-9_-]+?)_(?:(?:\d+_\d+(?:_[a-f0-9]+)?)|(?:[a-f0-9]+))$/;

As far as I can tell though a user naming a service december_12_2023 would make the name ambiguous 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, unfortunately that doesn't work either :(

'december_12_2023_11_22'.match(/(?:^|\/)([a-zA-Z0-9_-]+?)_(?:(?:\d+_\d+(?:_[a-f0-9]+)?)|(?:[a-f0-9]+))$/)
[
  'december_12_2023_11_22',
  'december_12',
  index: 0,
  input: 'december_12_2023_11_22',
  groups: undefined
]

Copy link
Contributor Author

@pipex pipex Dec 12, 2023

Choose a reason for hiding this comment

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

Without lazy ? it matches the first number for some reason

'december_12_2023_11_22'.match(/(?:^|\/)([a-zA-Z0-9_-]+)_(?:(?:\d+_\d+(?:_[a-f0-9]+)?)|(?:[a-f0-9]+))$/)
[
  'december_12_2023_11_22',
  'december_12_2023_11',
  index: 0,
  input: 'december_12_2023_11_22',
  groups: undefined
]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Testing alternatives

Copy link
Contributor Author

@pipex pipex Dec 13, 2023

Choose a reason for hiding this comment

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

Hmm, 'december_12_2023_11_22` matches both syntaxes

name 'december_12'
first id '2023'
second id '11'
commit '22'

It also matches

name 'december_12_2023'
first id '11'
second id '22'
commit 'none'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suspect the first one is the least of evils as container names have a release commit for a while

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you think @thgreasi ?

Copy link
Contributor Author

@pipex pipex Dec 13, 2023

Choose a reason for hiding this comment

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

Alternatively since at this point we alredy have access to the device, we could test depending on SV version, although that sounds a bit overkill particularly since we want the service name list just for the error message


const serviceNames: string[] = [];
const containerNames: string[] = [];
Expand Down Expand Up @@ -116,7 +124,9 @@ export async function getContainerIdForService(
throw new ExpectedError(
`Could not find a container matching service name "${s}" on device "${d}".${
serviceNames.length > 0
? `\nAvailable services:\n\t${serviceNames.join('\n\t')}`
? `\nAvailable services:\n\t${serviceNames
.map((n) => `'${n}'`)
.join('\n\t')}`
: ''
}`,
);
Expand Down
112 changes: 56 additions & 56 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading