From 8f6902f4cb582d48085527942cd8b6d8682d1b16 Mon Sep 17 00:00:00 2001 From: Rich Bayliss Date: Tue, 21 Jan 2020 16:51:06 +0000 Subject: [PATCH] configure: Allow passing system-connection files to 'os configure' command Allow passing files to `os configure` via `--system-connection` to allow pre-configuration of network connections, such as cellular/GSM. Change-type: minor Connects-to: #957 Connects-to: #1162 Connects-to: #1498 Signed-off-by: Rich Bayliss --- doc/cli.markdown | 11 +++++++ lib/actions-oclif/os/configure.ts | 53 +++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/doc/cli.markdown b/doc/cli.markdown index facf479d..49cea935 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -1300,6 +1300,13 @@ following sources, in precedence order: The --device-type option may be used to override the application's default device type, in case of an application with mixed device types. +The --system-connection (-c) option can be used to inject NetworkManager connection +profiles for additional network interfaces, such as cellular/GSM or additional +WiFi or ethernet connections. This option may be passed multiple times in case there +are multiple files to inject. See connection profile examples and reference at: +https://www.balena.io/docs/reference/OS/network/2.x/ +https://developer.gnome.org/NetworkManager/stable/nm-settings.html + The --device-api-key option is deprecated and will be removed in a future release. A suitable key is automatically generated or fetched if this option is omitted. @@ -1368,6 +1375,10 @@ device type slug (e.g. "raspberrypi3") to override the application device type balenaOS version, for example "2.32.0" or "2.44.0+rev1" +#### -c, --system-connection SYSTEM-CONNECTION + +paths to local files to place into the 'system-connections' directory + ## os initialize <image> Use this command to initialize a device with previously configured operating system image. diff --git a/lib/actions-oclif/os/configure.ts b/lib/actions-oclif/os/configure.ts index 178ddef2..b2aefe15 100644 --- a/lib/actions-oclif/os/configure.ts +++ b/lib/actions-oclif/os/configure.ts @@ -17,13 +17,18 @@ import { Command, flags } from '@oclif/command'; import BalenaSdk = require('balena-sdk'); +import Bluebird = require('bluebird'); import { stripIndent } from 'common-tags'; import * as _ from 'lodash'; +import * as path from 'path'; import { ExpectedError } from '../../errors'; import * as cf from '../../utils/common-flags'; import { CommandHelp } from '../../utils/oclif-utils'; +const BOOT_PARTITION = 1; +const CONNECTIONS_FOLDER = '/system-connections'; + interface FlagsDef { advanced?: boolean; app?: string; @@ -38,6 +43,7 @@ interface FlagsDef { 'device-type'?: string; help?: void; version?: string; + 'system-connection': string[]; } interface ArgsDef { @@ -67,7 +73,7 @@ export default class OsConfigureCmd extends Command { Configure a previously downloaded balenaOS image for a specific device type or balena application. - + Configuration settings such as WiFi authentication will be taken from the following sources, in precedence order: 1. Command-line options like \`--config-wifi-ssid\` @@ -77,6 +83,13 @@ export default class OsConfigureCmd extends Command { The --device-type option may be used to override the application's default device type, in case of an application with mixed device types. + The --system-connection (-c) option can be used to inject NetworkManager connection + profiles for additional network interfaces, such as cellular/GSM or additional + WiFi or ethernet connections. This option may be passed multiple times in case there + are multiple files to inject. See connection profile examples and reference at: + https://www.balena.io/docs/reference/OS/network/2.x/ + https://developer.gnome.org/NetworkManager/stable/nm-settings.html + ${deviceApiKeyDeprecationMsg.split('\n').join('\n\t\t')} `; public static examples = [ @@ -144,6 +157,13 @@ export default class OsConfigureCmd extends Command { version: flags.string({ description: 'balenaOS version, for example "2.32.0" or "2.44.0+rev1"', }), + 'system-connection': flags.string({ + multiple: true, + char: 'c', + required: false, + description: + "paths to local files to place into the 'system-connections' directory", + }), }; public async run() { @@ -163,6 +183,7 @@ export default class OsConfigureCmd extends Command { '../../utils/config' ); const helpers = await import('../../utils/helpers'); + const imagefs = await require('resin-image-fs'); let app: BalenaSdk.Application | undefined; let device: BalenaSdk.Device | undefined; let deviceTypeSlug: string; @@ -213,14 +234,42 @@ export default class OsConfigureCmd extends Command { console.info('Configuring operating system image'); + const image = params.image; await helpers.osProgressHandler( await devInit.configure( - params.image, + image, deviceTypeManifest, configJson || {}, answers, ), ); + + if (options['system-connection']) { + const files = await Bluebird.map( + options['system-connection'], + async filePath => { + const content = await fs.readFile(filePath, 'utf8'); + const name = path.basename(filePath); + + return { + name, + content, + }; + }, + ); + + await Bluebird.each(files, async ({ name, content }) => { + await imagefs.writeFile( + { + image, + partition: BOOT_PARTITION, + path: path.join(CONNECTIONS_FOLDER, name), + }, + content, + ); + console.info(`Copied system-connection file: ${name}`); + }); + } } }