Skip to content

Commit

Permalink
Merge pull request #14 from davide-scola/0.x-add_grenache_announce
Browse files Browse the repository at this point in the history
Announce
  • Loading branch information
davide-scola authored Apr 30, 2018
2 parents 9b6d4e1 + fe8a0de commit bcd5970
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ grenache-get
grenache-put
grenache-keygen
grenache-lookup
grenache-announce

#
# Distribution
Expand Down
44 changes: 41 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Grenache CLI

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
![license](https://img.shields.io/github/license/bitfinexcom/grenache-cli.svg)
![GitHub (pre-)release](https://img.shields.io/github/release/bitfinexcom/grenache-cli/all.svg)
![GitHub (Pre-)Release Date](https://img.shields.io/github/release-date-pre/bitfinexcom/grenache-cli.svg)
![GitHub last commit](https://img.shields.io/github/last-commit/bitfinexcom/grenache-cli.svg)
![GitHub pull requests](https://img.shields.io/github/issues-pr/bitfinexcom/grenache-cli.svg)
![stability-wip](https://img.shields.io/badge/stability-work_in_progress-lightgrey.svg)

<img align="right" width="15%" src="https://github.com/bitfinexcom/grenache/raw/master/logos/logo-square.png" />

* [Introduction](#introduction)
* [Prerequisites](#prerequisites)
Expand All @@ -17,7 +24,7 @@

## Introduction

The **Grenache** **C**ommand **L**ine **I**nterface is a set of tools to use the [grenache-grape](https://github.com/bitfinexcom/grenache-grape) suite directly from your command line. Using this set of tools you can create fancy scripts that communicate directly with the DHT.
The [**Grenache**](https://github.com/bitfinexcom/grenache) **C**ommand **L**ine **I**nterface is a set of tools to use the [grenache-grape](https://github.com/bitfinexcom/grenache-grape) suite directly from your command line. Using this set of tools you can create fancy scripts that communicate directly with the DHT.


## Prerequisites
Expand Down Expand Up @@ -135,7 +142,38 @@ to retrieve the complete options list.

## Announce services

Coming soon...
The `grenache-announce` command announces the given services in order to be stored in the DHT. To announce the [rest:net:util](https://github.com/bitfinexcom/bfx-util-net-js) service on port _31337_, simply run something like this:

```bash
grenache-announce 'rest:net:util,31337'
```

If no services are specified, `grenache-announce` enters the _streaming_ mode, reading the standard input. All comments (marked by a **#** or **;**) and blank lines are ignored. For example, a list of services can be announced using something like this:

```bash
grenache-announce <services.lst
```

An _announce service_ can also be created using a named pipe:

```bash
mkfifo --mode=0622 /run/grape/announce && \
grenache-announce <>/run/grape/announce
```

Storing a service in the DHT now simply requires something like this:

```bash
echo 'rest:net:util,31337' >/run/grape/announce
```

A JSON stream can also be required (perhaps to implement a network service with something like [tcpserver](https://cr.yp.to/ucspi-tcp/tcpserver.html)) using the `-j` switch or its long form `--json`. The JSON document must contain a `data` array in which the _first position_ is the service name while the _second_ is the port number. Another useful option is `-d` or `--delimiter` which allows you to set the delimiter string between the _service name_ and _port number_ when not using JSON mode. See

```bash
grenache-announce --help
```

to retrieve the complete options list.


## Maintainers
Expand Down
7 changes: 4 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ dnl implied. See the License for the specific language governing permissions
dnl and limitations under the License.

AC_PREREQ(2.69)
AC_INIT([grenache-cli], [0.5.0], [[email protected]])
AC_INIT([grenache-cli], [0.6.0], [[email protected]])

AC_SUBST([VERSION], [0.5.0])
AC_SUBST([VERSION], [0.6.0])
AC_SUBST([SB], [`$srcdir/shtool echo -n -e %B`])
AC_SUBST([EB], [`$srcdir/shtool echo -n -e %b`])

Expand Down Expand Up @@ -151,7 +151,7 @@ AC_MSG_CHECKING([whether you want to enable the x86 features check])
if test "x${ax_cv_enable_x86_features}" = 'xyes'; then
AC_MSG_RESULT([yes])

case "${target}" in
case "${host}" in
i?86-* | amd64-* | x86_64-*)
AX_CHECK_X86_FEATURES
;;
Expand Down Expand Up @@ -187,6 +187,7 @@ AC_CONFIG_FILES([ \
src/grenache-get \
src/grenache-keygen \
src/grenache-lookup \
src/grenache-announce \
tests/Makefile \
tests/sign-test-vector-1.sh \
tests/sign-test-vector-2.sh \
Expand Down
3 changes: 2 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ bin_SCRIPTS = \
grenache-put \
grenache-get \
grenache-keygen \
grenache-lookup
grenache-lookup \
grenache-announce

noinst_LTLIBRARIES = \
libed25519.la \
Expand Down
182 changes: 182 additions & 0 deletions src/grenache-announce.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#!/bin/bash
############################################################################
# This file is part of Grenache Command Line Interface. #
# #
# Copyright (C) 2017, 2018 Davide Scola <[email protected]> #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or #
# implied. See the License for the specific language governing permissions #
# and limitations under the License. #
############################################################################

readonly ME="${BASH_SOURCE[0]}"
readonly WHOAMI="$(@READLINK@ -snf "${ME}")"
readonly ARGV=$(@GETOPT@ -o 'd:g:hjp:tV' --long 'delimiter:,grape:,help,json,port:,tls,version' -n "${ME##*/}" -- "$@") || exit 1

TLS=''
PORT=30002
JQ_QUERY=''
EXIT_CODE=1
DELIMITER=','
HOSTNAME='127.0.0.1'
JQ_ARGV=(--unbuffered -r)


# Show program's help.
function show_help {
@CAT@ <<_EOF
Usage:
${ME##*/} [OPTIONS] <service>... -- announce a service on the DHT.
Options:
-d, --delimiter Set the service/port delimiter
-g, --grape Set the Grape hostname
-j, --json Use JSON as data serializer
-p, --port Set the Grape port number
-t, --tls Enable TLS
-h, --help Show this message
-V, --version Show version information
The order of the specified options is not relevant.
_EOF

exit 1
}

# Show program's version.
function show_version {
@CAT@ <<_EOF
${WHOAMI##*/} (@host_cpu@-@host_os@) @PACKAGE_NAME@/@PACKAGE_VERSION@
Copyright (C) 2017, 2018 Davide Scola <@PACKAGE_BUGREPORT@>
This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
_EOF

exit 1
}


while true; do
case "$1" in
-d | --delimiter )
[[ -z "${2}" ]] && {
echo "${ME##*/}: error: empty delimiter." >&2
exit 1
}

DELIMITER="${2}"; shift 2
;;
-g | --grape )
[[ -z "${2}" ]] && {
echo "${ME##*/}: error: empty Grape hostname." >&2
exit 1
}

HOSTNAME="${2}"; shift 2
;;
-h | --help )
show_help; shift 1
;;
-j | --json )
JQ_QUERY='select(.data) | (.data[0] + "\t" + (.data[1] | tostring))'; shift 1
;;
-p | --port )
[[ "${2}" =~ ^[0-9]+$ ]] || {
echo "${ME##*/}: error: invalid Grape port number." >&2
exit 1
}

[[ "$((${2} & 0xFFFF))" -eq 0 ]] && {
echo "${ME##*/}: error: Grape port number must be greater than zero." >&2
exit 1
}

[[ "${2}" -ne "$((${2} & 0xFFFF))" ]] && {
echo "${ME##*/}: error: Grape port number too big." >&2
exit 1
}

PORT="$((${2} & 0xFFFF))"; shift 2
;;
-t | --tls )
TLS='yes'; shift 1
;;
-V | --version )
show_version; shift 1
;;
-- ) shift; break ;;
* ) break ;;
esac
done


[[ -f "${HOME}/.grenache-cli/grenache-cli.conf" ]] || {
echo "${ME##*/}: error: you need to run \`grenache-keygen' first." >&2
exit 1
}

exec {stderr}>&2

[[ x"${GRENACHE_CLI_DEBUG:+set}" != xset ]] && {
exec 2>/dev/null
}

[[ -z "${JQ_QUERY}" ]] && {
JQ_ARGV+=(-R)
JQ_QUERY="$(printf 'split("%s") | (.[0] + "\t" + .[1])' "${DELIMITER}")"
}

while read service port
do
[[ -n "${service}" ]] || {
echo "${ME##*/}: warning: empty service name, skipping." >&"${stderr}"
continue
}

[[ "$((${port} & 0xFFFF))" -gt 0 && "${port}" -eq "$((${port} & 0xFFFF))" ]] || {
echo "${ME##*/}: warning: invalid port number, skipping." >&"${stderr}"
continue
}

NODES="$(@JQ@ -r '.' < <(
@CURL@ -qK "${HOME}/.grenache-cli/grenache-cli.conf" -A '@PACKAGE@/@PACKAGE_VERSION@' "http${TLS:+s}://${HOSTNAME}:${PORT}/announce" < <( \
@JQ@ -cnM \
--arg 'port' "${port}" \
--arg 'service' "${service}" \
--arg 'rid' "$(@UUIDGEN@)" \
'{ "data": [$service, ($port | tonumber)], "rid": $rid }' \
) \
))"

[[ "${NODES}" =~ ^[0-9]+$ ]] || {
echo "${ME##*/}: warning: unable to announce service ${service}: ${NODES}." >&"${stderr}"
continue
}

[[ "${NODES}" -gt 0 ]] && { \
EXIT_CODE=0
} || { \
echo "${ME##*/}: warning: service ${service} has not been indexed." >&"${stderr}"
}
done < <(
@JQ@ "${JQ_ARGV[@]}" "${JQ_QUERY}" < <( \
[[ "${#}" -eq 0 ]] \
&& @SED@ -u 's/\s*[#;].*$//;/^\s*$/d' - \
|| printf '%s\n' "${@}"
)
)

exec {stderr}>&-
exit "${EXIT_CODE}"
2 changes: 1 addition & 1 deletion src/grenache-get.in
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ _EOF
# Show program's version.
function show_version {
@CAT@ <<_EOF
@PACKAGE_NAME@ (@host_cpu@-@host_os@) @PACKAGE_VERSION@
${WHOAMI##*/} (@host_cpu@-@host_os@) @PACKAGE_NAME@/@PACKAGE_VERSION@
Copyright (C) 2017, 2018 Davide Scola <@PACKAGE_BUGREPORT@>
This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
Expand Down
2 changes: 1 addition & 1 deletion src/grenache-keygen.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ _EOF
# Show program's version.
function show_version {
@CAT@ <<_EOF
@PACKAGE_NAME@ (@host_cpu@-@host_os@) @PACKAGE_VERSION@
${WHOAMI##*/} (@host_cpu@-@host_os@) @PACKAGE_NAME@/@PACKAGE_VERSION@
Copyright (C) 2017, 2018 Davide Scola <@PACKAGE_BUGREPORT@>
This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
Expand Down
2 changes: 1 addition & 1 deletion src/grenache-lookup.in
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ _EOF
# Show program's version.
function show_version {
@CAT@ <<_EOF
@PACKAGE_NAME@ (@host_cpu@-@host_os@) @PACKAGE_VERSION@
${WHOAMI##*/} (@host_cpu@-@host_os@) @PACKAGE_NAME@/@PACKAGE_VERSION@
Copyright (C) 2017, 2018 Davide Scola <@PACKAGE_BUGREPORT@>
This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
Expand Down
2 changes: 1 addition & 1 deletion src/grenache-put.in
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ _EOF
# Show program's version.
function show_version {
@CAT@ <<_EOF
@PACKAGE_NAME@ (@host_cpu@-@host_os@) @PACKAGE_VERSION@
${WHOAMI##*/} (@host_cpu@-@host_os@) @PACKAGE_NAME@/@PACKAGE_VERSION@
Copyright (C) 2017, 2018 Davide Scola <@PACKAGE_BUGREPORT@>
This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
Expand Down

0 comments on commit bcd5970

Please sign in to comment.