From a7a0821a3e23142265904806df5d0a6ecd2ed4b5 Mon Sep 17 00:00:00 2001 From: Christina Wang Date: Tue, 14 Jun 2022 15:45:03 -0700 Subject: [PATCH] Read hostname from config.json with container /etc/hostname as backup We don't need to read the host's hostname through /mnt/root/etc/hostname, because the hostname is written to config.json on a change. When the hostname has never changed, it won't be found in config.json, so we can default to the Supervisor container's /etc/hostname as it will match the host's /etc/hostname, the network mode being `host`. Closes: #1968 Change-type: patch Signed-off-by: Christina Wang --- src/compose/app.ts | 16 ++++++---------- src/compose/service.ts | 2 +- src/compose/types/service.ts | 2 +- .../data/docker-states/entrypoint/imageInfo.json | 2 +- .../network-mode-service/imageInfo.json | 2 +- test/data/docker-states/simple/imageInfo.json | 2 +- 6 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/compose/app.ts b/src/compose/app.ts index 495afc7d..3cae07fe 100644 --- a/src/compose/app.ts +++ b/src/compose/app.ts @@ -1,6 +1,5 @@ import * as _ from 'lodash'; import { promises as fs } from 'fs'; -import * as path from 'path'; import Network from './network'; import Volume from './volume'; @@ -742,7 +741,7 @@ export class App { opts, supervisorApiHost, hostPathExists, - hostnameOnHost, + hostname, ] = await Promise.all([ config.get('extendedEnvOptions'), dockerUtils @@ -752,20 +751,17 @@ export class App { firmware: await pathExistsOnHost('/lib/firmware'), modules: await pathExistsOnHost('/lib/modules'), }))(), - (async () => - _.trim( - await fs.readFile( - path.join(constants.rootMountPoint, '/etc/hostname'), - 'utf8', - ), - ))(), + ( + (await config.get('hostname')) ?? + (await fs.readFile('/etc/hostname', 'utf-8')) + ).trim(), ]); const svcOpts = { appName: app.name, supervisorApiHost, hostPathExists, - hostnameOnHost, + hostname, ...opts, }; diff --git a/src/compose/service.ts b/src/compose/service.ts index 23a3873c..ce30ff44 100644 --- a/src/compose/service.ts +++ b/src/compose/service.ts @@ -300,7 +300,7 @@ export class Service { // Any other special case handling if (config.networkMode === 'host' && !config.hostname) { - config.hostname = options.hostnameOnHost; + config.hostname = options.hostname; } config.restart = ComposeUtils.createRestartPolicy(config.restart); config.command = ComposeUtils.getCommand(config.command, options.imageInfo); diff --git a/src/compose/types/service.ts b/src/compose/types/service.ts index 9cf05cad..28174975 100644 --- a/src/compose/types/service.ts +++ b/src/compose/types/service.ts @@ -327,7 +327,7 @@ export interface DeviceMetadata { apiSecret: string; supervisorApiHost: string; osVersion: string; - hostnameOnHost: string; + hostname: string; hostPathExists: { modules: boolean; firmware: boolean; diff --git a/test/data/docker-states/entrypoint/imageInfo.json b/test/data/docker-states/entrypoint/imageInfo.json index f34277f4..e84d60bd 100644 --- a/test/data/docker-states/entrypoint/imageInfo.json +++ b/test/data/docker-states/entrypoint/imageInfo.json @@ -90,7 +90,7 @@ "firmware": true, "modules": true }, - "hostnameOnHost": "7dadabd", + "hostname": "7dadabd", "uuid": "a7feb967fac7f559ccf2a006a36bcf5d", "listenPort": "48484", "name": "Office", diff --git a/test/data/docker-states/network-mode-service/imageInfo.json b/test/data/docker-states/network-mode-service/imageInfo.json index 0ba62dfc..45a98f2d 100644 --- a/test/data/docker-states/network-mode-service/imageInfo.json +++ b/test/data/docker-states/network-mode-service/imageInfo.json @@ -116,7 +116,7 @@ "firmware": true, "modules": true }, - "hostnameOnHost": "7dadabd", + "hostname": "7dadabd", "uuid": "7dadabd4edec3067948d5952c2f2f26f", "listenPort": "48484", "name": "Office", diff --git a/test/data/docker-states/simple/imageInfo.json b/test/data/docker-states/simple/imageInfo.json index 0ba62dfc..45a98f2d 100644 --- a/test/data/docker-states/simple/imageInfo.json +++ b/test/data/docker-states/simple/imageInfo.json @@ -116,7 +116,7 @@ "firmware": true, "modules": true }, - "hostnameOnHost": "7dadabd", + "hostname": "7dadabd", "uuid": "7dadabd4edec3067948d5952c2f2f26f", "listenPort": "48484", "name": "Office",