2019-03-12 22:07:57 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2019 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.
|
|
|
|
*/
|
2020-02-27 12:38:50 +00:00
|
|
|
import type * as BalenaSdk from 'balena-sdk';
|
2018-05-22 15:12:51 +00:00
|
|
|
|
2020-04-30 08:45:16 +00:00
|
|
|
import { ExpectedError, printErrorMessage } from '../errors';
|
2020-07-08 17:03:10 +00:00
|
|
|
import { getVisuals, stripIndent, getCliForm } from './lazy';
|
2019-03-12 22:07:57 +00:00
|
|
|
import Logger = require('./logger');
|
2021-08-26 23:49:54 +00:00
|
|
|
import { confirm } from './patterns';
|
2022-02-11 17:18:42 +00:00
|
|
|
import { getLocalDeviceCmdStdout, getDeviceOsRelease } from './ssh';
|
2018-05-22 15:12:51 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
const MIN_BALENAOS_VERSION = 'v2.14.0';
|
2018-05-22 15:12:51 +00:00
|
|
|
|
|
|
|
export async function join(
|
|
|
|
logger: Logger,
|
2018-10-19 14:38:50 +00:00
|
|
|
sdk: BalenaSdk.BalenaSDK,
|
2018-05-22 15:12:51 +00:00
|
|
|
deviceHostnameOrIp?: string,
|
|
|
|
appName?: string,
|
2020-09-09 12:20:57 +00:00
|
|
|
appUpdatePollInterval?: number,
|
2018-05-22 15:12:51 +00:00
|
|
|
): Promise<void> {
|
|
|
|
logger.logDebug('Determining device...');
|
2020-12-30 15:36:46 +00:00
|
|
|
deviceHostnameOrIp = deviceHostnameOrIp || (await selectLocalDevice());
|
|
|
|
await assertDeviceIsCompatible(deviceHostnameOrIp);
|
|
|
|
logger.logDebug(`Using device: ${deviceHostnameOrIp}`);
|
2018-05-22 15:12:51 +00:00
|
|
|
|
|
|
|
logger.logDebug('Determining device type...');
|
2020-12-30 15:36:46 +00:00
|
|
|
const deviceType = await getDeviceType(deviceHostnameOrIp);
|
2018-05-22 15:12:51 +00:00
|
|
|
logger.logDebug(`Device type: ${deviceType}`);
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
logger.logDebug('Determining fleet...');
|
2018-05-22 15:12:51 +00:00
|
|
|
const app = await getOrSelectApplication(sdk, deviceType, appName);
|
2020-07-31 14:35:20 +00:00
|
|
|
logger.logDebug(
|
2021-07-15 13:41:38 +00:00
|
|
|
`Using fleet: ${app.app_name} (${app.is_for__device_type[0].slug})`,
|
2020-07-31 14:35:20 +00:00
|
|
|
);
|
|
|
|
if (app.is_for__device_type[0].slug !== deviceType) {
|
2018-05-22 15:12:51 +00:00
|
|
|
logger.logDebug(`Forcing device type to: ${deviceType}`);
|
2020-07-31 14:35:20 +00:00
|
|
|
app.is_for__device_type[0].slug = deviceType;
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 17:03:21 +00:00
|
|
|
logger.logDebug('Determining device OS version...');
|
2020-12-30 15:36:46 +00:00
|
|
|
const deviceOsVersion = await getOsVersion(deviceHostnameOrIp);
|
2018-10-15 17:03:21 +00:00
|
|
|
logger.logDebug(`Device OS version: ${deviceOsVersion}`);
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
logger.logDebug('Generating fleet config...');
|
2018-10-15 17:03:21 +00:00
|
|
|
const config = await generateApplicationConfig(sdk, app, {
|
|
|
|
version: deviceOsVersion,
|
2020-09-09 12:20:57 +00:00
|
|
|
appUpdatePollInterval,
|
2018-10-15 17:03:21 +00:00
|
|
|
});
|
2018-05-22 15:12:51 +00:00
|
|
|
logger.logDebug(`Using config: ${JSON.stringify(config, null, 2)}`);
|
|
|
|
|
|
|
|
logger.logDebug('Configuring...');
|
2020-12-30 15:36:46 +00:00
|
|
|
await configure(deviceHostnameOrIp, config);
|
2018-05-22 15:12:51 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
const platformUrl = await sdk.settings.get('balenaUrl');
|
2018-05-22 15:12:51 +00:00
|
|
|
logger.logSuccess(`Device successfully joined ${platformUrl}!`);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function leave(
|
|
|
|
logger: Logger,
|
|
|
|
deviceHostnameOrIp?: string,
|
|
|
|
): Promise<void> {
|
|
|
|
logger.logDebug('Determining device...');
|
2020-12-30 15:36:46 +00:00
|
|
|
deviceHostnameOrIp = deviceHostnameOrIp || (await selectLocalDevice());
|
|
|
|
await assertDeviceIsCompatible(deviceHostnameOrIp);
|
|
|
|
logger.logDebug(`Using device: ${deviceHostnameOrIp}`);
|
2018-05-22 15:12:51 +00:00
|
|
|
|
|
|
|
logger.logDebug('Deconfiguring...');
|
2020-12-30 15:36:46 +00:00
|
|
|
await deconfigure(deviceHostnameOrIp);
|
2018-05-22 15:12:51 +00:00
|
|
|
|
2022-01-14 18:07:32 +00:00
|
|
|
logger.logSuccess(stripIndent`
|
|
|
|
Device successfully left the platform. The device will still be listed as part
|
|
|
|
of the fleet, but changes to the fleet will no longer affect the device and its
|
|
|
|
status will eventually be reported as 'Offline'. To irrecoverably delete the
|
|
|
|
device from the fleet, use the 'balena device rm' command or delete it through
|
|
|
|
the balenaCloud web dashboard.`);
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function execCommand(
|
|
|
|
deviceIp: string,
|
|
|
|
cmd: string,
|
|
|
|
msg: string,
|
|
|
|
): Promise<void> {
|
2022-02-11 17:04:32 +00:00
|
|
|
const { Writable } = await import('stream');
|
2020-02-28 13:06:14 +00:00
|
|
|
const visuals = getVisuals();
|
2018-05-22 15:12:51 +00:00
|
|
|
|
|
|
|
const spinner = new visuals.Spinner(`[${deviceIp}] Connecting...`);
|
|
|
|
const innerSpinner = spinner.spinner;
|
|
|
|
|
2022-02-11 17:04:32 +00:00
|
|
|
const stream = new Writable({
|
|
|
|
write(_chunk: Buffer, _enc, callback) {
|
|
|
|
innerSpinner.setSpinnerTitle(`%s [${deviceIp}] ${msg}`);
|
|
|
|
callback();
|
|
|
|
},
|
2018-05-22 15:12:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
spinner.start();
|
2022-02-11 17:18:42 +00:00
|
|
|
try {
|
|
|
|
await getLocalDeviceCmdStdout(deviceIp, cmd, stream);
|
|
|
|
} finally {
|
|
|
|
spinner.stop();
|
|
|
|
}
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function configure(deviceIp: string, config: any): Promise<void> {
|
|
|
|
// Passing the JSON is slightly tricky due to the many layers of indirection
|
|
|
|
// so we just base64-encode it here and decode it at the other end, when invoking
|
|
|
|
// os-config.
|
|
|
|
const json = JSON.stringify(config);
|
|
|
|
const b64 = Buffer.from(json).toString('base64');
|
|
|
|
const str = `"$(base64 -d <<< ${b64})"`;
|
2020-01-09 00:13:32 +00:00
|
|
|
await execCommand(deviceIp, `os-config join ${str}`, 'Configuring...');
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function deconfigure(deviceIp: string): Promise<void> {
|
|
|
|
await execCommand(deviceIp, 'os-config leave', 'Configuring...');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function assertDeviceIsCompatible(deviceIp: string): Promise<void> {
|
2020-01-09 00:13:32 +00:00
|
|
|
const cmd = 'os-config --version';
|
2018-05-22 15:12:51 +00:00
|
|
|
try {
|
2022-02-11 17:18:42 +00:00
|
|
|
await getLocalDeviceCmdStdout(deviceIp, cmd);
|
2018-05-22 15:12:51 +00:00
|
|
|
} catch (err) {
|
2020-01-09 00:13:32 +00:00
|
|
|
if (err instanceof ExpectedError) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
console.error(`${err}\n`);
|
|
|
|
throw new ExpectedError(stripIndent`
|
|
|
|
Failed to execute "${cmd}" on device "${deviceIp}".
|
|
|
|
Depending on more specific error messages above, this may mean that the device
|
|
|
|
is incompatible. Please ensure that the device is running a balenaOS release
|
|
|
|
newer than ${MIN_BALENAOS_VERSION}.`);
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getDeviceType(deviceIp: string): Promise<string> {
|
2020-01-09 00:13:32 +00:00
|
|
|
const output = await getDeviceOsRelease(deviceIp);
|
2018-05-22 15:12:51 +00:00
|
|
|
const match = /^SLUG="([^"]+)"$/m.exec(output);
|
|
|
|
if (!match) {
|
|
|
|
throw new Error('Failed to determine device type');
|
|
|
|
}
|
|
|
|
return match[1];
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:03:21 +00:00
|
|
|
async function getOsVersion(deviceIp: string): Promise<string> {
|
2020-01-09 00:13:32 +00:00
|
|
|
const output = await getDeviceOsRelease(deviceIp);
|
2018-10-15 17:03:21 +00:00
|
|
|
const match = /^VERSION_ID="([^"]+)"$/m.exec(output);
|
|
|
|
if (!match) {
|
|
|
|
throw new Error('Failed to determine OS version ID');
|
|
|
|
}
|
|
|
|
return match[1];
|
|
|
|
}
|
|
|
|
|
2020-12-30 15:36:46 +00:00
|
|
|
async function selectLocalDevice(): Promise<string> {
|
|
|
|
const { forms } = await import('balena-sync');
|
|
|
|
let hostnameOrIp;
|
|
|
|
try {
|
|
|
|
hostnameOrIp = await forms.selectLocalBalenaOsDevice();
|
|
|
|
console.error(`==> Selected device: ${hostnameOrIp}`);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.message.toLowerCase().includes('could not find any')) {
|
|
|
|
throw new ExpectedError(e);
|
2018-05-22 15:12:51 +00:00
|
|
|
} else {
|
2020-12-30 15:36:46 +00:00
|
|
|
throw e;
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 15:36:46 +00:00
|
|
|
return hostnameOrIp;
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 14:35:20 +00:00
|
|
|
async function selectAppFromList(
|
|
|
|
applications: ApplicationWithDeviceType[],
|
|
|
|
): Promise<ApplicationWithDeviceType> {
|
2018-10-20 12:35:34 +00:00
|
|
|
const _ = await import('lodash');
|
|
|
|
const { selectFromList } = await import('../utils/patterns');
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
// Present a list to the user which shows the fully qualified fleet
|
|
|
|
// name (user/fleetname) and allows them to select.
|
2018-10-20 12:35:34 +00:00
|
|
|
return selectFromList(
|
2021-07-15 13:41:38 +00:00
|
|
|
'Select fleet',
|
2020-06-15 22:53:07 +00:00
|
|
|
_.map(applications, (app) => {
|
2020-02-29 22:06:54 +00:00
|
|
|
return { name: app.slug, ...app };
|
2018-10-20 12:35:34 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:12:51 +00:00
|
|
|
async function getOrSelectApplication(
|
2018-10-19 14:38:50 +00:00
|
|
|
sdk: BalenaSdk.BalenaSDK,
|
2022-10-17 14:00:29 +00:00
|
|
|
deviceTypeSlug: string,
|
2018-05-22 15:12:51 +00:00
|
|
|
appName?: string,
|
2020-07-31 14:35:20 +00:00
|
|
|
): Promise<ApplicationWithDeviceType> {
|
2022-10-17 14:00:29 +00:00
|
|
|
const pineOptions = {
|
|
|
|
$select: 'slug',
|
|
|
|
$expand: {
|
|
|
|
is_of__cpu_architecture: {
|
|
|
|
$select: 'slug',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as const;
|
|
|
|
const [deviceType, allDeviceTypes] = await Promise.all([
|
|
|
|
sdk.models.deviceType.get(deviceTypeSlug, pineOptions) as Promise<
|
|
|
|
BalenaSdk.PineTypedResult<BalenaSdk.DeviceType, typeof pineOptions>
|
|
|
|
>,
|
|
|
|
sdk.models.deviceType.getAllSupported(pineOptions) as Promise<
|
|
|
|
Array<BalenaSdk.PineTypedResult<BalenaSdk.DeviceType, typeof pineOptions>>
|
|
|
|
>,
|
|
|
|
]);
|
|
|
|
|
|
|
|
const compatibleDeviceTypes = allDeviceTypes
|
|
|
|
.filter((dt) =>
|
|
|
|
sdk.models.os.isArchitectureCompatibleWith(
|
|
|
|
deviceType.is_of__cpu_architecture[0].slug,
|
|
|
|
dt.is_of__cpu_architecture[0].slug,
|
|
|
|
),
|
2019-09-16 13:21:07 +00:00
|
|
|
)
|
2022-10-17 14:00:29 +00:00
|
|
|
.map((type) => type.slug);
|
2018-05-22 15:12:51 +00:00
|
|
|
|
|
|
|
if (!appName) {
|
2022-10-17 14:00:29 +00:00
|
|
|
return createOrSelectApp(sdk, compatibleDeviceTypes, deviceTypeSlug);
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 14:35:20 +00:00
|
|
|
const options: BalenaSdk.PineOptions<BalenaSdk.Application> = {
|
|
|
|
$expand: {
|
|
|
|
is_for__device_type: { $select: 'slug' },
|
|
|
|
},
|
|
|
|
};
|
2018-05-22 15:12:51 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
// Check for a fleet slug of the form `user/fleet` and update the API query.
|
2018-10-20 12:32:35 +00:00
|
|
|
let name: string;
|
2018-05-22 15:12:51 +00:00
|
|
|
const match = appName.split('/');
|
|
|
|
if (match.length > 1) {
|
2021-07-15 13:41:38 +00:00
|
|
|
// These will match at most one fleet
|
2018-10-20 12:32:35 +00:00
|
|
|
options.$filter = { slug: appName.toLowerCase() };
|
|
|
|
name = match[1];
|
2018-10-20 12:35:34 +00:00
|
|
|
} else {
|
|
|
|
// We're given an application; resolve it if it's ambiguous and also validate
|
|
|
|
// it's of appropriate device type.
|
|
|
|
options.$filter = { app_name: appName };
|
2018-10-20 12:32:35 +00:00
|
|
|
name = appName;
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 00:02:45 +00:00
|
|
|
const applications = (await sdk.models.application.getAllDirectlyAccessible(
|
2020-07-31 14:35:20 +00:00
|
|
|
options,
|
|
|
|
)) as ApplicationWithDeviceType[];
|
2018-05-22 15:12:51 +00:00
|
|
|
|
|
|
|
if (applications.length === 0) {
|
2021-08-26 23:49:54 +00:00
|
|
|
await confirm(
|
|
|
|
false,
|
|
|
|
`No fleet found with name "${appName}".\n` +
|
2018-05-22 15:12:51 +00:00
|
|
|
'Would you like to create it now?',
|
2021-08-26 23:49:54 +00:00
|
|
|
undefined,
|
|
|
|
true,
|
|
|
|
);
|
2022-10-17 14:00:29 +00:00
|
|
|
return await createApplication(sdk, deviceTypeSlug, name);
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
// We've found at least one fleet with the given name.
|
|
|
|
// Filter out fleets for non-matching device types and see what we're left with.
|
2020-06-15 22:53:07 +00:00
|
|
|
const validApplications = applications.filter((app) =>
|
2022-10-17 14:00:29 +00:00
|
|
|
compatibleDeviceTypes.includes(app.is_for__device_type[0].slug),
|
2018-05-22 15:12:51 +00:00
|
|
|
);
|
|
|
|
|
2018-10-20 12:35:34 +00:00
|
|
|
if (validApplications.length === 0) {
|
2021-07-15 13:41:38 +00:00
|
|
|
throw new ExpectedError('No fleet found with a matching device type');
|
2018-10-20 12:35:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:12:51 +00:00
|
|
|
if (validApplications.length === 1) {
|
|
|
|
return validApplications[0];
|
|
|
|
}
|
|
|
|
|
2018-10-20 12:35:34 +00:00
|
|
|
return selectAppFromList(applications);
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 23:49:54 +00:00
|
|
|
async function createOrSelectApp(
|
2019-03-12 22:07:57 +00:00
|
|
|
sdk: BalenaSdk.BalenaSDK,
|
|
|
|
compatibleDeviceTypes: string[],
|
|
|
|
deviceType: string,
|
2020-07-31 14:35:20 +00:00
|
|
|
): Promise<ApplicationWithDeviceType> {
|
2021-07-15 13:41:38 +00:00
|
|
|
// No fleet specified, show a list to select one.
|
2021-12-21 00:02:45 +00:00
|
|
|
const applications = (await sdk.models.application.getAllDirectlyAccessible({
|
2020-07-31 14:35:20 +00:00
|
|
|
$expand: { is_for__device_type: { $select: 'slug' } },
|
|
|
|
$filter: {
|
|
|
|
is_for__device_type: {
|
|
|
|
$any: {
|
|
|
|
$alias: 'dt',
|
|
|
|
$expr: { dt: { slug: { $in: compatibleDeviceTypes } } },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})) as ApplicationWithDeviceType[];
|
2019-03-12 22:07:57 +00:00
|
|
|
|
|
|
|
if (applications.length === 0) {
|
2021-08-26 23:49:54 +00:00
|
|
|
await confirm(
|
|
|
|
false,
|
|
|
|
'You have no fleets this device can join.\n' +
|
2019-03-12 22:07:57 +00:00
|
|
|
'Would you like to create one now?',
|
2021-08-26 23:49:54 +00:00
|
|
|
undefined,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
return await createApplication(sdk, deviceType);
|
2019-03-12 22:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return selectAppFromList(applications);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:12:51 +00:00
|
|
|
async function createApplication(
|
2018-10-19 14:38:50 +00:00
|
|
|
sdk: BalenaSdk.BalenaSDK,
|
2018-05-22 15:12:51 +00:00
|
|
|
deviceType: string,
|
|
|
|
name?: string,
|
2020-07-31 14:35:20 +00:00
|
|
|
): Promise<ApplicationWithDeviceType> {
|
2018-05-22 15:12:51 +00:00
|
|
|
const validation = await import('./validation');
|
|
|
|
|
2020-07-31 14:35:20 +00:00
|
|
|
const username = await sdk.auth.whoami();
|
2018-10-20 12:32:35 +00:00
|
|
|
if (!username) {
|
2018-10-19 14:38:50 +00:00
|
|
|
throw new sdk.errors.BalenaNotLoggedIn();
|
2018-10-20 12:32:35 +00:00
|
|
|
}
|
2018-05-22 15:12:51 +00:00
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
const applicationName = await new Promise<string>(async (resolve, reject) => {
|
2018-05-22 15:12:51 +00:00
|
|
|
while (true) {
|
|
|
|
try {
|
2020-07-08 17:03:10 +00:00
|
|
|
const appName = await getCliForm().ask({
|
2021-07-15 13:41:38 +00:00
|
|
|
message: 'Enter a name for your new fleet:',
|
2018-05-22 15:12:51 +00:00
|
|
|
type: 'input',
|
|
|
|
default: name,
|
|
|
|
validate: validation.validateApplicationName,
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2021-12-21 00:02:45 +00:00
|
|
|
await sdk.models.application.getDirectlyAccessible(appName, {
|
2018-10-20 12:32:35 +00:00
|
|
|
$filter: {
|
2022-07-20 12:38:35 +00:00
|
|
|
slug: { $startswith: `${username!.toLowerCase()}/` },
|
2018-10-20 12:32:35 +00:00
|
|
|
},
|
2018-10-20 12:35:34 +00:00
|
|
|
});
|
2020-04-30 08:45:16 +00:00
|
|
|
// TODO: This is the only example in the codebase where `printErrorMessage()`
|
|
|
|
// is called directly. Consider refactoring.
|
|
|
|
printErrorMessage(
|
2021-07-15 13:41:38 +00:00
|
|
|
'You already have a fleet with that name; please choose another.',
|
2018-05-22 15:12:51 +00:00
|
|
|
);
|
|
|
|
continue;
|
|
|
|
} catch (err) {
|
|
|
|
return resolve(appName);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-31 14:35:20 +00:00
|
|
|
const app = await sdk.models.application.create({
|
2019-03-12 22:07:57 +00:00
|
|
|
name: applicationName,
|
2018-05-22 15:12:51 +00:00
|
|
|
deviceType,
|
2020-07-31 14:35:20 +00:00
|
|
|
organization: username,
|
2018-05-22 15:12:51 +00:00
|
|
|
});
|
2020-07-31 14:35:20 +00:00
|
|
|
return (await sdk.models.application.get(app.id, {
|
|
|
|
$expand: {
|
|
|
|
is_for__device_type: { $select: 'slug' },
|
|
|
|
},
|
|
|
|
})) as ApplicationWithDeviceType;
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 17:03:21 +00:00
|
|
|
async function generateApplicationConfig(
|
2018-10-19 14:38:50 +00:00
|
|
|
sdk: BalenaSdk.BalenaSDK,
|
2020-07-31 14:35:20 +00:00
|
|
|
app: ApplicationWithDeviceType,
|
2020-09-09 12:20:57 +00:00
|
|
|
options: {
|
|
|
|
version: string;
|
|
|
|
appUpdatePollInterval?: number;
|
|
|
|
},
|
2018-10-15 17:03:21 +00:00
|
|
|
) {
|
2018-05-22 15:12:51 +00:00
|
|
|
const { generateApplicationConfig: configGen } = await import('./config');
|
|
|
|
|
2020-07-31 14:35:20 +00:00
|
|
|
const manifest = await sdk.models.device.getManifestBySlug(
|
|
|
|
app.is_for__device_type[0].slug,
|
|
|
|
);
|
2018-05-22 15:12:51 +00:00
|
|
|
const opts =
|
2020-06-15 22:53:07 +00:00
|
|
|
manifest.options &&
|
|
|
|
manifest.options.filter((opt) => opt.name !== 'network');
|
2020-09-09 12:20:57 +00:00
|
|
|
|
|
|
|
const override = {
|
|
|
|
appUpdatePollInterval: options.appUpdatePollInterval,
|
|
|
|
};
|
|
|
|
|
2018-10-15 17:03:21 +00:00
|
|
|
const values = {
|
2020-09-09 12:20:57 +00:00
|
|
|
...(opts ? await getCliForm().run(opts, { override }) : {}),
|
2018-10-15 17:03:21 +00:00
|
|
|
...options,
|
|
|
|
};
|
2018-05-22 15:12:51 +00:00
|
|
|
|
|
|
|
const config = await configGen(app, values);
|
|
|
|
if (config.connectivity === 'connman') {
|
|
|
|
delete config.connectivity;
|
|
|
|
delete config.files;
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|