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.

212 lines
6.8 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
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. rclone_link_destination=$(jq -r '.params.linkDestination // ""' < "$payload")
  25. rclone_link_destination_subdir_file=$(jq -r '.params.linkDestinationSubDir // ""' < "$payload")
  26. if [[ -z "$rclone_source_directory" ]]; then
  27. echo "invalid source directory (missing source)"
  28. exit 1
  29. fi
  30. if [[ -z "$rclone_destinations" ]]; then
  31. echo "invalid destination (missing destination)"
  32. exit 1
  33. fi
  34. echo "Source directory: $rclone_source_directory"
  35. echo "Destinations:"
  36. jq -c '.params.destination[] | .dir' < "$payload"
  37. local_source_dir="${source}/${rclone_source_directory}"
  38. ls -alh "${local_source_dir}"
  39. echo "Generating SHA256"
  40. sha256_file="${TMPDIR}/rclone_source.sha256"
  41. touch "${sha256_file}"
  42. (find "${local_source_dir}" -type f -exec sha256sum {} \;) | cut -d ' ' -f1 | xargs echo >> "${sha256_file}"
  43. sha256=$(sha256sum "${sha256_file}" | cut -d' ' -f1)
  44. echo "sha256:${sha256}"
  45. # Source is always the same for each destination
  46. rclone_source="${local_source_dir}"
  47. echo "Source: ${rclone_source}"
  48. destinations=$(jq -r '.params.destination[] | @base64' < "$payload")
  49. echo "Using encoded destinations:"
  50. echo " ${destinations}"
  51. file_links=()
  52. for destination in $destinations; do
  53. _jq() {
  54. echo "${destination}" | base64 -d | jq -r "${1}"
  55. }
  56. rclone_destination=$(_jq '.dir')
  57. rclone_destination_subdir_file=$(_jq '.subdir // ""')
  58. rclone_command=$(_jq '.command // "copy"')
  59. rclone_args=$(_jq '.args // [] | join(" ")')
  60. rclone_dedupe=$(_jq '.dedupe // "false"')
  61. rclone_dedupe_mode=$(_jq '.dedupeMode // "newest"')
  62. rclone_link=$(_jq '.link // "false"')
  63. rclone_link_find_filter=$(_jq '.linkFilter // "-maxdepth 1 -type f"')
  64. rclone_allow_failure=$(_jq '.allowFailure // "false"')
  65. rclone_sleep=$(_jq '.sleep // ""')
  66. echo "Destination: $rclone_destination"
  67. echo "Destination subdir file: ${rclone_destination_subdir_file}"
  68. echo "Command: ${rclone_command}"
  69. echo "Additonal args: ${rclone_args}"
  70. echo "Run dedupe: ${rclone_dedupe}"
  71. echo "Dedupe mode: ${rclone_dedupe_mode}"
  72. echo "Generate link: ${rclone_link}"
  73. echo "Link find filter: ${rclone_link_find_filter}"
  74. echo "Allow failure: ${rclone_allow_failure}"
  75. if [[ "${rclone_allow_failure}" = "true" ]]; then
  76. set +e
  77. fi
  78. rclone_destination_subdir=""
  79. if [ -n "$rclone_destination_subdir_file" ]; then
  80. echo "Looking in ${source}/${rclone_destination_subdir_file} for subdir to use"
  81. rclone_destination_subdir=$(head -n 1 < "${source}/${rclone_destination_subdir_file}")
  82. fi
  83. if [[ -z "${rclone_destination_subdir}" ]]; then
  84. rclone_target="${rclone_destination}"
  85. else
  86. rclone_target="${rclone_destination}/${rclone_destination_subdir}"
  87. fi
  88. echo "Target: ${rclone_target}"
  89. # shellcheck disable=2086
  90. rclone ${rclone_command} "${rclone_source}" "${rclone_target}" --size-only --progress --stats=2s ${rclone_args}
  91. copy_rc=$?
  92. if [[ -n "${rclone_sleep}" ]]; then
  93. echo "Sleeping for ${rclone_sleep}"
  94. sleep ${rclone_sleep}
  95. fi
  96. if [[ "${rclone_allow_failure}" = "true" && $copy_rc -gt 0 ]]; then
  97. echo "Rclone copy failed. Switching to next destination."
  98. continue
  99. fi
  100. if [[ "$rclone_dedupe" == "true" ]]; then
  101. echo "Running Dedupe for: ${rclone_target}"
  102. # shellcheck disable=2086
  103. rclone dedupe --dedupe-mode "${rclone_dedupe_mode}" "${rclone_target}" --progress --stats=2s ${rclone_args}
  104. dedupe_rc=$?
  105. if [[ -n "${rclone_sleep}" ]]; then
  106. echo "Sleeping for ${rclone_sleep}"
  107. sleep ${rclone_sleep}
  108. fi
  109. if [[ "${rclone_allow_failure}" = "true" && $dedupe_rc -gt 0 ]]; then
  110. echo "Rclone dedupe failed. Switching to next destination."
  111. continue
  112. fi
  113. fi
  114. if [[ "${rclone_link}" == "true" ]]; then
  115. # shellcheck disable=2086
  116. filesToLink=$(find "${rclone_source}" ${rclone_link_find_filter})
  117. echo "Files to link:"
  118. echo "${filesToLink}"
  119. for file in ${filesToLink}; do
  120. remote_file=${file#"$rclone_source/"}
  121. rclone_target_file="${rclone_target}/${remote_file}"
  122. echo "Generating Link for: ${rclone_target_file}"
  123. file_link=$(rclone link "${rclone_target_file}")
  124. link_rc=$?
  125. if [[ -n "${rclone_sleep}" ]]; then
  126. echo "Sleeping for ${rclone_sleep}"
  127. sleep ${rclone_sleep}
  128. fi
  129. if [[ "${rclone_allow_failure}" = "true" && $link_rc -gt 0 ]]; then
  130. echo "Rclone linking failed. Switching to next target."
  131. continue
  132. fi
  133. file_links+=( "${rclone_target_file}" "${file_link}" )
  134. done
  135. fi
  136. if [[ "${rclone_allow_failure}" = "true" ]]; then
  137. set -e
  138. fi
  139. echo "rclone job complete for ${rclone_source} -> ${rclone_target}"
  140. done
  141. # Arithmetic expression to check size of array
  142. if (( ${#file_links[@]} )); then
  143. echo "File Links:" "${file_links[@]}"
  144. # shellcheck disable=2068
  145. metadata=$(printf '{"name": "%s", "value": "%s"},' ${file_links[@]})
  146. metadata="[${metadata::-1}]"
  147. links_json=$( echo -n "{\"links\":${metadata}}" | jq -r . )
  148. links_json_file="${TMPDIR}/.rclone_links.json"
  149. echo "$links_json" > "${links_json_file}"
  150. if [[ -n "${rclone_link_destination}" ]]; then
  151. rclone_link_destination_subdir=""
  152. if [ -n "$rclone_link_destination_subdir_file" ]; then
  153. echo "Looking in ${source}/${rclone_link_destination_subdir_file} for subdir to use"
  154. rclone_link_destination_subdir=$(head -n 1 < "${source}/${rclone_link_destination_subdir_file}")
  155. fi
  156. if [[ -z "${rclone_link_destination_subdir}" ]]; then
  157. rclone_link_target="${rclone_link_destination}"
  158. else
  159. rclone_link_target="${rclone_link_destination}/${rclone_link_destination_subdir}"
  160. fi
  161. echo "Link Target: ${rclone_link_target}"
  162. rclone copy ${links_json_file} ${rclone_link_target} --progress --stats=2s
  163. if [[ -n "${rclone_sleep}" ]]; then
  164. echo "Sleeping for ${rclone_sleep}"
  165. sleep ${rclone_sleep}
  166. fi
  167. fi
  168. cat "${links_json_file}"
  169. jq -n "{
  170. version: {
  171. build: $( echo -n "${BUILD_ID}" | jq -R .),
  172. digest: $( echo -n "sha256:${sha256}" | jq -R . )
  173. },
  174. metadata: ${metadata}
  175. }" >&3
  176. else
  177. jq -n "{
  178. version: {
  179. build: $( echo -n "${BUILD_ID}" | jq -R .),
  180. digest: $( echo -n "sha256:${sha256}" | jq -R . )
  181. }
  182. }" >&3
  183. fi