-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from davide-scola/0.x-add_grenache_announce
Announce
- Loading branch information
Showing
9 changed files
with
234 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ grenache-get | |
grenache-put | ||
grenache-keygen | ||
grenache-lookup | ||
grenache-announce | ||
|
||
# | ||
# Distribution | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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`]) | ||
|
||
|
@@ -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 | ||
;; | ||
|
@@ -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 \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters