48 lines
1.5 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. . lib/common
  9. set -e
  10. schedule=$(relation-get schedule)
  11. if ! echo "$schedule" | egrep '^\s*(([0-9/,*-]+\s+){4,4}[0-9/,*-]+|@[a-z]+)\s*$' >/dev/null 2>&1; then
  12. err "Unrecognized schedule '$schedule'."
  13. exit 1
  14. fi
  15. private_key=$(options-get private-key) || exit 1
  16. target=$(options-get target) || exit 1
  17. ident=$(options-get ident) || exit 1
  18. home=/var/lib/rsync
  19. local_path_key=$home/.ssh
  20. host_path_key="$SERVICE_CONFIGSTORE${local_path_key}"
  21. echo "$private_key" | file_put "$host_path_key/id_rsa"
  22. chmod 600 "$host_path_key/id_rsa"
  23. label="${SERVICE_NAME}"
  24. DST=$CONFIGSTORE/$TARGET_CHARM_NAME/etc/cron/$label
  25. ## Warning: using '\' in heredoc will be removed in the final cron file, which
  26. ## is totally wanted: cron does not support multilines.
  27. file_put "$DST" <<EOF
  28. $schedule root lock $label -v -D -p 10 -k -c "\
  29. docker run --rm \
  30. -e LABEL_HOSTNAME=\"$ident\" \
  31. -v \"$RSYNC_CONFIG_DIR:/etc/rsync\" \
  32. -v \"$host_path_key:$local_path_key\" \
  33. -v \"$HOST_DATASTORE:/mnt/source\" \
  34. --network ${PROJECT_NAME}_default \
  35. \"$DOCKER_BASE_IMAGE\" \
  36. /mnt/source \"$target\"" 2>&1 | ts '\%F \%T' >> /var/log/cron/${label}_script.log
  37. EOF
  38. chmod +x "$DST"