2017-12-20 21:46:01 +00:00
|
|
|
/*
|
2021-04-09 21:39:23 +00:00
|
|
|
Copyright 2016-2021 Balena Ltd.
|
2017-12-20 21:46:01 +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.
|
|
|
|
*/
|
|
|
|
|
2020-02-27 12:38:50 +00:00
|
|
|
import type { InitializeEmitter, OperationState } from 'balena-device-init';
|
|
|
|
import type * as BalenaSdk from 'balena-sdk';
|
2021-04-09 21:39:23 +00:00
|
|
|
|
2020-03-27 01:26:37 +00:00
|
|
|
import * as _ from 'lodash';
|
2021-04-09 21:39:23 +00:00
|
|
|
import { promisify } from 'util';
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-02-28 15:25:12 +00:00
|
|
|
import { getBalenaSdk, getChalk, getVisuals } from './lazy';
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-01-09 15:05:24 +00:00
|
|
|
export function getGroupDefaults(group: {
|
2020-07-16 10:15:06 +00:00
|
|
|
options: Array<{ name: string; default: string | number }>;
|
2019-10-31 01:46:14 +00:00
|
|
|
}): { [name: string]: string | number | undefined } {
|
2018-06-12 15:43:15 +00:00
|
|
|
return _.chain(group)
|
2017-12-20 21:46:01 +00:00
|
|
|
.get('options')
|
2020-06-15 22:53:07 +00:00
|
|
|
.map((question) => [question.name, question.default])
|
2017-12-20 21:46:01 +00:00
|
|
|
.fromPairs()
|
|
|
|
.value();
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
export function stateToString(state: OperationState) {
|
2018-01-04 16:17:43 +00:00
|
|
|
const percentage = _.padStart(`${state.percentage}`, 3, '0');
|
2020-02-28 15:25:12 +00:00
|
|
|
const chalk = getChalk();
|
2018-01-09 15:05:24 +00:00
|
|
|
const result = `${chalk.blue(percentage + '%')} ${chalk.cyan(
|
|
|
|
state.operation.command,
|
|
|
|
)}`;
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
switch (state.operation.command) {
|
|
|
|
case 'copy':
|
2020-01-20 21:21:05 +00:00
|
|
|
return `${result} ${state.operation.from.path} -> ${state.operation.to.path}`;
|
2017-12-20 21:46:01 +00:00
|
|
|
case 'replace':
|
2020-01-20 21:21:05 +00:00
|
|
|
return `${result} ${state.operation.file.path}, ${state.operation.copy} -> ${state.operation.replace}`;
|
2017-12-20 21:46:01 +00:00
|
|
|
case 'run-script':
|
|
|
|
return `${result} ${state.operation.script}`;
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported operation: ${state.operation.command}`);
|
|
|
|
}
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-10-13 23:52:43 +00:00
|
|
|
/**
|
|
|
|
* Execute a child process with admin / superuser privileges, prompting the user for
|
|
|
|
* elevation as needed, and taking care of shell-escaping arguments in a suitable way
|
|
|
|
* for Windows and Linux/Mac.
|
|
|
|
*
|
|
|
|
* @param command Unescaped array of command and args to be executed. If isCLIcmd is
|
|
|
|
* true, the command should not include the 'node' or 'balena' components, for example:
|
|
|
|
* ['internal', 'osinit', ...]. This function will add argv[0] and argv[1] as needed
|
|
|
|
* (taking process.pkg into account -- CLI standalone zip package), and will also
|
|
|
|
* shell-escape the arguments as needed, taking into account the differences between
|
|
|
|
* bash/sh and the Windows cmd.exe in relation to escape characters.
|
|
|
|
* @param msg Optional message for the user, before the password prompt
|
|
|
|
* @param stderr Optional stream to which stderr should be piped
|
2020-10-20 21:11:17 +00:00
|
|
|
* @param isCLIcmd (default: true) Whether the command array is a balena CLI command
|
2019-10-13 23:52:43 +00:00
|
|
|
* (e.g. ['internal', 'osinit', ...]), in which case process.argv[0] and argv[1] are
|
|
|
|
* added as necessary, depending on whether the CLI is running as a standalone zip
|
|
|
|
* package (with Node built in).
|
|
|
|
*/
|
|
|
|
export async function sudo(
|
2018-05-22 15:12:51 +00:00
|
|
|
command: string[],
|
2019-10-13 23:52:43 +00:00
|
|
|
{
|
|
|
|
stderr,
|
|
|
|
msg,
|
|
|
|
isCLIcmd,
|
|
|
|
}: { stderr?: NodeJS.WritableStream; msg?: string; isCLIcmd?: boolean } = {},
|
2018-05-22 15:12:51 +00:00
|
|
|
) {
|
2019-10-13 23:52:43 +00:00
|
|
|
const { executeWithPrivileges } = await import('./sudo');
|
2018-05-22 15:12:51 +00:00
|
|
|
|
2021-04-09 21:39:23 +00:00
|
|
|
if (process.platform !== 'win32') {
|
2018-05-22 15:12:51 +00:00
|
|
|
console.log(
|
2019-10-13 23:52:43 +00:00
|
|
|
msg ||
|
|
|
|
'Admin privileges required: you may be asked for your computer password to continue.',
|
2018-05-22 15:12:51 +00:00
|
|
|
);
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
2020-08-27 10:50:57 +00:00
|
|
|
isCLIcmd ??= true;
|
2019-10-13 23:52:43 +00:00
|
|
|
await executeWithPrivileges(command, stderr, isCLIcmd);
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2021-12-21 18:24:26 +00:00
|
|
|
export async function runCommand<T>(commandArgs: string[]): Promise<T> {
|
2021-07-20 13:57:00 +00:00
|
|
|
const { isSubcommand } =
|
|
|
|
require('../preparser') as typeof import('../preparser');
|
2021-12-21 18:24:26 +00:00
|
|
|
if (await isSubcommand(commandArgs)) {
|
2020-09-04 14:34:34 +00:00
|
|
|
commandArgs = [
|
|
|
|
commandArgs[0] + ':' + commandArgs[1],
|
|
|
|
...commandArgs.slice(2),
|
|
|
|
];
|
2020-07-02 13:56:32 +00:00
|
|
|
}
|
2020-09-04 14:34:34 +00:00
|
|
|
const { run } = require('@oclif/command');
|
|
|
|
return run(commandArgs);
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-11-05 08:18:18 +00:00
|
|
|
export async function getManifest(
|
2018-01-09 15:05:24 +00:00
|
|
|
image: string,
|
|
|
|
deviceType: string,
|
2020-07-31 14:35:20 +00:00
|
|
|
): Promise<BalenaSdk.DeviceTypeJson.DeviceType> {
|
2018-11-05 08:18:18 +00:00
|
|
|
const init = await import('balena-device-init');
|
2022-10-17 20:46:49 +00:00
|
|
|
const sdk = getBalenaSdk();
|
2018-11-05 08:18:18 +00:00
|
|
|
const manifest = await init.getImageManifest(image);
|
2022-10-17 20:46:49 +00:00
|
|
|
if (
|
|
|
|
manifest != null &&
|
|
|
|
manifest.slug !== deviceType &&
|
|
|
|
manifest.slug !== (await sdk.models.deviceType.get(deviceType)).slug
|
|
|
|
) {
|
|
|
|
const { ExpectedError } = await import('../errors');
|
|
|
|
throw new ExpectedError(
|
|
|
|
`The device type of the provided OS image ${manifest.slug}, does not match the expected device type ${deviceType}`,
|
|
|
|
);
|
2018-11-05 08:18:18 +00:00
|
|
|
}
|
2022-10-17 20:46:49 +00:00
|
|
|
return manifest ?? (await sdk.models.device.getManifestBySlug(deviceType));
|
2018-11-05 08:18:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 14:00:29 +00:00
|
|
|
export const areDeviceTypesCompatible = async (
|
|
|
|
appDeviceTypeSlug: string,
|
|
|
|
osDeviceTypeSlug: string,
|
|
|
|
) => {
|
|
|
|
if (appDeviceTypeSlug === osDeviceTypeSlug) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const sdk = getBalenaSdk();
|
|
|
|
const pineOptions = {
|
|
|
|
$select: 'is_of__cpu_architecture',
|
|
|
|
$expand: {
|
|
|
|
is_of__cpu_architecture: {
|
|
|
|
$select: 'slug',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as const;
|
|
|
|
const [appDeviceType, osDeviceType] = await Promise.all(
|
|
|
|
[appDeviceTypeSlug, osDeviceTypeSlug].map(
|
|
|
|
(dtSlug) =>
|
|
|
|
sdk.models.deviceType.get(dtSlug, pineOptions) as Promise<
|
|
|
|
BalenaSdk.PineTypedResult<BalenaSdk.DeviceType, typeof pineOptions>
|
|
|
|
>,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return sdk.models.os.isArchitectureCompatibleWith(
|
|
|
|
osDeviceType.is_of__cpu_architecture[0].slug,
|
|
|
|
appDeviceType.is_of__cpu_architecture[0].slug,
|
|
|
|
);
|
|
|
|
};
|
2018-12-14 10:55:35 +00:00
|
|
|
|
2019-10-13 23:52:43 +00:00
|
|
|
export async function osProgressHandler(step: InitializeEmitter) {
|
2017-12-20 21:46:01 +00:00
|
|
|
step.on('stdout', process.stdout.write.bind(process.stdout));
|
|
|
|
step.on('stderr', process.stderr.write.bind(process.stderr));
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
step.on('state', function (state) {
|
2018-01-09 15:05:24 +00:00
|
|
|
if (state.operation.command === 'burn') {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-04 16:17:43 +00:00
|
|
|
console.log(exports.stateToString(state));
|
2017-12-20 21:46:01 +00:00
|
|
|
});
|
|
|
|
|
2020-02-28 13:06:14 +00:00
|
|
|
const visuals = getVisuals();
|
2017-12-20 21:46:01 +00:00
|
|
|
const progressBars = {
|
|
|
|
write: new visuals.Progress('Writing Device OS'),
|
2018-01-04 14:07:55 +00:00
|
|
|
check: new visuals.Progress('Validating Device OS'),
|
2017-12-20 21:46:01 +00:00
|
|
|
};
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
step.on('burn', (state) => progressBars[state.type].update(state));
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-10-13 23:52:43 +00:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
step.on('error', reject);
|
2021-07-09 13:44:38 +00:00
|
|
|
step.on('end' as any, resolve);
|
2019-10-13 23:52:43 +00:00
|
|
|
});
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2021-08-30 22:10:54 +00:00
|
|
|
export async function getAppWithArch(
|
2018-01-09 15:05:24 +00:00
|
|
|
applicationName: string,
|
2020-07-31 14:35:20 +00:00
|
|
|
): Promise<ApplicationWithDeviceType & { arch: string }> {
|
2021-12-21 00:02:45 +00:00
|
|
|
const { getApplication } = await import('./sdk');
|
|
|
|
const options: BalenaSdk.PineOptions<BalenaSdk.Application> = {
|
2018-03-19 18:58:05 +00:00
|
|
|
$expand: {
|
|
|
|
application_type: {
|
2018-03-20 11:06:44 +00:00
|
|
|
$select: ['name', 'slug', 'supports_multicontainer', 'is_legacy'],
|
2018-03-19 18:58:05 +00:00
|
|
|
},
|
2020-07-31 14:35:20 +00:00
|
|
|
is_for__device_type: {
|
|
|
|
$select: 'slug',
|
2021-08-30 22:10:54 +00:00
|
|
|
$expand: {
|
|
|
|
is_of__cpu_architecture: {
|
|
|
|
$select: 'slug',
|
|
|
|
},
|
|
|
|
},
|
2020-07-31 14:35:20 +00:00
|
|
|
},
|
2018-03-19 18:58:05 +00:00
|
|
|
},
|
|
|
|
};
|
2020-02-27 14:55:30 +00:00
|
|
|
const balena = getBalenaSdk();
|
2021-12-21 00:02:45 +00:00
|
|
|
const app = (await getApplication(
|
|
|
|
balena,
|
2020-07-31 15:25:48 +00:00
|
|
|
applicationName,
|
2021-12-21 00:02:45 +00:00
|
|
|
options,
|
|
|
|
)) as ApplicationWithDeviceType;
|
|
|
|
const { getExpanded } = await import('./pine');
|
|
|
|
|
|
|
|
return {
|
|
|
|
...app,
|
|
|
|
arch: getExpanded(
|
|
|
|
getExpanded(app.is_for__device_type)!.is_of__cpu_architecture,
|
|
|
|
)!.slug,
|
|
|
|
};
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-11-07 22:28:14 +00:00
|
|
|
const second = 1000; // 1000 milliseconds
|
|
|
|
const minute = 60 * second;
|
2020-07-01 14:26:40 +00:00
|
|
|
export const delay = promisify(setTimeout);
|
|
|
|
|
2019-03-04 15:08:58 +00:00
|
|
|
/**
|
|
|
|
* Call `func`, and if func() throws an error or returns a promise that
|
2020-11-07 22:28:14 +00:00
|
|
|
* eventually rejects, retry it `times` many times, each time printing a log
|
|
|
|
* message including the given `label` and the error that led to retrying.
|
|
|
|
* Wait initialDelayMs before the first retry. Before each further retry,
|
|
|
|
* the delay is reduced by the time elapsed since the last retry, and
|
|
|
|
* increased by multiplying the result by backoffScaler.
|
2019-03-04 15:08:58 +00:00
|
|
|
* @param func: The function to call and, if needed, retry calling
|
2020-11-07 22:28:14 +00:00
|
|
|
* @param maxAttempts: How many times (max) to try calling func().
|
|
|
|
* func() will always be called at least once.
|
2019-03-04 15:08:58 +00:00
|
|
|
* @param label: Label to include in the retry log message
|
2020-11-07 22:28:14 +00:00
|
|
|
* @param initialDelayMs: How long to wait before the first retry
|
2019-03-04 15:08:58 +00:00
|
|
|
* @param backoffScaler: Multiplier to previous wait time
|
2020-11-07 22:28:14 +00:00
|
|
|
* @param maxSingleDelayMs: Maximum interval between retries
|
2019-03-04 15:08:58 +00:00
|
|
|
*/
|
2020-11-07 22:28:14 +00:00
|
|
|
export async function retry<T>({
|
|
|
|
func,
|
|
|
|
maxAttempts,
|
|
|
|
label,
|
|
|
|
initialDelayMs = 1000,
|
2019-03-04 15:08:58 +00:00
|
|
|
backoffScaler = 2,
|
2020-11-07 22:28:14 +00:00
|
|
|
maxSingleDelayMs = 1 * minute,
|
|
|
|
}: {
|
|
|
|
func: () => T;
|
|
|
|
maxAttempts: number;
|
|
|
|
label: string;
|
|
|
|
initialDelayMs?: number;
|
|
|
|
backoffScaler?: number;
|
|
|
|
maxSingleDelayMs?: number;
|
|
|
|
}): Promise<T> {
|
2021-04-09 21:39:23 +00:00
|
|
|
const { SIGINTError } = await import('../errors');
|
2020-11-07 22:28:14 +00:00
|
|
|
let delayMs = initialDelayMs;
|
|
|
|
for (let count = 0; count < maxAttempts - 1; count++) {
|
|
|
|
const lastAttemptMs = Date.now();
|
2020-07-01 14:26:40 +00:00
|
|
|
try {
|
|
|
|
return await func();
|
|
|
|
} catch (err) {
|
2020-12-03 20:50:00 +00:00
|
|
|
// Don't retry on SIGINT (CTRL-C)
|
|
|
|
if (err instanceof SIGINTError) {
|
|
|
|
throw err;
|
|
|
|
}
|
2020-11-07 22:28:14 +00:00
|
|
|
if (count) {
|
|
|
|
// use Math.max to work around system time changes, e.g. DST
|
|
|
|
const elapsedMs = Math.max(0, Date.now() - lastAttemptMs);
|
|
|
|
// reduce delayMs by the time elapsed since the last attempt
|
|
|
|
delayMs = Math.max(initialDelayMs, delayMs - elapsedMs);
|
|
|
|
// increase delayMs by the backoffScaler factor
|
|
|
|
delayMs = Math.min(maxSingleDelayMs, delayMs * backoffScaler);
|
|
|
|
}
|
|
|
|
const sec = delayMs / 1000;
|
|
|
|
const secStr = sec < 10 ? sec.toFixed(1) : Math.round(sec).toString();
|
2019-03-04 15:08:58 +00:00
|
|
|
console.log(
|
2020-11-07 22:28:14 +00:00
|
|
|
`Retrying "${label}" after ${secStr}s (${count + 1} of ${
|
|
|
|
maxAttempts - 1
|
|
|
|
}) due to: ${err}`,
|
2019-03-04 15:08:58 +00:00
|
|
|
);
|
2020-11-07 22:28:14 +00:00
|
|
|
await delay(delayMs);
|
2020-07-01 14:26:40 +00:00
|
|
|
}
|
2019-03-04 15:08:58 +00:00
|
|
|
}
|
2020-07-01 14:26:40 +00:00
|
|
|
return await func();
|
2019-03-04 15:08:58 +00:00
|
|
|
}
|
2019-04-02 11:26:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a compare(a, b) function suitable for use as the argument for the
|
|
|
|
* sort() method of an array. That function will use the given manuallySortedArray
|
|
|
|
* as "sorting guidance":
|
|
|
|
* - If both a and b are found in the manuallySortedArray, the returned
|
|
|
|
* compare(a, b) function will follow that ordering.
|
|
|
|
* - If neither a nor b are found in the manuallySortedArray, the returned
|
|
|
|
* compare(a, b) function will compare a and b using the standard '<' and
|
|
|
|
* '>' Javascript operators.
|
|
|
|
* - If only a or only b are found in the manuallySortedArray, the returned
|
2019-04-29 21:31:49 +00:00
|
|
|
* compare(a, b) function will treat the element that was found as being
|
|
|
|
* "smaller than" the not-found element (i.e. found elements appear before
|
2019-04-02 11:26:21 +00:00
|
|
|
* not-found elements in sorted order).
|
|
|
|
*
|
2019-04-29 21:31:49 +00:00
|
|
|
* The equalityFunc(a, x) argument is a function used to compare the items
|
|
|
|
* being sorted against the items in the manuallySortedArray. For example, if
|
|
|
|
* equalityFunc was (a, x) => a.startsWith(x), where a is an item being sorted
|
|
|
|
* and x is an item in the manuallySortedArray, then the manuallySortedArray
|
|
|
|
* could contain prefix substrings to guide the sorting.
|
2019-04-02 11:26:21 +00:00
|
|
|
*
|
|
|
|
* @param manuallySortedArray A pre-sorted array to guide the sorting
|
|
|
|
* @param equalityFunc An optional function used to compare the items being
|
|
|
|
* sorted against items in manuallySortedArray. It should return true if
|
|
|
|
* the two items compare equal, otherwise false. The arguments are the
|
|
|
|
* same as provided by the standard Javascript array.findIndex() method.
|
|
|
|
*/
|
|
|
|
export function getManualSortCompareFunction<T, U = T>(
|
|
|
|
manuallySortedArray: U[],
|
|
|
|
equalityFunc: (a: T, x: U, index: number, array: U[]) => boolean,
|
|
|
|
): (a: T, b: T) => number {
|
2020-06-15 22:53:07 +00:00
|
|
|
return function (a: T, b: T): number {
|
2019-04-02 11:26:21 +00:00
|
|
|
const indexA = manuallySortedArray.findIndex((x, index, array) =>
|
|
|
|
equalityFunc(a, x, index, array),
|
|
|
|
);
|
|
|
|
const indexB = manuallySortedArray.findIndex((x, index, array) =>
|
|
|
|
equalityFunc(b, x, index, array),
|
|
|
|
);
|
|
|
|
if (indexA >= 0 && indexB >= 0) {
|
|
|
|
return indexA - indexB;
|
|
|
|
} else if (indexA < 0 && indexB < 0) {
|
|
|
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
|
|
} else {
|
|
|
|
return indexA < 0 ? 1 : -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-10-13 23:52:43 +00:00
|
|
|
|
2020-01-09 00:13:32 +00:00
|
|
|
/**
|
|
|
|
* Decide whether the current shell (that executed the CLI process) is a Windows
|
|
|
|
* 'cmd.exe' shell, including PowerShell, by checking a few environment
|
|
|
|
* variables.
|
|
|
|
*/
|
|
|
|
export function isWindowsComExeShell() {
|
|
|
|
return (
|
|
|
|
// neither bash nor sh (e.g. not MSYS, MSYS2, Cygwin, WSL)
|
|
|
|
process.env.SHELL == null &&
|
|
|
|
// Windows cmd.exe or PowerShell
|
|
|
|
process.env.ComSpec != null &&
|
|
|
|
process.env.ComSpec.endsWith('cmd.exe')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:52:43 +00:00
|
|
|
/**
|
|
|
|
* Shell argument escaping compatible with sh, bash and Windows cmd.exe.
|
|
|
|
* @param arg Arguments to be escaped
|
|
|
|
* @param detectShell Whether to use the SHELL and ComSpec environment
|
|
|
|
* variables to determine the shell type (sh / bash / cmd.exe). This may be
|
|
|
|
* useful to detect MSYS / MSYS2, which use bash on Windows. However, if the
|
|
|
|
* purpose is to use child_process.spawn(..., {shell: true}) and related
|
|
|
|
* functions, set this to false because child_process.spawn() always uses
|
|
|
|
* env.ComSpec (cmd.exe) on Windows, even when running on MSYS / MSYS2.
|
|
|
|
*/
|
|
|
|
export function shellEscape(args: string[], detectShell = false): string[] {
|
2020-01-09 00:13:32 +00:00
|
|
|
const isCmdExe = detectShell
|
|
|
|
? isWindowsComExeShell()
|
|
|
|
: process.platform === 'win32';
|
|
|
|
if (isCmdExe) {
|
2020-06-15 22:53:07 +00:00
|
|
|
return args.map((v) => windowsCmdExeEscapeArg(v));
|
2019-10-13 23:52:43 +00:00
|
|
|
} else {
|
2021-04-09 21:39:23 +00:00
|
|
|
const shellEscapeFunc: typeof import('shell-escape') = require('shell-escape');
|
2020-06-15 22:53:07 +00:00
|
|
|
return args.map((v) => shellEscapeFunc([v]));
|
2019-10-13 23:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape a string argument to be passed through the Windows cmd.exe shell.
|
|
|
|
* cmd.exe escaping has some peculiarities, like using the caret character
|
|
|
|
* instead of a backslash for reserved / metacharacters. Reference:
|
|
|
|
* https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
|
|
|
|
*/
|
|
|
|
function windowsCmdExeEscapeArg(arg: string): string {
|
|
|
|
// if it is already double quoted, remove the double quotes
|
|
|
|
if (arg.length > 1 && arg.startsWith('"') && arg.endsWith('"')) {
|
|
|
|
arg = arg.slice(1, -1);
|
|
|
|
}
|
|
|
|
// escape cmd.exe metacharacters with the '^' (caret) character
|
|
|
|
arg = arg.replace(/[()%!^<>&|]/g, '^$&');
|
|
|
|
// duplicate internal double quotes, and double quote overall
|
|
|
|
return `"${arg.replace(/["]/g, '""')}"`;
|
|
|
|
}
|
2019-11-27 10:34:24 +00:00
|
|
|
|
2020-01-24 18:43:04 +00:00
|
|
|
export interface ProxyConfig {
|
|
|
|
host: string;
|
|
|
|
port: string;
|
|
|
|
username?: string;
|
|
|
|
password?: string;
|
|
|
|
proxyAuth?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether a proxy has been configured (whether global-tunnel-ng or
|
|
|
|
* global-agent) and if so, return a ProxyConfig object.
|
|
|
|
*/
|
|
|
|
export function getProxyConfig(): ProxyConfig | undefined {
|
|
|
|
const tunnelNgConfig: any = (global as any).PROXY_CONFIG;
|
|
|
|
// global-tunnel-ng
|
|
|
|
if (tunnelNgConfig) {
|
|
|
|
let username: string | undefined;
|
|
|
|
let password: string | undefined;
|
|
|
|
const proxyAuth: string = tunnelNgConfig.proxyAuth;
|
|
|
|
if (proxyAuth) {
|
|
|
|
const i = proxyAuth.lastIndexOf(':');
|
|
|
|
if (i > 0) {
|
|
|
|
username = proxyAuth.substring(0, i);
|
|
|
|
password = proxyAuth.substring(i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
host: tunnelNgConfig.host,
|
|
|
|
port: `${tunnelNgConfig.port}`,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
proxyAuth: tunnelNgConfig.proxyAuth,
|
|
|
|
};
|
|
|
|
// global-agent, or no proxy config
|
|
|
|
} else {
|
|
|
|
const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
|
|
|
|
if (proxyUrl) {
|
|
|
|
const { URL } = require('url') as typeof import('url');
|
|
|
|
let url: URL;
|
|
|
|
try {
|
|
|
|
url = new URL(proxyUrl);
|
|
|
|
} catch (_e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
host: url.hostname,
|
|
|
|
port: url.port,
|
|
|
|
username: url.username,
|
|
|
|
password: url.password,
|
|
|
|
proxyAuth:
|
|
|
|
url.username && url.password
|
|
|
|
? `${url.username}:${url.password}`
|
|
|
|
: undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-17 13:46:25 +00:00
|
|
|
|
2021-09-07 11:43:35 +00:00
|
|
|
export const expandForAppName = {
|
2020-07-31 14:35:20 +00:00
|
|
|
$expand: {
|
2021-12-20 01:13:50 +00:00
|
|
|
belongs_to__application: { $select: ['app_name', 'slug'] as any },
|
2020-07-31 14:35:20 +00:00
|
|
|
is_of__device_type: { $select: 'slug' },
|
|
|
|
is_running__release: { $select: 'commit' },
|
|
|
|
},
|
2021-09-07 11:43:35 +00:00
|
|
|
} as const;
|
|
|
|
|
|
|
|
export const expandForAppNameAndCpuArch = {
|
|
|
|
$expand: {
|
|
|
|
...expandForAppName.$expand,
|
|
|
|
is_of__device_type: {
|
|
|
|
$select: 'slug',
|
|
|
|
$expand: {
|
|
|
|
is_of__cpu_architecture: {
|
|
|
|
$select: 'slug',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as const;
|
2020-12-03 20:50:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Use the `readline` library on Windows to install SIGINT handlers.
|
|
|
|
* This appears to be necessary on MSYS / Git for Windows, and also useful
|
|
|
|
* with PowerShell to avoid the built-in "Terminate batch job? (Y/N)" prompt
|
|
|
|
* that appears to result in ungraceful / abrupt process termination.
|
|
|
|
*/
|
|
|
|
const installReadlineSigintEmitter = _.once(function emitSigint() {
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
const readline = require('readline') as typeof import('readline');
|
|
|
|
const rl = readline.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout,
|
|
|
|
});
|
|
|
|
rl.on('SIGINT', () => process.emit('SIGINT' as any));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Centralized cross-platform logic to install a SIGINT handler
|
|
|
|
* @param sigintHandler The handler function
|
|
|
|
* @param once Whether the handler should be called no more than once
|
|
|
|
*/
|
|
|
|
export function addSIGINTHandler(sigintHandler: () => void, once = true) {
|
|
|
|
installReadlineSigintEmitter();
|
|
|
|
if (once) {
|
|
|
|
process.once('SIGINT', sigintHandler);
|
|
|
|
} else {
|
|
|
|
process.on('SIGINT', sigintHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call the given task function (which returns a promise) with the given
|
|
|
|
* arguments, await the returned promise and resolve to the same result.
|
|
|
|
* While awaiting for that promise, also await for a SIGINT signal (if any),
|
|
|
|
* with a new SIGINT handler that is automatically removed on return.
|
|
|
|
* If a SIGINT signal is received while awaiting for the task function,
|
|
|
|
* immediately return a promise that rejects with SIGINTError.
|
|
|
|
* @param task An async function to be executed and awaited
|
|
|
|
* @param theArgs Arguments to be passed to the task function
|
|
|
|
*/
|
|
|
|
export async function awaitInterruptibleTask<
|
2021-07-20 13:57:00 +00:00
|
|
|
T extends (...args: any[]) => Promise<any>,
|
2020-12-03 20:50:00 +00:00
|
|
|
>(task: T, ...theArgs: Parameters<T>): Promise<ReturnType<T>> {
|
|
|
|
let sigintHandler: () => void = () => undefined;
|
|
|
|
const sigintPromise = new Promise<T>((_resolve, reject) => {
|
|
|
|
sigintHandler = () => {
|
2021-07-20 13:57:00 +00:00
|
|
|
const { SIGINTError } =
|
|
|
|
require('../errors') as typeof import('../errors');
|
2020-12-03 20:50:00 +00:00
|
|
|
reject(new SIGINTError('Task aborted on SIGINT signal'));
|
|
|
|
};
|
|
|
|
addSIGINTHandler(sigintHandler);
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
return await Promise.race([sigintPromise, task(...theArgs)]);
|
|
|
|
} finally {
|
|
|
|
process.removeListener('SIGINT', sigintHandler);
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 13:41:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pick object fields like lodash _.pick(), but also interpret "renaming
|
|
|
|
* specifications" for object keys such as "appName => fleetName" as used
|
|
|
|
* by the 'resin-cli-visuals' package.
|
|
|
|
*
|
|
|
|
* Sample input: ({ 'a': 1, 'b': 2, 'c': 3 }, ['b => x', 'c', 'd'])
|
|
|
|
* Sample output: { 'x': 2, 'c': 3 }
|
|
|
|
*/
|
|
|
|
export function pickAndRename<T extends Dictionary<any>>(
|
|
|
|
obj: T,
|
|
|
|
fields: string[],
|
|
|
|
): Dictionary<any> {
|
|
|
|
const rename: Dictionary<any> = {};
|
|
|
|
// map 'a => b' to 'a' and setup rename['a'] = 'b'
|
|
|
|
fields = fields.map((f) => {
|
|
|
|
let renameFrom = f;
|
|
|
|
let renameTo = f;
|
|
|
|
const match = f.match(/(?<from>\S+)\s+=>\s+(?<to>\S+)/);
|
|
|
|
if (match && match.groups) {
|
|
|
|
renameFrom = match.groups.from;
|
|
|
|
renameTo = match.groups.to;
|
|
|
|
}
|
|
|
|
rename[renameFrom] = renameTo;
|
|
|
|
return renameFrom;
|
|
|
|
});
|
|
|
|
return _.mapKeys(_.pick(obj, fields), (_val, key) => rename[key]);
|
|
|
|
}
|