Compare commits

..

1 Commits

Author SHA1 Message Date
Vitaliy Filippov 76a9f53965 Move base64 handling into antietcd.js 2024-06-12 14:49:23 +03:00
5 changed files with 12 additions and 86 deletions

View File

@ -48,9 +48,8 @@ Antietcd doesn't background itself, so use systemd or start-stop-daemon to run i
``` ```
node_modules/.bin/anticli [OPTIONS] put <key> [<value>] node_modules/.bin/anticli [OPTIONS] put <key> [<value>]
node_modules/.bin/anticli [OPTIONS] get <key> [-p|--prefix] [-v|--print-value-only] [-k|--keys-only] [--no-temp] node_modules/.bin/anticli [OPTIONS] get <key> [-p|--prefix] [-v|--print-value-only] [-k|--keys-only]
node_modules/.bin/anticli [OPTIONS] del <key> [-p|--prefix] node_modules/.bin/anticli [OPTIONS] del <key> [-p|--prefix]
node_modules/.bin/anticli [OPTIONS] load [--with-lease] < dump.json
``` ```
For `put`, if `<value>` is not specified, it will be read from STDIN. For `put`, if `<value>` is not specified, it will be read from STDIN.
@ -71,9 +70,6 @@ Options:
<dt>--timeout 1000</dt> <dt>--timeout 1000</dt>
<dd>Specify request timeout in milliseconds</dd> <dd>Specify request timeout in milliseconds</dd>
<dt>--json or --write-out=json</dt>
<dd>Print raw response in JSON</dd>
</dl> </dl>
## Options ## Options

86
anticli.js Executable file → Normal file
View File

@ -4,7 +4,6 @@
// (c) Vitaliy Filippov, 2024 // (c) Vitaliy Filippov, 2024
// License: Mozilla Public License 2.0 or Vitastor Network Public License 1.1 // License: Mozilla Public License 2.0 or Vitastor Network Public License 1.1
const fs = require('fs');
const fsp = require('fs').promises; const fsp = require('fs').promises;
const http = require('http'); const http = require('http');
const https = require('https'); const https = require('https');
@ -16,14 +15,13 @@ License: Mozilla Public License 2.0 or Vitastor Network Public License 1.1
Usage: Usage:
anticli.js [OPTIONS] put <key> [<value>] anticli.js [OPTIONS] put <key> [<value>]
anticli.js [OPTIONS] get <key> [-p|--prefix] [-v|--print-value-only] [-k|--keys-only] [--no-temp] anticli.js [OPTIONS] get <key> [-p|--prefix] [-v|--print-value-only] [-k|--keys-only]
anticli.js [OPTIONS] del <key> [-p|--prefix] anticli.js [OPTIONS] del <key> [-p|--prefix]
anticli.js [OPTIONS] load [--with-lease] < dump.json
Options: Options:
[--endpoints|-e http://node1:2379,http://node2:2379,http://node3:2379] [--endpoints|-e http://node1:2379,http://node2:2379,http://node3:2379]
[--cert cert.pem] [--key key.pem] [--timeout 1000] [--json] [--cert cert.pem] [--key key.pem] [--timeout 1000]
`; `;
class AntiEtcdCli class AntiEtcdCli
@ -56,19 +54,6 @@ class AntiEtcdCli
{ {
options['keys_only'] = true; options['keys_only'] = true;
} }
else if (arg == '--with_lease')
{
options['with_lease'] = true;
}
else if (arg == '--write_out' && args[i+1] == 'json')
{
i++;
options['json'] = true;
}
else if (arg == '--json' || arg == '--write_out=json')
{
options['json'] = true;
}
else if (arg[0] == '-' && arg[1] !== '-') else if (arg[0] == '-' && arg[1] !== '-')
{ {
process.stderr.write('Unknown option '+arg); process.stderr.write('Unknown option '+arg);
@ -83,9 +68,9 @@ class AntiEtcdCli
cmd.push(arg); cmd.push(arg);
} }
} }
if (!cmd.length || cmd[0] != 'get' && cmd[0] != 'put' && cmd[0] != 'del' && cmd[0] != 'load') if (!cmd.length || cmd[0] != 'get' && cmd[0] != 'put' && cmd[0] != 'del')
{ {
process.stderr.write('Supported commands: get, put, del, load. Use --help to see details\n'); process.stderr.write('Supported commands: get, put, del. Use --help to see details\n');
process.exit(1); process.exit(1);
} }
return [ cmd, options ]; return [ cmd, options ];
@ -117,51 +102,12 @@ class AntiEtcdCli
{ {
await this.del(cmd.slice(1)); await this.del(cmd.slice(1));
} }
else if (cmd[0] == 'load')
{
await this.load();
}
// wait until output is fully flushed // wait until output is fully flushed
await new Promise(ok => process.stdout.write('', ok)); await new Promise(ok => process.stdout.write('', ok));
await new Promise(ok => process.stderr.write('', ok)); await new Promise(ok => process.stderr.write('', ok));
process.exit(0); process.exit(0);
} }
async load()
{
const dump = JSON.parse(await new Promise((ok, no) => fs.readFile(0, { encoding: 'utf-8' }, (err, res) => err ? no(err) : ok(res))));
if (!dump.responses && !dump.kvs)
{
console.error('dump should be /kv/txn or /kv/range response in json format');
process.exit(1);
}
const success = [];
for (const r of (dump.responses
? dump.responses.map(r => r.response_range).filter(r => r)
: [ dump ]))
{
for (const kv of r.kvs)
{
if (kv.value == null)
{
console.error('dump should contain values');
process.exit(1);
}
success.push({ request_put: { key: kv.key, value: kv.value, lease: this.options.with_lease ? kv.lease||undefined : undefined } });
}
}
const res = await this.request('/v3/kv/txn', { success });
if (this.options.json)
{
process.stdout.write(JSON.stringify(res));
return;
}
if (res.succeeded)
{
process.stdout.write('OK, loaded '+success.length+' values\n');
}
}
async get(keys) async get(keys)
{ {
if (this.options.prefix) if (this.options.prefix)
@ -170,22 +116,6 @@ class AntiEtcdCli
} }
const txn = { success: keys.map(key => ({ request_range: this.options.prefix ? { key: b64(key+'/'), range_end: b64(key+'0') } : { key: b64(key) } })) }; const txn = { success: keys.map(key => ({ request_range: this.options.prefix ? { key: b64(key+'/'), range_end: b64(key+'0') } : { key: b64(key) } })) };
const res = await this.request('/v3/kv/txn', txn); const res = await this.request('/v3/kv/txn', txn);
if (this.options.notemp)
{
// Skip temporary values (values with lease)
for (const r of res.responses||[])
{
if (r.response_range)
{
r.response_range.kvs = r.response_range.kvs.filter(kv => !kv.lease);
}
}
}
if (this.options.json)
{
process.stdout.write(JSON.stringify(keys.length == 1 ? res.responses[0].response_range : res));
return;
}
for (const r of res.responses||[]) for (const r of res.responses||[])
{ {
if (r.response_range) if (r.response_range)
@ -209,7 +139,7 @@ class AntiEtcdCli
{ {
if (value === undefined) if (value === undefined)
{ {
value = await new Promise((ok, no) => fs.readFile(0, { encoding: 'utf-8' }, (err, res) => err ? no(err) : ok(res))); value = await fsp.readFile(0, { encoding: 'utf-8' });
} }
const res = await this.request('/v3/kv/put', { key: b64(key), value: b64(value) }); const res = await this.request('/v3/kv/put', { key: b64(key), value: b64(value) });
if (res.header) if (res.header)
@ -245,18 +175,18 @@ class AntiEtcdCli
{ {
if (res.json.error) if (res.json.error)
{ {
process.stderr.write(cur_url+': '+res.json.error+'\n'); process.stderr.write(cur_url+': '+res.json.error);
process.exit(1); process.exit(1);
} }
return res.json; return res.json;
} }
if (res.body) if (res.body)
{ {
process.stderr.write(cur_url+': '+res.body+'\n'); process.stderr.write(cur_url+': '+res.body);
} }
if (res.error) if (res.error)
{ {
process.stderr.write(cur_url+': '+res.error+'\n'); process.stderr.write(cur_url+': '+res.error);
if (!res.response || !res.response.statusCode) if (!res.response || !res.response.statusCode)
{ {
// This URL is unavailable // This URL is unavailable

0
antietcd-app.js Executable file → Normal file
View File

View File

@ -105,7 +105,7 @@ class EtcTree
{ {
const key = req.key; const key = req.key;
const end = req.range_end; const end = req.range_end;
if (end != null && (key !== '' && end !== '') && (key[key.length-1] != '/' || end[end.length-1] != '0' || if (end != null && (key[key.length-1] != '/' || end[end.length-1] != '0' ||
end.substr(0, end.length-1) !== key.substr(0, key.length-1))) end.substr(0, end.length-1) !== key.substr(0, key.length-1)))
{ {
throw new RequestError(501, 'Non-directory range queries are unsupported'); throw new RequestError(501, 'Non-directory range queries are unsupported');
@ -264,7 +264,7 @@ class EtcTree
{ {
cur_old.value = cur_new.value; cur_old.value = cur_new.value;
const key_watchers = (cur_old.key_watchers ? [ ...watchers, ...(cur_old.key_watchers||[]) ] : watchers); const key_watchers = (cur_old.key_watchers ? [ ...watchers, ...(cur_old.key_watchers||[]) ] : watchers);
const notify = { watchers: key_watchers, key: this.b64(key), value: this.b64(cur_new.value), mod_revision: cur_new.mod_revision }; const notify = { watchers: key_watchers, key, value: cur_new.value, mod_revision: cur_new.mod_revision };
if (cur_new.lease) if (cur_new.lease)
{ {
notify.lease = cur_new.lease; notify.lease = cur_new.lease;

View File

@ -1,6 +1,6 @@
{ {
"name": "antietcd", "name": "antietcd",
"version": "1.0.3", "version": "1.0.2",
"description": "Simplistic etcd replacement based on TinyRaft", "description": "Simplistic etcd replacement based on TinyRaft",
"main": "antietcd.js", "main": "antietcd.js",
"scripts": { "scripts": {