fork 0k-charms
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.5 KiB

  1. # -*- mode: shell-script -*-
  2. CONFIG_DIR="$SERVICE_CONFIGSTORE"/etc/etherpad
  3. ORIG_CONFIG_FILE="$CONFIG_DIR/settings.json.orig"
  4. etherpad:ensure-orig-json-exists() {
  5. if ! [ -e "$ORIG_CONFIG_FILE" ]; then
  6. mkdir -p "${ORIG_CONFIG_FILE%/*}" || return 1
  7. ## Remove comments from container file and copy it in
  8. ## configstore
  9. if out=$(docker run --rm --entrypoint bash "$DOCKER_BASE_IMAGE" \
  10. -c "npm install mjson && node_modules/.bin/mjson.js < settings.json"); then
  11. ## XXXvlab: output from mjson.js is still minified in my local install
  12. ## lets keep this code if necessary later.
  13. # if ! echo "$out" | jq . > "$ORIG_CONFIG_FILE"; then
  14. # err "Jq failed with output from container."
  15. # rm -fv "$ORIG_CONFIG_FILE"
  16. # return 1
  17. # fi
  18. echo "$out" > "$ORIG_CONFIG_FILE"
  19. else
  20. err "Could not out-source clean 'setting.json' from container."
  21. return 1
  22. fi
  23. fi
  24. }
  25. etherpad:api:init() {
  26. container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 9001 4) || {
  27. echo " Please ensure that ${DARKYELLOW}${SERVICE_NAME}${NORMAL} is running before using '$exname'." >&2
  28. return 1
  29. }
  30. container_ip=${container_network_ip##*:}
  31. container_network=${container_network_ip%%:*}
  32. export ETHERPAD_DOCKER_OPTS=(--network "$container_network")
  33. if ! API_KEY=$(cat "$SERVICE_DATASTORE/var/lib/etherpad/APIKEY.txt"); then
  34. err "Can't find API KEY for ${DARKYELLOW}${SERVICE_NAME}${NORMAL}."
  35. return 1
  36. fi
  37. export DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  38. export ETHERPAD_HOST="http://${container_ip}:9001"
  39. if ! out=$(etherpad:curl api/openapi.json); then
  40. err "Couldn't get API description from ${DARKYELLOW}${SERVICE_NAME}${NORMAL}."
  41. return 1
  42. fi
  43. if ! api_version=$(e "$out" | jq -r '.info.version'); then
  44. err "Couldn't get version information from ${DARKYELLOW}${SERVICE_NAME}${NORMAL} API's description."
  45. # echo "$out" | prefix " | " >&2
  46. return 1
  47. fi
  48. export ETHERPAD_API_BASE_PATH="api/${api_version}"
  49. }
  50. etherpad:curl() {
  51. local path="$1" cmd
  52. shift
  53. cmd=(
  54. docker run --rm "${ETHERPAD_DOCKER_OPTS[@]}"
  55. "$DEFAULT_CURL_IMAGE"
  56. -sS
  57. "$@"
  58. "${ETHERPAD_HOST}/$path"
  59. )
  60. # debug "Launching: ${cmd[@]}"
  61. "${cmd[@]}"
  62. }
  63. etherpad:api() {
  64. local method="$1" code out
  65. shift
  66. if [ -z "$ETHERPAD_API_BASE_PATH" ]; then
  67. etherpad:api:init
  68. fi
  69. if ! out=$(
  70. etherpad:curl "${ETHERPAD_API_BASE_PATH}/$method" -X POST -d "apikey=${API_KEY}" "$@"
  71. ); then
  72. err "Couldn't query API ${WHITE}$method${NORMAL} from ${DARKYELLOW}${SERVICE_NAME}${NORMAL}."
  73. return 1
  74. fi
  75. if ! code=$(e "$out" | jq -r ".code"); then
  76. err "Unexpected response format from call to ${WHITE}$method${NORMAL} " \
  77. "API endpoint for ${DARKYELLOW}${SERVICE_NAME}${NORMAL}."
  78. # echo "$out" | prefix " | " >&2
  79. exit 1
  80. fi
  81. if [[ "$code" != "0" ]]; then
  82. if ! message=$(e "$out" | jq -r ".message"); then
  83. err "Unexpected response format from call to ${WHITE}$method${NORMAL} " \
  84. "API endpoint for ${DARKYELLOW}${SERVICE_NAME}${NORMAL}."
  85. # echo "$out" | prefix " | " >&2
  86. exit 1
  87. fi
  88. err "${WHITE}$method${NORMAL}: $message"
  89. # echo "$out" | prefix " | " >&2
  90. exit 1
  91. fi
  92. e "$out" | jq '.data'
  93. }