UNPKG

balena-cli

Version:

The official balena Command Line Interface

216 lines (211 loc) • 8.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const util_1 = require("util"); const lazy_1 = require("../../utils/lazy"); class LocalConfigureCmd extends core_1.Command { constructor() { super(...arguments); this.CONNECTIONS_FOLDER = '/system-connections'; this.inquirerOptions = (data) => [ { message: 'Network SSID', type: 'input', name: 'networkSsid', default: data.networkSsid, }, { message: 'Network Key', type: 'input', name: 'networkKey', default: data.networkKey, }, { message: 'Enable development mode? (Open ports and root access - Not for production!)', type: 'confirm', name: 'developmentMode', default: false, }, { message: 'Do you want to set advanced settings?', type: 'confirm', name: 'advancedSettings', default: false, }, { message: 'Device Hostname', type: 'input', name: 'hostname', default: data.hostname, when(answers) { return answers.advancedSettings; }, }, { message: 'Do you want to enable persistent logging?', type: 'confirm', name: 'persistentLogging', default: data.persistentLogging, when(answers) { return answers.advancedSettings; }, }, ]; this.getConfiguration = async (data) => { const _ = await Promise.resolve().then(() => require('lodash')); const inquirer = await Promise.resolve().then(() => require('inquirer')); data = _.assign(data, { persistentLogging: data.persistentLogging || false, }); const answers = await inquirer.prompt(this.inquirerOptions(data)); return _.merge(data, answers); }; this.CONNECTION_FILE = (0, lazy_1.stripIndent) ` [connection] id=resin-wifi type=wifi [wifi] hidden=true mode=infrastructure ssid=My_Wifi_Ssid [wifi-security] auth-alg=open key-mgmt=wpa-psk psk=super_secret_wifi_password [ipv4] method=auto [ipv6] addr-gen-mode=stable-privacy method=auto\ `; } async run() { const { args: params } = await this.parse(LocalConfigureCmd); const reconfix = await Promise.resolve().then(() => require('reconfix')); const { denyMount, safeUmount } = await Promise.resolve().then(() => require('../../utils/umount')); const Logger = await Promise.resolve().then(() => require('../../utils/logger')); const logger = Logger.getLogger(); const configurationSchema = await this.prepareConnectionFile(params.target); await denyMount(params.target, async () => { await safeUmount(params.target); const config = await reconfix.readConfiguration(configurationSchema, params.target); logger.logDebug('Current config:'); logger.logDebug(JSON.stringify(config)); const answers = await this.getConfiguration(config); logger.logDebug('New config:'); logger.logDebug(JSON.stringify(answers)); if (!answers.hostname) { await this.removeHostname(configurationSchema); } await reconfix.writeConfiguration(configurationSchema, answers, params.target); }); console.log('Done!'); } getConfigurationSchema(bootPartition, connectionFileName) { connectionFileName !== null && connectionFileName !== void 0 ? connectionFileName : (connectionFileName = 'resin-wifi'); return { mapper: [ { template: { persistentLogging: '{{persistentLogging}}', }, domain: [['config_json', 'persistentLogging']], }, { template: { hostname: '{{hostname}}', }, domain: [['config_json', 'hostname']], }, { template: { developmentMode: '{{developmentMode}}', }, domain: [['config_json', 'developmentMode']], }, { template: { wifi: { ssid: '{{networkSsid}}', }, 'wifi-security': { psk: '{{networkKey}}', }, }, domain: [ ['system_connections', connectionFileName, 'wifi'], ['system_connections', connectionFileName, 'wifi-security'], ], }, ], files: { system_connections: { fileset: true, type: 'ini', location: { path: this.CONNECTIONS_FOLDER.slice(1), partition: bootPartition, }, }, config_json: { type: 'json', location: { path: 'config.json', partition: bootPartition, }, }, }, }; } async prepareConnectionFile(target) { const _ = await Promise.resolve().then(() => require('lodash')); const imagefs = await Promise.resolve().then(() => require('balena-image-fs')); const { getBootPartition } = await Promise.resolve().then(() => require('balena-config-json')); const bootPartition = await getBootPartition(target); const files = await imagefs.interact(target, bootPartition, async (_fs) => { return await (0, util_1.promisify)(_fs.readdir)(this.CONNECTIONS_FOLDER); }); let connectionFileName; if (_.includes(files, 'resin-wifi')) { } else if (_.includes(files, 'resin-sample.ignore')) { await imagefs.interact(target, bootPartition, async (_fs) => { const readFileAsync = (0, util_1.promisify)(_fs.readFile); const writeFileAsync = (0, util_1.promisify)(_fs.writeFile); const contents = await readFileAsync(`${this.CONNECTIONS_FOLDER}/resin-sample.ignore`, { encoding: 'utf8' }); return await writeFileAsync(`${this.CONNECTIONS_FOLDER}/resin-wifi`, contents); }); } else if (_.includes(files, 'resin-sample')) { connectionFileName = 'resin-sample'; } else { await imagefs.interact(target, bootPartition, async (_fs) => { return await (0, util_1.promisify)(_fs.writeFile)(`${this.CONNECTIONS_FOLDER}/resin-wifi`, this.CONNECTION_FILE); }); } return await this.getConfigurationSchema(bootPartition, connectionFileName); } async removeHostname(schema) { const _ = await Promise.resolve().then(() => require('lodash')); schema.mapper = _.reject(schema.mapper, (mapper) => _.isEqual(Object.keys(mapper.template), ['hostname'])); } } LocalConfigureCmd.description = (0, lazy_1.stripIndent) ` (Re)configure a balenaOS drive or image. Configure or reconfigure a balenaOS drive or image. `; LocalConfigureCmd.examples = [ '$ balena local configure /dev/sdc', '$ balena local configure path/to/image.img', ]; LocalConfigureCmd.args = { target: core_1.Args.string({ description: 'path of drive or image to configure', required: true, }), }; LocalConfigureCmd.root = true; LocalConfigureCmd.offlineCompatible = true; exports.default = LocalConfigureCmd; //# sourceMappingURL=configure.js.map