Skip to content

Commit

Permalink
fix: add rapid_deamon to package
Browse files Browse the repository at this point in the history
  • Loading branch information
akitaSummer committed Apr 1, 2024
1 parent a16b650 commit aa05edb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 13 deletions.
3 changes: 2 additions & 1 deletion bindings/binding-darwin-amd64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"index.node",
"nydusd",
"nydusd-bootstrap",
"unionfs"
"unionfs",
"rapid_deamon"
],
"scripts": {
"pack": "npm pack",
Expand Down
3 changes: 2 additions & 1 deletion bindings/binding-darwin-arm64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"index.node",
"nydusd",
"nydusd-bootstrap",
"unionfs"
"unionfs",
"rapid_deamon"
],
"scripts": {
"pack": "npm pack",
Expand Down
3 changes: 2 additions & 1 deletion bindings/binding-linux-amd64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"files": [
"index.node",
"nydusd",
"nydusd-bootstrap"
"nydusd-bootstrap",
"rapid_deamon"
],
"engines": {
"node": ">=12.0.0"
Expand Down
3 changes: 2 additions & 1 deletion bindings/binding-linux-arm64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"files": [
"index.node",
"nydusd",
"nydusd-bootstrap"
"nydusd-bootstrap",
"rapid_deamon"
],
"scripts": {
"pack": "npm pack",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@
},
"engines": {
"node": ">=14.19.1"
}
},
"rapidVersion": "0.0.1"
}
63 changes: 55 additions & 8 deletions packages/cli/lib/deamon.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ const addProject = async config => {
}
};

const compareVersions = (version1, version2) => {
if (!version1 || !version2) {
return true;
}
const v1Parts = version1.split('.');
const v2Parts = version2.split('.');

const maxLength = Math.max(v1Parts.length, v2Parts.length);

for (let i = 0; i < maxLength; i++) {
const v1Part = parseInt(v1Parts[i] || 0);
const v2Part = parseInt(v2Parts[i] || 0);

if (v1Part > v2Part) {
return false;
} else if (v1Part < v2Part) {
return true;
}
}

return false; // version1 === version2
};

const runDeamon = async () => {
const subprocess = execa(destinationFilePath, [], {
Expand Down Expand Up @@ -140,6 +162,22 @@ const killDeamon = async () => {
};

const registerDeamon = async () => {
try {
await execa.command('killall -9 rapid_deamon');

await execa.command(`umount -f ${nydusdMnt}`);

await execa.command('killall -9 nydusd');
} catch (error) {
debug('umount deamon error: ', error);
}

await fs.rm(deamonDir, { recursive: true, force: true });

await fs.mkdir(deamonDir, { recursive: true });

await fs.copyFile(path.join(__dirname, '../package.json'), path.join(deamonDir, 'package.json'));

const nydusConfigPath = path.join(deamonDir, 'nydus_config.json');

await fs.writeFile(nydusConfigPath, JSON.stringify({
Expand Down Expand Up @@ -191,16 +229,25 @@ root:
};

const initDeamon = async () => {
const isRunning = await checkDeamonAlive();
if (isRunning) {
console.info('[rapid] rapid daemon is running already.');
return;
}
await fs.mkdir(deamonDir, { recursive: true });
try {
const rapidVersion = require(path.join(__dirname, '../package.json')).deamonVersion;
const deamonVersion = require(path.join(deamonDir, './package.json')).deamonVersion;

await fs.mkdir(nydusdMnt, { recursive: true });
if (compareVersions(deamonVersion, rapidVersion)) {
const err = '[rapid] rapid and deamon version not match';
console.info(err);
throw Error(err);
}

const isRunning = await checkDeamonAlive();
if (isRunning) {
console.info('[rapid] rapid daemon is running already.');
return;
}
await fs.mkdir(deamonDir, { recursive: true });

await fs.mkdir(nydusdMnt, { recursive: true });

try {
await fs.stat(destinationFilePath);
} catch (e) {
await registerDeamon();
Expand Down

0 comments on commit aa05edb

Please sign in to comment.