forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.sh
executable file
·245 lines (222 loc) · 7.14 KB
/
common.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env bash
set -euo pipefail
WORKSPACE=${WORKSPACE:-"$(pwd)"}
BIN="${WORKSPACE}/bin"
platform_type="$(uname)"
platform_type_lowercase=$(echo "$platform_type" | tr '[:upper:]' '[:lower:]')
arch_type="$(uname -m)"
GITHUB_PR_TRIGGER_COMMENT=${GITHUB_PR_TRIGGER_COMMENT:-""}
ONLY_DOCS=${ONLY_DOCS:-"true"}
runLibbeat="$(buildkite-agent meta-data get runLibbeat --default ${runLibbeat:-"false"})"
runMetricbeat="$(buildkite-agent meta-data get runMetricbeat --default ${runMetricbeat:-"false"})"
runLibBeatArmTest="$(buildkite-agent meta-data get runLibbeat --default ${runLibbeat:-"false"})"
runMetricbeatMacOsTests="$(buildkite-agent meta-data get runMetricbeatMacOsTests --default ${runMetricbeatMacOsTests:-"false"})"
metricbeat_changeset=(
"^metricbeat/.*"
"^go.mod"
"^pytest.ini"
"^dev-tools/.*"
"^libbeat/.*"
"^testing/.*"
)
oss_changeset=(
"^go.mod"
"^pytest.ini"
"^dev-tools/.*"
"^libbeat/.*"
"^testing/.*"
)
ci_changeset=(
"^.buildkite/.*"
)
go_mod_changeset=(
"^go.mod"
)
docs_changeset=(
".*\\.(asciidoc|md)"
"deploy/kubernetes/.*-kubernetes\\.yaml"
)
packaging_changeset=(
"^dev-tools/packaging/.*"
".go-version"
)
with_docker_compose() {
local version=$1
echo "Setting up the Docker-compose environment..."
create_workspace
retry 3 curl -sSL -o ${BIN}/docker-compose "https://github.com/docker/compose/releases/download/${version}/docker-compose-${platform_type_lowercase}-${arch_type}"
chmod +x ${BIN}/docker-compose
export PATH="${BIN}:${PATH}"
docker-compose version
}
create_workspace() {
if [[ ! -d "${BIN}" ]]; then
mkdir -p "${BIN}"
fi
}
add_bin_path() {
echo "Adding PATH to the environment variables..."
create_workspace
export PATH="${BIN}:${PATH}"
}
check_platform_architeture() {
case "${arch_type}" in
"x86_64")
go_arch_type="amd64"
;;
"aarch64")
go_arch_type="arm64"
;;
"arm64")
go_arch_type="arm64"
;;
*)
echo "The current platform/OS type is unsupported yet"
;;
esac
}
with_mage() {
local install_packages=(
"github.com/magefile/mage"
"github.com/elastic/go-licenser"
"golang.org/x/tools/cmd/goimports"
"github.com/jstemmer/go-junit-report"
"gotest.tools/gotestsum"
)
create_workspace
for pkg in "${install_packages[@]}"; do
go install "${pkg}@latest"
done
}
with_go() {
echo "Setting up the Go environment..."
create_workspace
check_platform_architeture
retry 5 curl -sL -o "${BIN}/gvm" "https://github.com/andrewkroh/gvm/releases/download/${SETUP_GVM_VERSION}/gvm-${platform_type_lowercase}-${go_arch_type}"
chmod +x "${BIN}/gvm"
eval "$(gvm $GO_VERSION)"
go version
which go
local go_path="$(go env GOPATH):$(go env GOPATH)/bin"
export PATH="${go_path}:${PATH}"
}
with_python() {
if [ "${platform_type}" == "Linux" ]; then
#sudo command doesn't work at the "pre-command" hook because of another user environment (root with strange permissions)
sudo apt-get update
sudo apt-get install -y python3-pip python3-venv
elif [ "${platform_type}" == "Darwin" ]; then
brew update
pip3 install virtualenv
ulimit -Sn 10000
fi
}
with_dependencies() {
if [ "${platform_type}" == "Linux" ]; then
#sudo command doesn't work at the "pre-command" hook because of another user environment (root with strange permissions)
sudo apt-get update
sudo apt-get install -y libsystemd-dev libpcap-dev
elif [ "${platform_type}" == "Darwin" ]; then
pip3 install libpcap
fi
}
retry() {
local retries=$1
shift
local count=0
until "$@"; do
exit=$?
wait=$((2 ** count))
count=$((count + 1))
if [ $count -lt "$retries" ]; then
>&2 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
sleep $wait
else
>&2 echo "Retry $count/$retries exited $exit, no more retries left."
return $exit
fi
done
return 0
}
are_paths_changed() {
local patterns=("${@}")
local changelist=()
for pattern in "${patterns[@]}"; do
changed_files=($(git diff --name-only HEAD@{1} HEAD | grep -E "$pattern"))
if [ "${#changed_files[@]}" -gt 0 ]; then
changelist+=("${changed_files[@]}")
fi
done
if [ "${#changelist[@]}" -gt 0 ]; then
echo "Files changed:"
echo "${changelist[*]}"
return 0
else
echo "No files changed within specified changeset:"
echo "${patterns[*]}"
return 1
fi
}
are_changed_only_paths() {
local patterns=("${@}")
local changelist=()
local changed_files=$(git diff --name-only HEAD@{1} HEAD)
if [ -z "$changed_files" ] || grep -qE "$(IFS=\|; echo "${patterns[*]}")" <<< "$changed_files"; then
return 0
fi
return 1
}
are_conditions_met_mandatory_tests() {
if are_paths_changed "${metricbeat_changeset[@]}" || are_paths_changed "${oss_changeset[@]}" || are_paths_changed "${ci_changeset[@]}" ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L3-L12
if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-metricbeat" ]]; then
if [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "/test metricbeat" || "${GITHUB_PR_LABELS}" =~ Metricbeat || "${runMetricbeat}" == "true" ]]; then
return 0
fi
elif [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-libbeat" ]]; then
if [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "/test libbeat" || "${GITHUB_PR_LABELS}" =~ libbeat || "${runLibbeat}" == "true" ]]; then
return 0
fi
fi
fi
return 1
}
are_conditions_met_libbeat_arm_tests() {
if are_conditions_met_mandatory_tests; then #from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L145-L171
if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-libbeat" ]]; then
if [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "/test libbeat for arm" || "${GITHUB_PR_LABELS}" =~ arm || "${runLibBeatArmTest}" == "true" ]]; then
return 0
fi
fi
fi
return 1
}
are_conditions_met_metricbeat_macos_tests() {
if [[ "${runMetricbeatMacOsTests}" == true ]] || [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "/test metricbeat for macos" ]] || [[ "${GITHUB_PR_LABELS}" =~ macOS ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L3-L12
return 0
fi
return 1
}
are_conditions_met_packaging() {
if are_paths_changed "${metricbeat_changeset[@]}" || are_paths_changed "${oss_changeset[@]}" || [[ "${BUILDKITE_TAG}" == "" ]] || [[ "${BUILDKITE_PULL_REQUEST}" != "" ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L101-L103
return 0
fi
return 1
}
config_git() {
if [ -z "$(git config --get user.email)" ]; then
git config --global user.email "[email protected]"
git config --global user.name "beatsmachine"
fi
}
if ! are_changed_only_paths "${docs_changeset[@]}" ; then
ONLY_DOCS="false"
echo "Changes include files outside the docs_changeset vairiabe. ONLY_DOCS=$ONLY_DOCS."
else
echo "All changes are related to DOCS. ONLY_DOCS=$ONLY_DOCS."
fi
if are_paths_changed "${go_mod_changeset[@]}" ; then
GO_MOD_CHANGES="true"
fi
if are_paths_changed "${packaging_changeset[@]}" ; then
PACKAGING_CHANGES="true"
fi