2019-04-24 17:45:19 +00:00
|
|
|
/*
|
2020-03-27 01:26:37 +00:00
|
|
|
Copyright 2016-2020 Balena
|
2019-04-24 17:45:19 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2022-02-11 17:18:42 +00:00
|
|
|
|
|
|
|
import { ExpectedError } from '../../errors';
|
2020-05-04 14:57:23 +00:00
|
|
|
import { stripIndent } from '../lazy';
|
2019-04-24 17:45:19 +00:00
|
|
|
|
2022-02-24 22:37:21 +00:00
|
|
|
import {
|
|
|
|
findBestUsernameForDevice,
|
|
|
|
getRemoteCommandOutput,
|
|
|
|
runRemoteCommand,
|
|
|
|
SshRemoteCommandOpts,
|
|
|
|
} from '../ssh';
|
|
|
|
|
|
|
|
export interface DeviceSSHOpts extends SshRemoteCommandOpts {
|
2020-03-31 13:50:09 +00:00
|
|
|
forceTTY?: boolean;
|
2019-04-24 17:45:19 +00:00
|
|
|
service?: string;
|
|
|
|
}
|
|
|
|
|
2022-02-24 22:37:21 +00:00
|
|
|
const deviceContainerEngineBinary = `$(if [ -f /usr/bin/balena ]; then echo "balena"; else echo "docker"; fi)`;
|
2019-04-24 17:45:19 +00:00
|
|
|
|
2022-02-11 17:18:42 +00:00
|
|
|
/**
|
2022-02-24 22:37:21 +00:00
|
|
|
* List the running containers on the device over ssh, and return the full
|
|
|
|
* container name that matches the given service name.
|
|
|
|
*
|
|
|
|
* Note: In the past, two other approaches were implemented for this function:
|
|
|
|
*
|
|
|
|
* - Obtaining container IDs through a supervisor API call:
|
|
|
|
* '/supervisor/v2/containerId' endpoint, via cloud.
|
|
|
|
* - Obtaining container IDs using 'dockerode' connected directly to
|
|
|
|
* balenaEngine on a device, TCP port 2375.
|
|
|
|
*
|
|
|
|
* The problem with using the supervisor API is that it means that 'balena ssh'
|
|
|
|
* becomes dependent on the supervisor being up an running, but sometimes ssh
|
|
|
|
* is needed to investigate devices issues where the supervisor has got into
|
|
|
|
* trouble (e.g. supervisor in restart loop). This is the subject of CLI issue
|
|
|
|
* https://github.com/balena-io/balena-cli/issues/1560 .
|
|
|
|
*
|
|
|
|
* The problem with using dockerode to connect directly to port 2375 (balenaEngine)
|
|
|
|
* is that it only works with development variants of balenaOS. Production variants
|
|
|
|
* block access to port 2375 for security reasons. 'balena ssh' should support
|
|
|
|
* production variants as well, especially after balenaOS v2.44.0 that introduced
|
|
|
|
* support for using the cloud account username for ssh authentication.
|
|
|
|
*
|
|
|
|
* Overall, the most reliable approach is to run 'balena-engine ps' over ssh.
|
|
|
|
* It is OK to depend on balenaEngine because ssh to a container is implemented
|
|
|
|
* through 'balena-engine exec' anyway, and of course it is OK to depend on ssh
|
|
|
|
* itself.
|
2022-02-11 17:18:42 +00:00
|
|
|
*/
|
2022-02-24 22:37:21 +00:00
|
|
|
export async function getContainerIdForService(
|
|
|
|
opts: SshRemoteCommandOpts & { service: string; deviceUuid?: string },
|
2022-02-11 17:18:42 +00:00
|
|
|
): Promise<string> {
|
2022-02-24 22:37:21 +00:00
|
|
|
opts.cmd = `"${deviceContainerEngineBinary}" ps --format "{{.ID}} {{.Names}}"`;
|
|
|
|
if (opts.deviceUuid) {
|
|
|
|
// If a device UUID is given, perform ssh via cloud proxy 'host' command
|
|
|
|
opts.cmd = `host ${opts.deviceUuid} ${opts.cmd}`;
|
2022-02-11 17:18:42 +00:00
|
|
|
}
|
2019-04-24 17:45:19 +00:00
|
|
|
|
2022-02-24 22:37:21 +00:00
|
|
|
const psLines: string[] = (
|
|
|
|
await getRemoteCommandOutput({ ...opts, stderr: 'inherit' })
|
|
|
|
).stdout
|
|
|
|
.toString()
|
|
|
|
.split('\n')
|
|
|
|
.filter((l) => l);
|
|
|
|
|
|
|
|
const { escapeRegExp } = await import('lodash');
|
|
|
|
const regex = new RegExp(`(?:^|\\/)${escapeRegExp(opts.service)}_\\d+_\\d+`);
|
|
|
|
// 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+(?:_.+)?$/;
|
|
|
|
|
2022-02-11 17:18:42 +00:00
|
|
|
const serviceNames: string[] = [];
|
2022-02-24 22:37:21 +00:00
|
|
|
const containerNames: string[] = [];
|
|
|
|
let containerId: string | undefined;
|
|
|
|
|
|
|
|
// sample psLine: 'b603c74e951e bar_4587562_2078151_3261c9d4c22f2c53a5267be459c89990'
|
|
|
|
for (const psLine of psLines) {
|
|
|
|
const [cId, name] = psLine.split(' ');
|
|
|
|
if (cId && name) {
|
2022-02-11 17:18:42 +00:00
|
|
|
if (regex.test(name)) {
|
2022-02-24 22:37:21 +00:00
|
|
|
containerNames.push(name);
|
|
|
|
containerId = cId;
|
2022-02-11 17:18:42 +00:00
|
|
|
}
|
|
|
|
const match = name.match(nameRegex);
|
|
|
|
if (match) {
|
|
|
|
serviceNames.push(match[1]);
|
2020-10-28 00:13:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-11 17:18:42 +00:00
|
|
|
}
|
2022-02-24 22:37:21 +00:00
|
|
|
|
|
|
|
if (containerNames.length > 1) {
|
|
|
|
const [s, d] = [opts.service, opts.deviceUuid || opts.hostname];
|
2022-02-11 17:18:42 +00:00
|
|
|
throw new ExpectedError(stripIndent`
|
2022-02-24 22:37:21 +00:00
|
|
|
Found more than one container matching service name "${s}" on device "${d}":
|
|
|
|
${containerNames.join(', ')}
|
2022-02-11 17:18:42 +00:00
|
|
|
Use different service names to avoid ambiguity.
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
if (!containerId) {
|
2022-02-24 22:37:21 +00:00
|
|
|
const [s, d] = [opts.service, opts.deviceUuid || opts.hostname];
|
2022-02-11 17:18:42 +00:00
|
|
|
throw new ExpectedError(
|
2022-02-24 22:37:21 +00:00
|
|
|
`Could not find a container matching service name "${s}" on device "${d}".${
|
2022-02-11 17:18:42 +00:00
|
|
|
serviceNames.length > 0
|
2022-02-24 22:37:21 +00:00
|
|
|
? `\nAvailable services:\n\t${serviceNames.join('\n\t')}`
|
2022-02-11 17:18:42 +00:00
|
|
|
: ''
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return containerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function performLocalDeviceSSH(
|
|
|
|
opts: DeviceSSHOpts,
|
|
|
|
): Promise<void> {
|
2022-02-24 22:37:21 +00:00
|
|
|
// Before we started using `findBestUsernameForDevice`, we tried the approach
|
|
|
|
// of attempting ssh with the 'root' username first and, if that failed, then
|
|
|
|
// attempting ssh with a regular user (balenaCloud username). The problem with
|
|
|
|
// that approach was that it would print the following message to the console:
|
|
|
|
// "root@192.168.1.36: Permission denied (publickey)"
|
|
|
|
// ... right before having success as a regular user, which looked broken or
|
|
|
|
// confusing from users' point of view. Capturing stderr to prevent that
|
|
|
|
// message from being printed is tricky because the messages printed to stderr
|
|
|
|
// may include the stderr output of remote commands that are of interest to
|
|
|
|
// the user.
|
|
|
|
const username = await findBestUsernameForDevice(opts.hostname, opts.port);
|
2022-02-11 17:18:42 +00:00
|
|
|
let cmd = '';
|
|
|
|
|
|
|
|
if (opts.service) {
|
2022-02-24 22:37:21 +00:00
|
|
|
const containerId = await getContainerIdForService({
|
|
|
|
...opts,
|
|
|
|
service: opts.service,
|
|
|
|
username,
|
|
|
|
});
|
2019-04-24 17:45:19 +00:00
|
|
|
|
2020-03-27 01:26:37 +00:00
|
|
|
const shellCmd = `/bin/sh -c "if [ -e /bin/bash ]; then exec /bin/bash; else exec /bin/sh; fi"`;
|
2020-03-29 23:19:07 +00:00
|
|
|
// stdin (fd=0) is not a tty when data is piped in, for example
|
|
|
|
// echo 'ls -la; exit;' | balena ssh 192.168.0.20 service1
|
|
|
|
// See https://www.balena.io/blog/balena-monthly-roundup-january-2020/#charliestipsntricks
|
|
|
|
// https://assets.balena.io/newsletter/2020-01/pipe.png
|
2020-03-31 13:50:09 +00:00
|
|
|
const isTTY = !!opts.forceTTY || (await import('tty')).isatty(0);
|
|
|
|
const ttyFlag = isTTY ? '-t' : '';
|
2022-02-11 17:18:42 +00:00
|
|
|
cmd = `${deviceContainerEngineBinary} exec -i ${ttyFlag} ${containerId} ${shellCmd}`;
|
2019-04-24 17:45:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 22:37:21 +00:00
|
|
|
await runRemoteCommand({ ...opts, cmd, username });
|
2019-04-24 17:45:19 +00:00
|
|
|
}
|