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 { exec } from './fs-utils';
import log from './supervisor-console';
import * as conf from '../config';
// Retrieve the data for the OS once only per path
const getOSReleaseData = _.memoize(
@ -37,7 +38,7 @@ async function getOSReleaseField(
try {
const data = await getOSReleaseData(path);
const value = data[field];
if (value == null) {
if (value == null && field !== 'VARIANT_ID') {
log.warn(
`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');
}
export function getOSVariant(path: string): Promise<string | undefined> {
return getOSReleaseField(path, 'VARIANT_ID');
export async function getOSVariant(path: string): Promise<string | undefined> {
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> {

View File

@ -99,13 +99,13 @@ describe('Config', () => {
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;
constants.hostOSVersionPath = 'test/data/etc/os-release-novariant';
const osVariant = await conf.get('osVariant');
constants.hostOSVersionPath = oldPath;
expect(osVariant).to.be.undefined;
expect(osVariant).to.equal('prod');
});
it('reads and exposes MAC addresses', async () => {