2017-12-20 21:46:01 +00:00
|
|
|
/*
|
2018-10-19 14:38:50 +00:00
|
|
|
Copyright 2016-2017 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.
|
|
|
|
*/
|
|
|
|
import Promise = require('bluebird');
|
2018-10-19 14:38:50 +00:00
|
|
|
import BalenaSdk = require('balena-sdk');
|
2018-07-11 18:56:13 +00:00
|
|
|
import * as semver from 'resin-semver';
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
const balena = BalenaSdk.fromSharedOptions();
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-10-15 17:03:21 +00:00
|
|
|
type ImgConfig = {
|
|
|
|
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;
|
|
|
|
|
|
|
|
// props for older OS versions
|
|
|
|
connectivity?: string;
|
|
|
|
files?: {
|
|
|
|
[filepath: string]: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
// device specific config props
|
|
|
|
deviceId?: number;
|
|
|
|
uuid?: string;
|
|
|
|
registered_at?: number;
|
|
|
|
};
|
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-10-15 17:03:21 +00:00
|
|
|
options: { version: string; appUpdatePollInterval?: number },
|
|
|
|
): Promise<ImgConfig> {
|
|
|
|
options = {
|
|
|
|
...options,
|
|
|
|
appUpdatePollInterval: options.appUpdatePollInterval || 10,
|
|
|
|
};
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
const promise = balena.models.os.getConfig(
|
2018-10-29 19:32:21 +00:00
|
|
|
application.app_name,
|
|
|
|
options,
|
|
|
|
) as Promise<ImgConfig & { apiKey?: string }>;
|
2018-10-15 17:03:21 +00:00
|
|
|
return promise.tap(config => {
|
|
|
|
// os.getConfig always returns a config for an app
|
|
|
|
delete config.apiKey;
|
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-10-15 17:03:21 +00:00
|
|
|
options: { version: string },
|
2018-01-09 15:05:24 +00:00
|
|
|
) {
|
2018-07-11 18:56:13 +00:00
|
|
|
return generateBaseConfig(application, options).tap(config => {
|
|
|
|
if (semver.satisfies(options.version, '>=2.7.8')) {
|
|
|
|
return addProvisioningKey(config, application.id);
|
|
|
|
} else {
|
|
|
|
return addApplicationKey(config, application.id);
|
|
|
|
}
|
|
|
|
});
|
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;
|
|
|
|
},
|
2018-07-10 17:45:48 +00:00
|
|
|
deviceApiKey: string | true | null,
|
2018-10-15 17:03:21 +00:00
|
|
|
options: { version: string },
|
2017-12-20 21:46:01 +00:00
|
|
|
) {
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.application
|
2018-07-11 16:57:26 +00:00
|
|
|
.get(device.belongs_to__application.__id)
|
2018-01-09 15:05:24 +00:00
|
|
|
.then(application => {
|
|
|
|
return generateBaseConfig(application, options).tap(config => {
|
|
|
|
if (deviceApiKey) {
|
|
|
|
return addDeviceKey(config, device.uuid, deviceApiKey);
|
2018-07-11 18:56:13 +00:00
|
|
|
} else if (semver.satisfies(options.version, '>=2.0.3')) {
|
|
|
|
return addDeviceKey(config, device.uuid, true);
|
2018-01-09 15:05:24 +00:00
|
|
|
} else {
|
|
|
|
return addApplicationKey(config, application.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(config => {
|
|
|
|
// 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) {
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.application
|
2018-01-09 15:05:24 +00:00
|
|
|
.generateApiKey(applicationNameOrId)
|
|
|
|
.tap(apiKey => {
|
|
|
|
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) {
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.application
|
2018-07-11 18:56:13 +00:00
|
|
|
.generateProvisioningKey(applicationNameOrId)
|
|
|
|
.tap(apiKey => {
|
|
|
|
config.apiKey = apiKey;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-10 17:45:48 +00:00
|
|
|
function addDeviceKey(
|
|
|
|
config: any,
|
|
|
|
uuid: string,
|
|
|
|
customDeviceApiKey: string | true,
|
|
|
|
) {
|
2017-12-20 21:46:01 +00:00
|
|
|
return Promise.try(() => {
|
2018-07-10 17:45:48 +00:00
|
|
|
if (customDeviceApiKey === true) {
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.device.generateDeviceKey(uuid);
|
2018-07-10 17:45:48 +00:00
|
|
|
} else {
|
|
|
|
return customDeviceApiKey;
|
|
|
|
}
|
2018-01-09 15:05:24 +00:00
|
|
|
}).tap(deviceApiKey => {
|
2017-12-20 21:46:01 +00:00
|
|
|
config.deviceApiKey = deviceApiKey;
|
|
|
|
});
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|