etcd client module for Lua.
this module supports etcd API v2.
- halo: https://github.com/mah0x211/lua-halo
- util: https://github.com/mah0x211/lua-util
- path: https://github.com/mah0x211/lua-path
- httpcli: https://github.com/mah0x211/lua-httpcli
- lua-cjson: http://www.kyne.com.au/~mark/software/lua-cjson.php
luarocks install etcd --from=http://mah0x211.github.io/rocks/
etcd.luasocket
isluasocket
based client.
local Etcd = require('etcd.luasocket');
- lua-etcd-resty: https://github.com/mah0x211/lua-etcd-resty
local Etcd = require('etcd.luasocket');
local cli, err = Etcd.new();
Parameters
-
option:table
host
: string - defaulthttp://127.0.0.1:4001
peer
: string - defaulthttp://127.0.0.1:7001
ttl
: int - default-1
default ttl for key operation. set -1 to disable ttl.prefix
: string
append this prefix path string to key operation url'/v2/keys'
.
etcd.luasocket
module optiontimeout
: int
request timeout seconds.
Returns
cli
: client object.err
: error string.
client methods returns either a HTTP Response Entity
or an error string
.
a HTTP Response Entity
contains the following fields except 408
timeout status;
status
: number - HTTP status code.header
: table - response header ifstatus
is not408
timeout status.body
: string or table - response body ifstatus
is not408
timeout status.
Note: a client method will decode a response body as a JSON string if a Content-Type
response header value is a application/json
.
please refer the etcd API documentaion at - https://github.com/coreos/etcd for more details of a response entity.
get the value for key.
local inspect = require('util').inspect;
local res, err = cli:get( '/path/to/key' );
print( inspect( { res, err } ) );
Parameters
consistent
: boolean - read from master if set a true.
set a key-value pair.
local inspect = require('util').inspect;
local res, err = cli:set( '/path/to/key', 'val', 10 );
print( inspect( { res, err } ) );
set a key-value pair if that key does not exist.
local inspect = require('util').inspect;
local res, err = cli:setnx( '/path/to/key', 'val', 10 );
print( inspect( { res, err } ) );
set a key-value pair when that key is exists.
local inspect = require('util').inspect;
local res, err = cli:setx( '/path/to/key', 'val', 10 );
print( inspect( { res, err } ) );
Parameters
modifiedIndex
: uint - this argument to use to theprevIndex
query of atomic operation.
local inspect = require('util').inspect;
local res, err = cli:get( '/path/to/key' );
-- this operation will be failed if the `modifiedIndex` of specified key has already been updated by another client.
res, err = cli:setx( '/path/to/key', 'val', 10, res.body.node.modifiedIndex );
print( inspect( { res, err } ) );
delete a key-value pair.
local inspect = require('util').inspect;
local res, err = cli:delete( '/path/to/key' );
print( inspect( { res, err } ) );
Parameters
val
: JSON encodable value - this argument to use to theprevValue
query of atomic operation.modifiedIndex
: uint - this argument to use to theprevIndex
query of atomic operation.
local inspect = require('util').inspect;
local res, err = cli:get( '/path/to/key' );
-- delete key-value pair if both of `value` and `modifiedIndex` has matched to the passed arguments
res, err = cli:delete( '/path/to/key', res.body.node.value, res.body.node.modifiedIndex );
-- delete key-value pair if `value` has matched to the passed value
res, err = cli:delete( '/path/to/key', res.body.node.value );
-- delete key-value pair if `modifiedIndex` has matched to the passed modifiedIndex
res, err = cli:delete( '/path/to/key', nil, res.body.node.modifiedIndex );
print( inspect( { res, err } ) );
local inspect = require('util').inspect;
local res, err = cli:wait( '/path/to/key' );
print( inspect( { res, err } ) );
Parameters
modifiedIndex
: uint - this argument to use to theprevIndex
query of atomic operation.timeout
: uint - request timeout seconds. set 0 to disable timeout.
local inspect = require('util').inspect;
local res, err = cli:get( '/path/to/key' );
-- Wait forever the update of key until that modifiedIndex of key has changed to modifiedIndex + 1
res, err = cli:wait( '/path/to/key', res.body.node.modifiedIndex + 1, 0 );
-- Wait for 10 seconds the update of key until that modifiedIndex of key has changed to modifiedIndex + 2
res, err = cli:wait( '/path/to/key', res.body.node.modifiedIndex + 2, 10 );
print( inspect( { res, err } ) );
local inspect = require('util').inspect;
local res, err = cli:readdir( '/path/to/dir' );
print( inspect( { res, err } ) );
Parameters
recursive
: boolean - get all the contents under a directory.consistent
: boolean - read from master if set a true.
create a directory.
local inspect = require('util').inspect;
local res, err = cli:mkdir( '/path/to/dir', 10 );
print( inspect( { res, err } ) );
create a directory if that directory does not exist.
local inspect = require('util').inspect;
local res, err = cli:mkdirnx( '/path/to/dir', 10 );
print( inspect( { res, err } ) );
local inspect = require('util').inspect;
local res, err = cli:readdir( '/path/to/dir' );
print( inspect( { res, err } ) );
Parameters
recursive
: boolean - remove all the contents under a directory.
local inspect = require('util').inspect;
local res, err = cli:wait( '/path/to/dir' );
print( inspect( { res, err } ) );
Parameters
modifiedIndex
: uint - this argument to use to theprevIndex
query of atomic operation.timeout
: uint - request timeout seconds. set 0 to disable timeout.
push a value into the specified directory.
local inspect = require('util').inspect;
local res, err = cli:mkdir( '/path/to/dir' );
res, err = cli:push( '/path/to/dir', 'val', 10 );
print( inspect( { res, err } ) );
update the TTL
value of specified key with the atomic operation.
local inspect = require('util').inspect;
local res, err = cli:setTTL( '/path/to/key', 10 );
print( inspect( { res, err } ) );
the following client methods to use to get or set the cluster informations.
getting the etcd version info.
local inspect = require('util').inspect;
local res, err = cli:version();
print( inspect( { res, err } ) );
the following client methods to use to get the cluster statistics information.
getting the leader statistics info.
local inspect = require('util').inspect;
local res, err = cli:statsLeader();
print( inspect( { res, err } ) );
getting the self statistics info.
local inspect = require('util').inspect;
local res, err = cli:statsSelf();
print( inspect( { res, err } ) );
getting the store statistics info.
local inspect = require('util').inspect;
local res, err = cli:statsSelf();
print( inspect( { res, err } ) );
getting the list of all machines in the cluster.
local inspect = require('util').inspect;
local res, err = cli:config();
print( inspect( { res, err } ) );
getting the list of all machines in the cluster.
local inspect = require('util').inspect;
local res, err = cli:setConfig();
print( inspect( { res, err } ) );
getting the list of all machines in the cluster.
local inspect = require('util').inspect;
local res, err = cli:machines();
print( inspect( { res, err } ) );
removing the machine from the cluster.
local inspect = require('util').inspect;
-- get list of all machines
local res, err = cli:machines();
local name;
-- lookup the machine name to remove.
name = -- your code...
res, err = cli:removeMachine( name );
print( inspect( { res, err } ) );