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.

89 lines
2.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/bin/bash
  2. set -e
  3. exec 3>&1 # make stdout available as fd 3 for the result
  4. exec 1>&2 # redirect all output to stderr for logging
  5. echo "BUILD_ID: $BUILD_ID"
  6. echo "BUILD_NAME: $BUILD_NAME"
  7. echo "BUILD_JOB_NAME: $BUILD_JOB_NAME"
  8. echo "BUILD_PIPELINE_NAME: $BUILD_PIPELINE_NAME"
  9. # shellcheck disable=1090
  10. source "$(dirname $0)/common.sh"
  11. source=$1
  12. if [[ -z "$source" ]]; then
  13. echo "usage: $0 <path/to/source>"
  14. exit 1
  15. fi
  16. # for jq
  17. PATH=/usr/local/bin:$PATH
  18. payload=$(mktemp "$TMPDIR/rclone-resource-request.XXXXXX")
  19. cat > "$payload" <&0
  20. load_config "$payload"
  21. load_files "$payload"
  22. rclone_source_directory=$(jq -r '.params.source // ""' < "$payload")
  23. rclone_destinations=$(jq -r '.params.destination[]? | .dir // ""' < "$payload")
  24. if [[ -z "$rclone_source_directory" ]]; then
  25. echo "invalid source directory (missing source)"
  26. exit 1
  27. fi
  28. if [[ -z "$rclone_destinations" ]]; then
  29. echo "invalid destination (missing destination)"
  30. exit 1
  31. fi
  32. echo "Source directory: $rclone_source_directory"
  33. echo "Destinations:"
  34. jq -c '.params.destination[] | .dir' < "$payload"
  35. local_source_dir="${source}/${rclone_source_directory}"
  36. ls -alh "${local_source_dir}"
  37. echo "Generating SHA256"
  38. (find "${local_source_dir}" -type f -exec sha256sum {} \;) | cut -d ' ' -f1 | xargs echo /tmp/rclone_source.sha256
  39. sha256=$(sha256sum /tmp/rclone_source.sha256 | cut -d' ' -f1)
  40. echo "sha256:${sha256}"
  41. # Source is always the same for each destination
  42. rclone_source="local:${local_source_dir}"
  43. echo "Source: ${rclone_source}"
  44. destinations=$(jq -r '.params.destination[] | @base64' < "$payload")
  45. echo "Using encoded destinations:"
  46. echo " ${destinations}"
  47. for destination in $destinations; do
  48. _jq() {
  49. echo "${destination}" | base64 -d | jq -r "${1}"
  50. }
  51. rclone_destination=$(_jq '.dir')
  52. echo "Destination: $rclone_destination"
  53. rclone_destination_subdir_file=$(_jq '.subdir // ""')
  54. echo "Destination subdir file: ${rclone_destination_subdir_file}"
  55. rclone_destination_subdir=""
  56. if [ -n "$rclone_destination_subdir_file" ]; then
  57. echo "Looking in ${source}/${rclone_destination_subdir_file} for subdir to use"
  58. rclone_destination_subdir=$(head -n 1 < "${source}/${rclone_destination_subdir_file}")
  59. fi
  60. rclone_target="${rclone_destination}/${rclone_destination_subdir}"
  61. echo "Target: ${rclone_target}"
  62. rclone copy "${rclone_source}" "${rclone_target}" --config /opt/rclone/config/.rclone.conf --size-only --progress --stats-one-line --stats=2s
  63. done
  64. jq -n "{
  65. version: {
  66. digest: $( echo -n "sha256:$sha256" | jq -R . )
  67. }
  68. }" >&3