From 18382d60d199dd852dec40801373a087684f8e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Thu, 25 Mar 2021 13:56:04 +0100 Subject: [PATCH] use rpc.sh script for RPC healthcheck MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- tasks/consul.yml | 20 ++------------------ templates/rpc.sh.j2 | 16 +++++++++------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/tasks/consul.yml b/tasks/consul.yml index f4bc5ae..926489b 100644 --- a/tasks/consul.yml +++ b/tasks/consul.yml @@ -1,20 +1,4 @@ --- -- name: Geth | Create sync check script - copy: - dest: '{{ geth_sync_check_script }}' - mode: 0755 - content: | - #!/usr/bin/env bash - set -e - echo -n "Geth synced: " - RESP=$(curl -s -X POST \ - --connect-timeout 5 \ - -H 'Content-type:application/json' \ - --data '{"jsonrpc":"2.0","method":"eth_syncing","id":1}' \ - http://localhost:{{ geth_rpc_port }}/) - echo "${RESP}" | jq -e ".result == false" \ - || ( echo "${RESP}" | jq . ; exit 1 ) - - name: Geth | Create Consul service definition include_role: name=consul-service vars: @@ -43,8 +27,8 @@ stage: '{{ stage }}' checks: - id: '{{ geth_cont_name }}-rpc-status' - type: script - script: '{{ geth_sync_check_script }}' + type: 'script' + script: '{{ geth_service_path }}/rpc.sh eth_syncing' timeout: '5s' - name: '{{ geth_cont_name }}-ws' diff --git a/templates/rpc.sh.j2 b/templates/rpc.sh.j2 index 60d1b51..de3c500 100644 --- a/templates/rpc.sh.j2 +++ b/templates/rpc.sh.j2 @@ -1,30 +1,32 @@ #!/usr/bin/env bash +# vim: set ft=sh: +set -euo pipefail URL="http://localhost:{{ geth_rpc_port }}/" METHOD="$1" shift -PARAMS=("$@") if [[ -z "${METHOD}" ]]; then echo "No method specified!" >&2 exit 1 fi -if [[ -n "${PARAMS}" ]]; then - PARAMS_STR=$(printf '%s\",\"' "${PARAMS[@]}") - PARAMS_STR="\"${PARAMS_STR%%\",\"}\"" +if [[ -n "${@}" ]]; then + PARAMS=$(printf '%s\",\"' "${@}") + PARAMS="\"${PARAMS_STR%%\",\"}\"" else - PARAMS_STR='' + PARAMS='' fi PAYLOAD="{ \"id\": 1, \"jsonrpc\": \"2.0\", \"method\": \"${METHOD}\", - \"params\": [${PARAMS_STR}] + \"params\": [${PARAMS}] }" curl -s -X POST \ -H "Content-type:application/json" \ --data "${PAYLOAD}" \ - "${URL}" | jq . + "${URL}" | \ + jq -e '., if .error != null then null|halt_error(2) else halt end'