Merge pull request #1944 from balena-os/1943-allow-v2-state-preload-without-internet

Do not migrate apps.json if preload v2 to v3 fails
This commit is contained in:
bulldozer-balena[bot] 2022-05-27 14:51:20 +00:00 committed by GitHub
commit 08d742c832
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 25 deletions

View File

@ -2,6 +2,7 @@ import * as _ from 'lodash';
import { promises as fs } from 'fs';
import { Image, imageFromService } from '../compose/images';
import { NumericIdentifier } from '../types';
import * as deviceState from '../device-state';
import * as config from '../config';
import * as deviceConfig from '../device-config';
@ -26,6 +27,26 @@ export function appsJsonBackup(appsPath: string) {
return `${appsPath}.preloaded`;
}
async function migrateAppsJson(appsPath: string) {
const targetPath = appsJsonBackup(appsPath);
if (!(await fsUtils.exists(targetPath))) {
// Try to rename the path so the preload target state won't
// be used again if the database gets deleted for any reason.
// If the target file already exists or something fails, just debug
// the failure.
await fsUtils
.safeRename(appsPath, targetPath)
.then(() => fsUtils.writeFileAtomic(appsPath, '{}'))
.then(() => log.debug(`Migrated existing apps.json`))
.catch((e) =>
log.debug(
`Continuing without migrating apps.json because of`,
e.message,
),
);
}
}
export async function loadTargetFromFile(appsPath: string): Promise<boolean> {
log.info('Attempting to load any preloaded applications');
try {
@ -46,11 +67,7 @@ export async function loadTargetFromFile(appsPath: string): Promise<boolean> {
}
// if apps.json apps are keyed by numeric ids, then convert to v3 target state
if (
Object.keys(stateFromFile.apps || {}).some(
(appId) => !isNaN(parseInt(appId, 10)),
)
) {
if (Object.keys(stateFromFile.apps || {}).some(NumericIdentifier.is)) {
stateFromFile = await fromV2AppsJson(stateFromFile as any);
}
@ -119,6 +136,7 @@ export async function loadTargetFromFile(appsPath: string): Promise<boolean> {
};
await deviceState.setTarget(localState);
await migrateAppsJson(appsPath);
log.success('Preloading complete');
if (preloadState.pinDevice) {
// Multi-app warning!
@ -152,24 +170,6 @@ export async function loadTargetFromFile(appsPath: string): Promise<boolean> {
error: e,
});
}
} finally {
const targetPath = appsJsonBackup(appsPath);
if (!(await fsUtils.exists(targetPath))) {
// Try to rename the path so the preload target state won't
// be used again if the database gets deleted for any reason.
// If the target file already exists or something fails, just debug
// the failure.
await fsUtils
.safeRename(appsPath, targetPath)
.then(() => fsUtils.writeFileAtomic(appsPath, '{}'))
.then(() => log.debug(`Migrated existing apps.json`))
.catch((e) =>
log.debug(
`Continuing without migrating apps.json because of`,
e.message,
),
);
}
}
return false;
}

View File

@ -309,8 +309,14 @@ export async function fromV2TargetApps(
const appId = parseInt(id, 10);
const app = apps[appId];
// If local mode just use id as uuid
const uuid = local ? id : await getUUIDFromAPI(appId);
// If local mode or connectivity is not available just use id as uuid
const uuid = local
? id
: await getUUIDFromAPI(appId).catch(() => {
throw new Error(
'Cannot migrate from v2 apps.json without Internet connectivity. Please use balenaCLI v13.5.1+ for offline preload support.',
);
});
const releases = app.commit
? {