From 338e74d6e6acd845dab8e9d476cf5acf91452c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Thu, 29 Jul 2021 09:13:32 +0200 Subject: [PATCH] add rpc.sh script for easier JSON RPC calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- defaults/main.yml | 1 + tasks/config.yml | 5 +++++ tasks/main.yml | 1 + templates/rpc.sh.j2 | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 tasks/config.yml create mode 100644 templates/rpc.sh.j2 diff --git a/defaults/main.yml b/defaults/main.yml index fceaf99..6aba3e9 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -6,6 +6,7 @@ beacon_node_service_home: 'C:\Users\{{ beacon_node_service_user_name }}' beacon_node_service_path: '{{ beacon_node_service_home }}\{{ beacon_node_service_name }}' beacon_node_service_bin_path: '{{ beacon_node_service_path }}\bin' beacon_node_service_exe: '{{ beacon_node_service_bin_path }}\nimbus_beacon_node.exe' +beacon_node_rpc_script_path: '{{ beacon_node_service_path }}\rpc.sh' beacon_node_repo_url: 'https://github.com/status-im/nimbus-eth2.git' beacon_node_repo_branch: 'stable' diff --git a/tasks/config.yml b/tasks/config.yml new file mode 100644 index 0000000..635867f --- /dev/null +++ b/tasks/config.yml @@ -0,0 +1,5 @@ +--- +- name: Create JSON RPC wrapper script + win_template: + src: 'rpc.sh.j2' + dest: '{{ beacon_node_rpc_script_path }}' diff --git a/tasks/main.yml b/tasks/main.yml index b3e2968..1e081f7 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -3,6 +3,7 @@ - import_tasks: user.yml - import_tasks: install.yml - import_tasks: build.yml +- import_tasks: config.yml - import_tasks: service.yml - import_tasks: validators.yml when: beacon_node_dist_validators_enabled diff --git a/templates/rpc.sh.j2 b/templates/rpc.sh.j2 new file mode 100644 index 0000000..f4dcbe0 --- /dev/null +++ b/templates/rpc.sh.j2 @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# vim: set ft=sh: +set -euo pipefail + +URL="http://localhost:{{ beacon_node_rpc_port }}/" + +METHOD="$1" +shift + +if [[ -z "${METHOD}" ]]; then + echo "No method specified!" >&2 + exit 1 +fi +if [[ -n "${@}" ]]; then + PARAMS=$(printf '%s,' "${@}") + PARAMS="${@%%,}\"" +else + PARAMS='' +fi + +PAYLOAD="{ + \"id\": 1, + \"jsonrpc\": \"2.0\", + \"method\": \"${METHOD}\", + \"params\": [${PARAMS}] +}" + +# The jq script checks if error exists and adjusts exit code. +curl -s -X POST --max-time 10 \ + -H "Content-type:application/json" \ + --data "${PAYLOAD}" \ + "${URL}" | \ + jq -e '., if .error != null then null|halt_error(2) else halt end'