|
1 | | -#!/bin/bash |
2 | | -set -euo pipefail |
| 1 | +#!/bin/sh |
| 2 | +set -eu |
3 | 3 |
|
4 | 4 | parse_yaml_array() { |
5 | | - local file="$1" |
6 | | - [[ -f "$file" ]] && grep "^- " "$file" | sed 's/^- //' | sed 's/^["'\'']//' | sed 's/["'\'']$//' |
| 5 | + file="$1" |
| 6 | + [ -f "$file" ] && grep "^- " "$file" | sed 's/^- //' | sed 's/^["'\'']//' | sed 's/["'\'']$//' |
7 | 7 | } |
8 | 8 |
|
9 | 9 | in_array() { |
10 | | - local target="$1" |
| 10 | + target="$1" |
11 | 11 | shift |
12 | | - for item in "$@"; do |
13 | | - [[ "$item" == "$target" ]] && return 0 |
14 | | - done |
15 | | - return 1 |
16 | | -} |
17 | | - |
18 | | -build_json_with_jq() { |
19 | | - local json="{}" |
| 12 | + items="$*" |
20 | 13 |
|
21 | | - for key in "${!patroni_dict[@]}"; do |
22 | | - json=$(echo "$json" | jq --arg k "$key" --arg v "${patroni_dict[$key]}" '. + {($k): $v}') |
| 14 | + for item in $items; do |
| 15 | + [ "$item" = "$target" ] && return 0 |
23 | 16 | done |
24 | | - |
25 | | - if [[ ${#postgresql_dict[@]} -gt 0 ]]; then |
26 | | - local pg_params="{}" |
27 | | - for key in "${!postgresql_dict[@]}"; do |
28 | | - pg_params=$(echo "$pg_params" | jq --arg k "$key" --arg v "${postgresql_dict[$key]}" '. + {($k): $v}') |
29 | | - done |
30 | | - json=$(echo "$json" | jq --argjson params "$pg_params" '. + {postgresql: {parameters: $params}}') |
31 | | - fi |
32 | | - |
33 | | - echo "$json" |
| 17 | + return 1 |
34 | 18 | } |
35 | 19 |
|
36 | | -BOOTSTRAP_FILE="./restart-parameter.yaml" |
37 | | -PATRONI_PARAMS_FILE="./patroni-parameter.yaml" |
| 20 | +SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) |
| 21 | +BOOTSTRAP_FILE="${SCRIPT_DIR}/restart-parameter.yaml" |
| 22 | +PATRONI_PARAMS_FILE="${SCRIPT_DIR}/patroni-parameter.yaml" |
38 | 23 |
|
39 | | -mapfile -t restart_params < <(parse_yaml_array "$BOOTSTRAP_FILE") |
40 | | -mapfile -t patroni_params < <(parse_yaml_array "$PATRONI_PARAMS_FILE") |
| 24 | +restart_params=$(parse_yaml_array "$BOOTSTRAP_FILE") |
| 25 | +patroni_params=$(parse_yaml_array "$PATRONI_PARAMS_FILE") |
41 | 26 |
|
42 | 27 | command="reload" |
43 | 28 | paramName="${1:?missing param name}" |
44 | 29 | paramValue="${2:?missing value}" |
45 | | -paramValue="${paramValue//\'/}" |
| 30 | +paramValue=$(echo "$paramValue" | sed "s/'//g") |
46 | 31 |
|
47 | | -declare -A patroni_dict |
48 | | -declare -A postgresql_dict |
49 | | -if in_array "$paramName" "${patroni_params[@]}"; then |
50 | | - patroni_dict["$paramName"]="$paramValue" |
| 32 | +json_params="{}" |
| 33 | +if in_array "$paramName" "$patroni_params"; then |
| 34 | + json_params=$(echo "$json_params" | jq --arg k "$paramName" --arg v "$paramValue" '. + {($k): $v}') |
51 | 35 | else |
52 | | - postgresql_dict["$paramName"]="$paramValue" |
| 36 | + pg_params=$(echo "{}" | jq --arg k "$paramName" --arg v "$paramValue" '. + {($k): $v}') |
| 37 | + json_params=$(echo "$json_params" | jq --argjson params "$pg_params" '. + {postgresql: {parameters: $params}}') |
53 | 38 | fi |
54 | 39 |
|
55 | | -in_array "$paramName" "${restart_params[@]}" && command="restart" |
| 40 | +in_array "$paramName" "$restart_params" && command="restart" |
56 | 41 |
|
57 | | -json_params=$(build_json_with_jq) |
58 | 42 | curl -s -X PATCH -H "Content-Type: application/json" \ |
59 | 43 | --data "$json_params" \ |
60 | 44 | "http://localhost:8008/config" |
61 | 45 |
|
62 | | -if [[ "$command" == "restart" ]]; then |
| 46 | +if [ "$command" = "restart" ]; then |
63 | 47 | curl -s -X POST "http://localhost:8008/restart" |
64 | 48 | else |
65 | 49 | curl -s -X POST "http://localhost:8008/reload" |
|
0 commit comments