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.

217 lines
7.0 KiB

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