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.
|
|
|
|
*/
|
2018-10-19 14:38:50 +00:00
|
|
|
import BalenaSdk = require('balena-sdk');
|
2020-03-02 17:13:16 +00:00
|
|
|
import * as semver from 'balena-semver';
|
2020-06-23 10:29:03 +00:00
|
|
|
import Bluebird = require('bluebird');
|
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
|
|
|
|
2018-01-09 15:05:24 +00:00
|
|
|
export 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-06-23 10:29:03 +00:00
|
|
|
): Bluebird<ImgConfig> {
|
2018-10-15 17:03:21 +00:00
|
|
|
options = {
|
|
|
|
...options,
|
|
|
|
appUpdatePollInterval: options.appUpdatePollInterval || 10,
|
|
|
|
};
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-02-27 14:55:30 +00:00
|
|
|
const promise = getBalenaSdk().models.os.getConfig(
|
2018-10-29 19:32:21 +00:00
|
|
|
application.app_name,
|
|
|
|
options,
|
2020-06-23 10:29:03 +00:00
|
|
|
) as Bluebird<ImgConfig & { apiKey?: string }>;
|
2020-06-15 22:53:07 +00:00
|
|
|
return promise.tap((config) => {
|
2018-10-15 17:03:21 +00:00
|
|
|
// os.getConfig always returns a config for an app
|
|
|
|
delete config.apiKey;
|
2019-07-08 16:04:33 +00:00
|
|
|
|
|
|
|
// merge sshKeys to config, when they have been specified
|
2019-07-05 12:58:19 +00:00
|
|
|
if (options.os && options.os.sshKeys) {
|
2019-07-08 16:04:33 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
});
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-01-09 15:05:24 +00:00
|
|
|
export function generateApplicationConfig(
|
2018-10-19 14:38:50 +00:00
|
|
|
application: BalenaSdk.Application,
|
2018-12-14 10:55:35 +00:00
|
|
|
options: { version: string; deviceType?: string },
|
2018-01-09 15:05:24 +00:00
|
|
|
) {
|
2020-06-15 22:53:07 +00:00
|
|
|
return generateBaseConfig(application, options).tap((config) => {
|
2018-11-05 08:18:18 +00:00
|
|
|
if (semver.satisfies(options.version, '<2.7.8')) {
|
2018-07-11 18:56:13 +00:00
|
|
|
return addApplicationKey(config, application.id);
|
|
|
|
}
|
2018-11-05 08:18:18 +00:00
|
|
|
return addProvisioningKey(config, application.id);
|
2018-07-11 18:56:13 +00:00
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function generateDeviceConfig(
|
2018-10-19 14:38:50 +00:00
|
|
|
device: BalenaSdk.Device & {
|
|
|
|
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-06-15 22:53:07 +00:00
|
|
|
.then((application) => {
|
2018-11-23 14:04:04 +00:00
|
|
|
const baseConfigOpts = {
|
|
|
|
...options,
|
|
|
|
deviceType: device.device_type,
|
2018-12-14 10:55:35 +00:00
|
|
|
};
|
2020-06-15 22:53:07 +00:00
|
|
|
return generateBaseConfig(application, baseConfigOpts).tap((config) => {
|
2018-11-05 08:18:18 +00:00
|
|
|
if (
|
|
|
|
deviceApiKey == null &&
|
|
|
|
semver.satisfies(options.version, '<2.0.3')
|
|
|
|
) {
|
2018-01-09 15:05:24 +00:00
|
|
|
return addApplicationKey(config, application.id);
|
|
|
|
}
|
2018-11-05 08:18:18 +00:00
|
|
|
return addDeviceKey(config, device.uuid, deviceApiKey || true);
|
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-06-15 22:53:07 +00:00
|
|
|
.tap((apiKey) => {
|
2018-01-09 15:05:24 +00:00
|
|
|
config.apiKey = apiKey;
|
|
|
|
});
|
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-06-15 22:53:07 +00:00
|
|
|
.tap((apiKey) => {
|
2018-07-11 18:56:13 +00:00
|
|
|
config.apiKey = apiKey;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-10 17:45:48 +00:00
|
|
|
function addDeviceKey(
|
|
|
|
config: any,
|
|
|
|
uuid: string,
|
|
|
|
customDeviceApiKey: string | true,
|
|
|
|
) {
|
2020-06-23 10:29:03 +00:00
|
|
|
return Bluebird.try(() => {
|
2018-07-10 17:45:48 +00:00
|
|
|
if (customDeviceApiKey === true) {
|
2020-02-27 14:55:30 +00:00
|
|
|
return getBalenaSdk().models.device.generateDeviceKey(uuid);
|
2018-07-10 17:45:48 +00:00
|
|
|
} else {
|
|
|
|
return customDeviceApiKey;
|
|
|
|
}
|
2020-06-15 22:53:07 +00:00
|
|
|
}).tap((deviceApiKey) => {
|
2017-12-20 21:46:01 +00:00
|
|
|
config.deviceApiKey = deviceApiKey;
|
|
|
|
});
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|