Skip to content

Commit

Permalink
Merge branch 'master' of github.com:plebbit/plebbit-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinse12 committed Mar 29, 2024
2 parents 1953379 + d7e4f88 commit c345790
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 90 deletions.
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,13 @@ $ plebbit subplebbit edit mysub.eth '--roles["author-address.eth"]' null
# Commands

<!-- commands -->

- [`plebbit daemon`](#plebbit-daemon)
- [`plebbit help [COMMAND]`](#plebbit-help-command)
- [`plebbit subplebbit create`](#plebbit-subplebbit-create)
- [`plebbit subplebbit edit ADDRESS`](#plebbit-subplebbit-edit-address)
- [`plebbit subplebbit list`](#plebbit-subplebbit-list)
- [`plebbit subplebbit start ADDRESSES`](#plebbit-subplebbit-start-addresses)
- [`plebbit subplebbit stop ADDRESSES`](#plebbit-subplebbit-stop-addresses)
* [`plebbit daemon`](#plebbit-daemon)
* [`plebbit help [COMMAND]`](#plebbit-help-command)
* [`plebbit subplebbit create`](#plebbit-subplebbit-create)
* [`plebbit subplebbit edit ADDRESS`](#plebbit-subplebbit-edit-address)
* [`plebbit subplebbit list`](#plebbit-subplebbit-list)
* [`plebbit subplebbit start ADDRESSES`](#plebbit-subplebbit-start-addresses)
* [`plebbit subplebbit stop ADDRESSES`](#plebbit-subplebbit-stop-addresses)

## `plebbit daemon`

Expand Down Expand Up @@ -185,7 +184,7 @@ EXAMPLES
$ plebbit daemon --plebbitRpcPort 80
```

_See code: [src/cli/commands/daemon.ts](https://github.com/plebbit/plebbit-cli/blob/v0.11.31/src/cli/commands/daemon.ts)_
_See code: [src/cli/commands/daemon.ts](https://github.com/plebbit/plebbit-cli/blob/v0.11.32/src/cli/commands/daemon.ts)_

## `plebbit help [COMMAND]`

Expand Down Expand Up @@ -216,7 +215,7 @@ USAGE
$ plebbit subplebbit create --plebbitRpcApiUrl <value> [--privateKeyPath <value>]
FLAGS
--plebbitRpcApiUrl=<value> (required) [default: ws://localhost:9138/] URL to Plebbit RPC
--plebbitRpcApiUrl=<value> (required) [default: ws://localhost:9138/] URL to Plebbit RPC API
--privateKeyPath=<value> Private key (PEM) of the subplebbit signer that will be used to determine address (if
address is not a domain). If it's not provided then Plebbit will generate a private key
Expand All @@ -231,7 +230,7 @@ EXAMPLES
$ plebbit subplebbit create --title 'Hello Plebs' --description 'Welcome'
```

_See code: [src/cli/commands/subplebbit/create.ts](https://github.com/plebbit/plebbit-cli/blob/v0.11.31/src/cli/commands/subplebbit/create.ts)_
_See code: [src/cli/commands/subplebbit/create.ts](https://github.com/plebbit/plebbit-cli/blob/v0.11.32/src/cli/commands/subplebbit/create.ts)_

## `plebbit subplebbit edit ADDRESS`

Expand All @@ -245,7 +244,7 @@ ARGUMENTS
ADDRESS Address of the subplebbit address to edit
FLAGS
--plebbitRpcApiUrl=<value> (required) [default: ws://localhost:9138/] URL to Plebbit RPC
--plebbitRpcApiUrl=<value> (required) [default: ws://localhost:9138/] URL to Plebbit RPC API
DESCRIPTION
Edit a subplebbit properties. For a list of properties, visit
Expand All @@ -272,7 +271,7 @@ EXAMPLES
$ plebbit subplebbit edit mysub.eth --title "This is the new title" --description "This is the new description"
```

_See code: [src/cli/commands/subplebbit/edit.ts](https://github.com/plebbit/plebbit-cli/blob/v0.11.31/src/cli/commands/subplebbit/edit.ts)_
_See code: [src/cli/commands/subplebbit/edit.ts](https://github.com/plebbit/plebbit-cli/blob/v0.11.32/src/cli/commands/subplebbit/edit.ts)_

## `plebbit subplebbit list`

Expand All @@ -293,14 +292,14 @@ FLAGS
--no-truncate do not truncate output to fit screen
--output=<option> output in a more machine friendly format
<options: csv|json|yaml>
--plebbitRpcApiUrl=<value> (required) [default: ws://localhost:9138/] URL to Plebbit RPC
--plebbitRpcApiUrl=<value> (required) [default: ws://localhost:9138/] URL to Plebbit RPC API
--sort=<value> property to sort by (prepend '-' for descending)
DESCRIPTION
List your subplebbits
```

_See code: [src/cli/commands/subplebbit/list.ts](https://github.com/plebbit/plebbit-cli/blob/v0.11.31/src/cli/commands/subplebbit/list.ts)_
_See code: [src/cli/commands/subplebbit/list.ts](https://github.com/plebbit/plebbit-cli/blob/v0.11.32/src/cli/commands/subplebbit/list.ts)_

## `plebbit subplebbit start ADDRESSES`

Expand All @@ -314,13 +313,18 @@ ARGUMENTS
ADDRESSES... Addresses of subplebbits to start. Separated by space
FLAGS
--plebbitRpcApiUrl=<value> (required) [default: ws://localhost:9138/] URL to Plebbit RPC
--plebbitRpcApiUrl=<value> (required) [default: ws://localhost:9138/] URL to Plebbit RPC API
DESCRIPTION
Start a subplebbit
EXAMPLES
$ plebbit subplebbit start plebbit.eth
$ plebbit subplebbit start 12D3KooWG3XbzoVyAE6Y9vHZKF64Yuuu4TjdgQKedk14iYmTEPWu
```

_See code: [src/cli/commands/subplebbit/start.ts](https://github.com/plebbit/plebbit-cli/blob/v0.11.31/src/cli/commands/subplebbit/start.ts)_
_See code: [src/cli/commands/subplebbit/start.ts](https://github.com/plebbit/plebbit-cli/blob/v0.11.32/src/cli/commands/subplebbit/start.ts)_

## `plebbit subplebbit stop ADDRESSES`

Expand All @@ -334,7 +338,7 @@ ARGUMENTS
ADDRESSES... Addresses of subplebbits to stop. Separated by space
FLAGS
--plebbitRpcApiUrl=<value> (required) [default: ws://localhost:9138/] URL to Plebbit RPC
--plebbitRpcApiUrl=<value> (required) [default: ws://localhost:9138/] URL to Plebbit RPC API
DESCRIPTION
Stop a subplebbit. The subplebbit will not publish or receive any publications until it is started again.
Expand All @@ -345,8 +349,7 @@ EXAMPLES
$ plebbit subplebbit stop Qmb99crTbSUfKXamXwZBe829Vf6w5w5TktPkb6WstC9RFW
```

_See code: [src/cli/commands/subplebbit/stop.ts](https://github.com/plebbit/plebbit-cli/blob/v0.11.31/src/cli/commands/subplebbit/stop.ts)_

_See code: [src/cli/commands/subplebbit/stop.ts](https://github.com/plebbit/plebbit-cli/blob/v0.11.32/src/cli/commands/subplebbit/stop.ts)_
<!-- commandsstop -->

# Contribution
Expand Down
129 changes: 66 additions & 63 deletions dist/cli/commands/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,8 @@ class Daemon extends core_1.Command {
required: true,
default: defaults_js_1.default.PLEBBIT_DATA_PATH
}),
// seed: Flags.boolean({
// description:
// "Seeding flag. Seeding helps subplebbits distribute their publications and latest updates, as well as receiving new publications",
// required: false,
// default: false
// }),
// seedSubs: Flags.string({
// description: "Subplebbits to seed. If --seed is used and no subs was provided, it will default to seeding default subs",
// required: false,
// multiple: true,
// default: []
// }),
plebbitRpcPort: core_1.Flags.integer({
description: "Specify Plebbit RPC API port to listen on",
description: "Specify Plebbit RPC port to listen on",
required: true,
default: defaults_js_1.default.PLEBBIT_RPC_API_PORT
}),
Expand Down Expand Up @@ -61,81 +49,96 @@ class Daemon extends core_1.Command {
}
catch (e) {
plebbitRpcAuthKey = (0, crypto_1.randomBytes)(32).toString("base64").replace(/[/+=]/g, "").substring(0, 40);
await promises_1.default.writeFile(plebbitRpcAuthKey, plebbitRpcAuthKey, { flag: "wx" });
await promises_1.default.writeFile(plebbitRpcAuthKeyPath, plebbitRpcAuthKey, { flag: "wx" });
}
return plebbitRpcAuthKey;
}
async run() {
const { flags } = await this.parse(Daemon);
const log = (await (0, util_js_1.getPlebbitLogger)())("plebbit-cli:daemon");
log(`flags: `, flags);
const ipfsApiEndpoint = `http://localhost:${flags.ipfsApiPort}/api/v0`;
const ipfsGatewayEndpoint = `http://localhost:${flags.ipfsGatewayPort}`;
let mainProcessExited = false;
let isIpfsNodeAlreadyRunningByAnotherProgram = false;
// Ipfs Node may fail randomly, we need to set a listener so when it exits because of an error we restart it
let ipfsProcess;
const keepIpfsUp = async () => {
isIpfsNodeAlreadyRunningByAnotherProgram = await tcp_port_used_1.default.check(flags.ipfsApiPort, "127.0.0.1");
if (isIpfsNodeAlreadyRunningByAnotherProgram) {
if (ipfsProcess || usingDifferentProcessRpc)
return; // already started, no need to intervene
const isIpfsApiPortTaken = await tcp_port_used_1.default.check(flags.ipfsApiPort, "127.0.0.1");
if (isIpfsApiPortTaken) {
log(`Ipfs API already running on port (${flags.ipfsApiPort}) by another program. Plebbit-cli will use the running ipfs daemon instead of starting a new one`);
return;
}
ipfsProcess = await (0, startIpfs_js_1.startIpfsNode)(flags.ipfsApiPort, flags.ipfsGatewayPort);
log(`Started ipfs process with pid (${ipfsProcess.pid})`);
console.log(`IPFS API listening on: ${ipfsApiEndpoint}`);
console.log(`IPFS Gateway listening on: ${ipfsGatewayEndpoint}`);
ipfsProcess.on("exit", async () => {
// Restart IPFS process because it failed
log(`Ipfs node with pid (${ipfsProcess.pid}) exited`);
if (!mainProcessExited)
log(`Ipfs node with pid (${ipfsProcess?.pid}) exited`);
if (!mainProcessExited) {
ipfsProcess = undefined;
await keepIpfsUp();
}
else
ipfsProcess.removeAllListeners();
});
};
let subsToSeed;
// if (flags.seed) {
// if (lodash.isEmpty(flags.seedSubs)) {
// // load default subs here
// const res = await fetch("https://raw.githubusercontent.com/plebbit/temporary-default-subplebbits/master/subplebbits.json");
// const subs: { title: string; address: string }[] = await res.json();
// subsToSeed = subs.map((sub) => sub.address);
// } else subsToSeed = flags.seedSubs;
// }
log.trace(`subs to seed:`, subsToSeed);
let startedOwnRpc = false;
let usingDifferentProcessRpc = false;
const createOrConnectRpc = async () => {
if (startedOwnRpc)
return;
const isRpcPortTaken = await tcp_port_used_1.default.check(flags.plebbitRpcPort, "127.0.0.1");
if (isRpcPortTaken && usingDifferentProcessRpc)
return;
if (isRpcPortTaken) {
log(`Plebbit RPC is already running on port (${flags.plebbitRpcPort}) by another program. Plebbit-cli will use the running RPC server, and if shuts down, plebbit-cli will start a new RPC instance`);
const plebbitRpcApiUrl = `ws://localhost:${flags.plebbitRpcPort}`;
console.log("Using the already started RPC server at:", plebbitRpcApiUrl);
console.log("plebbit-cli daemon will monitor the plebbit RPC and ipfs API to make sure they're always up");
const Plebbit = await import("@plebbit/plebbit-js");
const plebbit = await Plebbit.default({ plebbitRpcClientsOptions: [plebbitRpcApiUrl] });
plebbit.on("error", () => { });
console.log(`Subplebbits in data path: `, await plebbit.listSubplebbits());
usingDifferentProcessRpc = true;
return;
}
const rpcAuthKey = await this._generateRpcAuthKeyIfNotExisting(flags.plebbitDataPath);
const PlebbitWsServer = await import("@plebbit/plebbit-js/dist/node/rpc/src/index.js");
const rpcServer = await PlebbitWsServer.default.PlebbitWsServer({
port: flags.plebbitRpcPort,
plebbitOptions: {
ipfsHttpClientsOptions: [ipfsApiEndpoint],
dataPath: flags.plebbitDataPath
},
authKey: rpcAuthKey
});
usingDifferentProcessRpc = false;
startedOwnRpc = true;
console.log(`plebbit rpc: listening on ws://localhost:${flags.plebbitRpcPort} (local connections only)`);
console.log(`plebbit rpc: listening on ws://localhost:${flags.plebbitRpcPort}/${rpcAuthKey} (secret auth key for remote connections)`);
console.log(`Plebbit data path: ${path_1.default.resolve(rpcServer.plebbit.dataPath)}`);
console.log(`Subplebbits in data path: `, await rpcServer.plebbit.listSubplebbits());
const handlRpcExit = async (signal) => {
log(`in handle exit (${signal})`);
await rpcServer.destroy();
process.exit();
};
["SIGINT", "SIGTERM", "SIGHUP", "beforeExit"].forEach((exitSignal) => process.on(exitSignal, handlRpcExit));
};
await keepIpfsUp();
process.on("exit", () => (mainProcessExited = true) && !isIpfsNodeAlreadyRunningByAnotherProgram && process.kill(ipfsProcess.pid));
const ipfsApiEndpoint = `http://localhost:${flags.ipfsApiPort}/api/v0`;
const ipfsGatewayEndpoint = `http://localhost:${flags.ipfsGatewayPort}`;
const rpcAuthKey = await this._generateRpcAuthKeyIfNotExisting(flags.plebbitDataPath);
//@ts-expect-error
const PlebbitWsServer = await import("@plebbit/plebbit-js/dist/node/rpc/src/index.js?");
const rpcServer = await PlebbitWsServer.default.PlebbitWsServer({
port: flags.plebbitRpcPort,
plebbitOptions: {
ipfsHttpClientsOptions: [ipfsApiEndpoint],
dataPath: flags.plebbitDataPath
},
authKey: rpcAuthKey
await createOrConnectRpc();
setInterval(async () => {
await keepIpfsUp();
await createOrConnectRpc();
}, 5000);
process.on("exit", () => {
mainProcessExited = true;
if (typeof ipfsProcess?.pid === "number" && !ipfsProcess.killed)
process.kill(ipfsProcess.pid);
});
const handleExit = async (signal) => {
log(`in handle exit (${signal})`);
await rpcServer.destroy();
process.exit();
};
const subs = await rpcServer.plebbit.listSubplebbits();
["SIGINT", "SIGTERM", "SIGHUP", "beforeExit"].forEach((exitSignal) => process.on(exitSignal, handleExit));
console.log(`IPFS API listening on: ${ipfsApiEndpoint}`);
console.log(`IPFS Gateway listening on: ${ipfsGatewayEndpoint}`);
console.log(`plebbit rpc: listening on ws://localhost:${flags.plebbitRpcPort} (local connections only)`);
console.log(`plebbit rpc: listening on ws://localhost:${flags.plebbitRpcPort}/${rpcAuthKey} (secret auth key for remote connections)`);
console.log(`Plebbit data path: ${path_1.default.resolve(rpcServer.plebbit.dataPath)}`);
console.log(`Subplebbits in data path: `, subs);
// if (Array.isArray(subsToSeed)) {
// const seedSubsLoop = () => {
// // I think calling setTimeout constantly here will overflow memory. Need to check later
// seedSubplebbits(<string[]>subsToSeed, rpcServer.plebbit).then(() => setTimeout(seedSubsLoop, 600000)); // Seed subs every 10 minutes
// };
// console.log(`Seeding subplebbits:`, subsToSeed);
// seedSubsLoop();
// }
}
}
exports.default = Daemon;
2 changes: 1 addition & 1 deletion dist/cli/commands/subplebbit/start.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export default class Start extends BaseCommand {
static args: {
addresses: import("@oclif/core/lib/interfaces/parser.js").Arg<string, Record<string, unknown>>;
};
static examples: never[];
static examples: string[];
run(): Promise<void>;
}
5 changes: 4 additions & 1 deletion dist/cli/commands/subplebbit/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class Start extends base_command_js_1.BaseCommand {
description: "Addresses of subplebbits to start. Separated by space"
})
};
static examples = [];
static examples = [
"plebbit subplebbit start plebbit.eth",
"plebbit subplebbit start 12D3KooWG3XbzoVyAE6Y9vHZKF64Yuuu4TjdgQKedk14iYmTEPWu"
];
async run() {
const { argv, flags } = await this.parse(Start);
const addresses = argv;
Expand Down
2 changes: 1 addition & 1 deletion dist/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@


## [0.11.32](https://github.com/plebbit/plebbit-cli/compare/v0.11.31...v0.11.32) (2024-03-23)


### Bug Fixes

* **daemon:** fix bugs with starting multiple ipfs and plebbit daemons ([c2d1539](https://github.com/plebbit/plebbit-cli/commit/c2d153941fe8f4cb2952d8a272195ecad179d014))
* **daemon:** plebbit daemon will use and monitor IPFS and Plebbit RPC started by other processes ([2aea8f5](https://github.com/plebbit/plebbit-cli/commit/2aea8f58512fab0c02ad703cebe978c4c2e4b2b1))
* **daemon:** write rpc auth key to the correct path ([5408abf](https://github.com/plebbit/plebbit-cli/commit/5408abf949a19f04369f1df0dd3d4fc8733b4a3d))

## [0.11.31](https://github.com/plebbit/plebbit-cli/compare/v0.11.30...v0.11.31) (2024-03-22)


Expand Down
9 changes: 6 additions & 3 deletions oclif.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"type": "option"
},
"plebbitRpcPort": {
"description": "Specify Plebbit RPC API port to listen on",
"description": "Specify Plebbit RPC port to listen on",
"name": "plebbitRpcPort",
"required": true,
"default": 9138,
Expand Down Expand Up @@ -290,7 +290,10 @@
}
},
"description": "Start a subplebbit",
"examples": [],
"examples": [
"plebbit subplebbit start plebbit.eth",
"plebbit subplebbit start 12D3KooWG3XbzoVyAE6Y9vHZKF64Yuuu4TjdgQKedk14iYmTEPWu"
],
"flags": {
"plebbitRpcApiUrl": {
"name": "plebbitRpcApiUrl",
Expand Down Expand Up @@ -362,5 +365,5 @@
]
}
},
"version": "0.11.31"
"version": "0.11.32"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plebbit-cli",
"version": "0.11.31",
"version": "0.11.32",
"description": "Command line interface to Plebbit API",
"types": "./dist/index.d.ts",
"homepage": "https://github.com/plebbit/plebbit-cli",
Expand Down

0 comments on commit c345790

Please sign in to comment.