master
sh 105 lines 2.82 KB
Raw
1 #!/bin/bash
2
3 set -euo pipefail
4
5 url="$(cat packaging/vendor/ibm_mq.url)"
6 sha256="$(cat packaging/vendor/ibm_mq.sha256)"
7 file="${url##*/}"
8 cache_dir="artifacts/cache"
9 tmp_dir="tmp"
10 hash_path="${tmp_dir}/hash"
11 dl_path="${tmp_dir}/${file}"
12 dl_log="${tmp_dir}/dl.log"
13 cache_path="${cache_dir}/${file}"
14 extract_path="${tmp_dir}/ibm_mq"
15 retry_delay=90
16 max_tries=5
17 need_to_fetch=1
18
19 rm -rf "${extract_path}"
20 mkdir -p "${tmp_dir}" "${cache_dir}" "${extract_path}"
21
22 fetch_url() {
23 local success=0
24 local ret
25 local status
26
27 for i in $(seq "${max_tries}") ; do
28 echo "Download attempt ${i}"
29 set +e
30 rm -f "${dl_log}"
31 curl --output "${dl_path}" \
32 --fail \
33 --location \
34 --max-time 300 \
35 --connect-timeout 90 \
36 --remote-time \
37 --write-out "%{http_code}" \
38 "${url}" > "${dl_log}"
39 ret="$?"
40 set -e
41
42 case "${ret}" in
43 0)
44 echo "Download successful."
45 success=1
46 break
47 ;;
48 22|78)
49 status="$(tail -n 1 "${dl_log}")"
50 case "${status}" in
51 403|404)
52 echo "Download failed, got ${status} from remote server."
53 exit 1
54 ;;
55 *) echo "Download failed, got ${status} from remote server, retrying in ${retry_delay} seconds." ;;
56 esac
57 ;;
58 28) echo "Operation timed out, retrying in ${retry_delay} seconds." ;;
59 18|52) echo "Incorrect amount of data returned, retrying in ${retry_delay} seconds." ;;
60 56|92|95) echo "Network communications error, retrying in ${retry_delay} seconds." ;;
61 5|6|7) echo "Failed to connect to remote server, retrying in ${retry_delay} seconds." ;;
62 35|60|83) echo "TLS error connecting to remote server, retrying in ${retry_delay} seconds." ;;
63 *)
64 echo "Unknown error (exit code ${ret}) downloading remote file."
65 exit 1
66 ;;
67 esac
68
69 if [ "${i}" -ne "${max_tries}" ]; then
70 sleep "${retry_delay}"
71 fi
72 done
73
74 if [ "${success}" -ne 1 ]; then
75 echo "Exhausted all retry attempts."
76 exit 1
77 fi
78 }
79
80 hash_valid() {
81 local path="${1}"
82 echo "${sha256} ${path}" > "${hash_path}"
83 if sha256sum -c "${hash_path}"; then
84 return 0
85 else
86 return 1
87 fi
88 }
89
90 if [ -f "${cache_path}" ]; then
91 if ! hash_valid "${cache_path}" ; then
92 rm -f "${cache_path}"
93 else
94 need_to_fetch=0
95 fi
96 fi
97
98 if [ "${need_to_fetch}" -eq 1 ]; then
99 rm -f "${dl_path}"
100 fetch_url
101 hash_valid "${dl_path}"
102 cp "${dl_path}" "${cache_path}"
103 fi
104
105 tar -xvzf "${cache_path}" -C "${extract_path}"