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.

72 lines
2.1 KiB

  1. #!/bin/bash
  2. ## When writing relation script, remember:
  3. ## - they should be idempotents
  4. ## - they can be launched while the dockers is already up
  5. ## - they are launched from the host
  6. ## - the target of the link is launched first, and get a chance to ``relation-set``
  7. ## - both side of the scripts get to use ``relation-get``.
  8. ## - use actions of other side for other side's business logic
  9. . lib/common
  10. ##
  11. ## This script has to replace `exclude-dbs` options to match definition
  12. ## from `schedule-command` interface that is awaited by the target side
  13. ## in the `pre_deploy` script.
  14. ##
  15. if [ -z "$RELATION_DATA_FILE" ]; then
  16. err "$FUNCNAME: relation does not seems to be correctly setup."
  17. exit 1
  18. fi
  19. if ! [ -r "$RELATION_DATA_FILE" ]; then
  20. err "$FUNCNAME: can't read relation's data." >&2
  21. exit 1
  22. fi
  23. if [ "$(cat "$RELATION_DATA_FILE" | shyaml get-type)" == "str" ]; then
  24. ## Cached version already there
  25. ## Note that we rely on the fact that when the relations
  26. ## options are changed in the `compose.yml` file, the relation
  27. ## data file is recreated from the values of the
  28. ## `compose.yml`.
  29. info "Previous relation data is still valid."
  30. exit 0
  31. fi
  32. schedule=$(relation-get schedule) || true
  33. exclude_dbs=$(relation-get exclude-dbs 2>/dev/null) || true
  34. exclude_dbs=$(echo "$exclude_dbs" | shyaml get-values 2>/dev/null |
  35. nspc) || true
  36. lock_opts=(-D -p 10)
  37. command=(
  38. docker run --rm
  39. -e PGHOST=${SERVICE_NAME}
  40. --network ${PROJECT_NAME}_default
  41. -e exclude_dbs="$exclude_dbs"
  42. -v "$LOCAL_DB_PASSFILE":/root/.pgpass
  43. -v "$HOST_CHARM_STORE/${CHARM_REL_PATH#${CHARM_STORE}/}/resources/bin/pg-backup:/usr/sbin/pg-backup"
  44. -v "$SERVICE_DATASTORE/var/backups/pg:/var/backups/pg"
  45. --entrypoint pg-backup
  46. "$DOCKER_BASE_IMAGE"
  47. )
  48. quoted_command=()
  49. for arg in "${command[@]}"; do
  50. quoted_command+=("$(printf "%q" "$arg")")
  51. done
  52. printf "(%s) {%s} %s\n" \
  53. "$schedule" \
  54. "${lock_opts[*]}" \
  55. "${quoted_command[*]}" > "$RELATION_DATA_FILE"
  56. echo "data: '$(cat "$RELATION_DATA_FILE")'" >&2