From c61b16655e7b25999aac444ce39414635cd3455c Mon Sep 17 00:00:00 2001 From: Cameron Diver Date: Thu, 12 Jul 2018 13:27:47 +0100 Subject: [PATCH] Remove resinApiEndpoint meta-endpoint and use config.json entry instead The resinApiEndpoint config option existed for legacy reasons, where the apiEndpoint was passed in via env vars, but this is no longer the case, and the current supervisor wouldn't run on these older versions of resinOS anymore anyway, so I've removed the references to this legacy endpoint, as it made reasoning about offline mode weird. Change-type: minor Signed-off-by: Cameron Diver --- src/api-binder.coffee | 34 +++++++++++++++++----------------- src/application-manager.coffee | 4 ++-- src/config.ts | 1 - src/config/functions.ts | 26 ++++++++------------------ src/device-state.coffee | 8 ++++---- src/lib/constants.ts | 1 - src/lib/docker-utils.coffee | 4 ++-- 7 files changed, 33 insertions(+), 45 deletions(-) diff --git a/src/api-binder.coffee b/src/api-binder.coffee index 4098dd53..89932ee9 100644 --- a/src/api-binder.coffee +++ b/src/api-binder.coffee @@ -63,47 +63,47 @@ module.exports = class APIBinder release() initClient: => - @config.getMany([ 'offlineMode', 'resinApiEndpoint', 'currentApiKey' ]) - .then ({ offlineMode, resinApiEndpoint, currentApiKey }) => + @config.getMany([ 'offlineMode', 'apiEndpoint', 'currentApiKey' ]) + .then ({ offlineMode, apiEndpoint, currentApiKey }) => if offlineMode console.log('Offline Mode is set, skipping API client initialization') return - baseUrl = url.resolve(resinApiEndpoint, '/v4/') + baseUrl = url.resolve(apiEndpoint, '/v4/') passthrough = _.cloneDeep(requestOpts) passthrough.headers ?= {} passthrough.headers.Authorization = "Bearer #{currentApiKey}" @resinApi = new PinejsClient apiPrefix: baseUrl passthrough: passthrough - baseUrlLegacy = url.resolve(resinApiEndpoint, '/v2/') + baseUrlLegacy = url.resolve(apiEndpoint, '/v2/') @resinApiLegacy = new PinejsClient apiPrefix: baseUrlLegacy passthrough: passthrough @cachedResinApi = @resinApi.clone({}, cache: {}) start: => - @config.getMany([ 'resinApiEndpoint', 'offlineMode', 'bootstrapRetryDelay' ]) - .then ({ resinApiEndpoint, offlineMode, bootstrapRetryDelay }) => + @config.getMany([ 'apiEndpoint', 'offlineMode', 'bootstrapRetryDelay' ]) + .then ({ apiEndpoint, offlineMode, bootstrapRetryDelay }) => if offlineMode console.log('Offline Mode is set, skipping API binder initialization') # If we are offline because there is no apiEndpoint, there's a chance # we've went through a deprovision. We need to set the initialConfigReported # value to '', to ensure that when we do re-provision, we'll report # the config and hardward-specific options won't be lost - if !Boolean(resinApiEndpoint) + if !Boolean(apiEndpoint) return @config.set({ initialConfigReported: '' }) return console.log('Ensuring device is provisioned') @provisionDevice() .then => - @config.getMany([ 'initialConfigReported', 'resinApiEndpoint' ]) - .then ({ initialConfigReported, resinApiEndpoint }) => + @config.getMany([ 'initialConfigReported', 'apiEndpoint' ]) + .then ({ initialConfigReported, apiEndpoint }) => # Either we haven't reported our initial config or we've # been re-provisioned - if resinApiEndpoint != initialConfigReported + if apiEndpoint != initialConfigReported console.log('Reporting initial configuration') - @reportInitialConfig(resinApiEndpoint, bootstrapRetryDelay) + @reportInitialConfig(apiEndpoint, bootstrapRetryDelay) .then => console.log('Starting current state report') @startCurrentStateReport() @@ -308,7 +308,7 @@ module.exports = class APIBinder _sendLogsRequest: (uuid, data) => reqBody = _.map(data, (msg) -> _.mapKeys(msg, (v, k) -> _.snakeCase(k))) - @config.get('resinApiEndpoint') + @config.get('apiEndpoint') .then (resinApiEndpoint) => endpoint = url.resolve(resinApiEndpoint, "/device/v2/#{uuid}/logs") requestParams = _.extend @@ -364,9 +364,9 @@ module.exports = class APIBinder @reportInitialConfig(apiEndpoint, retryDelay) getTargetState: => - @config.getMany([ 'uuid', 'resinApiEndpoint', 'apiTimeout' ]) - .then ({ uuid, resinApiEndpoint, apiTimeout }) => - endpoint = url.resolve(resinApiEndpoint, "/device/v2/#{uuid}/state") + @config.getMany([ 'uuid', 'apiEndpoint', 'apiTimeout' ]) + .then ({ uuid, apiEndpoint, apiTimeout }) => + endpoint = url.resolve(apiEndpoint, "/device/v2/#{uuid}/state") requestParams = _.extend method: 'GET' @@ -422,7 +422,7 @@ module.exports = class APIBinder return _.pickBy(diff, _.negate(_.isEmpty)) _sendReportPatch: (stateDiff, conf) => - endpoint = url.resolve(conf.resinApiEndpoint, "/device/v2/#{conf.uuid}/state") + endpoint = url.resolve(conf.apiEndpoint, "/device/v2/#{conf.uuid}/state") requestParams = _.extend method: 'PATCH' url: "#{endpoint}" @@ -432,7 +432,7 @@ module.exports = class APIBinder @cachedResinApi._request(requestParams) _report: => - @config.getMany([ 'deviceId', 'apiTimeout', 'resinApiEndpoint', 'uuid' ]) + @config.getMany([ 'deviceId', 'apiTimeout', 'apiEndpoint', 'uuid' ]) .then (conf) => stateDiff = @_getStateDiff() if _.size(stateDiff) is 0 diff --git a/src/application-manager.coffee b/src/application-manager.coffee index a57bed30..53771069 100644 --- a/src/application-manager.coffee +++ b/src/application-manager.coffee @@ -272,7 +272,7 @@ module.exports = class ApplicationManager extends EventEmitter ).get(appId) getTargetApp: (appId) => - @config.get('resinApiEndpoint').then (endpoint = '') -> + @config.get('apiEndpoint').then (endpoint = '') -> @db.models('app').where({ appId, source: endpoint }).select() .then ([ app ]) => if !app? @@ -714,7 +714,7 @@ module.exports = class ApplicationManager extends EventEmitter @_targetVolatilePerImageId[imageId] = {} getTargetApps: => - @config.get('resinApiEndpoint'). then (source = '') => + @config.get('apiEndpoint'). then (source = '') => Promise.map(@db.models('app').where({ source }), @normaliseAndExtendAppFromDB) .map (app) => if !_.isEmpty(app.services) diff --git a/src/config.ts b/src/config.ts index c13d05d6..539def43 100644 --- a/src/config.ts +++ b/src/config.ts @@ -49,7 +49,6 @@ class Config extends EventEmitter { currentApiKey: { source: 'func' }, offlineMode: { source: 'func' }, pubnub: { source: 'func' }, - resinApiEndpoint: { source: 'func' }, provisioned: { source: 'func' }, osVersion: { source: 'func' }, osVariant: { source: 'func' }, diff --git a/src/config/functions.ts b/src/config/functions.ts index ba6ae7fe..76d85423 100644 --- a/src/config/functions.ts +++ b/src/config/functions.ts @@ -75,9 +75,9 @@ export function createProviderFunctions(config: Config, db: DB): ConfigProviderF }, offlineMode: { get: () => { - return config.getMany([ 'resinApiEndpoint', 'supervisorOfflineMode' ]) - .then(({ resinApiEndpoint, supervisorOfflineMode }) => { - return Boolean(supervisorOfflineMode) || !Boolean(resinApiEndpoint); + return config.getMany([ 'apiEndpoint', 'supervisorOfflineMode' ]) + .then(({ apiEndpoint, supervisorOfflineMode }) => { + return Boolean(supervisorOfflineMode) || !Boolean(apiEndpoint); }); }, }, @@ -93,21 +93,11 @@ export function createProviderFunctions(config: Config, db: DB): ConfigProviderF }); }, }, - resinApiEndpoint: { - get: () => { - // Fallback to checking if an API endpoint was passed via env vars if there's none - // in config.json (legacy) - return config.get('apiEndpoint') - .then((apiEndpoint) => { - return apiEndpoint || (constants.apiEndpointFromEnv || ''); - }); - }, - }, provisioned: { get: () => { return config.getMany([ 'uuid', - 'resinApiEndpoint', + 'apiEndpoint', 'registered_at', 'deviceId', ]) @@ -135,7 +125,7 @@ export function createProviderFunctions(config: Config, db: DB): ConfigProviderF 'apiKey', 'deviceApiKey', 'deviceType', - 'resinApiEndpoint', + 'apiEndpoint', 'apiTimeout', 'registered_at', 'deviceId', @@ -147,7 +137,7 @@ export function createProviderFunctions(config: Config, db: DB): ConfigProviderF deviceType: conf.deviceType, provisioningApiKey: conf.apiKey, deviceApiKey: conf.deviceApiKey, - apiEndpoint: conf.resinApiEndpoint, + apiEndpoint: conf.apiEndpoint, apiTimeout: conf.apiTimeout, registered_at: conf.registered_at, deviceId: conf.deviceId, @@ -157,7 +147,7 @@ export function createProviderFunctions(config: Config, db: DB): ConfigProviderF }, mixpanelHost: { get: () => { - return config.get('resinApiEndpoint') + return config.get('apiEndpoint') .then((apiEndpoint) => { return `${apiEndpoint}/mixpanel`; }); @@ -182,7 +172,7 @@ export function createProviderFunctions(config: Config, db: DB): ConfigProviderF return config.getMany([ 'uuid', 'currentApiKey', - 'resinApiEndpoint', + 'apiEndpoint', 'deltaEndpoint', 'delta', 'deltaRequestTimeout', diff --git a/src/device-state.coffee b/src/device-state.coffee index 858244ec..02867464 100644 --- a/src/device-state.coffee +++ b/src/device-state.coffee @@ -166,7 +166,7 @@ module.exports = class DeviceState extends EventEmitter @config.getMany([ 'initialConfigSaved', 'listenPort', 'apiSecret', 'osVersion', 'osVariant', 'logsChannelSecret', - 'version', 'provisioned', 'resinApiEndpoint', 'connectivityCheckEnabled', 'legacyAppsPresent' + 'version', 'provisioned', 'apiEndpoint', 'connectivityCheckEnabled', 'legacyAppsPresent' ]) .then (conf) => Promise.try => @@ -200,8 +200,8 @@ module.exports = class DeviceState extends EventEmitter .then => @triggerApplyTarget({ initial: true }) - initNetworkChecks: ({ resinApiEndpoint, connectivityCheckEnabled }) => - network.startConnectivityCheck resinApiEndpoint, connectivityCheckEnabled, (connected) => + initNetworkChecks: ({ apiEndpoint, connectivityCheckEnabled }) => + network.startConnectivityCheck apiEndpoint, connectivityCheckEnabled, (connected) => @connected = connected @config.on 'change', (changedConfig) -> if changedConfig.connectivityCheckEnabled? @@ -242,7 +242,7 @@ module.exports = class DeviceState extends EventEmitter setTarget: (target) -> Promise.join( - @config.get('resinApiEndpoint'), + @config.get('apiEndpoint'), validateState(target), (source) => @usingWriteLockTarget => diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 6e757b8e..975652f9 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -25,7 +25,6 @@ const constants = { configJsonPathOnHost: checkString(process.env.CONFIG_JSON_PATH), proxyvisorHookReceiver: checkString(process.env.RESIN_PROXYVISOR_HOOK_RECEIVER) || 'http://0.0.0.0:1337', - apiEndpointFromEnv: checkString(process.env.API_ENDPOINT), configJsonNonAtomicPath: '/boot/config.json', defaultPubnubSubscribeKey: process.env.DEFAULT_PUBNUB_SUBSCRIBE_KEY, defaultPubnubPublishKey: process.env.DEFAULT_PUBNUB_PUBLISH_KEY, diff --git a/src/lib/docker-utils.coffee b/src/lib/docker-utils.coffee index 3c4ef2ed..f0c8efd5 100644 --- a/src/lib/docker-utils.coffee +++ b/src/lib/docker-utils.coffee @@ -58,7 +58,7 @@ module.exports = class DockerUtils extends DockerToolbelt fetchDeltaWithProgress: (imgDest, fullDeltaOpts, onProgress) => { deltaRequestTimeout, deltaApplyTimeout, deltaRetryCount, deltaRetryInterval, - uuid, currentApiKey, deltaEndpoint, resinApiEndpoint, + uuid, currentApiKey, deltaEndpoint, apiEndpoint, deltaSource, deltaSourceId, deltaVersion, startFromEmpty = false } = fullDeltaOpts retryCount = checkInt(deltaRetryCount) @@ -80,7 +80,7 @@ module.exports = class DockerUtils extends DockerToolbelt log("Starting delta to #{imgDest}") Promise.join @getRegistryAndName(imgDest), @getRegistryAndName(deltaSource), (dstInfo, srcInfo) -> - tokenEndpoint = "#{resinApiEndpoint}/auth/v1/token" + tokenEndpoint = "#{apiEndpoint}/auth/v1/token" opts = auth: user: 'd_' + uuid