From 2aed29d4df017af98c390a359ce35ccf23e53709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Tue, 13 Feb 2024 22:03:15 +0100 Subject: [PATCH] docker: drop RPC flag and add REST API flags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to removal of RPC API support in: https://github.com/waku-org/ nwaku/pull/2416 Signed-off-by: Jakub SokoĊ‚owski --- README.md | 8 +++++++ defaults/main.yml | 6 +++-- tasks/consul.yml | 16 ++++++-------- tasks/container.yml | 6 ++--- tasks/query.yml | 22 +++++++------------ templates/api.sh.j2 | 33 ++++++++++++++++++++++++++++ templates/docker-compose.yml.j2 | 12 ++++++---- templates/rpc.sh.j2 | 39 --------------------------------- 8 files changed, 71 insertions(+), 71 deletions(-) create mode 100644 templates/api.sh.j2 delete mode 100644 templates/rpc.sh.j2 diff --git a/README.md b/README.md index 6cc8727..9141475 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,14 @@ The configured domain URL should be in the format 'enrtree://@', for nim_waku_dns_disc_enabled: true nim_waku_dns_disc_url: 'enrtree://AOFTICU2XWDULNLZGRMQS4RIZPAZEHYMV4FYHAPW563HNRAOERP7C@test.waku.nodes.status.im' ``` +REST API is avalable to inspect the node and use protocols: +```yaml +nim_waku_rest_enabled: true +nim_waku_rest_addr: '127.0.0.1' +nim_waku_rest_port: 8645 +nim_waku_rest_apis_enabled: ['admin', 'private'] +``` +For full docs see [API docs page](https://waku-org.github.io/waku-rest-api/). # Usage diff --git a/defaults/main.yml b/defaults/main.yml index 2f006cc..ea94a9a 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -47,8 +47,10 @@ nim_waku_disc_v5_bootstrap_nodes: [] nim_waku_dns4_domain_name: '{{ ansible_hostname }}' nim_waku_public_address: '{{ ansible_host }}' -nim_waku_rpc_tcp_addr: '127.0.0.1' -nim_waku_rpc_tcp_port: 8545 +nim_waku_rest_enabled: true +nim_waku_rest_addr: '127.0.0.1' +nim_waku_rest_port: 8645 +nim_waku_rest_apis_enabled: ['admin'] # admin, private nim_waku_websock_port: 8000 nim_waku_disc_v5_port: 9000 nim_waku_p2p_tcp_port: 30303 diff --git a/tasks/consul.yml b/tasks/consul.yml index 51f995c..268b7a4 100644 --- a/tasks/consul.yml +++ b/tasks/consul.yml @@ -40,17 +40,15 @@ # Single-threated nature blocking responses due to DB I/O. disabled: true - - id: '{{ nim_waku_cont_name }}-rpc' - name: '{{ nim_waku_consul_service_name }}-rpc' - port: '{{ nim_waku_rpc_tcp_port }}' + - id: '{{ nim_waku_cont_name }}-rest' + name: '{{ nim_waku_consul_service_name }}-rest' + port: '{{ nim_waku_rest_port }}' address: '{{ ansible_local.wireguard.vpn_ip }}' - tags: ['env:{{ env }}', 'stage:{{ stage }}', 'nim', 'waku', 'rpc'] + tags: ['env:{{ env }}', 'stage:{{ stage }}', 'nim', 'waku', 'rest'] checks: - - id: '{{ nim_waku_cont_name }}-rpc-health' - type: 'script' - script: '{{ nim_waku_cont_vol }}/rpc.sh get_waku_v2_debug_v1_info' - # Single-threated nature blocking responses due to DB I/O. - disabled: true + - id: '{{ nim_waku_cont_name }}-rest-health' + type: 'http' + http: 'http://localhost:{{ nim_waku_rest_port }}/debug/v1/version' - name: Define Websocket service when: nim_waku_websocket_enabled diff --git a/tasks/container.yml b/tasks/container.yml index d1917ea..985edba 100644 --- a/tasks/container.yml +++ b/tasks/container.yml @@ -7,10 +7,10 @@ group: docker mode: 0775 -- name: Create script for calling RPC endpoint +- name: Create script for calling REST API template: - src: 'rpc.sh.j2' - dest: '{{ nim_waku_cont_vol }}/rpc.sh' + src: 'api.sh.j2' + dest: '{{ nim_waku_cont_vol }}/api.sh' owner: dockremap group: docker mode: 0755 diff --git a/tasks/query.yml b/tasks/query.yml index c75e155..3252fa1 100644 --- a/tasks/query.yml +++ b/tasks/query.yml @@ -1,18 +1,12 @@ --- -- name: 'Wait for RPC port to be available' +- name: 'Wait for REST port to be available' wait_for: - port: '{{ nim_waku_rpc_tcp_port }}' + port: '{{ nim_waku_rest_port }}' delay: 5 - name: 'Extract address of the node for Consul' uri: - url: 'http://localhost:{{ nim_waku_rpc_tcp_port }}/' - method: POST - body: - method: 'get_waku_v2_debug_v1_info' - params: [] - jsonrpc: '2.0' - id: 1 + url: 'http://localhost:{{ nim_waku_rest_port }}/debug/v1/info' status_code: 200 body_format: json register: waku_info @@ -20,25 +14,25 @@ - name: Verify we got the node address assert: - that: 'waku_info.json.result.listenAddresses is defined' + that: 'waku_info.json.listenAddresses is defined' fail_msg: 'Did not receive Nim-Waku node address!' quiet: true - name: Extract LibP2P TCP address from list set_fact: nim_waku_libp2p_enr_uri: |- - {{ waku_info.json.result.enrUri }} + {{ waku_info.json.enrUri }} nim_waku_libp2p_multiaddr: |- - {{ waku_info.json.result.listenAddresses + {{ waku_info.json.listenAddresses | reject("contains", "/ws") | first }} - name: Extract LibP2P WS address from list when: nim_waku_websocket_enabled or nim_waku_websockify_enabled set_fact: nim_waku_libp2p_websocket: |- - {{ waku_info.json.result.listenAddresses + {{ waku_info.json.listenAddresses | select("contains", "/ws") | first }} -- name: Extract peer ID from RPC response +- name: Extract peer ID from API response set_fact: nim_waku_peer_id: '{{ nim_waku_libp2p_multiaddr.split("/") | last }}' diff --git a/templates/api.sh.j2 b/templates/api.sh.j2 new file mode 100644 index 0000000..33bada5 --- /dev/null +++ b/templates/api.sh.j2 @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# vim: set ft=sh: + +TIMEOUT="${TIMEOUT:-5}" +URL="http://localhost:{{ nim_waku_rest_port }}" + +# List known useful endpoints when called without arguments. +read -r -d '' ENDPOINTS << EOF +/debug/v1/info +/debug/v1/version +/admin/v1/peers +/health +EOF + +if [[ $# == 0 ]]; then + ENDPOINT=$( + echo "${ENDPOINTS}" | fzf \ + --preview='{{ nim_waku_cont_vol }}/api.sh ${1}' \ + --preview-window='bottom,80%' + ) + if [[ -z "${ENDPOINT}" ]]; then + exit 0 + fi +elif [[ $# > 1 ]]; then + echo "Too many arguments!" >&2 + exit 1 +else + ENDPOINT="${1}" +fi + +# Not all endpoints return JSON, that's the readon for jq magic. +curl --silent --max-time "${TIMEOUT}" --show-error --fail-with-body "${URL}${ENDPOINT}" \ + | jq --color-output --raw-input --raw-output '. as $line | try (fromjson) catch $line' diff --git a/templates/docker-compose.yml.j2 b/templates/docker-compose.yml.j2 index a0d211b..c7144c8 100644 --- a/templates/docker-compose.yml.j2 +++ b/templates/docker-compose.yml.j2 @@ -24,7 +24,7 @@ services: - '{{ nim_waku_websocket_ssl_dir }}:{{ nim_waku_websocket_ssl_dir | mandatory }}' {% endif %} ports: - - '{{ nim_waku_rpc_tcp_addr }}:{{ nim_waku_rpc_tcp_port }}:{{ nim_waku_rpc_tcp_port }}/tcp' + - '{{ nim_waku_rest_addr }}:{{ nim_waku_rest_port }}:{{ nim_waku_rest_port }}/tcp' - '{{ nim_waku_p2p_tcp_port }}:{{ nim_waku_p2p_tcp_port }}/tcp' - '{{ nim_waku_p2p_udp_port }}:{{ nim_waku_p2p_udp_port }}/udp' - '{{ nim_waku_metrics_port }}:{{ nim_waku_metrics_port }}/tcp' @@ -44,7 +44,6 @@ services: {% for protocol in nim_waku_protocols_enabled %} --{{ protocol }}=true {% endfor %} - --rpc-admin=true --peer-persistence={{ nim_waku_peer_persistence | to_json }} --keep-alive={{ nim_waku_keep_alive | to_json }} --max-connections={{ nim_waku_p2p_max_connections }} @@ -103,9 +102,14 @@ services: {% endif %} --nat=extip:{{ nim_waku_public_address }} --log-level={{ nim_waku_log_level | upper }} - --rpc-port={{ nim_waku_rpc_tcp_port }} - --rpc-address=0.0.0.0 --tcp-port={{ nim_waku_p2p_tcp_port }} + --rest={{ nim_waku_rest_enabled }} +{% if nim_waku_rest_enabled %} + --rest-address=0.0.0.0 + --rest-port={{ nim_waku_rest_port }} + --rest-admin={{ "admin" in nim_waku_rest_apis_enabled }} + --rest-private={{ "private" in nim_waku_rest_apis_enabled }} +{% endif %} --metrics-server={{ nim_waku_metrics_enabled }} --metrics-server-port={{ nim_waku_metrics_port }} --metrics-server-address=0.0.0.0 diff --git a/templates/rpc.sh.j2 b/templates/rpc.sh.j2 deleted file mode 100644 index e34eef5..0000000 --- a/templates/rpc.sh.j2 +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env bash -# vim: set ft=sh: - -URL="http://localhost:{{ nim_waku_rpc_tcp_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 are a nested array because of a bug in nim-json-rpc. - # https://github.com/status-im/nim-json-rpc/issues/90 - PARAMS_STR="[\"${PARAMS_STR%%\",\"}\"]" -else - PARAMS_STR='' -fi - -PAYLOAD="{ - \"id\": 1, - \"jsonrpc\": \"2.0\", - \"method\": \"${METHOD}\", - \"params\": [${PARAMS_STR}] -}" - -# The jq script checks if error exists and adjusts exit code. -curl --request POST \ - --silent \ - --max-time 5 \ - --show-error \ - --fail-with-body \ - --header 'Content-type:application/json' \ - --data "${PAYLOAD}" \ - "${URL}" | \ - jq -e '., if .error != null then null|halt_error(2) else halt end'