Update balena-register-device and send extra info at provision time

This extra info will mean the API is able to immediately set default
config vars based on the os/supervisor version so that they are
available on the first target state fetch rather than having a delay
whilst waiting for the supervisor to report them as part of a state
patch

Update balena-register-device from 6.1.6 to 7.2.0

Change-type: patch
This commit is contained in:
Pagan Gazzard 2021-04-29 13:14:18 +00:00
parent 8ceabbdb4d
commit 9e52bb33ac
10 changed files with 73 additions and 44 deletions

38
package-lock.json generated
View File

@ -798,6 +798,12 @@
"source-map": "^0.6.1" "source-map": "^0.6.1"
} }
}, },
"@types/uuid": {
"version": "8.3.0",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.0.tgz",
"integrity": "sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ==",
"dev": true
},
"@types/webpack": { "@types/webpack": {
"version": "4.41.21", "version": "4.41.21",
"resolved": "https://registry.npmjs.org/@types/webpack/-/webpack-4.41.21.tgz", "resolved": "https://registry.npmjs.org/@types/webpack/-/webpack-4.41.21.tgz",
@ -1425,24 +1431,28 @@
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
}, },
"balena-register-device": { "balena-register-device": {
"version": "6.1.6", "version": "7.2.0",
"resolved": "https://registry.npmjs.org/balena-register-device/-/balena-register-device-6.1.6.tgz", "resolved": "https://registry.npmjs.org/balena-register-device/-/balena-register-device-7.2.0.tgz",
"integrity": "sha512-kS8JZoLyucZ9oUFicspN/k3jRrvdtQ4UYXVCHHyw91C3y2Z1T5CQpwfMBSqA8dp5wQKyP527K/0+lMUa2ncLhA==", "integrity": "sha512-Uo8iceob2Zg8gBedZKzSZWTyr5XoDkUN+2oSyyuKVuhZYcZS623Lsj/+I8QdJQutlObgVHjD2k3mM1Pa6loFxA==",
"dev": true, "dev": true,
"requires": { "requires": {
"bluebird": "^3.7.2", "@types/uuid": "^8.3.0",
"randomstring": "^1.1.5", "tslib": "^2.2.0",
"typed-error": "^2.0.0" "typed-error": "^3.2.1",
"uuid": "^8.3.2"
}, },
"dependencies": { "dependencies": {
"typed-error": { "tslib": {
"version": "2.0.0", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/typed-error/-/typed-error-2.0.0.tgz", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.2.0.tgz",
"integrity": "sha1-05j9hin8K3nIOfm30b/Ay7ZSYBI=", "integrity": "sha512-gS9GVHRU+RGn5KQM2rllAlR3dU6m7AcpJKdtH8gFvQiC4Otgk98XnmMU+nZenHt/+VhnBPWwgrJsyrdcw6i23w==",
"dev": true, "dev": true
"requires": { },
"tslib": "^1.7.1" "uuid": {
} "version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"dev": true
} }
} }
}, },

View File

@ -72,7 +72,7 @@
"@types/tmp": "^0.1.0", "@types/tmp": "^0.1.0",
"@types/webpack": "^4.41.21", "@types/webpack": "^4.41.21",
"@types/yargs": "^15.0.12", "@types/yargs": "^15.0.12",
"balena-register-device": "^6.1.6", "balena-register-device": "^7.2.0",
"blinking": "^0.0.4", "blinking": "^0.0.4",
"bluebird": "^3.7.2", "bluebird": "^3.7.2",
"chai-as-promised": "^7.1.1", "chai-as-promised": "^7.1.1",

View File

@ -6,6 +6,8 @@ backports:
sunset: 10.3.x sunset: 10.3.x
upstream: upstream:
- repo: 'balena-register-device'
url: 'https://github.com/balena-io-modules/balena-register-device'
- repo: 'docker-delta' - repo: 'docker-delta'
url: 'https://github.com/balena-io-modules/node-docker-delta' url: 'https://github.com/balena-io-modules/node-docker-delta'
- repo: 'docker-progress' - repo: 'docker-progress'

View File

@ -90,6 +90,10 @@ export const fnSchema = {
'apiTimeout', 'apiTimeout',
'registered_at', 'registered_at',
'deviceId', 'deviceId',
'version',
'osVersion',
'osVariant',
'macAddress',
]) ])
.then((conf) => { .then((conf) => {
return { return {
@ -103,6 +107,10 @@ export const fnSchema = {
apiTimeout: conf.apiTimeout, apiTimeout: conf.apiTimeout,
registered_at: conf.registered_at, registered_at: conf.registered_at,
deviceId: conf.deviceId, deviceId: conf.deviceId,
supervisorVersion: conf.version,
osVersion: conf.osVersion,
osVariant: conf.osVariant,
macAddress: conf.macAddress,
}; };
}); });
}, },

View File

@ -211,6 +211,10 @@ export const schemaTypes = {
apiTimeout: PermissiveNumber, apiTimeout: PermissiveNumber,
registered_at: t.union([PermissiveNumber, NullOrUndefined]), registered_at: t.union([PermissiveNumber, NullOrUndefined]),
deviceId: t.union([PermissiveNumber, NullOrUndefined]), deviceId: t.union([PermissiveNumber, NullOrUndefined]),
supervisorVersion: t.union([t.string, t.undefined]),
osVersion: t.union([t.string, t.undefined]),
osVariant: t.union([t.string, t.undefined]),
macAddress: t.union([t.string, t.undefined]),
}), }),
default: t.never, default: t.never,
}, },

View File

@ -170,9 +170,31 @@ export const provision = async (
device = await exchangeKeyAndGetDeviceOrRegenerate(balenaApi, opts); device = await exchangeKeyAndGetDeviceOrRegenerate(balenaApi, opts);
} else if (opts.registered_at == null) { } else if (opts.registered_at == null) {
if (opts.provisioningApiKey == null) {
throw new Error('Cannot provision without a provisioning api key');
}
if (opts.applicationId == null) {
throw new Error('Cannot provision without an application id');
}
if (opts.uuid == null) {
throw new Error('Cannot provision without a uuid');
}
log.info('New device detected. Provisioning...'); log.info('New device detected. Provisioning...');
try { try {
device = await deviceRegister.register(opts).timeout(opts.apiTimeout); device = await Bluebird.resolve(
deviceRegister.register({
applicationId: opts.applicationId,
uuid: opts.uuid,
deviceType: opts.deviceType,
deviceApiKey: opts.deviceApiKey,
provisioningApiKey: opts.provisioningApiKey,
apiEndpoint: opts.apiEndpoint,
supervisorVersion: opts.supervisorVersion,
osVersion: opts.osVersion,
osVariant: opts.osVariant,
macAddress: opts.macAddress,
}),
).timeout(opts.apiTimeout);
} catch (err) { } catch (err) {
if ( if (
err instanceof deviceRegister.ApiError && err instanceof deviceRegister.ApiError &&

View File

@ -1,5 +1,4 @@
import { endsWith, map } from 'lodash'; import { endsWith, map } from 'lodash';
import { Response } from 'request';
import { TypedError } from 'typed-error'; import { TypedError } from 'typed-error';
import { checkInt } from './validation'; import { checkInt } from './validation';
@ -48,7 +47,7 @@ export class InvalidAppIdError extends TypedError {
export class UpdatesLockedError extends TypedError {} export class UpdatesLockedError extends TypedError {}
export function isHttpConflictError(err: StatusCodeError | Response): boolean { export function isHttpConflictError(err: { statusCode: number }): boolean {
return checkInt(err.statusCode) === 409; return checkInt(err.statusCode) === 409;
} }

View File

@ -1,15 +1,13 @@
import factory = require('balena-register-device'); import { getRegisterDevice } from 'balena-register-device';
import * as Bluebird from 'bluebird'; export { ApiError } from 'balena-register-device';
import { getRequestInstance } from './request'; import { getRequestInstance } from './request';
export const { ApiError } = factory; export const { generateUniqueKey, register } = getRegisterDevice({
export const { generateUniqueKey, register } = factory({
request: { request: {
send: Bluebird.method(async (options: {}) => { send: async (options: {}) => {
const request = await getRequestInstance(); const request = await getRequestInstance();
const [response] = await request.postAsync({ ...options, json: true }); const [response] = await request.postAsync({ ...options, json: true });
return response; return response;
}), },
}, },
}); });

View File

@ -15,6 +15,7 @@ import * as TargetState from '../src/device-state/target-state';
import { DeviceStatus } from '../src/types/state'; import { DeviceStatus } from '../src/types/state';
import * as CurrentState from '../src/device-state/current-state'; import * as CurrentState from '../src/device-state/current-state';
import * as ApiHelper from '../src/lib/api-helper'; import * as ApiHelper from '../src/lib/api-helper';
import supervisorVersion = require('../src/lib/supervisor-version');
import { TypedError } from 'typed-error'; import { TypedError } from 'typed-error';
import { DeviceNotFoundError } from '../src/lib/errors'; import { DeviceNotFoundError } from '../src/lib/errors';
@ -166,6 +167,10 @@ describe('ApiBinder', () => {
uuid: conf.uuid, uuid: conf.uuid,
device_type: conf.deviceType, device_type: conf.deviceType,
api_key: conf.deviceApiKey, api_key: conf.deviceApiKey,
mac_address: '00:11:22:33:44:55 66:77:88:99:AA:BB',
os_variant: 'dev',
os_version: 'balenaOS 2.0.6+rev1',
supervisor_version: supervisorVersion,
}, },
}); });
}); });

View File

@ -1,19 +0,0 @@
// Typings (incomplete) for balena-register-device@v6.0.1
// TODO: Upstream types to the repo
declare module 'balena-register-device' {
import { Response } from 'request';
import { TypedError } from 'typed-error';
function factory({
request,
}): {
generateUniqueKey: () => string;
register: (opts: Dictionary<any>) => Bluebird<{ id: string }>;
};
factory.ApiError = class ApiError extends TypedError {
public response: Response;
};
export = factory;
}