mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-30 10:38:50 +00:00
f7075d7db9
Change-Type: patch
108 lines
3.5 KiB
JavaScript
108 lines
3.5 KiB
JavaScript
// Generated by CoffeeScript 1.12.7
|
|
|
|
/*
|
|
Copyright 2016-2017 Resin.io
|
|
|
|
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.
|
|
*/
|
|
var authenticateWithApplicationKey, authenticateWithDeviceKey;
|
|
|
|
exports.generateBaseConfig = function(application, options) {
|
|
var Promise, _, deviceConfig, resin;
|
|
Promise = require('bluebird');
|
|
_ = require('lodash');
|
|
deviceConfig = require('resin-device-config');
|
|
resin = require('resin-sdk-preconfigured');
|
|
options = _.mapValues(options, function(value, key) {
|
|
if (key === 'appUpdatePollInterval') {
|
|
return value * 60 * 1000;
|
|
} else {
|
|
return value;
|
|
}
|
|
});
|
|
return Promise.props({
|
|
userId: resin.auth.getUserId(),
|
|
username: resin.auth.whoami(),
|
|
apiUrl: resin.settings.get('apiUrl'),
|
|
vpnUrl: resin.settings.get('vpnUrl'),
|
|
registryUrl: resin.settings.get('registryUrl'),
|
|
deltaUrl: resin.settings.get('deltaUrl'),
|
|
pubNubKeys: resin.models.config.getPubNubKeys(),
|
|
mixpanelToken: resin.models.config.getMixpanelToken()
|
|
}).then(function(results) {
|
|
return deviceConfig.generate({
|
|
application: application,
|
|
user: {
|
|
id: results.userId,
|
|
username: results.username
|
|
},
|
|
endpoints: {
|
|
api: results.apiUrl,
|
|
vpn: results.vpnUrl,
|
|
registry: results.registryUrl,
|
|
delta: results.deltaUrl
|
|
},
|
|
pubnub: results.pubNubKeys,
|
|
mixpanel: {
|
|
token: results.mixpanelToken
|
|
}
|
|
}, options);
|
|
});
|
|
};
|
|
|
|
exports.generateApplicationConfig = function(application, options) {
|
|
return exports.generateBaseConfig(application, options).tap(function(config) {
|
|
return authenticateWithApplicationKey(config, application.id);
|
|
});
|
|
};
|
|
|
|
exports.generateDeviceConfig = function(device, deviceApiKey, options) {
|
|
var resin;
|
|
resin = require('resin-sdk-preconfigured');
|
|
return resin.models.application.get(device.application_name).then(function(application) {
|
|
return exports.generateBaseConfig(application, options).tap(function(config) {
|
|
if (deviceApiKey != null) {
|
|
return authenticateWithDeviceKey(config, device.uuid, deviceApiKey);
|
|
} else {
|
|
return authenticateWithApplicationKey(config, application.id);
|
|
}
|
|
});
|
|
}).then(function(config) {
|
|
config.registered_at = Math.floor(Date.now() / 1000);
|
|
config.deviceId = device.id;
|
|
config.uuid = device.uuid;
|
|
return config;
|
|
});
|
|
};
|
|
|
|
authenticateWithApplicationKey = function(config, applicationNameOrId) {
|
|
var resin;
|
|
resin = require('resin-sdk-preconfigured');
|
|
return resin.models.application.generateApiKey(applicationNameOrId).then(function(apiKey) {
|
|
config.apiKey = apiKey;
|
|
return config;
|
|
});
|
|
};
|
|
|
|
authenticateWithDeviceKey = function(config, uuid, customDeviceApiKey) {
|
|
var Promise, resin;
|
|
Promise = require('bluebird');
|
|
resin = require('resin-sdk-preconfigured');
|
|
return Promise["try"](function() {
|
|
return customDeviceApiKey || resin.models.device.generateDeviceKey(uuid);
|
|
}).then(function(deviceApiKey) {
|
|
config.deviceApiKey = deviceApiKey;
|
|
return config;
|
|
});
|
|
};
|