Skip to content

Commit

Permalink
Merge pull request #1208 from balena-io/909-preload-device-name
Browse files Browse the repository at this point in the history
Respect an initialDeviceName field in the config.json
  • Loading branch information
CameronDiver authored Jun 11, 2020
2 parents dec79e1 + b5918f0 commit fad7020
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
56 changes: 51 additions & 5 deletions src/api-binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export class APIBinder {
'apiEndpoint',
'unmanaged',
'bootstrapRetryDelay',
'initialDeviceName',
]);
let { apiEndpoint } = conf;
const { unmanaged, bootstrapRetryDelay } = conf;
Expand Down Expand Up @@ -197,7 +198,19 @@ export class APIBinder {
// Either we haven't reported our initial config or we've been re-provisioned
if (apiEndpoint !== initialConfigReported) {
log.info('Reporting initial configuration');
await this.reportInitialConfig(apiEndpoint, bootstrapRetryDelay);
// We fetch the deviceId here to ensure it's been set
const deviceId = await config.get('deviceId');
if (deviceId == null) {
throw new InternalInconsistencyError(
`Attempt to report initial configuration without a device ID`,
);
}
await this.reportInitialConfig(
apiEndpoint,
deviceId,
bootstrapRetryDelay,
conf.initialDeviceName ?? undefined,
);
}

log.debug('Starting current state report');
Expand Down Expand Up @@ -605,7 +618,11 @@ export class APIBinder {

// Creates the necessary config vars in the API to match the current device state,
// without overwriting any variables that are already set.
private async reportInitialEnv(apiEndpoint: string) {
private async reportInitialEnv(
apiEndpoint: string,
deviceId: number,
initialName?: string,
) {
if (this.balenaApi == null) {
throw new InternalInconsistencyError(
'Attempt to report initial environment without an API client',
Expand All @@ -628,7 +645,6 @@ export class APIBinder {
const targetConfig = await this.deviceState.deviceConfig.formatConfigKeys(
targetConfigUnformatted,
);
const deviceId = await config.get('deviceId');

if (!currentState.local.config) {
throw new InternalInconsistencyError(
Expand Down Expand Up @@ -660,19 +676,30 @@ export class APIBinder {
}
}

if (initialName != null) {
await this.reportInitialName(deviceId, initialName);
}

await config.set({ initialConfigReported: apiEndpoint });
}

private async reportInitialConfig(
apiEndpoint: string,
deviceId: number,
retryDelay: number,
initialName?: string,
): Promise<void> {
try {
await this.reportInitialEnv(apiEndpoint);
await this.reportInitialEnv(apiEndpoint, deviceId, initialName);
} catch (err) {
log.error('Error reporting initial configuration, will retry', err);
await Bluebird.delay(retryDelay);
await this.reportInitialConfig(apiEndpoint, retryDelay);
await this.reportInitialConfig(
apiEndpoint,
deviceId,
retryDelay,
initialName,
);
}
}

Expand Down Expand Up @@ -897,6 +924,25 @@ export class APIBinder {

return router;
}

private async reportInitialName(
deviceId: number,
name: string,
): Promise<void> {
if (this.balenaApi == null) {
throw new InternalInconsistencyError(
`Attempt to set an initial device name without an API client`,
);
}

await this.balenaApi.patch({
resource: 'device',
id: deviceId,
body: {
device_name: name,
},
});
}
}

export default APIBinder;
4 changes: 4 additions & 0 deletions src/config/schema-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export const schemaTypes = {
type: PermissiveBoolean,
default: false,
},
initialDeviceName: {
type: t.string,
default: NullOrUndefined,
},

// Database types
apiSecret: {
Expand Down
5 changes: 5 additions & 0 deletions src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export const schema = {
mutable: true,
removeIfNull: false,
},
initialDeviceName: {
source: 'config.json',
mutable: false,
removeIfNull: false,
},

apiSecret: {
source: 'db',
Expand Down

0 comments on commit fad7020

Please sign in to comment.