mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-06-10 11:31:41 +00:00
Merge pull request #1007 from balena-io/roman/report-status-in-local-mode
Report device state in local mode
This commit is contained in:
commit
56c4f1d23f
@ -38,7 +38,7 @@ const INTERNAL_STATE_KEYS = [
|
|||||||
'update_failed',
|
'update_failed',
|
||||||
];
|
];
|
||||||
|
|
||||||
interface APIBinderConstructOpts {
|
export interface APIBinderConstructOpts {
|
||||||
config: Config;
|
config: Config;
|
||||||
// FIXME: Remove this
|
// FIXME: Remove this
|
||||||
db: Database;
|
db: Database;
|
||||||
@ -450,7 +450,7 @@ export class APIBinder {
|
|||||||
|
|
||||||
private async sendReportPatch(
|
private async sendReportPatch(
|
||||||
stateDiff: DeviceApplicationState,
|
stateDiff: DeviceApplicationState,
|
||||||
conf: { apiEndpoint: string; uuid: string },
|
conf: { apiEndpoint: string; uuid: string; localMode: boolean },
|
||||||
) {
|
) {
|
||||||
if (this.cachedBalenaApi == null) {
|
if (this.cachedBalenaApi == null) {
|
||||||
throw new InternalInconsistencyError(
|
throw new InternalInconsistencyError(
|
||||||
@ -458,6 +458,16 @@ export class APIBinder {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let body = stateDiff;
|
||||||
|
if (conf.localMode) {
|
||||||
|
body = this.stripDeviceStateInLocalMode(stateDiff);
|
||||||
|
// In local mode, check if it still makes sense to send any updates after data strip.
|
||||||
|
if (_.isEmpty(body.local)) {
|
||||||
|
// Nothing to send.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const endpoint = url.resolve(
|
const endpoint = url.resolve(
|
||||||
conf.apiEndpoint,
|
conf.apiEndpoint,
|
||||||
`/device/v2/${conf.uuid}/state`,
|
`/device/v2/${conf.uuid}/state`,
|
||||||
@ -467,7 +477,7 @@ export class APIBinder {
|
|||||||
{
|
{
|
||||||
method: 'PATCH',
|
method: 'PATCH',
|
||||||
url: endpoint,
|
url: endpoint,
|
||||||
body: stateDiff,
|
body,
|
||||||
},
|
},
|
||||||
this.cachedBalenaApi.passthrough,
|
this.cachedBalenaApi.passthrough,
|
||||||
);
|
);
|
||||||
@ -475,6 +485,18 @@ export class APIBinder {
|
|||||||
await this.cachedBalenaApi._request(requestParams);
|
await this.cachedBalenaApi._request(requestParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns an object that contains only status fields relevant for the local mode.
|
||||||
|
// It basically removes information about applications state.
|
||||||
|
public stripDeviceStateInLocalMode(
|
||||||
|
state: DeviceApplicationState,
|
||||||
|
): DeviceApplicationState {
|
||||||
|
return {
|
||||||
|
local: _.cloneDeep(
|
||||||
|
_.omit(state.local, 'apps', 'is_on__commit', 'logs_channel'),
|
||||||
|
),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private async report() {
|
private async report() {
|
||||||
const conf = await this.config.getMany([
|
const conf = await this.config.getMany([
|
||||||
'deviceId',
|
'deviceId',
|
||||||
@ -484,17 +506,12 @@ export class APIBinder {
|
|||||||
'localMode',
|
'localMode',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (conf.localMode) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const stateDiff = this.getStateDiff();
|
const stateDiff = this.getStateDiff();
|
||||||
if (_.size(stateDiff) === 0) {
|
if (_.size(stateDiff) === 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiEndpoint = conf.apiEndpoint;
|
const { apiEndpoint, uuid, localMode } = conf;
|
||||||
const uuid = conf.uuid;
|
|
||||||
if (uuid == null || apiEndpoint == null) {
|
if (uuid == null || apiEndpoint == null) {
|
||||||
throw new InternalInconsistencyError(
|
throw new InternalInconsistencyError(
|
||||||
'No uuid or apiEndpoint provided to ApiBinder.report',
|
'No uuid or apiEndpoint provided to ApiBinder.report',
|
||||||
@ -503,7 +520,7 @@ export class APIBinder {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await Bluebird.resolve(
|
await Bluebird.resolve(
|
||||||
this.sendReportPatch(stateDiff, { apiEndpoint, uuid }),
|
this.sendReportPatch(stateDiff, { apiEndpoint, uuid, localMode }),
|
||||||
).timeout(conf.apiTimeout);
|
).timeout(conf.apiTimeout);
|
||||||
|
|
||||||
this.stateReportErrors = 0;
|
this.stateReportErrors = 0;
|
||||||
@ -526,9 +543,6 @@ export class APIBinder {
|
|||||||
|
|
||||||
private reportCurrentState(): null {
|
private reportCurrentState(): null {
|
||||||
(async () => {
|
(async () => {
|
||||||
if ((await this.config.get('localMode')) === true) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.reportPending = true;
|
this.reportPending = true;
|
||||||
try {
|
try {
|
||||||
const currentDeviceState = await this.deviceState.getStatus();
|
const currentDeviceState = await this.deviceState.getStatus();
|
||||||
|
44
test/api-binder.ts
Normal file
44
test/api-binder.ts
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import { expect } from 'chai';
|
||||||
|
import { APIBinder, APIBinderConstructOpts } from '../src/api-binder';
|
||||||
|
import { DeviceApplicationState } from '../src/types/state';
|
||||||
|
|
||||||
|
describe('APIBinder', () => {
|
||||||
|
let apiBinder: APIBinder;
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
apiBinder = new APIBinder({} as APIBinderConstructOpts);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('stripDeviceStateInLocalMode', () => {
|
||||||
|
const sampleState = {
|
||||||
|
local: {
|
||||||
|
ip_address: '192.168.1.42 192.168.1.99',
|
||||||
|
api_port: 48484,
|
||||||
|
api_secret:
|
||||||
|
'20ffbd6e15aba827dca6381912d6aeb6c3a7a7c7206d4dfadf0d2f0a9e1136',
|
||||||
|
os_version: 'balenaOS 2.32.0+rev4',
|
||||||
|
os_variant: 'dev',
|
||||||
|
supervisor_version: '9.16.3',
|
||||||
|
provisioning_progress: null,
|
||||||
|
provisioning_state: '',
|
||||||
|
status: 'Idle',
|
||||||
|
logs_channel: null,
|
||||||
|
apps: {},
|
||||||
|
is_on__commit: 'whatever',
|
||||||
|
},
|
||||||
|
dependent: { apps: {} },
|
||||||
|
} as DeviceApplicationState;
|
||||||
|
|
||||||
|
it('should strip applications data', () => {
|
||||||
|
const result = apiBinder.stripDeviceStateInLocalMode(
|
||||||
|
sampleState,
|
||||||
|
) as Dictionary<any>;
|
||||||
|
expect(result).to.not.have.property('dependent');
|
||||||
|
|
||||||
|
const local = result['local'];
|
||||||
|
expect(local).to.not.have.property('apps');
|
||||||
|
expect(local).to.not.have.property('is_on__commit');
|
||||||
|
expect(local).to.not.have.property('logs_channel');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user