Skip to content

Commit

Permalink
New bashlet client/etcd.
Browse files Browse the repository at this point in the history
Rudimentary interaction with #coreos #etcd.
  • Loading branch information
Roberto Reale committed Jan 3, 2017
1 parent b06fa9c commit 439500f
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions lib/client/etcd
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/bash


################################################################################
#
# | | | |
# |---.,---.,---.|---.| ,---.|--- ,---.
# | |,---|`---.| || |---'| `---.
# `---'`---^`---'` '`---'`---'`---'`---'
#
#
# Bashlets -- A modular extensible toolbox for Bash
#
# Copyright (c) 2014-6 Roberto Reale
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
################################################################################


BASHLETS_CLIENT_ETCD_HOST=localhost
BASHLETS_CLIENT_ETCD_PORT=2379


function __bashlets_client_etcd_get_version()
{
local system="$1" json val

# inspired by code at https://gist.github.com/mway/9841302/
# TODO: remove dependency on curl and Python

json="$(curl -sL "http://$BASHLETS_CLIENT_ETCD_HOST:$BASHLETS_CLIENT_ETCD_PORT/version")"
[ -n "$json" ] && val="$(python -c "import json; o=json.loads('$json'); print(o['$system'] if o.get('errorCode') is None else '')")"
[ -n "$val" ] && echo $val
}


#@method
function bashlets_client_etcd_get_server_version()
{
__bashlets_client_etcd_get_version etcdserver
}

#@method
function bashlets_client_etcd_get_cluster_version()
{
__bashlets_client_etcd_get_version etcdcluster
}

#@method
function bashlets_client_etcd_get()
{
# inspired by code at https://gist.github.com/mway/9841302/
# TODO: remove dependency on curl and Python

local key="$1" json val

json="$(curl -sL "http://$BASHLETS_CLIENT_ETCD_HOST:$BASHLETS_CLIENT_ETCD_PORT/v2/keys/$key")"
[ -n "$json" ] && val="$(python -c "import json; o=json.loads('$json'); print(o['node']['value'] if o.get('errorCode') is None else '')")"
[ -n "$val" ] && echo $val
}

#@method
function bashlets_client_etcd_set()
{
# TODO: check errors

local key="$1" value="$2"

curl -sL "http://$BASHLETS_CLIENT_ETCD_HOST:$BASHLETS_CLIENT_ETCD_PORT/v2/keys/$key" -XPUT -d value="$value" > /dev/null
}


# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh

0 comments on commit 439500f

Please sign in to comment.