Matrix Docker Ansible eploy
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

56 строки
1.7 KiB

  1. #!/bin/sh
  2. # Talks to Meowlnir's management API.
  3. #
  4. # The API is not published outside the container network, so requests are made from inside the container, which ships with curl.
  5. # The management secret is read out of the live configuration file, so that it lives in exactly one place.
  6. #
  7. # Usage: meowlnir-api <METHOD> <PATH> [JSON_BODY]
  8. # Example: meowlnir-api GET /_meowlnir/v1/bots
  9. #
  10. # Prints the response body, followed by the HTTP status code on its own final line.
  11. set -eu
  12. CONFIG_FILE='{{ matrix_bot_meowlnir_config_path }}/config.yaml'
  13. CONTAINER_NAME='matrix-bot-meowlnir'
  14. API_BASE='http://localhost:{{ matrix_bot_meowlnir_config_meowlnir_port }}'
  15. if [ $# -lt 2 ]; then
  16. echo "Usage: $(basename "$0") <METHOD> <PATH> [JSON_BODY]" >&2
  17. echo "Example: $(basename "$0") GET /_meowlnir/v1/bots" >&2
  18. exit 2
  19. fi
  20. method="$1"
  21. api_path="$2"
  22. body="${3:-}"
  23. # The configuration file is generated by Ansible, so its layout is predictable.
  24. secret="$(awk '$1 == "management_secret:" { print $2; exit }' "$CONFIG_FILE" | sed 's/^"//; s/"$//')"
  25. if [ -z "$secret" ]; then
  26. echo "Could not read management_secret from $CONFIG_FILE" >&2
  27. exit 1
  28. fi
  29. if [ "$secret" = 'disable' ]; then
  30. echo "Meowlnir's management API is disabled (management_secret is set to 'disable')" >&2
  31. exit 1
  32. fi
  33. if [ -n "$body" ]; then
  34. exec {{ devture_systemd_docker_base_host_command_docker }} exec "$CONTAINER_NAME" \
  35. curl -sS -X "$method" \
  36. -H "Authorization: Bearer $secret" \
  37. -H 'Content-Type: application/json' \
  38. -d "$body" \
  39. -w '\n%{http_code}' \
  40. "$API_BASE$api_path"
  41. fi
  42. exec {{ devture_systemd_docker_base_host_command_docker }} exec "$CONTAINER_NAME" \
  43. curl -sS -X "$method" \
  44. -H "Authorization: Bearer $secret" \
  45. -w '\n%{http_code}' \
  46. "$API_BASE$api_path"