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';
|
2020-02-27 14:55:30 +00:00
|
|
|
import { getBalenaSdk } 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
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-07-29 17:27:20 +00:00
|
|
|
export async function generateBaseConfig(
|
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,
|
2020-07-29 17:27:20 +00:00
|
|
|
)) as ImgConfig & { apiKey?: string };
|
|
|
|
// os.getConfig always returns a config for an app
|
|
|
|
delete config.apiKey;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-07-29 17:27:20 +00:00
|
|
|
export async function generateApplicationConfig(
|
2018-10-19 14:38:50 +00:00
|
|
|
application: BalenaSdk.Application,
|
2020-09-09 12:20:57 +00:00
|
|
|
options: {
|
|
|
|
version: string;
|
|
|
|
deviceType?: string;
|
|
|
|
appUpdatePollInterval?: number;
|
|
|
|
},
|
2018-01-09 15:05:24 +00:00
|
|
|
) {
|
2020-07-29 17:27:20 +00:00
|
|
|
const config = await generateBaseConfig(application, options);
|
|
|
|
|
|
|
|
if (semver.satisfies(options.version, '<2.7.8')) {
|
|
|
|
await addApplicationKey(config, application.id);
|
|
|
|
} else {
|
|
|
|
await addProvisioningKey(config, application.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
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
|
|
|
) {
|
2020-02-27 14:55:30 +00:00
|
|
|
return getBalenaSdk()
|
|
|
|
.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
|
|
|
};
|
2020-07-29 17:27:20 +00:00
|
|
|
const config = await generateBaseConfig(application, baseConfigOpts);
|
|
|
|
|
|
|
|
if (deviceApiKey == null && semver.satisfies(options.version, '<2.0.3')) {
|
|
|
|
await addApplicationKey(config, application.id);
|
|
|
|
} else {
|
|
|
|
await addDeviceKey(config, device.uuid, deviceApiKey || true);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-01-04 16:17:43 +00:00
|
|
|
function addApplicationKey(config: any, applicationNameOrId: string | number) {
|
2020-02-27 14:55:30 +00:00
|
|
|
return getBalenaSdk()
|
|
|
|
.models.application.generateApiKey(applicationNameOrId)
|
2020-07-31 15:25:48 +00:00
|
|
|
.then((apiKey) => {
|
2018-01-09 15:05:24 +00:00
|
|
|
config.apiKey = apiKey;
|
2020-07-31 15:25:48 +00:00
|
|
|
return apiKey;
|
2018-01-09 15:05:24 +00:00
|
|
|
});
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-07-11 18:56:13 +00:00
|
|
|
function addProvisioningKey(config: any, applicationNameOrId: string | number) {
|
2020-02-27 14:55:30 +00:00
|
|
|
return getBalenaSdk()
|
|
|
|
.models.application.generateProvisioningKey(applicationNameOrId)
|
2020-07-31 15:25:48 +00:00
|
|
|
.then((apiKey) => {
|
2018-07-11 18:56:13 +00:00
|
|
|
config.apiKey = apiKey;
|
2020-07-31 15:25:48 +00:00
|
|
|
return apiKey;
|
2018-07-11 18:56:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:27:20 +00:00
|
|
|
async function addDeviceKey(
|
2018-07-10 17:45:48 +00:00
|
|
|
config: any,
|
|
|
|
uuid: string,
|
|
|
|
customDeviceApiKey: string | true,
|
|
|
|
) {
|
2020-07-29 17:27:20 +00:00
|
|
|
if (customDeviceApiKey === true) {
|
|
|
|
config.deviceApiKey = await getBalenaSdk().models.device.generateDeviceKey(
|
|
|
|
uuid,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
config.deviceApiKey = customDeviceApiKey;
|
|
|
|
}
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|