forked from DataDog/datadog-process-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prebuild-datadog-agent.sh
executable file
·265 lines (238 loc) · 8.79 KB
/
prebuild-datadog-agent.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env sh
# Okay, here it goes: The process agent depends on the datadog agent, which:
# - for some modules (EBPF) uses go:generate which needs to be run to even build the dependency.
# - for some other modules generate EBPF code, which we need but is not part of the regular go build
# This file makes sure those prebuild steps (go:generate and ebpf files) get built and installed.
set -e
DIR="${BASH_SOURCE%/*}"
if [ ! -d "$DIR" ]; then DIR="$PWD"; fi
if [ "$DIR" = "." ]; then DIR="$PWD"; fi
printUsage() {
cat << USAGE
Usage: ./prebuild-datadog-agent.sh --generate|--clean|--shell|--install-go|--help
This script run the prebuild steps for the datadog agent dependency. Those steps are not automatically invoked by the go build.
-g/--generate -- Generate the prebuild files by running inside a docker container
--generate-no-docker -- Generate the prebuild files without spinning pu a separate docker container (in the current environment)
-c/--clean -- Clean the generated files
-i/--install-go -- Install the prebuild go files
--install-ebpf -- Install the prebuild ebpf files to ./ebpf-object-files for further processing
--install-ebpf-from-dep -- Install the prebuild ebpf files from the go sum dependency. Requires root, because typically those files are generated as root user.
--install-ebpf-root -- Install the prebuild ebpf files to ./ebpf-object-files-root for use in the process-agent binary
-t/--test -- Run tests on the upstream datadog agent
-s/--shell -- Launch into a shell in which the prebuild files are built
-h/--help -- Print this help page
USAGE
}
ACTION=""
while [ $# -gt 0 ]; do
case $1 in
-g|--generate)
ACTION="generate"
shift
;;
--generate-no-docker)
ACTION="generate-no-docker"
shift
;;
-i|--install-go)
ACTION="install-go"
shift
;;
--install-ebpf)
ACTION="install-ebpf"
shift
;;
--install-ebpf-from-dep)
ACTION="install-ebpf-from-dep"
shift
;;
--install-ebpf-root)
ACTION="install-ebpf-root"
shift
;;
-s|--shell)
ACTION="shell"
shift
;;
-t|--test)
ACTION="test"
shift
;;
-c|--clean)
ACTION="clean"
shift
;;
-h|--help)
ACTION="help"
shift
;;
*)
ACTION="$1"
shift
break
;;
esac
done
# First make sure all dependencies are downloaded
echo "Downloading go files"
go mod download
ALL_ARTIFACTS_DIR="$DIR/prebuild_artifacts"
GO_MOD_DEPENDENCY_DIR=$(go list -f '{{ .Dir }}' -m github.com/DataDog/datadog-agent)
# Check if the dependency was replaced
if [ "$(go list -f '{{ .Replace }}' -m github.com/DataDog/datadog-agent)" = "<nil>" ]; then
DEPENDENCY_VERSION=$(go list -f '{{ .Version }}' -m github.com/DataDog/datadog-agent)
REPO_PATH="https://github.com/DataDog/datadog-agent"
SOURCE_DIR="$ALL_ARTIFACTS_DIR/checkout/$DEPENDENCY_VERSION"
else
GO_MOD_DEPENDENCY_DIR=$(go list -f '{{ .Replace.Dir }}' -m github.com/DataDog/datadog-agent)
if [ -d "$GO_MOD_DEPENDENCY_DIR/.git" ]; then
echo "Running the data prebuild for a local dependency $DEPENDENCY_VERSION. Be aware that generate will not automatically pickup changes. Be sure to run -clean whenever the generated code would change."
# The dependency is a local git repo. No need to pull or pick a version
DEPENDENCY_VERSION="local"
SOURCE_DIR=$GO_MOD_DEPENDENCY_DIR
elif [ -d "$GO_MOD_DEPENDENCY_DIR" ]; then
echo "Running for replacement git remote"
DEPENDENCY_VERSION=$(go list -f '{{ .Replace.Version }}' -m github.com/DataDog/datadog-agent)
REPO_PATH="https://$(go list -f '{{ .Replace.Path }}' -m github.com/DataDog/datadog-agent)"
SOURCE_DIR="$ALL_ARTIFACTS_DIR/checkout/$DEPENDENCY_VERSION"
else
echo "Unknown path in go.mod: '$GO_MOD_DEPENDENCY_DIR'"
exit 1
fi
fi
DEPENDENCY_ARTIFACTS_DIR="$ALL_ARTIFACTS_DIR/artifacts/$DEPENDENCY_VERSION"
DOCKER_IMAGE=quay.io/stackstate/datadog_build_system-probe_x64:c3847b73
checkoutSource() {
if [ ! -d "$SOURCE_DIR" ]; then
echo "datadog-agent was not cloned at ${SOURCE_DIR}, cloning"
mkdir -p "$ALL_ARTIFACTS_DIR/checkout"
GIT_VERSION=$(echo "$DEPENDENCY_VERSION" | cut -d'-' -f 3)
(cd "$ALL_ARTIFACTS_DIR/checkout" &&
git clone "$REPO_PATH" "$DEPENDENCY_VERSION" &&
cd "$DEPENDENCY_VERSION" &&
git checkout "$GIT_VERSION")
fi
}
runPrebuildNoDocker() {
set -x
checkoutSource
SOURCEDIR="$SOURCE_DIR" \
OUTPUTDIR="$DEPENDENCY_ARTIFACTS_DIR" \
WORKDIR="$DIR/datadog-agent-workdir" \
$DIR/prebuild-datadog-agent-scripts/run-datadog-agent-prebuild.sh
set +x
}
runPrebuildInDocker() {
set -x
checkoutSource
docker run \
--platform linux/amd64 \
-e "OUTPUT_USER_ID=$(id -u "${USER}")" \
-e "OUTPUT_GROUP_ID=$(id -g "${USER}")" \
-v "$SOURCE_DIR":/source-datadog-agent \
-e SOURCEDIR="/source-datadog-agent" \
-v "$DEPENDENCY_ARTIFACTS_DIR":/output \
-e OUTPUTDIR="/output" \
-e WORKDIR="/workdir-datadog-agent" \
-v "$DIR/prebuild-datadog-agent-scripts":/scripts \
-v /proc:/host/proc \
-e HOST_PROC=/host/proc \
-v /sys:/host/sys \
-e HOST_SYS=/host/sys \
-v /etc:/host/etc:ro \
-e HOST_ETC=/host/etc \
-v /var/run/docker.sock:/var/run/docker.sock \
--network host \
--privileged \
--pid host \
--cap-add all \
"$@"
set +x
}
if [ "$ACTION" = "generate" ]; then
echo "Generating code"
if [ -d "$DEPENDENCY_ARTIFACTS_DIR/gofiles" ]; then
echo "Prebuild artifacts were already generated under $DEPENDENCY_ARTIFACTS_DIR. Skipping. To regenerate first run with --clean"
exit 0
fi
mkdir -p "$DEPENDENCY_ARTIFACTS_DIR"
runPrebuildInDocker "$DOCKER_IMAGE" /scripts/run-datadog-agent-prebuild.sh
elif [ "$ACTION" = "generate-no-docker" ]; then
echo "Generating code in the current environment"
if [ -d "$DEPENDENCY_ARTIFACTS_DIR" ]; then
echo "Prebuild artifacts were already generated under $DEPENDENCY_ARTIFACTS_DIR. Skipping. To regenerate first run with --clean"
exit 0
fi
mkdir -p "$DEPENDENCY_ARTIFACTS_DIR"
runPrebuildNoDocker
elif [ "$ACTION" = "install-go" ]; then
echo "Installing go files to $GO_MOD_DEPENDENCY_DIR"
if [ ! -d "$DEPENDENCY_ARTIFACTS_DIR/gofiles" ]; then
echo "No generated files found at $DEPENDENCY_ARTIFACTS_DIR/gofiles, please run --generate first"
exit 1
fi
set -x
chmod -R ug+w "$GO_MOD_DEPENDENCY_DIR"
if [ "$(cp -v -a -u "$DEPENDENCY_ARTIFACTS_DIR/gofiles"/* "$GO_MOD_DEPENDENCY_DIR")" != "" ]; then
echo "Nuking GOCACHE after changing go files, because we messed with the 'mod' directory (which gets cached)"
rm -rf "$(go env GOCACHE)"
fi
set +x
elif [ "$ACTION" = "install-ebpf" ]; then
echo "Installing ebpf files"
if [ ! -d "$DEPENDENCY_ARTIFACTS_DIR/ebpf" ]; then
echo "No generated files found at $DEPENDENCY_ARTIFACTS_DIR/ebpf, please run --generate first"
exit 1
fi
set -x
mkdir -p $DIR/ebpf-object-files
rm -rf $DIR/ebpf-object-files/*
cp -v -a "$DEPENDENCY_ARTIFACTS_DIR/ebpf"/* "$DIR/ebpf-object-files/"
set +x
elif [ "$ACTION" = "install-ebpf-from-dep" ]; then
echo "Installing ebpf files from the dependent go module"
if [ ! -d "$GO_MOD_DEPENDENCY_DIR/pkg/ebpf/bytecode/build" ]; then
echo "No generated files found at $GO_MOD_DEPENDENCY_DIR/pkg/ebpf/bytecode/build, please make sure the files are generated"
exit 1
fi
set -x
mkdir -p $DIR/ebpf-object-files
rm -rf $DIR/ebpf-object-files/*
sudo cp -v -a "$GO_MOD_DEPENDENCY_DIR/pkg/ebpf/bytecode/build"/* "$DIR/ebpf-object-files/"
CURRENT_USER=$(whoami)
sudo chown "$CURRENT_USER:$CURRENT_USER" -R "$DIR/ebpf-object-files/"
set +x
elif [ "$ACTION" = "install-ebpf-root" ]; then
echo "Installing ebpf files as root"
if [ ! -d "$DEPENDENCY_ARTIFACTS_DIR/ebpf" ]; then
echo "No generated files found at $DEPENDENCY_ARTIFACTS_DIR/ebpf, please run --generate first"
exit 1
fi
set -x
mkdir -p $DIR/ebpf-object-files-root
cp -v -a "$DEPENDENCY_ARTIFACTS_DIR/ebpf"/* "$DIR/ebpf-object-files-root/"
sudo chown -R root:root "$DIR/ebpf-object-files-root/"
set +x
elif [ "$ACTION" = "clean" ]; then
echo "Cleaning prebuild files from $PREBUILD_ARTIFACTS_DIR"
rm -rf "$ALL_ARTIFACTS_DIR"
rm -rf ebpf-object-files
if [ -d "$DIR/ebpf-object-files-root/" ]; then
sudo rm -rf ebpf-object-files-root
fi
elif [ "$ACTION" = "test" ]; then
echo "Running tests"
runPrebuildInDocker "$DOCKER_IMAGE" /scripts/run-datadog-agent-test.sh
elif [ "$ACTION" = "shell" ]; then
echo "Launching generate shell"
echo "From the shell it is possible to run the scripts in the /scripts directory to regenerate artifacts or run tests."
runPrebuildInDocker -it "$DOCKER_IMAGE" /bin/bash
elif [ -z "$ACTION" ]; then
echo "No argument was passed"
printUsage
exit 1
else
echo "Illegal argument $ACTION"
printUsage
exit 1
fi