|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +CURL_TIMEOUT=5 |
| 6 | +CURL_CMD=(curl -fsS -m "$CURL_TIMEOUT") |
| 7 | + |
| 8 | +ENVS_AND_DSO_URLS=( |
| 9 | + DevNet https://docs.dev.global.canton.network.sync.global/dso |
| 10 | + TestNet https://docs.test.global.canton.network.sync.global/dso |
| 11 | + MainNet https://docs.global.canton.network.sync.global/dso |
| 12 | +) |
| 13 | + |
| 14 | +SCRIPTS_DIR=$(dirname "$0") |
| 15 | +CONFIGS_DIR="$SCRIPTS_DIR/../configs" |
| 16 | + |
| 17 | +IS_LINUX=$([[ "$(uname -s)" == "Linux" ]] && echo true || echo false) |
| 18 | + |
| 19 | + |
| 20 | +fetch_dso_data() { |
| 21 | + local dso_response; dso_response=$( |
| 22 | + # "${CURL_CMD[@]}" -w '%{header_json}' "$dso_url" # NOTE: header_json is supported by curl >= 7.83.0, below is a workaround for older versions |
| 23 | + local response; response=$("${CURL_CMD[@]}" -i "$dso_url") || exit 1 |
| 24 | + local response_body; response_body=$(echo "$response" | sed '1,/^\r*$/d') |
| 25 | + local response_header; response_header=$(echo "$response" | sed '/^\r*$/,$d') |
| 26 | + local header_last_modified; header_last_modified=$(echo "$response_header" | grep '^last-modified:' | sed 's/^last-modified: //' | jq -nR '{"last-modified": [inputs | rtrimstr("\r")]}') |
| 27 | + echo "$response_body$header_last_modified" |
| 28 | + ) || { echo "ERROR: Unable to fetch DSO from $dso_url" >&2; return 1; } |
| 29 | + |
| 30 | + [[ $(echo "$dso_response" | jq -s length) -eq 2 ]] || |
| 31 | + { echo "ERROR: Unable to parse the response from $dso_url" >&2; return 1; } |
| 32 | + |
| 33 | + echo "$dso_response" |
| 34 | +} |
| 35 | + |
| 36 | +fetch_dso_configs() { |
| 37 | + local env=$1 |
| 38 | + local dso_url=$2 |
| 39 | + |
| 40 | + local configs_dir_env="$CONFIGS_DIR/$env" |
| 41 | + mkdir -p "$configs_dir_env" |
| 42 | + |
| 43 | + local dso_rules_file="$configs_dir_env/dso-rules.json" |
| 44 | + local amulet_rules_file="$configs_dir_env/amulet-rules.json" |
| 45 | + |
| 46 | + local dso_response; dso_response=$(fetch_dso_data "$dso_url") || return 1 |
| 47 | + |
| 48 | + local dso_data; dso_data=$(echo "$dso_response" | jq -s '.[0]') |
| 49 | + local header_json; header_json=$(echo "$dso_response" | jq -s '.[1]') |
| 50 | + local last_modified; last_modified=$(echo "$header_json" | jq -r '."last-modified" // empty | .[]') |
| 51 | + |
| 52 | + if [[ -n $last_modified ]]; then |
| 53 | + local last_modified_seconds modified_seconds_ago |
| 54 | + |
| 55 | + "$IS_LINUX" && |
| 56 | + last_modified_seconds=$(date -d "$last_modified" +%s) || |
| 57 | + last_modified_seconds=$(date -j -f "%a, %d %b %Y %T %Z" "$last_modified" +%s) |
| 58 | + |
| 59 | + modified_seconds_ago=$(( $(date +%s) - last_modified_seconds )) |
| 60 | + fi |
| 61 | + |
| 62 | + echo "INFO: DSO data for $env fetched successfully. Last modified: $last_modified ($modified_seconds_ago seconds ago)" |
| 63 | + |
| 64 | + echo "$dso_data" | jq -S '.dso_rules' > "$dso_rules_file" |
| 65 | + echo "$dso_data" | jq -S '.amulet_rules' > "$amulet_rules_file" |
| 66 | +} |
| 67 | + |
| 68 | +main() { |
| 69 | + for ((i = 0; i < ${#ENVS_AND_DSO_URLS[@]}; i += 2)); do |
| 70 | + env=${ENVS_AND_DSO_URLS[i]} |
| 71 | + dso_url=${ENVS_AND_DSO_URLS[i + 1]} |
| 72 | + fetch_dso_configs "$env" "$dso_url" |
| 73 | + done |
| 74 | +} |
| 75 | + |
| 76 | +main "$@" |
0 commit comments