master
sh 165 lines 4.32 KB
Raw
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: GPL-3.0-or-later
3
4 # -----------------------------------------------------------------------------
5
6 # allow running the jobs by hand
7 [ -z "${NETDATA_BUILD_WITH_DEBUG}" ] && export NETDATA_BUILD_WITH_DEBUG=0
8 [ -z "${NETDATA_INSTALL_PATH}" ] && export NETDATA_INSTALL_PATH="${1-/opt/netdata}"
9 [ -z "${NETDATA_MAKESELF_PATH}" ] && NETDATA_MAKESELF_PATH="$(
10 self=${0}
11 while [ -L "${self}" ]
12 do
13 cd "${self%/*}" || exit 1
14 self=$(readlink "${self}")
15 done
16 cd "${self%/*}" || exit 1
17 cd ../.. || exit 1
18 echo "$(pwd -P)/${self##*/}"
19 )"
20 [ -z "${NETDATA_SOURCE_PATH}" ] && NETDATA_SOURCE_PATH="$(
21 cd "${NETDATA_MAKESELF_PATH}/../.." || exit 1
22 pwd -P
23 )"
24 export NETDATA_MAKESELF_PATH NETDATA_SOURCE_PATH
25 export NULL=
26
27 # make sure the path does not end with /
28 if [ "${NETDATA_INSTALL_PATH:$((${#NETDATA_INSTALL_PATH} - 1)):1}" = "/" ]; then
29 export NETDATA_INSTALL_PATH="${NETDATA_INSTALL_PATH:0:$((${#NETDATA_INSTALL_PATH} - 1))}"
30 fi
31
32 # find the parent directory
33 NETDATA_INSTALL_PARENT="$(dirname "${NETDATA_INSTALL_PATH}")"
34 export NETDATA_INSTALL_PARENT
35
36 # -----------------------------------------------------------------------------
37
38 # bash strict mode
39 set -euo pipefail
40
41 # -----------------------------------------------------------------------------
42
43 cache_path() {
44 local key="${1}"
45 echo "${NETDATA_SOURCE_PATH}/artifacts/cache/${BUILDARCH}/${key}"
46 }
47
48 build_path() {
49 local dir="${1}"
50 local prefix="/build"
51
52 mkdir -p "${prefix}"
53
54 echo "${prefix}/${dir}"
55 }
56
57 fetch() {
58 local dir="${1}" url="${2}" sha256="${3}" key="${4}"
59 local tar
60 tar="$(basename "${2}")"
61 local cache
62 cache="$(cache_path "${key}")"
63 local path
64 path="$(build_path "${dir}")"
65
66 if [ -d "${path}" ]; then
67 rm -rf "${path}"
68 fi
69
70 if [ -d "${cache}/${dir}" ]; then
71 echo "Found cached copy of build directory for ${key}, using it."
72 cp -a "${cache}/${dir}" "${path}"
73 CACHE_HIT=1
74 else
75 echo "No cached copy of build directory for ${key} found, fetching sources instead."
76
77 if [ ! -f "/tmp/${tar}" ]; then
78 run wget -O "/tmp/${tar}" "${url}"
79 fi
80
81 # Check SHA256 of gzip'd tar file (apparently alpine's sha256sum requires
82 # two empty spaces between the checksum and the file's path)
83 set +e
84 echo "${sha256} /tmp/${tar}" | sha256sum --c --status
85 local rc=$?
86 if [ ${rc} -ne 0 ]; then
87 echo >&2 "SHA256 verification of tar file ${tar} failed (rc=${rc})"
88 echo >&2 "expected: ${sha256}, got $(sha256sum "/tmp/${tar}")"
89 exit 1
90 fi
91
92 set -e
93 run tar -axpf "/tmp/${tar}" -C "/build"
94
95 CACHE_HIT=0
96 fi
97
98 run cd "${path}"
99 }
100
101 fetch_git() {
102 local dir="${1}" url="${2}" tag="${3}" key="${4}" fetch_via_checkout="${5:-""}"
103 local cache
104 cache="$(cache_path "${key}")"
105 local path
106 path="$(build_path "${dir}")"
107
108 if [ -d "${path}" ]; then
109 rm -rf "${path}"
110 fi
111
112 if [ -d "${cache}/${dir}" ]; then
113 echo "Found cached copy of build directory for ${key}, using it."
114 cp -a "${cache}/${dir}" "${path}"
115 CACHE_HIT=1
116 else
117 echo "No cached copy of build directory for ${key} found, fetching sources instead."
118 if [ -n "${fetch_via_checkout}" ]; then
119 run git clone "${url}" "${path}"
120 cd "${path}" && run git checkout "${tag}"
121 else
122 run git clone --branch "${tag}" --single-branch --depth 1 "${url}" "${path}"
123 fi
124 CACHE_HIT=0
125 fi
126
127 run cd "${path}"
128 }
129
130 store_cache() {
131 local key="${1}"
132 local dir="${2}"
133 local src
134 src="$(build_path "${dir}")"
135
136 local cache
137 cache="$(cache_path "${key}")"
138
139 if [ "${CACHE_HIT:-0}" -eq 0 ]; then
140 if [ -d "${cache}" ]; then
141 rm -rf "${cache}"
142 fi
143
144 mkdir -p "${cache}"
145
146 cp -a "${src}" "${cache}"
147 chown -R "$(stat -c '%u:%g' "${NETDATA_SOURCE_PATH}")" "${cache}"
148 fi
149 }
150
151 # -----------------------------------------------------------------------------
152
153 # load the functions of the netdata-installer.sh
154 # shellcheck source=packaging/installer/functions.sh
155 . "${NETDATA_SOURCE_PATH}/packaging/installer/functions.sh"
156
157 # -----------------------------------------------------------------------------
158
159 # debug
160 echo "ME=${0}"
161 echo "NETDATA_INSTALL_PARENT=${NETDATA_INSTALL_PARENT}"
162 echo "NETDATA_INSTALL_PATH=${NETDATA_INSTALL_PATH}"
163 echo "NETDATA_MAKESELF_PATH=${NETDATA_MAKESELF_PATH}"
164 echo "NETDATA_SOURCE_PATH=${NETDATA_SOURCE_PATH}"
165 echo "PROCESSORS=$(nproc)"