Merge pull request #1683 from balena-os/send-info-on-provision

Update balena-register-device and send extra info at provision time
This commit is contained in:
bulldozer-balena[bot] 2021-05-06 12:45:07 +00:00 committed by GitHub
commit 2ec3e9241b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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"
}
},
"@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": {
"version": "4.41.21",
"resolved": "https://registry.npmjs.org/@types/webpack/-/webpack-4.41.21.tgz",
@ -1425,24 +1431,28 @@
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
},
"balena-register-device": {
"version": "6.1.6",
"resolved": "https://registry.npmjs.org/balena-register-device/-/balena-register-device-6.1.6.tgz",
"integrity": "sha512-kS8JZoLyucZ9oUFicspN/k3jRrvdtQ4UYXVCHHyw91C3y2Z1T5CQpwfMBSqA8dp5wQKyP527K/0+lMUa2ncLhA==",
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/balena-register-device/-/balena-register-device-7.2.0.tgz",
"integrity": "sha512-Uo8iceob2Zg8gBedZKzSZWTyr5XoDkUN+2oSyyuKVuhZYcZS623Lsj/+I8QdJQutlObgVHjD2k3mM1Pa6loFxA==",
"dev": true,
"requires": {
"bluebird": "^3.7.2",
"randomstring": "^1.1.5",
"typed-error": "^2.0.0"
"@types/uuid": "^8.3.0",
"tslib": "^2.2.0",
"typed-error": "^3.2.1",
"uuid": "^8.3.2"
},
"dependencies": {
"typed-error": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/typed-error/-/typed-error-2.0.0.tgz",
"integrity": "sha1-05j9hin8K3nIOfm30b/Ay7ZSYBI=",
"dev": true,
"requires": {
"tslib": "^1.7.1"
}
"tslib": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.2.0.tgz",
"integrity": "sha512-gS9GVHRU+RGn5KQM2rllAlR3dU6m7AcpJKdtH8gFvQiC4Otgk98XnmMU+nZenHt/+VhnBPWwgrJsyrdcw6i23w==",
"dev": true
},
"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/webpack": "^4.41.21",
"@types/yargs": "^15.0.12",
"balena-register-device": "^6.1.6",
"balena-register-device": "^7.2.0",
"blinking": "^0.0.4",
"bluebird": "^3.7.2",
"chai-as-promised": "^7.1.1",

View File

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

View File

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

View File

@ -170,9 +170,31 @@ export const provision = async (
device = await exchangeKeyAndGetDeviceOrRegenerate(balenaApi, opts);
} 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...');
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) {
if (
err instanceof deviceRegister.ApiError &&

View File

@ -1,5 +1,4 @@
import { endsWith, map } from 'lodash';
import { Response } from 'request';
import { TypedError } from 'typed-error';
import { checkInt } from './validation';
@ -48,7 +47,7 @@ export class InvalidAppIdError 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;
}

View File

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

View File

@ -15,6 +15,7 @@ import * as TargetState from '../src/device-state/target-state';
import { DeviceStatus } from '../src/types/state';
import * as CurrentState from '../src/device-state/current-state';
import * as ApiHelper from '../src/lib/api-helper';
import supervisorVersion = require('../src/lib/supervisor-version');
import { TypedError } from 'typed-error';
import { DeviceNotFoundError } from '../src/lib/errors';
@ -166,6 +167,10 @@ describe('ApiBinder', () => {
uuid: conf.uuid,
device_type: conf.deviceType,
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;
}