2017-12-20 21:46:01 +00:00
|
|
|
/*
|
2019-10-31 01:46:14 +00:00
|
|
|
Copyright 2016-2019 Balena
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
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';
|
2020-03-02 17:13:16 +00:00
|
|
|
import * as semver from 'balena-semver';
|
2021-12-24 15:10:59 +00:00
|
|
|
import { getBalenaSdk, stripIndent } from './lazy';
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-10-31 01:46:14 +00:00
|
|
|
export interface ImgConfig {
|
2018-10-15 17:03:21 +00:00
|
|
|
applicationName: string;
|
|
|
|
applicationId: number;
|
|
|
|
deviceType: string;
|
|
|
|
userId: number;
|
|
|
|
username: string;
|
|
|
|
appUpdatePollInterval: number;
|
|
|
|
listenPort: number;
|
|
|
|
vpnPort: number;
|
|
|
|
apiEndpoint: string;
|
|
|
|
vpnEndpoint: string;
|
|
|
|
registryEndpoint: string;
|
|
|
|
deltaEndpoint: string;
|
|
|
|
mixpanelToken: string;
|
|
|
|
wifiSsid?: string;
|
|
|
|
wifiKey?: string;
|
2020-06-11 11:32:20 +00:00
|
|
|
initialDeviceName?: string;
|
2018-10-15 17:03:21 +00:00
|
|
|
|
2021-11-25 01:07:03 +00:00
|
|
|
apiKey?: string;
|
|
|
|
deviceApiKey?: string;
|
|
|
|
|
2018-10-15 17:03:21 +00:00
|
|
|
// props for older OS versions
|
|
|
|
connectivity?: string;
|
|
|
|
files?: {
|
|
|
|
[filepath: string]: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
// device specific config props
|
|
|
|
deviceId?: number;
|
|
|
|
uuid?: string;
|
|
|
|
registered_at?: number;
|
2019-07-08 16:04:33 +00:00
|
|
|
|
|
|
|
os?: {
|
|
|
|
sshKeys?: string[];
|
|
|
|
};
|
2019-03-12 22:07:57 +00:00
|
|
|
}
|
2018-07-19 19:18:43 +00:00
|
|
|
|
2021-11-25 01:07:03 +00:00
|
|
|
export async function generateApplicationConfig(
|
2018-10-19 14:38:50 +00:00
|
|
|
application: BalenaSdk.Application,
|
2018-12-14 10:55:35 +00:00
|
|
|
options: {
|
|
|
|
version: string;
|
|
|
|
appUpdatePollInterval?: number;
|
|
|
|
deviceType?: string;
|
2019-07-08 16:04:33 +00:00
|
|
|
os?: {
|
|
|
|
sshKeys?: string[];
|
2019-07-05 12:58:19 +00:00
|
|
|
};
|
2018-12-14 10:55:35 +00:00
|
|
|
},
|
2020-07-29 17:27:20 +00:00
|
|
|
): Promise<ImgConfig> {
|
2018-10-15 17:03:21 +00:00
|
|
|
options = {
|
|
|
|
...options,
|
|
|
|
appUpdatePollInterval: options.appUpdatePollInterval || 10,
|
|
|
|
};
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-07-29 17:27:20 +00:00
|
|
|
const config = (await getBalenaSdk().models.os.getConfig(
|
2020-10-25 16:52:31 +00:00
|
|
|
application.slug,
|
2018-10-29 19:32:21 +00:00
|
|
|
options,
|
2021-11-25 01:07:03 +00:00
|
|
|
)) as ImgConfig;
|
2020-07-29 17:27:20 +00:00
|
|
|
|
|
|
|
// merge sshKeys to config, when they have been specified
|
|
|
|
if (options.os && options.os.sshKeys) {
|
|
|
|
// Create config.os object if it does not exist
|
|
|
|
config.os = config.os ? config.os : {};
|
|
|
|
config.os.sshKeys = config.os.sshKeys
|
|
|
|
? [...config.os.sshKeys, ...options.os.sshKeys]
|
|
|
|
: options.os.sshKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
export function generateDeviceConfig(
|
2020-07-31 14:35:20 +00:00
|
|
|
device: DeviceWithDeviceType & {
|
2018-10-19 14:38:50 +00:00
|
|
|
belongs_to__application: BalenaSdk.PineDeferred;
|
|
|
|
},
|
2019-10-31 01:46:14 +00:00
|
|
|
deviceApiKey: string | true | undefined,
|
2018-12-14 10:55:35 +00:00
|
|
|
options: { version: string },
|
2017-12-20 21:46:01 +00:00
|
|
|
) {
|
2021-11-25 01:07:03 +00:00
|
|
|
const sdk = getBalenaSdk();
|
|
|
|
return sdk.models.application
|
|
|
|
.get(device.belongs_to__application.__id)
|
2020-07-29 17:27:20 +00:00
|
|
|
.then(async (application) => {
|
2018-11-23 14:04:04 +00:00
|
|
|
const baseConfigOpts = {
|
|
|
|
...options,
|
2020-07-31 14:35:20 +00:00
|
|
|
deviceType: device.is_of__device_type[0].slug,
|
2018-12-14 10:55:35 +00:00
|
|
|
};
|
2021-11-25 01:07:03 +00:00
|
|
|
// TODO: Generate the correct key beforehand and pass it to os.getConfig() once
|
|
|
|
// the API supports injecting a provided key, to avoid generating an unused one.
|
|
|
|
const config = await generateApplicationConfig(
|
|
|
|
application,
|
|
|
|
baseConfigOpts,
|
|
|
|
);
|
|
|
|
// os.getConfig always returns a config for an app
|
|
|
|
delete config.apiKey;
|
2020-07-29 17:27:20 +00:00
|
|
|
|
|
|
|
if (deviceApiKey == null && semver.satisfies(options.version, '<2.0.3')) {
|
2021-11-25 01:07:03 +00:00
|
|
|
config.apiKey = await sdk.models.application.generateApiKey(
|
|
|
|
application.id,
|
|
|
|
);
|
2020-07-29 17:27:20 +00:00
|
|
|
} else {
|
2021-11-25 01:07:03 +00:00
|
|
|
config.deviceApiKey =
|
|
|
|
typeof deviceApiKey === 'string' && deviceApiKey
|
|
|
|
? deviceApiKey
|
|
|
|
: await sdk.models.device.generateDeviceKey(device.uuid);
|
2020-07-29 17:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
2018-01-09 15:05:24 +00:00
|
|
|
})
|
2020-06-15 22:53:07 +00:00
|
|
|
.then((config) => {
|
2018-01-09 15:05:24 +00:00
|
|
|
// Associate a device, to prevent the supervisor
|
|
|
|
// from creating another one on its own.
|
|
|
|
config.registered_at = Math.floor(Date.now() / 1000);
|
|
|
|
config.deviceId = device.id;
|
|
|
|
config.uuid = device.uuid;
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-01-09 15:05:24 +00:00
|
|
|
return config;
|
|
|
|
});
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2021-12-24 15:10:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Chech whether the `--dev` option of commands related to OS configuration
|
|
|
|
* such as `os configure` and `config generate` is compatible with a given
|
|
|
|
* balenaOS version, and print a warning regarding the consequences of using
|
|
|
|
* that option.
|
|
|
|
*/
|
|
|
|
export async function validateDevOptionAndWarn(
|
|
|
|
dev?: boolean,
|
|
|
|
version?: string,
|
|
|
|
logger?: import('./logger'),
|
|
|
|
) {
|
|
|
|
if (!dev) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (version && /\bprod\b/.test(version)) {
|
|
|
|
const { ExpectedError } = await import('../errors');
|
|
|
|
throw new ExpectedError(
|
|
|
|
`Error: The '--dev' option conflicts with production balenaOS version '${version}'`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!logger) {
|
|
|
|
const Logger = await import('./logger');
|
|
|
|
logger = Logger.getLogger();
|
|
|
|
}
|
|
|
|
logger.logInfo(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.`);
|
|
|
|
}
|