balena-cli
Version:
The official balena Command Line Interface
110 lines • 4.56 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateApplicationConfig = generateApplicationConfig;
exports.generateDeviceConfig = generateDeviceConfig;
exports.validateDevOptionAndWarn = validateDevOptionAndWarn;
exports.validateSecureBootOptionAndWarn = validateSecureBootOptionAndWarn;
const semver = require("balena-semver");
const lazy_1 = require("./lazy");
async function generateApplicationConfig(application, options) {
var _a;
options = {
...options,
appUpdatePollInterval: options.appUpdatePollInterval || 10,
};
const config = (await (0, lazy_1.getBalenaSdk)().models.os.getConfig(application.slug, options));
if (options.os && options.os.sshKeys) {
config.os = config.os ? config.os : {};
config.os.sshKeys = config.os.sshKeys
? [...config.os.sshKeys, ...options.os.sshKeys]
: options.os.sshKeys;
}
if (options.secureBoot) {
(_a = config.installer) !== null && _a !== void 0 ? _a : (config.installer = {});
config.installer.secureboot = options.secureBoot;
}
return config;
}
function generateDeviceConfig(device, deviceApiKey, options) {
const sdk = (0, lazy_1.getBalenaSdk)();
return sdk.models.application
.get(device.belongs_to__application.__id)
.then(async (application) => {
const baseConfigOpts = {
...options,
deviceType: device.is_of__device_type[0].slug,
};
const config = await generateApplicationConfig(application, baseConfigOpts);
delete config.apiKey;
if (deviceApiKey == null && semver.satisfies(options.version, '<2.0.3')) {
config.apiKey = await sdk.models.application.generateApiKey(application.id);
}
else {
config.deviceApiKey =
typeof deviceApiKey === 'string' && deviceApiKey
? deviceApiKey
: await sdk.models.device.generateDeviceKey(device.uuid);
}
return config;
})
.then((config) => {
config.registered_at = Math.floor(Date.now() / 1000);
config.deviceId = device.id;
config.uuid = device.uuid;
return config;
});
}
async function validateDevOptionAndWarn(dev, version, logger) {
if (!dev) {
return;
}
if (version && /\bprod\b/.test(version)) {
const { ExpectedError } = await Promise.resolve().then(() => require('../errors'));
throw new ExpectedError(`Error: The '--dev' option conflicts with production balenaOS version '${version}'`);
}
if (!logger) {
const Logger = await Promise.resolve().then(() => require('./logger'));
logger = Logger.getLogger();
}
logger.logInfo((0, lazy_1.stripIndent) `
The '--dev' option is being used to configure a balenaOS image in development mode.
Please note that development mode allows unauthenticated, passwordless root ssh access
and exposes network ports such as 2375 that allows unencrypted access to balenaEngine.
Therefore, development mode should only be used in private, trusted local networks.`);
}
async function validateSecureBootOptionAndWarn(secureBoot, slug, version, logger) {
var _a;
if (!secureBoot) {
return;
}
const { ExpectedError } = await Promise.resolve().then(() => require('../errors'));
if (!version) {
throw new ExpectedError(`Error: No version provided`);
}
if (!slug) {
throw new ExpectedError(`Error: No device type provided`);
}
const sdk = (0, lazy_1.getBalenaSdk)();
const [osRelease] = await sdk.models.os.getAllOsVersions(slug, {
$select: 'contract',
$filter: { raw_version: version },
});
if (!osRelease) {
throw new ExpectedError(`Error: No ${version} release for ${slug}`);
}
if ((_a = osRelease.contract) === null || _a === void 0 ? void 0 : _a.provides.some((entry) => {
return entry.type === 'sw.feature' && entry.slug === 'secureboot';
})) {
if (!logger) {
const Logger = await Promise.resolve().then(() => require('./logger'));
logger = Logger.getLogger();
}
logger.logInfo((0, lazy_1.stripIndent) `
The '--secureBoot' option is being used to configure a balenaOS installer image
into secure boot and full disk encryption.`);
}
else {
throw new ExpectedError(`Error: The '--secureBoot' option is not supported for ${slug} in ${version}`);
}
}
//# sourceMappingURL=config.js.map
;