Only migrate apps.json on preload after target has been set

Change-type: patch
Signed-off-by: Christina Wang <christina@balena.io>
Signed-off-by: Felipe Lalanne <felipe@balena.io>
This commit is contained in:
Christina Wang 2022-05-16 13:11:10 -07:00 committed by Felipe Lalanne
parent 564ff186e7
commit 95bf4718d6

View File

@ -26,6 +26,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 {
@ -119,6 +139,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 +173,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;
}