2020-08-26 08:39:47 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2016-2020 Balena Ltd.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Command from '../command';
|
2021-07-15 13:41:38 +00:00
|
|
|
import { ExpectedError } from '../errors';
|
2020-12-10 12:30:17 +00:00
|
|
|
import * as cf from '../utils/common-flags';
|
2020-08-26 08:39:47 +00:00
|
|
|
import {
|
|
|
|
getBalenaSdk,
|
|
|
|
getCliForm,
|
|
|
|
getVisuals,
|
|
|
|
stripIndent,
|
|
|
|
} from '../utils/lazy';
|
2021-12-19 20:50:03 +00:00
|
|
|
import { applicationIdInfo } from '../utils/messages';
|
2020-08-26 08:39:47 +00:00
|
|
|
import type { DockerConnectionCliFlags } from '../utils/docker';
|
|
|
|
import { dockerConnectionCliFlags } from '../utils/docker';
|
2021-07-15 13:41:38 +00:00
|
|
|
import { parseAsInteger } from '../utils/validation';
|
|
|
|
|
|
|
|
import { flags } from '@oclif/command';
|
2020-08-26 08:39:47 +00:00
|
|
|
import * as _ from 'lodash';
|
2021-09-07 07:44:04 +00:00
|
|
|
import type { Application, BalenaSDK, PineExpand, Release } from 'balena-sdk';
|
2020-08-26 08:39:47 +00:00
|
|
|
import type { Preloader } from 'balena-preload';
|
|
|
|
|
|
|
|
interface FlagsDef extends DockerConnectionCliFlags {
|
2021-07-15 13:41:38 +00:00
|
|
|
fleet?: string;
|
2020-08-26 08:39:47 +00:00
|
|
|
commit?: string;
|
|
|
|
'splash-image'?: string;
|
|
|
|
'dont-check-arch': boolean;
|
|
|
|
'pin-device-to-release': boolean;
|
2020-12-28 16:08:20 +00:00
|
|
|
'additional-space'?: number;
|
2020-10-20 21:05:51 +00:00
|
|
|
'add-certificate'?: string[];
|
2020-08-26 08:39:47 +00:00
|
|
|
help: void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ArgsDef {
|
|
|
|
image: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class PreloadCmd extends Command {
|
|
|
|
public static description = stripIndent`
|
2021-07-15 13:41:38 +00:00
|
|
|
Preload a release on a disk image (or Edison zip archive).
|
2020-08-26 08:39:47 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
Preload a release (service images/containers) from a balena fleet, and optionally
|
2020-08-26 08:39:47 +00:00
|
|
|
a balenaOS splash screen, in a previously downloaded '.img' balenaOS image file
|
|
|
|
in the local disk (a zip file is only accepted for the Intel Edison device type).
|
|
|
|
After preloading, the balenaOS image file can be flashed to a device's SD card.
|
2021-07-15 13:41:38 +00:00
|
|
|
When the device boots, it will not need to download the release, as it was
|
2021-04-04 21:47:27 +00:00
|
|
|
preloaded. This is usually combined with release pinning
|
|
|
|
(https://www.balena.io/docs/learn/deploy/release-strategy/release-policy/)
|
2021-07-15 13:41:38 +00:00
|
|
|
to avoid the device downloading a newer release straight away, if available.
|
2021-04-04 21:47:27 +00:00
|
|
|
Check also the Preloading and Preregistering section of the balena CLI's advanced
|
|
|
|
masterclass document:
|
|
|
|
https://www.balena.io/docs/learn/more/masterclasses/advanced-cli/#5-preloading-and-preregistering
|
2020-08-26 08:39:47 +00:00
|
|
|
|
2020-12-10 12:30:17 +00:00
|
|
|
${applicationIdInfo.split('\n').join('\n\t\t')}
|
|
|
|
|
2021-04-04 21:47:27 +00:00
|
|
|
Note that the this command requires Docker to be installed, as further detailed
|
|
|
|
in the balena CLI's installation instructions:
|
|
|
|
https://github.com/balena-io/balena-cli/blob/master/INSTALL.md
|
|
|
|
The \`--dockerHost\` and \`--dockerPort\` flags allow a remote Docker engine to
|
|
|
|
be used, however the image file must be accessible to the remote Docker engine
|
|
|
|
on the same path given on the command line. This is because Docker's bind mount
|
|
|
|
feature is used to "share" the image with a container that performs the preload.
|
2020-08-26 08:39:47 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
public static examples = [
|
2021-07-15 13:41:38 +00:00
|
|
|
'$ balena preload balena.img --fleet MyFleet --commit e1f2592fc6ee949e68756d4f4a48e49bff8d72a0',
|
|
|
|
'$ balena preload balena.img --fleet myorg/myfleet --splash-image image.png',
|
2020-08-26 08:39:47 +00:00
|
|
|
'$ balena preload balena.img',
|
|
|
|
];
|
|
|
|
|
|
|
|
public static args = [
|
|
|
|
{
|
|
|
|
name: 'image',
|
|
|
|
description: 'the image file path',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
public static usage = 'preload <image>';
|
|
|
|
|
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
2021-07-15 13:41:38 +00:00
|
|
|
fleet: cf.fleet,
|
2020-08-26 08:39:47 +00:00
|
|
|
commit: flags.string({
|
|
|
|
description: `\
|
2021-07-15 13:41:38 +00:00
|
|
|
The commit hash of the release to preload. Use "current" to specify the current
|
|
|
|
release (ignored if no appId is given). The current release is usually also the
|
|
|
|
latest, but can be pinned to a specific release. See:
|
|
|
|
https://www.balena.io/docs/learn/deploy/release-strategy/release-policy/
|
|
|
|
https://www.balena.io/docs/learn/more/masterclasses/fleet-management/#63-pin-using-the-api
|
|
|
|
https://github.com/balena-io-examples/staged-releases\
|
2020-08-26 08:39:47 +00:00
|
|
|
`,
|
|
|
|
char: 'c',
|
|
|
|
}),
|
|
|
|
'splash-image': flags.string({
|
|
|
|
description: 'path to a png image to replace the splash screen',
|
|
|
|
char: 's',
|
|
|
|
}),
|
|
|
|
'dont-check-arch': flags.boolean({
|
2020-12-07 13:35:57 +00:00
|
|
|
default: false,
|
2020-08-26 08:39:47 +00:00
|
|
|
description:
|
2021-07-15 13:41:38 +00:00
|
|
|
'disable architecture compatibility check between image and fleet',
|
2020-08-26 08:39:47 +00:00
|
|
|
}),
|
|
|
|
'pin-device-to-release': flags.boolean({
|
2020-12-07 13:35:57 +00:00
|
|
|
default: false,
|
2020-08-26 08:39:47 +00:00
|
|
|
description:
|
|
|
|
'pin the preloaded device to the preloaded release on provision',
|
|
|
|
char: 'p',
|
|
|
|
}),
|
2020-12-28 16:08:20 +00:00
|
|
|
'additional-space': flags.integer({
|
|
|
|
description:
|
|
|
|
'expand the image by this amount of bytes instead of automatically estimating the required amount',
|
|
|
|
parse: (x) => parseAsInteger(x, 'additional-space'),
|
|
|
|
}),
|
2020-08-26 08:39:47 +00:00
|
|
|
'add-certificate': flags.string({
|
|
|
|
description: `\
|
|
|
|
Add the given certificate (in PEM format) to /etc/ssl/certs in the preloading container.
|
|
|
|
The file name must end with '.crt' and must not be already contained in the preloader's
|
|
|
|
/etc/ssl/certs folder.
|
|
|
|
Can be repeated to add multiple certificates.\
|
|
|
|
`,
|
2020-10-20 21:05:51 +00:00
|
|
|
multiple: true,
|
2020-08-26 08:39:47 +00:00
|
|
|
}),
|
|
|
|
...dockerConnectionCliFlags,
|
|
|
|
// Redefining --dockerPort here (defined already in dockerConnectionCliFlags)
|
|
|
|
// without -p alias, to avoid clash with -p alias of pin-device-to-release
|
|
|
|
dockerPort: flags.integer({
|
|
|
|
description:
|
|
|
|
'Docker daemon TCP port number (hint: 2375 for balena devices)',
|
|
|
|
parse: (p) => parseAsInteger(p, 'dockerPort'),
|
|
|
|
}),
|
|
|
|
// Not supporting -h for help, because of clash with -h in DockerCliFlags
|
|
|
|
// Revisit this in future release.
|
|
|
|
help: flags.help({}),
|
|
|
|
};
|
|
|
|
|
|
|
|
public static authenticated = true;
|
|
|
|
|
|
|
|
public static primary = true;
|
|
|
|
|
|
|
|
public async run() {
|
|
|
|
const { args: params, flags: options } = this.parse<FlagsDef, ArgsDef>(
|
|
|
|
PreloadCmd,
|
|
|
|
);
|
|
|
|
|
|
|
|
const balena = getBalenaSdk();
|
|
|
|
const balenaPreload = await import('balena-preload');
|
|
|
|
const visuals = getVisuals();
|
|
|
|
const nodeCleanup = await import('node-cleanup');
|
|
|
|
const { instanceOf } = await import('../errors');
|
|
|
|
|
|
|
|
// Check image file exists
|
|
|
|
try {
|
|
|
|
const fs = await import('fs');
|
|
|
|
await fs.promises.access(params.image);
|
2021-06-23 20:01:51 +00:00
|
|
|
const path = await import('path');
|
|
|
|
if (path.extname(params.image) === '.zip') {
|
|
|
|
console.warn(stripIndent`
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
Warning: A zip file is only accepted for the Intel Edison device type.
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
`);
|
|
|
|
}
|
2020-08-26 08:39:47 +00:00
|
|
|
} catch (error) {
|
|
|
|
throw new ExpectedError(
|
|
|
|
`The provided image path does not exist: ${params.image}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-11 13:14:12 +00:00
|
|
|
// balena-preload currently does not work with numerical app IDs
|
|
|
|
// Load app here, and use app slug from hereon
|
2021-12-19 20:50:03 +00:00
|
|
|
const fleetSlug: string | undefined = options.fleet
|
|
|
|
? await (await import('../utils/sdk')).getFleetSlug(balena, options.fleet)
|
|
|
|
: undefined;
|
2020-12-11 13:14:12 +00:00
|
|
|
|
2020-08-26 08:39:47 +00:00
|
|
|
const progressBars: {
|
2022-12-28 22:26:14 +00:00
|
|
|
[key: string]: InstanceType<ReturnType<typeof getVisuals>['Progress']>;
|
2020-08-26 08:39:47 +00:00
|
|
|
} = {};
|
|
|
|
|
|
|
|
const progressHandler = function (event: {
|
|
|
|
name: string;
|
|
|
|
percentage: number;
|
|
|
|
}) {
|
2020-08-27 10:50:57 +00:00
|
|
|
const progressBar = (progressBars[event.name] ??= new visuals.Progress(
|
|
|
|
event.name,
|
|
|
|
));
|
2020-08-26 08:39:47 +00:00
|
|
|
return progressBar.update({ percentage: event.percentage });
|
|
|
|
};
|
|
|
|
|
|
|
|
const spinners: {
|
2022-12-28 22:26:14 +00:00
|
|
|
[key: string]: InstanceType<ReturnType<typeof getVisuals>['Spinner']>;
|
2020-08-26 08:39:47 +00:00
|
|
|
} = {};
|
|
|
|
|
|
|
|
const spinnerHandler = function (event: { name: string; action: string }) {
|
2020-08-27 10:50:57 +00:00
|
|
|
const spinner = (spinners[event.name] ??= new visuals.Spinner(
|
|
|
|
event.name,
|
|
|
|
));
|
2020-08-26 08:39:47 +00:00
|
|
|
if (event.action === 'start') {
|
|
|
|
return spinner.start();
|
|
|
|
} else {
|
|
|
|
console.log();
|
|
|
|
return spinner.stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-27 10:50:57 +00:00
|
|
|
const commit = this.isCurrentCommit(options.commit || '')
|
2020-08-26 08:39:47 +00:00
|
|
|
? 'latest'
|
|
|
|
: options.commit;
|
2020-08-27 10:50:57 +00:00
|
|
|
const image = params.image;
|
|
|
|
const splashImage = options['splash-image'];
|
2020-12-28 16:08:20 +00:00
|
|
|
const additionalSpace = options['additional-space'];
|
2020-08-27 10:50:57 +00:00
|
|
|
const dontCheckArch = options['dont-check-arch'] || false;
|
|
|
|
const pinDevice = options['pin-device-to-release'] || false;
|
|
|
|
|
2021-12-19 20:50:03 +00:00
|
|
|
if (dontCheckArch && !fleetSlug) {
|
2020-08-26 08:39:47 +00:00
|
|
|
throw new ExpectedError(
|
2021-07-15 13:41:38 +00:00
|
|
|
'You need to specify a fleet if you disable the architecture check.',
|
2020-08-26 08:39:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-20 21:05:51 +00:00
|
|
|
const certificates: string[] = options['add-certificate'] || [];
|
2020-08-26 08:39:47 +00:00
|
|
|
for (const certificate of certificates) {
|
|
|
|
if (!certificate.endsWith('.crt')) {
|
|
|
|
throw new ExpectedError('Certificate file name must end with ".crt"');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a configured dockerode instance
|
|
|
|
const dockerUtils = await import('../utils/docker');
|
|
|
|
const docker = await dockerUtils.getDocker(options);
|
|
|
|
const preloader = new balenaPreload.Preloader(
|
|
|
|
null,
|
|
|
|
docker,
|
2021-12-19 20:50:03 +00:00
|
|
|
fleetSlug,
|
2020-08-27 10:50:57 +00:00
|
|
|
commit,
|
|
|
|
image,
|
|
|
|
splashImage,
|
|
|
|
undefined, // TODO: Currently always undefined, investigate approach in ssh command.
|
|
|
|
dontCheckArch,
|
|
|
|
pinDevice,
|
2020-08-26 08:39:47 +00:00
|
|
|
certificates,
|
2020-12-28 16:08:20 +00:00
|
|
|
additionalSpace,
|
2020-08-26 08:39:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let gotSignal = false;
|
|
|
|
|
|
|
|
nodeCleanup(function (_exitCode, signal) {
|
|
|
|
if (signal) {
|
|
|
|
gotSignal = true;
|
|
|
|
nodeCleanup.uninstall(); // don't call cleanup handler again
|
2021-11-23 18:11:25 +00:00
|
|
|
preloader
|
|
|
|
.cleanup()
|
|
|
|
.then(() => {
|
|
|
|
// calling process.exit() won't inform parent process of signal
|
|
|
|
process.kill(process.pid, signal);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
if (process.env.DEBUG) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
});
|
2020-08-26 08:39:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (process.env.DEBUG) {
|
|
|
|
preloader.stderr.pipe(process.stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
preloader.on('progress', progressHandler);
|
|
|
|
preloader.on('spinner', spinnerHandler);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
preloader.on('error', reject);
|
2020-08-27 10:50:57 +00:00
|
|
|
resolve(
|
|
|
|
this.prepareAndPreload(preloader, balena, {
|
2022-07-15 15:01:37 +00:00
|
|
|
slug: fleetSlug,
|
2020-08-27 10:50:57 +00:00
|
|
|
commit,
|
|
|
|
pinDevice,
|
|
|
|
}),
|
|
|
|
);
|
2020-08-26 08:39:47 +00:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
if (instanceOf(err, balena.errors.BalenaError)) {
|
|
|
|
const code = err.code ? `(${err.code})` : '';
|
|
|
|
throw new ExpectedError(`${err.message} ${code}`);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (!gotSignal) {
|
|
|
|
await preloader.cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 11:05:04 +00:00
|
|
|
readonly applicationExpandOptions: PineExpand<Application> = {
|
2020-08-26 08:39:47 +00:00
|
|
|
owns__release: {
|
|
|
|
$select: ['id', 'commit', 'end_timestamp', 'composition'],
|
|
|
|
$expand: {
|
|
|
|
contains__image: {
|
|
|
|
$select: ['image'],
|
|
|
|
$expand: {
|
|
|
|
image: {
|
|
|
|
$select: ['image_size', 'is_stored_at__image_location'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
$filter: {
|
|
|
|
status: 'success',
|
|
|
|
},
|
2021-09-06 23:09:39 +00:00
|
|
|
$orderby: [{ end_timestamp: 'desc' }, { id: 'desc' }],
|
2020-08-26 08:39:47 +00:00
|
|
|
},
|
|
|
|
should_be_running__release: {
|
|
|
|
$select: 'commit',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
isCurrentCommit(commit: string) {
|
|
|
|
return commit === 'latest' || commit === 'current';
|
|
|
|
}
|
|
|
|
|
2020-08-28 12:43:10 +00:00
|
|
|
async getApplicationsWithSuccessfulBuilds(deviceTypeSlug: string) {
|
2020-08-26 08:39:47 +00:00
|
|
|
const balena = getBalenaSdk();
|
|
|
|
|
2021-09-07 07:44:04 +00:00
|
|
|
try {
|
|
|
|
await balena.models.deviceType.get(deviceTypeSlug);
|
|
|
|
} catch {
|
|
|
|
throw new Error(`Device type "${deviceTypeSlug}" not found in API query`);
|
|
|
|
}
|
2021-12-21 00:02:45 +00:00
|
|
|
return (await balena.models.application.getAllDirectlyAccessible({
|
|
|
|
$select: ['id', 'slug', 'should_track_latest_release'],
|
2021-09-06 23:09:39 +00:00
|
|
|
$expand: this.applicationExpandOptions,
|
|
|
|
$filter: {
|
2021-09-07 07:44:04 +00:00
|
|
|
// get the apps that are of the same arch as the device type of the image
|
2021-09-06 23:09:39 +00:00
|
|
|
is_for__device_type: {
|
|
|
|
$any: {
|
|
|
|
$alias: 'dt',
|
|
|
|
$expr: {
|
|
|
|
dt: {
|
2021-09-07 07:44:04 +00:00
|
|
|
is_of__cpu_architecture: {
|
|
|
|
$any: {
|
|
|
|
$alias: 'ioca',
|
|
|
|
$expr: {
|
|
|
|
ioca: {
|
|
|
|
is_supported_by__device_type: {
|
|
|
|
$any: {
|
|
|
|
$alias: 'isbdt',
|
|
|
|
$expr: {
|
|
|
|
isbdt: {
|
|
|
|
slug: deviceTypeSlug,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-08-26 08:39:47 +00:00
|
|
|
},
|
2020-08-28 12:43:10 +00:00
|
|
|
},
|
|
|
|
},
|
2021-09-06 23:09:39 +00:00
|
|
|
},
|
|
|
|
owns__release: {
|
|
|
|
$any: {
|
|
|
|
$alias: 'r',
|
|
|
|
$expr: {
|
|
|
|
r: {
|
|
|
|
status: 'success',
|
2020-08-26 08:39:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-08-28 12:43:10 +00:00
|
|
|
},
|
2020-08-26 08:39:47 +00:00
|
|
|
},
|
2021-12-21 00:02:45 +00:00
|
|
|
$orderby: 'slug asc',
|
2021-09-06 23:09:39 +00:00
|
|
|
})) as Array<
|
|
|
|
ApplicationWithDeviceType & {
|
|
|
|
should_be_running__release: [Release?];
|
|
|
|
}
|
|
|
|
>;
|
2020-08-26 08:39:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 12:43:10 +00:00
|
|
|
async selectApplication(deviceTypeSlug: string) {
|
2020-08-26 08:39:47 +00:00
|
|
|
const visuals = getVisuals();
|
|
|
|
|
|
|
|
const applicationInfoSpinner = new visuals.Spinner(
|
|
|
|
'Downloading list of applications and releases.',
|
|
|
|
);
|
|
|
|
applicationInfoSpinner.start();
|
|
|
|
|
2020-08-28 12:43:10 +00:00
|
|
|
const applications = await this.getApplicationsWithSuccessfulBuilds(
|
|
|
|
deviceTypeSlug,
|
2020-08-26 08:39:47 +00:00
|
|
|
);
|
2020-08-28 12:43:10 +00:00
|
|
|
applicationInfoSpinner.stop();
|
|
|
|
if (applications.length === 0) {
|
|
|
|
throw new ExpectedError(
|
2021-07-15 13:41:38 +00:00
|
|
|
`No fleets found with successful releases for device type '${deviceTypeSlug}'`,
|
2020-08-28 12:43:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return getCliForm().ask({
|
2021-07-15 13:41:38 +00:00
|
|
|
message: 'Select a fleet',
|
2020-08-28 12:43:10 +00:00
|
|
|
type: 'list',
|
|
|
|
choices: applications.map((app) => ({
|
2021-12-21 00:02:45 +00:00
|
|
|
name: app.slug,
|
2020-08-28 12:43:10 +00:00
|
|
|
value: app,
|
|
|
|
})),
|
|
|
|
});
|
2020-08-26 08:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
selectApplicationCommit(releases: Release[]) {
|
|
|
|
if (releases.length === 0) {
|
2021-07-15 13:41:38 +00:00
|
|
|
throw new ExpectedError('This fleet has no successful releases.');
|
2020-08-26 08:39:47 +00:00
|
|
|
}
|
|
|
|
const DEFAULT_CHOICE = { name: 'current', value: 'current' };
|
|
|
|
const choices = [DEFAULT_CHOICE].concat(
|
|
|
|
releases.map((release) => ({
|
|
|
|
name: `${release.end_timestamp} - ${release.commit}`,
|
|
|
|
value: release.commit,
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
return getCliForm().ask({
|
|
|
|
message: 'Select a release',
|
|
|
|
type: 'list',
|
|
|
|
default: 'current',
|
|
|
|
choices,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async offerToDisableAutomaticUpdates(
|
|
|
|
application: Application,
|
|
|
|
commit: string,
|
|
|
|
pinDevice: boolean,
|
|
|
|
) {
|
|
|
|
const balena = getBalenaSdk();
|
|
|
|
|
|
|
|
if (
|
|
|
|
this.isCurrentCommit(commit) ||
|
|
|
|
!application.should_track_latest_release ||
|
|
|
|
pinDevice
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const message = `\
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
This fleet is set to track the latest release, and non-pinned devices
|
2020-08-26 08:39:47 +00:00
|
|
|
are automatically updated when a new release is available. This may lead to
|
|
|
|
unexpected behavior: The preloaded device will download and install the latest
|
|
|
|
release once it is online.
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
This prompt gives you the opportunity to disable automatic updates for
|
|
|
|
this fleet now. Note that this would result in the fleet being pinned to
|
|
|
|
the current latest release, rather than some other release that may have
|
2020-08-26 08:39:47 +00:00
|
|
|
been selected for preloading. The pinned released may be further managed
|
|
|
|
through the web dashboard or programatically through the balena API / SDK.
|
2021-07-15 13:41:38 +00:00
|
|
|
Documentation about release policies and pinning can be found at:
|
2020-08-26 08:39:47 +00:00
|
|
|
https://www.balena.io/docs/learn/deploy/release-strategy/release-policy/
|
|
|
|
|
|
|
|
Alternatively, the --pin-device-to-release flag may be used to pin only the
|
|
|
|
preloaded device to the selected release.
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
Would you like to disable automatic updates for this fleet now?\
|
2020-08-26 08:39:47 +00:00
|
|
|
`;
|
2020-08-28 12:43:10 +00:00
|
|
|
const update = await getCliForm().ask({
|
|
|
|
message,
|
|
|
|
type: 'confirm',
|
|
|
|
});
|
|
|
|
if (!update) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return await balena.pine.patch({
|
|
|
|
resource: 'application',
|
|
|
|
id: application.id,
|
|
|
|
body: {
|
|
|
|
should_track_latest_release: false,
|
|
|
|
},
|
|
|
|
});
|
2020-08-26 08:39:47 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 15:01:37 +00:00
|
|
|
async getAppWithReleases(balenaSdk: BalenaSDK, slug: string) {
|
2020-11-04 15:42:31 +00:00
|
|
|
const { getApplication } = await import('../utils/sdk');
|
|
|
|
|
2022-07-15 15:01:37 +00:00
|
|
|
return (await getApplication(balenaSdk, slug, {
|
2020-08-26 08:39:47 +00:00
|
|
|
$expand: this.applicationExpandOptions,
|
2020-11-04 15:42:31 +00:00
|
|
|
})) as Application & { should_be_running__release: [Release?] };
|
2020-08-26 08:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async prepareAndPreload(
|
|
|
|
preloader: Preloader,
|
|
|
|
balenaSdk: BalenaSDK,
|
2020-08-27 10:50:57 +00:00
|
|
|
options: {
|
2022-07-15 15:01:37 +00:00
|
|
|
slug?: string;
|
2020-08-27 10:50:57 +00:00
|
|
|
commit?: string;
|
|
|
|
pinDevice: boolean;
|
|
|
|
},
|
2020-08-26 08:39:47 +00:00
|
|
|
) {
|
|
|
|
await preloader.prepare();
|
|
|
|
|
2022-07-15 15:01:37 +00:00
|
|
|
const application = options.slug
|
|
|
|
? await this.getAppWithReleases(balenaSdk, options.slug)
|
2020-08-26 08:39:47 +00:00
|
|
|
: await this.selectApplication(preloader.config.deviceType);
|
|
|
|
|
|
|
|
let commit: string; // commit hash or the strings 'latest' or 'current'
|
|
|
|
|
|
|
|
const appCommit = application.should_be_running__release[0]?.commit;
|
|
|
|
|
|
|
|
// Use the commit given as --commit or show an interactive commit selection menu
|
|
|
|
if (options.commit) {
|
|
|
|
if (this.isCurrentCommit(options.commit)) {
|
|
|
|
if (!appCommit) {
|
|
|
|
throw new Error(
|
2022-07-15 15:01:37 +00:00
|
|
|
`Unexpected empty commit hash for fleet slug "${application.slug}"`,
|
2020-08-26 08:39:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
// handle `--commit current` (and its `--commit latest` synonym)
|
|
|
|
commit = 'latest';
|
|
|
|
} else {
|
|
|
|
const release = _.find(application.owns__release, (r) =>
|
|
|
|
r.commit.startsWith(options.commit!),
|
|
|
|
);
|
|
|
|
if (!release) {
|
|
|
|
throw new ExpectedError(
|
|
|
|
`There is no release matching commit "${options.commit}"`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
commit = release.commit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// this could have the value 'current'
|
|
|
|
commit = await this.selectApplicationCommit(
|
|
|
|
application.owns__release as Release[],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await preloader.setAppIdAndCommit(
|
|
|
|
application.id,
|
|
|
|
this.isCurrentCommit(commit) ? appCommit! : commit,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Propose to disable automatic app updates if the commit is not the current release
|
|
|
|
await this.offerToDisableAutomaticUpdates(
|
|
|
|
application,
|
|
|
|
commit,
|
2020-08-27 10:50:57 +00:00
|
|
|
options.pinDevice,
|
2020-08-26 08:39:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// All options are ready: preload the image.
|
|
|
|
await preloader.preload();
|
|
|
|
}
|
|
|
|
}
|