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
1.9 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. if [ -z "$RELATION_DATA_FILE" ]; then
  11. err "$FUNCNAME: relation does not seems to be correctly setup."
  12. exit 1
  13. fi
  14. if ! [ -r "$RELATION_DATA_FILE" ]; then
  15. err "$FUNCNAME: can't read relation's data." >&2
  16. exit 1
  17. fi
  18. if [ "$(cat "$RELATION_DATA_FILE" | shyaml get-type)" == "str" ]; then
  19. ## Cached version already there
  20. ## Note that we rely on the fact that when the relations
  21. ## options are changed in the `compose.yml` file, the relation
  22. ## data file is recreated from the values of the
  23. ## `compose.yml`.
  24. info "Previous relation data is still valid."
  25. exit 0
  26. fi
  27. set -e
  28. private_key=$(options-get private-key) || exit 1
  29. target=$(options-get target) || exit 1
  30. ident=$(options-get ident) || exit 1
  31. home=/var/lib/rsync
  32. local_path_key=$home/.ssh
  33. host_path_key="$SERVICE_CONFIGSTORE${local_path_key}"
  34. echo "$private_key" | file_put "$host_path_key/id_rsa"
  35. chmod 600 "$host_path_key/id_rsa"
  36. schedule=$(relation-get schedule) || true
  37. lock_opts=(-D -p 10 -k)
  38. command=(
  39. docker run --rm
  40. -e LABEL_HOSTNAME="$ident"
  41. -v "$RSYNC_CONFIG_DIR:/etc/rsync"
  42. -v "$host_path_key:$local_path_key"
  43. -v "$HOST_DATASTORE:/mnt/source"
  44. -v "$HOST_COMPOSE_YML_FILE:/mnt/source/compose.yml"
  45. --network ${PROJECT_NAME}_default
  46. "$DOCKER_BASE_IMAGE"
  47. /mnt/source "$target"
  48. )
  49. quoted_command=()
  50. for arg in "${command[@]}"; do
  51. quoted_command+=("$(printf "%q" "$arg")")
  52. done
  53. printf "(%s) {%s} %s\n" \
  54. "$schedule" \
  55. "${lock_opts[*]}" \
  56. "${quoted_command[*]}" > "$RELATION_DATA_FILE"