balena-cli/lib/utils/config.ts
Akis Kesoglou 8291c96e69 Make specifying the version during configuration optional
`version` used to be optional but it seems we recently had to make it a required parameter. However it really feels redundant when all it’s used for is to determine whether the command should issue a legacy user API key or a provisioning key.

This makes version optional but tries to figure it out by itself by reading os-release from the image's boot partition. This is not foul-proof however, and while it'll work with most recent images it won't work with all and in that case it'll bail out and only then warn the user to specify it via the --version argument.

Change-type: minor
2018-11-16 19:39:43 +02:00

144 lines
3.7 KiB
TypeScript

/*
Copyright 2016-2017 Balena
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');
import BalenaSdk = require('balena-sdk');
import * as semver from 'resin-semver';
const balena = BalenaSdk.fromSharedOptions();
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;
};
export function generateBaseConfig(
application: BalenaSdk.Application,
options: { version: string; appUpdatePollInterval?: number },
): Promise<ImgConfig> {
options = {
...options,
appUpdatePollInterval: options.appUpdatePollInterval || 10,
};
const promise = balena.models.os.getConfig(
application.app_name,
options,
) as Promise<ImgConfig & { apiKey?: string }>;
return promise.tap(config => {
// os.getConfig always returns a config for an app
delete config.apiKey;
});
}
export function generateApplicationConfig(
application: BalenaSdk.Application,
options: { version: string },
) {
return generateBaseConfig(application, options).tap(config => {
if (semver.satisfies(options.version, '<2.7.8')) {
return addApplicationKey(config, application.id);
}
return addProvisioningKey(config, application.id);
});
}
export function generateDeviceConfig(
device: BalenaSdk.Device & {
belongs_to__application: BalenaSdk.PineDeferred;
},
deviceApiKey: string | true | null,
options: { version: string },
) {
return balena.models.application
.get(device.belongs_to__application.__id)
.then(application => {
return generateBaseConfig(application, options).tap(config => {
if (
deviceApiKey == null &&
semver.satisfies(options.version, '<2.0.3')
) {
return addApplicationKey(config, application.id);
}
return addDeviceKey(config, device.uuid, deviceApiKey || true);
});
})
.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;
return config;
});
}
function addApplicationKey(config: any, applicationNameOrId: string | number) {
return balena.models.application
.generateApiKey(applicationNameOrId)
.tap(apiKey => {
config.apiKey = apiKey;
});
}
function addProvisioningKey(config: any, applicationNameOrId: string | number) {
return balena.models.application
.generateProvisioningKey(applicationNameOrId)
.tap(apiKey => {
config.apiKey = apiKey;
});
}
function addDeviceKey(
config: any,
uuid: string,
customDeviceApiKey: string | true,
) {
return Promise.try(() => {
if (customDeviceApiKey === true) {
return balena.models.device.generateDeviceKey(uuid);
} else {
return customDeviceApiKey;
}
}).tap(deviceApiKey => {
config.deviceApiKey = deviceApiKey;
});
}