Skip to content

Commit

Permalink
fix: use runscript instead of execa.command
Browse files Browse the repository at this point in the history
  • Loading branch information
tianding.wk committed May 15, 2023
1 parent 1a6c5fa commit 3832536
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 29 deletions.
22 changes: 11 additions & 11 deletions packages/cli/lib/nydusd/csi_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const path = require('node:path');
const fs = require('node:fs/promises');
const execa = require('execa');
const runscript = require('runscript');
const constants = require('../constants');
const {
wrapSudo,
Expand Down Expand Up @@ -50,7 +50,7 @@ async function mount(cwd, allPkgs) {
console.time(`[rapid] mount '/${dirname}' to nydusd daemon using socket api`);
const {
stderr,
} = await execa.command(wrapSudo(`${process.execPath} ${path.join(__dirname, './csi_script.js')} ${path.basename(bootstrap)} ${volumeId} ${socketPath} ${csiMountId}`), {
} = await runscript(wrapSudo(`${process.execPath} ${path.join(__dirname, './csi_script.js')} ${path.basename(bootstrap)} ${volumeId} ${socketPath} ${csiMountId}`), {
stdio: 'pipe',
});

Expand All @@ -69,7 +69,7 @@ async function umount(mountId) {

const {
stderr,
} = await execa.command(wrapSudo(`${process.execPath} ${path.join(__dirname, './csi_umount_script.js')} ${volumeId} ${socketPath} ${mountId}`), {
} = await runscript(wrapSudo(`${process.execPath} ${path.join(__dirname, './csi_umount_script.js')} ${volumeId} ${socketPath} ${mountId}`), {
stdio: 'pipe',
});

Expand All @@ -84,8 +84,8 @@ async function generateBootStrap(cwd, allPkgs) {
const tarBucketsDir = constants.NYDUS_CSI_BLOB_ROOT;
await Promise.all(allPkgs.map(async pkgPath => {
const { bootstrap, tarIndex } = await getWorkdir(cwd, pkgPath, constants.NYDUS_CSI_BLOB_ROOT);
await execa.command(wrapSudo(`mkdir -p ${path.dirname(bootstrap)}`));
await execa.command(`${constants.BOOTSTRAP_BIN} --stargz-config-path=${tarIndex} --stargz-dir=${tarBucketsDir} --bootstrap=${bootstrap}`);
await runscript(wrapSudo(`mkdir -p ${path.dirname(bootstrap)}`));
await runscript(`${constants.BOOTSTRAP_BIN} --stargz-config-path=${tarIndex} --stargz-dir=${tarBucketsDir} --bootstrap=${bootstrap}`);
}));
}

Expand All @@ -94,8 +94,8 @@ async function moveBlobToData() {
// blob dir is root
const mkdirp = wrapSudo(`mkdir -p ${blobDir}`);
const chown = wrapSudo(`chown -R admin:admin ${blobDir}`);
await execa.command(`${mkdirp} && ${chown}`);
await execa.command(`mv ${constants.tarBucketsDir}/* ${blobDir}`);
await runscript(`${mkdirp} && ${chown}`);
await runscript(`mv ${constants.tarBucketsDir}/* ${blobDir}`);
}

async function linkToCurrent(cwd, allPkgs) {
Expand All @@ -112,7 +112,7 @@ async function linkToCurrent(cwd, allPkgs) {
const nydusdMnt = path.join(process.env[constants.NYDUS_CSI_ROOT_ENV], mountId);
await fs.mkdir(nodeModulesDir, { recursive: true });
await fs.mkdir(overlay, { recursive: true });
await execa.command(wrapSudo(`mount -t tmpfs tmpfs ${overlay}`));
await runscript(wrapSudo(`mount -t tmpfs tmpfs ${overlay}`));
await fs.mkdir(upper, { recursive: true });
await fs.mkdir(workdir, { recursive: true });

Expand All @@ -122,7 +122,7 @@ async function linkToCurrent(cwd, allPkgs) {
-o lowerdir=${nydusdMnt},upperdir=${upper},workdir=${workdir} \
${nodeModulesDir}`);
console.info('[rapid] mountOverlay: `%s`', shScript);
await execa.command(shScript);
await runscript(shScript);
console.info('[rapid] overlay mounted.');
}));
}
Expand All @@ -145,8 +145,8 @@ async function endNydusFs(cwd, pkg) {
// umount overlay
try {
await umount(mountId);
await execa.command(wrapSudo(`umount ${nodeModulesDir}`));
await execa.command(wrapSudo(`umount ${overlay}`));
await runscript(wrapSudo(`umount ${nodeModulesDir}`));
await runscript(wrapSudo(`umount ${overlay}`));
} catch (e) {
e.message = '[rapid/csi] umount failed: ' + e.message;
console.error(e);
Expand Down
14 changes: 7 additions & 7 deletions packages/cli/lib/nydusd/fuse_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const fs = require('node:fs/promises');
const os = require('node:os');
const path = require('node:path');
const execa = require('execa');
const runscript = require('runscript');
const {
tarBucketsDir,
unionfs,
Expand Down Expand Up @@ -35,7 +35,7 @@ async function generateBootstrapFile(cwd, pkg) {
await Promise.all(allPkgs.map(async pkgPath => {
const { bootstrap, tarIndex } = await getWorkdir(cwd, pkgPath);
await fs.mkdir(path.dirname(bootstrap), { recursive: true });
await execa.command(`${BOOTSTRAP_BIN} --stargz-config-path=${tarIndex} --stargz-dir=${tarBucketsDir} --bootstrap=${bootstrap}`);
await runscript(`${BOOTSTRAP_BIN} --stargz-config-path=${tarIndex} --stargz-dir=${tarBucketsDir} --bootstrap=${bootstrap}`);
}));
console.timeEnd('[rapid] generate bootstrap');
}
Expand Down Expand Up @@ -64,9 +64,9 @@ async function mountOverlay(cwd, pkg) {
await fs.mkdir(nodeModulesDir, { recursive: true });
await fs.mkdir(overlay, { recursive: true });
if (os.type() === 'Linux') {
await execa.command(wrapSudo(`mount -t tmpfs tmpfs ${overlay}`));
await runscript(wrapSudo(`mount -t tmpfs tmpfs ${overlay}`));
} else if (os.type() === 'Darwin') {
await execa.command(wrapSudo(`mount_tmpfs -o union -e ${overlay}`));
await runscript(wrapSudo(`mount_tmpfs -o union -e ${overlay}`));
}
await fs.mkdir(upper, { recursive: true });
await fs.mkdir(workdir, { recursive: true });
Expand All @@ -84,7 +84,7 @@ ${upper}=RW:${mnt}=RO \
${nodeModulesDir}`;
}
console.info('[rapid] mountOverlay: `%s`', shScript);
await execa.command(shScript);
await runscript(shScript);
console.info('[rapid] overlay mounted.');
}));
}
Expand All @@ -99,8 +99,8 @@ async function endNydusFs(cwd, pkg) {
baseDir,
nodeModulesDir,
} = await getWorkdir(cwd, pkgPath);
await execa.command(wrapSudo(`umount ${nodeModulesDir}`));
await execa.command(wrapSudo(`umount ${overlay}`));
await runscript(wrapSudo(`umount ${nodeModulesDir}`));
await runscript(wrapSudo(`umount ${overlay}`));
await nydusdApi.umount(`/${dirname}`);
// 清除 nydus 相关目录
await fs.rm(baseDir, { recursive: true, force: true });
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/lib/nydusd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const {
const path = require('node:path');
const fs = require('node:fs/promises');
const debug = require('debug')('nydusd');
const execa = require('execa');
const runscript = require('runscript');
const util = require('../util');
const fuseMode = require('./fuse_mode');
const csiMode = require('./csi_mode');

exports.startNydusFs = async function(mode, cwd, pkg) {
exports.startNydusFs = async function (mode, cwd, pkg) {
switch (mode) {
case NYDUS_TYPE.FUSE: {
await fuseMode.startNydusFs(cwd, pkg);
Expand All @@ -29,7 +29,7 @@ exports.startNydusFs = async function(mode, cwd, pkg) {
}
};

exports.endNydusFs = async function(mode, cwd, pkg) {
exports.endNydusFs = async function (mode, cwd, pkg) {
debug('endNydusFs, mode: %s, cwd: %s', mode, cwd);
switch (mode) {
case NYDUS_TYPE.CSI: {
Expand All @@ -44,7 +44,7 @@ exports.endNydusFs = async function(mode, cwd, pkg) {
}
};

exports.getNydusMode = async function(cwd) {
exports.getNydusMode = async function (cwd) {
if (cwd) {
const installMode = await exports.getNydusInstallMode(cwd);
if (installMode) return installMode;
Expand All @@ -60,14 +60,14 @@ exports.getNydusMode = async function(cwd) {
}
};

exports.getNydusInstallMode = async function(cwd) {
exports.getNydusInstallMode = async function (cwd) {
const nmDir = path.join(cwd, 'node_modules');
try {
await fs.access(nmDir);
} catch (_) {
return NYDUS_TYPE.NONE;
}
const stdio = await execa.command('mount', {
const stdio = await runscript('mount', {
stdio: 'pipe',
});
const stdout = stdio.stdout.toString();
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/lib/nydusd/nydusd_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const urllib = require('urllib');
const debug = require('debug')('rapid:nydusd_api');
const util = require('../util');
const execa = require('execa');
const runscript = require('runscript');
const awaitEvent = require('await-event');
const fs = require('node:fs/promises');

Expand Down Expand Up @@ -118,7 +119,7 @@ async function checkDaemon() {
debug('mount error: ', error);
// linux 下需要用 sudo 启动,如果没有权限,这里
if (error.code === 'EACCES' && process.platform === 'linux') {
await execa.command(wrapSudo(`chmod 777 ${socketPath}`));
await runscript(wrapSudo(`chmod 777 ${socketPath}`));
}
if (Date.now() - startTime <= maxWaitDuration) {
await util.sleep(100);
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
NotSupportedError,
FuseDeviceError,
} = require('./error');
const execa = require('execa');
const runscript = require('runscript');
const normalize = require('npm-normalize-package-bin');
const {
tarBucketsDir,
Expand Down Expand Up @@ -79,7 +79,7 @@ async function shouldFuseSupport() {
console.info(`[rapid] detect /dev/fuse: ${sh}`);

try {
await execa.command(sh, {
await runscript(sh, {
stdio: 'ignore',
});
} catch (e) {
Expand Down Expand Up @@ -468,7 +468,7 @@ exports.runScript = async (pkgDir, script, options) => {
}

try {
const res = await execa.command(script, {
const res = await runscript(script, {
cwd: pkgDir,
env,
stdio: 'inherit',
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cnpmjs/rapid",
"version": "0.0.7",
"version": "0.0.8",
"description": "The rapid npm installed cli built with Nydus",
"main": "lib/index.js",
"bin": {
Expand All @@ -24,6 +24,7 @@
"npm-package-arg": "^10.1.0",
"p-map": "^4.0.0",
"pacote": "^15.0.7",
"runscript": "^1.5.3",
"semver": "^7.3.8",
"urllib": "^2.14.0",
"yargs-parser": "^9.0.2"
Expand Down

0 comments on commit 3832536

Please sign in to comment.