config write: Fix EBUSY error on macOS

Change-type: patch
This commit is contained in:
Paulo Castro 2021-07-21 02:22:45 +01:00
parent f914fa2d8a
commit e624726e44
3 changed files with 59 additions and 41 deletions

View File

@ -75,7 +75,7 @@ export default class ConfigWriteCmd extends Command {
ConfigWriteCmd, ConfigWriteCmd,
); );
const { safeUmount } = await import('../../utils/umount'); const { denyMount, safeUmount } = await import('../../utils/umount');
const drive = const drive =
options.drive || (await getVisuals().drive('Select the device drive')); options.drive || (await getVisuals().drive('Select the device drive'));
@ -87,9 +87,10 @@ export default class ConfigWriteCmd extends Command {
console.info(`Setting ${params.key} to ${params.value}`); console.info(`Setting ${params.key} to ${params.value}`);
ConfigWriteCmd.updateConfigJson(configJSON, params.key, params.value); ConfigWriteCmd.updateConfigJson(configJSON, params.key, params.value);
await safeUmount(drive); await denyMount(drive, async () => {
await safeUmount(drive);
await config.write(drive, options.type, configJSON); await config.write(drive, options.type, configJSON);
});
console.info('Done'); console.info('Done');
} }

View File

@ -60,50 +60,36 @@ export default class LocalConfigureCmd extends Command {
public async run() { public async run() {
const { args: params } = this.parse<FlagsDef, ArgsDef>(LocalConfigureCmd); const { args: params } = this.parse<FlagsDef, ArgsDef>(LocalConfigureCmd);
const path = await import('path');
const reconfix = await import('reconfix'); const reconfix = await import('reconfix');
const denymount = promisify(await import('denymount')); const { denyMount, safeUmount } = await import('../../utils/umount');
const { safeUmount } = await import('../../utils/umount');
const Logger = await import('../../utils/logger'); const Logger = await import('../../utils/logger');
const logger = Logger.getLogger(); const logger = Logger.getLogger();
const configurationSchema = await this.prepareConnectionFile(params.target); const configurationSchema = await this.prepareConnectionFile(params.target);
await safeUmount(params.target); await denyMount(params.target, async () => {
// TODO: safeUmount umounts drives like '/dev/sdc', but does not
const dmOpts: any = {}; // umount image files like 'balena.img'
if (process.pkg) { await safeUmount(params.target);
// when running in a standalone pkg install, the 'denymount' const config = await reconfix.readConfiguration(
// executable is placed on the same folder as process.execPath configurationSchema,
dmOpts.executablePath = path.join( params.target,
path.dirname(process.execPath),
'denymount',
); );
} logger.logDebug('Current config:');
logger.logDebug(JSON.stringify(config));
const dmHandler = (cb: () => void) => const answers = await this.getConfiguration(config);
reconfix logger.logDebug('New config:');
.readConfiguration(configurationSchema, params.target) logger.logDebug(JSON.stringify(answers));
.then(async (config: any) => { if (!answers.hostname) {
logger.logDebug('Current config:'); await this.removeHostname(configurationSchema);
logger.logDebug(JSON.stringify(config)); }
const answers = await this.getConfiguration(config); await reconfix.writeConfiguration(
logger.logDebug('New config:'); configurationSchema,
logger.logDebug(JSON.stringify(answers)); answers,
params.target,
if (!answers.hostname) { );
await this.removeHostname(configurationSchema); });
}
return await reconfix.writeConfiguration(
configurationSchema,
answers,
params.target,
);
})
.asCallback(cb);
await denymount(params.target, dmHandler, dmOpts);
console.log('Done!'); console.log('Done!');
} }

View File

@ -25,8 +25,9 @@
* - Convert callbacks to async/await * - Convert callbacks to async/await
*/ */
import { promisify } from 'util';
import * as child_process from 'child_process'; import * as child_process from 'child_process';
import * as path from 'path';
import { promisify } from 'util';
const execFile = promisify(child_process.execFile); const execFile = promisify(child_process.execFile);
@ -120,3 +121,33 @@ export async function safeUmount(drive: string) {
await umount(drive); await umount(drive);
} }
} }
/**
* Wrapper around the `denymount` package. See:
* https://github.com/balena-io-modules/denymount
*/
export async function denyMount(
target: string,
handler: () => any,
opts: { autoMountOnSuccess?: boolean; executablePath?: string } = {},
) {
const denymount = promisify(await import('denymount'));
if (process.pkg) {
// when running in a standalone pkg install, the 'denymount'
// executable is placed on the same folder as process.execPath
opts.executablePath ||= path.join(
path.dirname(process.execPath),
'denymount',
);
}
const dmHandler = async (cb: (err?: Error) => void) => {
let err: Error | undefined;
try {
await handler();
} catch (e) {
err = e;
}
cb(err);
};
await denymount(target, dmHandler, opts);
}