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 <christina@balena.io>
This commit is contained in:
Christina Wang 2022-06-14 15:45:03 -07:00
parent c1bfbd5f3d
commit a7a0821a3e
6 changed files with 11 additions and 15 deletions

View File

@ -1,6 +1,5 @@
import * as _ from 'lodash'; import * as _ from 'lodash';
import { promises as fs } from 'fs'; import { promises as fs } from 'fs';
import * as path from 'path';
import Network from './network'; import Network from './network';
import Volume from './volume'; import Volume from './volume';
@ -742,7 +741,7 @@ export class App {
opts, opts,
supervisorApiHost, supervisorApiHost,
hostPathExists, hostPathExists,
hostnameOnHost, hostname,
] = await Promise.all([ ] = await Promise.all([
config.get('extendedEnvOptions'), config.get('extendedEnvOptions'),
dockerUtils dockerUtils
@ -752,20 +751,17 @@ export class App {
firmware: await pathExistsOnHost('/lib/firmware'), firmware: await pathExistsOnHost('/lib/firmware'),
modules: await pathExistsOnHost('/lib/modules'), modules: await pathExistsOnHost('/lib/modules'),
}))(), }))(),
(async () => (
_.trim( (await config.get('hostname')) ??
await fs.readFile( (await fs.readFile('/etc/hostname', 'utf-8'))
path.join(constants.rootMountPoint, '/etc/hostname'), ).trim(),
'utf8',
),
))(),
]); ]);
const svcOpts = { const svcOpts = {
appName: app.name, appName: app.name,
supervisorApiHost, supervisorApiHost,
hostPathExists, hostPathExists,
hostnameOnHost, hostname,
...opts, ...opts,
}; };

View File

@ -300,7 +300,7 @@ export class Service {
// Any other special case handling // Any other special case handling
if (config.networkMode === 'host' && !config.hostname) { if (config.networkMode === 'host' && !config.hostname) {
config.hostname = options.hostnameOnHost; config.hostname = options.hostname;
} }
config.restart = ComposeUtils.createRestartPolicy(config.restart); config.restart = ComposeUtils.createRestartPolicy(config.restart);
config.command = ComposeUtils.getCommand(config.command, options.imageInfo); config.command = ComposeUtils.getCommand(config.command, options.imageInfo);

View File

@ -327,7 +327,7 @@ export interface DeviceMetadata {
apiSecret: string; apiSecret: string;
supervisorApiHost: string; supervisorApiHost: string;
osVersion: string; osVersion: string;
hostnameOnHost: string; hostname: string;
hostPathExists: { hostPathExists: {
modules: boolean; modules: boolean;
firmware: boolean; firmware: boolean;

View File

@ -90,7 +90,7 @@
"firmware": true, "firmware": true,
"modules": true "modules": true
}, },
"hostnameOnHost": "7dadabd", "hostname": "7dadabd",
"uuid": "a7feb967fac7f559ccf2a006a36bcf5d", "uuid": "a7feb967fac7f559ccf2a006a36bcf5d",
"listenPort": "48484", "listenPort": "48484",
"name": "Office", "name": "Office",

View File

@ -116,7 +116,7 @@
"firmware": true, "firmware": true,
"modules": true "modules": true
}, },
"hostnameOnHost": "7dadabd", "hostname": "7dadabd",
"uuid": "7dadabd4edec3067948d5952c2f2f26f", "uuid": "7dadabd4edec3067948d5952c2f2f26f",
"listenPort": "48484", "listenPort": "48484",
"name": "Office", "name": "Office",

View File

@ -116,7 +116,7 @@
"firmware": true, "firmware": true,
"modules": true "modules": true
}, },
"hostnameOnHost": "7dadabd", "hostname": "7dadabd",
"uuid": "7dadabd4edec3067948d5952c2f2f26f", "uuid": "7dadabd4edec3067948d5952c2f2f26f",
"listenPort": "48484", "listenPort": "48484",
"name": "Office", "name": "Office",