Skip to content

Commit f1fd84e

Browse files
committed
Implement our own package sync function with exponential backoff.
1 parent 6e4a06e commit f1fd84e

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

entrypoint.sh

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ CLOUDSMITH_REPO="${2}"
77
CLOUDSMITH_USERNAME="${3}"
88
export CLOUDSMITH_API_KEY="${4}"
99

10-
cloudsmith_default_args=(--error-retry-max 30 --republish)
10+
cloudsmith_default_args=(--no-wait-for-sync --republish)
1111

1212
# required to make python 3 work with cloudsmith script
1313
export LC_ALL=C.UTF-8
1414
export LANG=C.UTF-8
1515

16+
# redirect fd 5 to stdout
17+
exec 5>&1
18+
1619
function upload_rpm {
17-
sync=$1
18-
distro=$2
19-
pkg_fullpath=$3
20+
distro=$1
21+
pkg_fullpath=$2
2022
pkg_filename="$(basename "${pkg_fullpath}")"
2123
rev_filename=$(echo "${pkg_filename}" | rev)
2224

@@ -26,40 +28,53 @@ function upload_rpm {
2628
pkg_rel=$(echo "${rev_filename}" | cut -d '.' -f3 | rev)
2729
release_ver="${pkg_rel:2}"
2830

29-
extra_args=("${cloudsmith_default_args[@]}")
30-
if [ "${sync}" == "nosync" ]; then
31-
extra_args+=(--no-wait-for-sync)
32-
fi
33-
34-
cloudsmith push rpm "${extra_args[@]}" "${CLOUDSMITH_REPO}/${distro}/${release_ver}" "${pkg_fullpath}"
31+
output=$(cloudsmith push rpm "${cloudsmith_default_args[@]}" "${CLOUDSMITH_REPO}/${distro}/${release_ver}" "${pkg_fullpath}" | tee /dev/fd/5)
32+
pkg_slug=$(echo "${output}" | grep "Created: ${CLOUDSMITH_REPO}" | awk '{print $2}')
33+
cloudsmith_sync "${pkg_slug}"
3534
}
3635

3736
function upload_deb {
38-
sync=$1
39-
distro=$2
40-
release=$3
41-
pkg_fullpath=$4
42-
43-
extra_args=("${cloudsmith_default_args[@]}")
44-
if [ "${sync}" == "nosync" ]; then
45-
extra_args+=(--no-wait-for-sync)
46-
fi
37+
distro=$1
38+
release=$2
39+
pkg_fullpath=$3
4740

48-
cloudsmith push deb "${extra_args[@]}" "${CLOUDSMITH_REPO}/${distro}/${release}" "${pkg_fullpath}"
41+
output=$(cloudsmith push deb "${cloudsmith_default_args[@]}" "${CLOUDSMITH_REPO}/${distro}/${release}" "${pkg_fullpath}" | tee /dev/fd/5)
42+
pkg_slug=$(echo "${output}" | grep "Created: ${CLOUDSMITH_REPO}" | awk '{print $2}')
43+
cloudsmith_sync "${pkg_slug}"
44+
}
45+
46+
function cloudsmith_sync {
47+
pkg_slug=$1
48+
49+
retry_count=1
50+
timeout=5
51+
backoff=1.2
52+
while true; do
53+
if [ "${retry_count}" -gt 20 ]; then
54+
echo "Exceeded retry attempts for package synchronisation"
55+
exit 1
56+
fi
57+
output=$(cloudsmith status "${pkg_slug}" | tee /dev/fd/5)
58+
if echo "${output}" | grep "Completed / Fully Synchronised"; then
59+
break
60+
fi
61+
sleep ${timeout}
62+
retry_count=$((retry_count+1))
63+
timeout=$(python3 -c "print(round(${timeout}*${backoff}))")
64+
done
4965
}
5066

5167
function cloudsmith_upload {
52-
sync=$1
53-
distro=$2
54-
release=$3
55-
pkg_fullpath=$4
68+
distro=$1
69+
release=$2
70+
pkg_fullpath=$3
5671

5772
if [[ ${distro} =~ centos ]]; then
58-
upload_rpm "${sync}" "centos" "${pkg_fullpath}"
73+
upload_rpm "centos" "${pkg_fullpath}"
5974
elif [[ ${distro} =~ fedora ]]; then
60-
upload_rpm "${sync}" "fedora" "${pkg_fullpath}"
75+
upload_rpm "fedora" "${pkg_fullpath}"
6176
else
62-
upload_deb "${sync}" "${distro}" "${release}" "${pkg_fullpath}"
77+
upload_deb "${distro}" "${release}" "${pkg_fullpath}"
6378
fi
6479
}
6580

@@ -69,21 +84,7 @@ pip3 install --upgrade cloudsmith-cli
6984
while IFS= read -r -d '' path; do
7085
IFS=_ read -r distro release <<< "$(basename "${path}")"
7186

72-
pkgs=()
73-
7487
while IFS= read -r -d '' pkg; do
75-
pkgs+=("${pkg}")
88+
cloudsmith_upload "${distro}" "${release}" "${pkg}"
7689
done < <(find "${path}" -maxdepth 1 -type f -print0)
77-
78-
i=0
79-
last=$((${#pkgs[@]}-1))
80-
for pkg in "${pkgs[@]}"; do
81-
if [ ${i} -eq ${last} ]; then
82-
# wait for final package upload for each distro release to synchronise
83-
cloudsmith_upload "sync" "${distro}" "${release}" "${pkg}"
84-
else
85-
cloudsmith_upload "nosync" "${distro}" "${release}" "${pkg}"
86-
fi
87-
i=$((i+1))
88-
done
8990
done < <(find "${PACKAGE_LOCATION}" -mindepth 1 -maxdepth 1 -type d -print0)

0 commit comments

Comments
 (0)