os-release: Use developmentMode to ascertain OS variant in new releases

Newer BalenaOS releases have replaced OS variants for a developmentMode
configuration setting. This commit uses this variable to set the OS
variant in the absence of `VARIANT_ID` from the os-release file.

Change-type: patch
Signed-off-by: Alex Gonzalez <alexg@balena.io>
This commit is contained in:
Alex Gonzalez 2021-07-27 16:42:53 +00:00
parent 4ad7a3ae91
commit 1abd10a129
2 changed files with 11 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import { promises as fs } from 'fs';
import { InternalInconsistencyError } from './errors'; import { InternalInconsistencyError } from './errors';
import { exec } from './fs-utils'; import { exec } from './fs-utils';
import log from './supervisor-console'; import log from './supervisor-console';
import * as conf from '../config';
// Retrieve the data for the OS once only per path // Retrieve the data for the OS once only per path
const getOSReleaseData = _.memoize( const getOSReleaseData = _.memoize(
@ -37,7 +38,7 @@ async function getOSReleaseField(
try { try {
const data = await getOSReleaseData(path); const data = await getOSReleaseData(path);
const value = data[field]; const value = data[field];
if (value == null) { if (value == null && field !== 'VARIANT_ID') {
log.warn( log.warn(
`Field ${field} is not available in OS information file: ${path}`, `Field ${field} is not available in OS information file: ${path}`,
); );
@ -52,8 +53,13 @@ export async function getOSVersion(path: string): Promise<string | undefined> {
return getOSReleaseField(path, 'PRETTY_NAME'); return getOSReleaseField(path, 'PRETTY_NAME');
} }
export function getOSVariant(path: string): Promise<string | undefined> { export async function getOSVariant(path: string): Promise<string | undefined> {
return getOSReleaseField(path, 'VARIANT_ID'); const osVariant = await getOSReleaseField(path, 'VARIANT_ID');
if (osVariant === undefined) {
const developmentMode = await conf.get('developmentMode');
return developmentMode === true ? 'dev' : 'prod';
}
return osVariant;
} }
export function getOSSemver(path: string): Promise<string | undefined> { export function getOSSemver(path: string): Promise<string | undefined> {

View File

@ -99,13 +99,13 @@ describe('Config', () => {
conf.set({ name: 'someValue' }); conf.set({ name: 'someValue' });
}); });
it("returns an undefined OS variant if it doesn't exist", async () => { it("returns production OS variant if it doesn't exist", async () => {
const oldPath = constants.hostOSVersionPath; const oldPath = constants.hostOSVersionPath;
constants.hostOSVersionPath = 'test/data/etc/os-release-novariant'; constants.hostOSVersionPath = 'test/data/etc/os-release-novariant';
const osVariant = await conf.get('osVariant'); const osVariant = await conf.get('osVariant');
constants.hostOSVersionPath = oldPath; constants.hostOSVersionPath = oldPath;
expect(osVariant).to.be.undefined; expect(osVariant).to.equal('prod');
}); });
it('reads and exposes MAC addresses', async () => { it('reads and exposes MAC addresses', async () => {