repman-manager.sh.in 2.53 KB
Newer Older
1 2 3 4 5 6 7 8
#!{{ bash_bin }}

#set -e

curl () {
  {{ curl_bin }} -k --silent -H "Accept: application/json" "$@"
}

9 10 11 12
get_token () {
  curl -s -X POST --data '{"username":"{{ username }}","password":"{{ password}}"}' {{ secure_url }}/api/login
}

13 14
wait_database () {
  NAME=$1
15
  for retry in {1..50}; do
16 17 18 19 20 21 22 23
    echo ">> Wait until $NAME databases are ready...";
    CODE=$(curl -H "Authorization: Bearer ${TOKEN}" -o /dev/null -w "%{http_code}" {{ secure_url }}/api/clusters/$NAME/actions/waitdatabases);
    if [ $CODE -eq 504 ]; then
      # We have a timeout try again
      echo ">> [$retry] Timeout on {{ secure_url }}/api/clusters/$NAME/actions/waitdatabases, trying again...";
    else
      if [ $CODE -eq 200 ]; then
        break;
24 25 26 27 28
      else
        if [ $CODE -eq 401 ]; then
          # try again with new token
          TOKEN=$(get_token | {{ jq_bin }} -r '.token')
        fi
29
      fi
30
      echo ">> [$retry] waitdatabases returned code $CODE...";
31
    fi
32
    sleep 30
33 34 35
  done
}

36 37 38 39 40 41 42 43 44 45 46 47 48
activate_proxy () {
  NAME=$1
  URL="{{ secure_url }}/api/clusters/$NAME/settings/actions/switch/database-hearbeat"
  echo ">> Calling $URL...";
  CODE=$(curl -H "Authorization: Bearer ${TOKEN}" -o /dev/null -w "%{http_code}" $URL)
  if [ $CODE -eq 200 ]; then
    return 0;
  else
    echo ">> ERROR: failed to activate proxy: $URL returned code $CODE"
    return 1;
  fi
}

49
TOKEN=$(get_token | {{ jq_bin }} -r '.token')
50 51 52 53 54 55 56 57 58 59 60 61 62 63

# Always reload cluster configuration to apply recent changes
{% for name in cluster_name_list -%}
# reload {{ name }} settings
echo "Reloading settings for {{ name }}..."
curl -H "Authorization: Bearer ${TOKEN}" \
  {{ secure_url }}/api/clusters/{{ name }}/settings/actions/reload

# Start Replication on {{ name }}
if [ ! -f "{{ parameter_dict['bootstrap'] }}/{{ name }}_bootstrapped" ]; then

  wait_database {{ name }}

  echo "Bootstrap replication on {{ name }}..."
64
  TOKEN=$(get_token | {{ jq_bin }} -r '.token')
65 66 67 68

  curl -H "Authorization: Bearer ${TOKEN}" \
    {{ secure_url }}/api/clusters/{{ name }}/actions/replication/cleanup
  CODE=$(curl -H "Authorization: Bearer ${TOKEN}" -o /dev/null -w "%{http_code}" {{ secure_url }}/api/clusters/{{ name }}/actions/replication/bootstrap/master-slave)
69
  SUCCESS=0
70
  if [ $CODE -eq 200 ]; then
71 72 73 74 75 76
    activate_proxy {{ name }}
    if [ $? -eq 0 ]; then
      # Mark boostrap done!
      echo "Cluster {{ name }} replication bootstrapped"
      echo "DO NOT REMOVE THIS FILE" > {{ parameter_dict['bootstrap'] }}/{{ name }}_bootstrapped
    fi
77 78 79 80 81
  else
    echo "ERROR: Failed to bootstrap cluster {{ name }}... http_code $CODE"
  fi
fi

82
{% endfor %}