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.

114 lines
2.4 KiB

  1. #!/bin/bash
  2. ## compose: no-hooks
  3. if [ -z "$SERVICE_DATASTORE" ]; then
  4. echo "This script is meant to be run through 'compose' to work properly." >&2
  5. exit 1
  6. fi
  7. . $CHARM_PATH/lib/common
  8. version=0.1
  9. usage="$exname [-h|--help]"
  10. help="
  11. USAGE:
  12. $usage
  13. DESCRIPTION:
  14. Request an invite code.
  15. EXAMPLES:
  16. $exname
  17. "
  18. dbname=
  19. neutralize=
  20. while [ "$1" ]; do
  21. case "$1" in
  22. "--help"|"-h")
  23. print_help >&2
  24. exit 0
  25. ;;
  26. --*|-*)
  27. err "Unexpected optional argument '$1'"
  28. print_usage >&2
  29. exit 1
  30. ;;
  31. *)
  32. err "Unexpected positional argument '$1'"
  33. print_usage >&2
  34. exit 1
  35. ;;
  36. esac
  37. shift
  38. done
  39. set -e
  40. . "$PDS_ENV_FILE"
  41. curl_opts=()
  42. service_def=$(get_compose_service_def "$SERVICE_NAME")
  43. containers="$(get_running_containers_for_service "$SERVICE_NAME")"
  44. if [ -z "$containers" ]; then
  45. err "No containers running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
  46. exit 1
  47. fi
  48. if [ "$(echo "$containers" | wc -l)" -gt 1 ]; then
  49. err "More than 1 container running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
  50. echo " Please contact administrator to fix this issue." >&2
  51. exit 1
  52. fi
  53. container="$(echo "$containers" | head -n 1)"
  54. container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 3000 4) || {
  55. err "Please ensure that $DARKYELLOW$service$NORMAL is running before using '$exname'."
  56. exit 1
  57. }
  58. container_ip=${container_network_ip##*:}
  59. container_network=${container_network_ip%%:*}
  60. DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
  61. cmd=(
  62. docker run -i --rm --network "$container_network"
  63. "$DEFAULT_CURL_IMAGE"
  64. --fail \
  65. --silent \
  66. --show-error \
  67. --request POST \
  68. --user "admin:${PDS_ADMIN_PASSWORD}" \
  69. --header "Content-Type: application/json" \
  70. --data '{"useCount": 1}' \
  71. "http://${container_ip}:3000/xrpc/com.atproto.server.createInviteCode"
  72. )
  73. ## XXXvlab: contains password, left only for advanced debug
  74. #echo "COMMAND: ${cmd[@]}" >&2
  75. if ! out=$("${cmd[@]}"); then
  76. err "Failed to request an invite code."
  77. echo " $out" | prefix " $GRAY|$NORMAL " >&2
  78. exit 1
  79. fi
  80. e "$out" | jq -r '.code' || {
  81. err "Failed to parse invite code from response."
  82. echo " $out" | prefix " $GRAY|$NORMAL " >&2
  83. exit 1
  84. }