2018-05-22 15:12:51 +00:00
|
|
|
import { stripIndent } from 'common-tags';
|
2018-10-19 14:38:50 +00:00
|
|
|
import * as BalenaSdk from 'balena-sdk';
|
2018-05-22 15:12:51 +00:00
|
|
|
|
|
|
|
import Logger = require('./logger');
|
|
|
|
|
|
|
|
import { runCommand } from './helpers';
|
|
|
|
import { exec, execBuffered } from './ssh';
|
|
|
|
|
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,
|
|
|
|
): Promise<void> {
|
|
|
|
logger.logDebug('Checking login...');
|
|
|
|
const isLoggedIn = await sdk.auth.isLoggedIn();
|
|
|
|
if (!isLoggedIn) {
|
|
|
|
logger.logInfo("Looks like you're not logged in yet!");
|
|
|
|
logger.logInfo("Let's go through a quick wizard to get you started.\n");
|
|
|
|
await runCommand('login');
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.logDebug('Determining device...');
|
|
|
|
const deviceIp = await getOrSelectLocalDevice(deviceHostnameOrIp);
|
|
|
|
await assertDeviceIsCompatible(deviceIp);
|
|
|
|
logger.logDebug(`Using device: ${deviceIp}`);
|
|
|
|
|
|
|
|
logger.logDebug('Determining device type...');
|
|
|
|
const deviceType = await getDeviceType(deviceIp);
|
|
|
|
logger.logDebug(`Device type: ${deviceType}`);
|
|
|
|
|
|
|
|
logger.logDebug('Determining application...');
|
|
|
|
const app = await getOrSelectApplication(sdk, deviceType, appName);
|
|
|
|
logger.logDebug(`Using application: ${app.app_name} (${app.device_type})`);
|
|
|
|
if (app.device_type != deviceType) {
|
|
|
|
logger.logDebug(`Forcing device type to: ${deviceType}`);
|
|
|
|
app.device_type = deviceType;
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:03:21 +00:00
|
|
|
logger.logDebug('Determining device OS version...');
|
|
|
|
const deviceOsVersion = await getOsVersion(deviceIp);
|
|
|
|
logger.logDebug(`Device OS version: ${deviceOsVersion}`);
|
|
|
|
|
2018-05-22 15:12:51 +00:00
|
|
|
logger.logDebug('Generating application config...');
|
2018-10-15 17:03:21 +00:00
|
|
|
const config = await generateApplicationConfig(sdk, app, {
|
|
|
|
version: deviceOsVersion,
|
|
|
|
});
|
2018-05-22 15:12:51 +00:00
|
|
|
logger.logDebug(`Using config: ${JSON.stringify(config, null, 2)}`);
|
|
|
|
|
|
|
|
logger.logDebug('Configuring...');
|
|
|
|
await configure(deviceIp, config);
|
|
|
|
logger.logDebug('All done.');
|
|
|
|
|
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,
|
2018-10-19 14:38:50 +00:00
|
|
|
_sdk: BalenaSdk.BalenaSDK,
|
2018-05-22 15:12:51 +00:00
|
|
|
deviceHostnameOrIp?: string,
|
|
|
|
): Promise<void> {
|
|
|
|
logger.logDebug('Determining device...');
|
|
|
|
const deviceIp = await getOrSelectLocalDevice(deviceHostnameOrIp);
|
|
|
|
await assertDeviceIsCompatible(deviceIp);
|
|
|
|
logger.logDebug(`Using device: ${deviceIp}`);
|
|
|
|
|
|
|
|
logger.logDebug('Deconfiguring...');
|
|
|
|
await deconfigure(deviceIp);
|
|
|
|
logger.logDebug('All done.');
|
|
|
|
|
|
|
|
logger.logSuccess('Device successfully left the platform.');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function execCommand(
|
|
|
|
deviceIp: string,
|
|
|
|
cmd: string,
|
|
|
|
msg: string,
|
|
|
|
): Promise<void> {
|
|
|
|
const through = await import('through2');
|
|
|
|
const visuals = await import('resin-cli-visuals');
|
|
|
|
|
|
|
|
const spinner = new visuals.Spinner(`[${deviceIp}] Connecting...`);
|
|
|
|
const innerSpinner = spinner.spinner;
|
|
|
|
|
|
|
|
const stream = through(function(data, _enc, cb) {
|
|
|
|
innerSpinner.setSpinnerTitle(`%s [${deviceIp}] ${msg}`);
|
|
|
|
cb(null, data);
|
|
|
|
});
|
|
|
|
|
|
|
|
spinner.start();
|
|
|
|
await exec(deviceIp, cmd, stream);
|
|
|
|
spinner.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
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})"`;
|
|
|
|
await execCommand(deviceIp, `os-config join '${str}'`, 'Configuring...');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deconfigure(deviceIp: string): Promise<void> {
|
|
|
|
await execCommand(deviceIp, 'os-config leave', 'Configuring...');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function assertDeviceIsCompatible(deviceIp: string): Promise<void> {
|
|
|
|
const { exitWithExpectedError } = await import('../utils/patterns');
|
|
|
|
try {
|
|
|
|
await execBuffered(deviceIp, 'os-config --version');
|
|
|
|
} catch (err) {
|
|
|
|
exitWithExpectedError(stripIndent`
|
|
|
|
Device "${deviceIp}" is incompatible and cannot join or leave an application.
|
2018-10-19 14:38:50 +00:00
|
|
|
Please select or provision device with balenaOS newer than ${MIN_BALENAOS_VERSION}.`);
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getDeviceType(deviceIp: string): Promise<string> {
|
|
|
|
const output = await execBuffered(deviceIp, 'cat /etc/os-release');
|
|
|
|
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> {
|
|
|
|
const output = await execBuffered(deviceIp, 'cat /etc/os-release');
|
|
|
|
const match = /^VERSION_ID="([^"]+)"$/m.exec(output);
|
|
|
|
if (!match) {
|
|
|
|
throw new Error('Failed to determine OS version ID');
|
|
|
|
}
|
|
|
|
return match[1];
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:12:51 +00:00
|
|
|
async function getOrSelectLocalDevice(deviceIp?: string): Promise<string> {
|
|
|
|
if (deviceIp) {
|
|
|
|
return deviceIp;
|
|
|
|
}
|
|
|
|
|
|
|
|
const through = await import('through2');
|
|
|
|
|
|
|
|
let ip: string | null = null;
|
|
|
|
const stream = through(function(data, _enc, cb) {
|
|
|
|
const match = /^==> Selected device: (.*)$/m.exec(data.toString());
|
|
|
|
if (match) {
|
|
|
|
ip = match[1];
|
|
|
|
cb();
|
|
|
|
} else {
|
|
|
|
cb(null, data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.pipe(process.stderr);
|
|
|
|
|
|
|
|
const { sudo } = await import('../utils/helpers');
|
|
|
|
const command = process.argv.slice(0, 2).concat(['internal', 'scandevices']);
|
|
|
|
await sudo(command, {
|
|
|
|
stderr: stream,
|
|
|
|
msg:
|
|
|
|
'Scanning for local devices. If asked, please type your computer password.',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!ip) {
|
|
|
|
throw new Error('No device selected');
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
async function selectAppFromList(applications: BalenaSdk.Application[]) {
|
2018-10-20 12:35:34 +00:00
|
|
|
const _ = await import('lodash');
|
|
|
|
const { selectFromList } = await import('../utils/patterns');
|
|
|
|
|
2018-10-20 12:32:35 +00:00
|
|
|
// Present a list to the user which shows the fully qualified application
|
2018-10-20 12:35:34 +00:00
|
|
|
// name (user/appname) and allows them to select.
|
|
|
|
return selectFromList(
|
2018-10-20 12:32:35 +00:00
|
|
|
'Select application',
|
2018-10-20 12:35:34 +00:00
|
|
|
_.map(applications, app => {
|
2018-10-20 12:32:35 +00:00
|
|
|
return _.merge({ 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,
|
2018-05-22 15:12:51 +00:00
|
|
|
deviceType: string,
|
|
|
|
appName?: string,
|
2018-10-19 14:38:50 +00:00
|
|
|
): Promise<BalenaSdk.Application> {
|
2018-05-22 15:12:51 +00:00
|
|
|
const _ = await import('lodash');
|
|
|
|
const form = await import('resin-cli-form');
|
|
|
|
|
|
|
|
const allDeviceTypes = await sdk.models.config.getDeviceTypes();
|
|
|
|
const deviceTypeManifest = _.find(allDeviceTypes, { slug: deviceType });
|
|
|
|
if (!deviceTypeManifest) {
|
|
|
|
throw new Error(`"${deviceType}" is not a valid device type`);
|
|
|
|
}
|
|
|
|
const compatibleDeviceTypes = _(allDeviceTypes)
|
|
|
|
.filter({ arch: deviceTypeManifest.arch })
|
|
|
|
.map(type => type.slug)
|
|
|
|
.value();
|
|
|
|
|
|
|
|
if (!appName) {
|
2018-10-20 12:35:34 +00:00
|
|
|
const options = {
|
|
|
|
$filter: { device_type: { $in: compatibleDeviceTypes } },
|
|
|
|
};
|
|
|
|
|
2018-05-22 15:12:51 +00:00
|
|
|
// No application specified, show a list to select one.
|
2018-10-20 12:32:35 +00:00
|
|
|
const applications = await sdk.models.application.getAll(options);
|
2018-10-20 12:35:34 +00:00
|
|
|
|
2018-05-22 15:12:51 +00:00
|
|
|
if (applications.length === 0) {
|
|
|
|
const shouldCreateApp = await form.ask({
|
|
|
|
message:
|
|
|
|
'You have no applications this device can join.\n' +
|
|
|
|
'Would you like to create one now?',
|
|
|
|
type: 'confirm',
|
|
|
|
default: true,
|
|
|
|
});
|
|
|
|
if (shouldCreateApp) {
|
|
|
|
return createApplication(sdk, deviceType);
|
|
|
|
}
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-10-20 12:35:34 +00:00
|
|
|
|
|
|
|
return selectAppFromList(applications);
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
const options: BalenaSdk.PineOptionsFor<BalenaSdk.Application> = {};
|
2018-05-22 15:12:51 +00:00
|
|
|
|
|
|
|
// Check for an app of the form `user/application` 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) {
|
2018-10-20 12:35:34 +00:00
|
|
|
// These will match at most one app
|
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
|
|
|
}
|
|
|
|
|
2018-10-20 12:32:35 +00:00
|
|
|
const applications = await sdk.models.application.getAll(options);
|
2018-05-22 15:12:51 +00:00
|
|
|
|
|
|
|
if (applications.length === 0) {
|
|
|
|
const shouldCreateApp = await form.ask({
|
|
|
|
message:
|
|
|
|
`No application found with name "${appName}".\n` +
|
|
|
|
'Would you like to create it now?',
|
|
|
|
type: 'confirm',
|
|
|
|
default: true,
|
|
|
|
});
|
|
|
|
if (shouldCreateApp) {
|
2018-10-20 12:32:35 +00:00
|
|
|
return createApplication(sdk, deviceType, name);
|
2018-05-22 15:12:51 +00:00
|
|
|
}
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've found at least one app with the given name.
|
|
|
|
// Filter out apps for non-matching device types and see what we're left with.
|
|
|
|
const validApplications = applications.filter(app =>
|
|
|
|
_.includes(compatibleDeviceTypes, app.device_type),
|
|
|
|
);
|
|
|
|
|
2018-10-20 12:35:34 +00:00
|
|
|
if (validApplications.length === 0) {
|
2018-10-29 19:32:21 +00:00
|
|
|
throw new Error('No application 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
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2018-10-19 14:38:50 +00:00
|
|
|
): Promise<BalenaSdk.Application> {
|
2018-05-22 15:12:51 +00:00
|
|
|
const form = await import('resin-cli-form');
|
|
|
|
const validation = await import('./validation');
|
|
|
|
const patterns = await import('./patterns');
|
|
|
|
|
2018-10-20 12:32:35 +00:00
|
|
|
let username = await sdk.auth.whoami();
|
|
|
|
if (!username) {
|
2018-10-19 14:38:50 +00:00
|
|
|
throw new sdk.errors.BalenaNotLoggedIn();
|
2018-10-20 12:32:35 +00:00
|
|
|
}
|
|
|
|
username = username.toLowerCase();
|
2018-05-22 15:12:51 +00:00
|
|
|
|
|
|
|
const appName = await new Promise<string>(async (resolve, reject) => {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
const appName = await form.ask({
|
|
|
|
message: 'Enter a name for your new application:',
|
|
|
|
type: 'input',
|
|
|
|
default: name,
|
|
|
|
validate: validation.validateApplicationName,
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2018-10-20 12:35:34 +00:00
|
|
|
await sdk.models.application.get(appName, {
|
2018-10-20 12:32:35 +00:00
|
|
|
$filter: {
|
|
|
|
$or: [
|
|
|
|
{ slug: { $startswith: `${username}/` } },
|
|
|
|
{ $not: { slug: { $contains: '/' } } },
|
|
|
|
],
|
|
|
|
},
|
2018-10-20 12:35:34 +00:00
|
|
|
});
|
2018-05-22 15:12:51 +00:00
|
|
|
patterns.printErrorMessage(
|
|
|
|
'You already have an application with that name; please choose another.',
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
} catch (err) {
|
|
|
|
return resolve(appName);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return sdk.models.application.create({
|
|
|
|
name: appName,
|
|
|
|
deviceType,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:03:21 +00:00
|
|
|
async function generateApplicationConfig(
|
2018-10-19 14:38:50 +00:00
|
|
|
sdk: BalenaSdk.BalenaSDK,
|
|
|
|
app: BalenaSdk.Application,
|
2018-10-15 17:03:21 +00:00
|
|
|
options: { version: string },
|
|
|
|
) {
|
2018-05-22 15:12:51 +00:00
|
|
|
const form = await import('resin-cli-form');
|
|
|
|
const { generateApplicationConfig: configGen } = await import('./config');
|
|
|
|
|
|
|
|
const manifest = await sdk.models.device.getManifestBySlug(app.device_type);
|
|
|
|
const opts =
|
|
|
|
manifest.options && manifest.options.filter(opt => opt.name !== 'network');
|
2018-10-15 17:03:21 +00:00
|
|
|
const values = {
|
|
|
|
...(await form.run(opts)),
|
|
|
|
...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;
|
|
|
|
}
|