forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlocal.sh
More file actions
executable file
·399 lines (316 loc) · 13 KB
/
local.sh
File metadata and controls
executable file
·399 lines (316 loc) · 13 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/bin/bash
set -e
# We want to produce a final binary on a branded path, but it is convenient to run unit tests and the like on
# the original unbranded paths. Hence we allow for both.
if [[ "${RELOCATED}" != "true" ]]; then
SRC_PATH="/go/src/github.com/DataDog/datadog-agent"
export AGENT_GITHUB_ORG=DataDog
export AGENT_REPO_NAME=datadog-agent
else
SRC_PATH="/go/src/github.com/StackVista/stackstate-agent"
export AGENT_GITHUB_ORG=StackVista
export AGENT_REPO_NAME=stackstate-agent
fi
export PYTHON_RUNTIMES="3"
export BRANDED=true
export OMNIBUS_FORCE_PACKAGES=true
WHAT=$1
if [ -z "${WHAT}" ]; then
echo "Usage: $0 [shell | all | prep | deps_deb | build_binaries | build_cluster_agent | build_agent | build_deb | build_agent_image | build_cluster_agent_image | build_images | unit_tests | copy_rtloader | lint | cmd | up_to_cluster_agent | up_to_agent | up_to_build_deb | up_to_unit_tests]"
exit 1
fi
WHAT=$(echo "${WHAT}" | tr '[:lower:]' '[:upper:]')
if [ "${WHAT}" = "SHELL" ]; then
if [ ! -f "${PWD}/.bash_history" ]; then
touch ${PWD}/.bash_history
fi
# Detect Docker socket and CLI for building images from inside the container
DOCKER_MOUNTS=""
if [ -S /var/run/docker.sock ]; then
DOCKER_MOUNTS="-v /var/run/docker.sock:/var/run/docker.sock:z"
fi
DOCKER_CLI="$(which docker 2>/dev/null || true)"
if [ -n "$DOCKER_CLI" ]; then
DOCKER_MOUNTS="$DOCKER_MOUNTS -v ${DOCKER_CLI}:/usr/local/bin/docker:z"
fi
docker run --rm -it \
--network host \
-v ${PWD}:${PWD}:z \
-v ${PWD}/.bash_history:/root/.bash_history:z \
$DOCKER_MOUNTS \
-e MAJOR_VERSION="3" \
-e USER_ID="$(id -u)" -e GROUP_ID="$(id -g)" \
-e CI_PROJECT_DIR=${PWD} \
-e GITLAB_PACKAGE_REGISTRY_PYPI_SIMPLE_URL="${GITLAB_PACKAGE_REGISTRY_PYPI_SIMPLE_URL}" \
-e GITLAB_PACKAGE_REGISTRY_USER="${GITLAB_PACKAGE_REGISTRY_USER}" \
-e GITLAB_PACKAGE_REGISTRY_TOKEN="${GITLAB_PACKAGE_REGISTRY_TOKEN}" \
-e BRANDED=${BRANDED} \
-e REGISTRY="${REGISTRY}" \
-e ORG="${ORG:-stackstate}" \
--workdir=${PWD} \
registry.tooling.stackstate.io/quay/stackstate/datadog_build_linux_x64:0cd01a13 bash
fi
# Prepare a copy of the agent in the SRC_DIR to make sure that in a containerized environment the source directory
# does not get tainted, and all files have the proper user for within the container.
function prepare() {
. /usr/local/rvm/scripts/rvm
# Ensure gofmt is available for fix_branding.sh
export PATH="/usr/local/go/bin:$PATH"
if ! type "rsync" > /dev/null; then
apt install rsync -y --no-install-recommends
fi
mkdir -p $SRC_PATH
echo "Syncing files to $SRC_PATH"
rsync -au "$CI_PROJECT_DIR"/. $SRC_PATH
chown -R root:root $SRC_PATH
rm -rf "$SRC_PATH/rtloader/build" || true
cd "$SRC_PATH" || exit
if [[ "${RELOCATED}" = "true" ]]; then
git config --global --add safe.directory "$SRC_PATH"
echo "Fixing import paths"
./fix_package_paths.sh "$SRC_PATH"
echo "Cleaning module cache to remove stale entries"
go clean -modcache
echo "Removing go.sum and vendor to force clean regeneration"
rm -f go.sum
rm -rf vendor
echo "Syncing workspace after path transformation"
go work sync
echo "Vendoring workspace modules"
go work vendor
fi
if [[ "${BRANDED}" = "true" ]]; then
echo "Applying Branding to $SRC_PATH..."
./fix_branding.sh "$SRC_PATH"
fi
cd "$CI_PROJECT_DIR" || exit
}
if [ "${WHAT}" = "PREP" ]; then
prepare
fi
if [ "${WHAT}" = "ALL" ]; then
prepare
fi
if [ "${WHAT}" = "ALL" ] || [ "${WHAT}" = "DEPS_DEB" ] || [ "${WHAT}" = "UP_TO_CLUSTER_AGENT" ] || [ "${WHAT}" = "UP_TO_AGENT" ] || [ "${WHAT}" = "UP_TO_BUILD_DEB" ] || [ "${WHAT}" = "UP_TO_UNIT_TESTS" ]; then
if [ "${WHAT}" = "DEPS_DEB" ] || [ "${WHAT}" = "UP_TO_CLUSTER_AGENT" ] || [ "${WHAT}" = "UP_TO_AGENT" ] || [ "${WHAT}" = "UP_TO_BUILD_DEB" ] || [ "${WHAT}" = "UP_TO_UNIT_TESTS" ]; then
prepare
fi
cd $SRC_PATH || exit
echo "Running debian dependencies in $SRC_PATH"
# shellcheck disable=SC2164
go clean -modcache
echo " --- ---"
echo " --- Getting dependencies ---"
echo " --- ---"
inv -e deps --verbose
go mod tidy
if [[ "${RELOCATED}" != "true" ]]; then
echo " --- ---"
echo " --- Regenerating vendor ---"
echo " --- ---"
GOWORK=off go mod vendor
go work sync
go work vendor
fi
inv agent.version --major-version 3 -u > version.txt
echo " --- ---"
echo " --- Agent Version String ---"
echo " --- ---"
cat version.txt
cd "$CI_PROJECT_DIR" || exit
fi
if [ "${WHAT}" = "ALL" ] || [ "${WHAT}" = "BUILD_CLUSTER_AGENT" ] || [ "${WHAT}" = "UP_TO_CLUSTER_AGENT" ] || [ "${WHAT}" = "UP_TO_AGENT" ] || [ "${WHAT}" = "UP_TO_BUILD_DEB" ] || [ "${WHAT}" = "UP_TO_UNIT_TESTS" ]; then
if [ "${WHAT}" = "BUILD_CLUSTER_AGENT" ]; then
prepare
fi
cd $SRC_PATH || exit
echo " --- ---"
echo " --- Building cluster agent ---"
echo " --- ---"
inv -e cluster-agent.build
cd "$CI_PROJECT_DIR" || exit
fi
if [ "${WHAT}" = "ALL" ] || [ "${WHAT}" = "BUILD_AGENT" ] || [ "${WHAT}" = "UP_TO_AGENT" ] || [ "${WHAT}" = "UP_TO_BUILD_DEB" ] || [ "${WHAT}" = "UP_TO_UNIT_TESTS" ]; then
if [ "${WHAT}" = "BUILD_AGENT" ] ; then
prepare
fi
cd $SRC_PATH || exit
echo " --- ---"
echo " --- Building agent ---"
echo " --- ---"
echo " ******** --- Building dogstatsd ---"
# inv -e dogstatsd.build --static --major-version 3
echo " ******** --- Building rtloader ---"
inv -e rtloader.make
echo " ******** --- Installing rtloader ---"
inv -e rtloader.install
echo " ******** --- Building agent ---"
# shellcheck disable=SC2164
inv -e agent.build --major-version "3"
cd "$CI_PROJECT_DIR" || exit
fi
if [ "${WHAT}" = "ALL" ] || [ "${WHAT}" = "BUILD_DEB" ] || [ "${WHAT}" = "UP_TO_BUILD_DEB" ] || [ "${WHAT}" = "UP_TO_UNIT_TESTS" ]; then
if [ "${WHAT}" = "BUILD_DEB" ]; then
prepare
fi
cd $SRC_PATH || exit
echo " --- ---"
echo " --- Building deb package ---"
echo " --- ---"
mv "$SRC_PATH"/.omnibus /omnibus || mkdir -p /omnibus
inv agent.version --major-version 3
cat version.txt || true
source setup_artifact_registry.sh
export OMNIBUS_BASE_DIR="/.omnibus"
inv -e omnibus.build --gem-path $SRC_PATH/.gems --base-dir $OMNIBUS_BASE_DIR --go-mod-cache $SRC_PATH/vendor --skip-deps --skip-sign --major-version 3
# Prepare outputs
mkdir -p $SRC_PATH/outcomes/pkg && mkdir -p $SRC_PATH/outcomes/dockerfiles && mkdir -p $SRC_PATH/outcomes/binary
cp -r $OMNIBUS_BASE_DIR/pkg $SRC_PATH/outcomes
cp -r $SRC_PATH/Dockerfiles $SRC_PATH/outcomes
# - cp -r /opt/stackstate-agent/embedded/bin/trace-agent $SRC_PATH/outcomes/binary/
ls -la $SRC_PATH/outcomes/Dockerfiles
# Prepare cache
# Drop packages for cache
rm -rf /omnibus/pkg
# Drop agent for cache (will be resynced anyway)
rm -rf /omnibus/src/datadog-agent
# Drop symlink because it will fail the build when coming from a cache
rm /omnibus/src/datadog-agent/src/github.com/StackVista/stackstate-agent/vendor/github.com/coreos/etcd/cmd/etcd || echo "Not found"
mv /omnibus $SRC_PATH/.omnibus
cd "$CI_PROJECT_DIR" || exit
fi
if [ "${WHAT}" = "ALL" ] || [ "${WHAT}" = "UNIT_TESTS" ] || [ "${WHAT}" = "UP_TO_UNIT_TESTS" ]; then
if [ "${WHAT}" = "UNIT_TESTS" ]; then
prepare
fi
cd $SRC_PATH || exit
echo " --- ---"
echo " --- Running Unit Tests ---"
echo " --- ---"
inv -e agent.build --race --major-version $MAJOR_VERSION
# TODO: check why formatting rules differ from previous step
# - gofmt -l -w -s ./pkg ./cmd
inv -e rtloader.test
invoke install-tools
echo "inv -e test --coverage --race --profile --cpus 4 --major-version $MAJOR_VERSION"
inv -e test --coverage --race --profile --cpus 4 --major-version $MAJOR_VERSION
cd "$CI_PROJECT_DIR" || exit
fi
if [ "${WHAT}" = "ALL" ] || [ "${WHAT}" = "COPY_RTLOADER" ]; then
if [ "${WHAT}" = "COPY_RTLOADER" ]; then
prepare
fi
rm -rf "$CI_PROJECT_DIR/rtloader/build" || true
rm -rf "$SRC_PATH/rtloader/build" || true
cd $SRC_PATH || exit
echo " --- ---"
echo " --- Copying Rtloader to source ---"
echo " --- ---"
inv -e rtloader.make
cp -a "$SRC_PATH"/rtloader/build "$CI_PROJECT_DIR"/rtloader/
chown "$USER_ID":"$GROUP_ID" -R "$CI_PROJECT_DIR"/rtloader/build
cd "$CI_PROJECT_DIR" || exit
fi
if [ "${WHAT}" = "ALL" ] || [ "${WHAT}" = "LINT" ]; then
if [ "${WHAT}" = "LINT" ]; then
prepare
fi
cd $SRC_PATH || exit
echo " --- ---"
echo " --- Running Lint ---"
echo " --- ---"
inv -e lint-go
cd "$CI_PROJECT_DIR" || exit
fi
## Docker image build steps
## These require Docker socket + CLI to be mounted (done automatically by the SHELL command).
## Run after ./local.sh all (or the individual build steps) has produced artifacts in outcomes/.
ARCH="${ARCH:-amd64}"
IMAGE_TAG="${IMAGE_TAG:-$(git rev-parse --short HEAD 2>/dev/null || echo 'local')}"
ORG="${ORG:-stackstate}"
AGENT_IMAGE_REPO="stackstate-k8s-agent"
CLUSTER_IMAGE_REPO="stackstate-k8s-cluster-agent"
function check_docker() {
if ! command -v docker &>/dev/null; then
echo "ERROR: docker CLI not available inside this container."
echo "Run './local.sh shell' instead of './local.sh cmd' to get Docker socket + CLI mounted."
exit 1
fi
if ! docker info &>/dev/null; then
echo "ERROR: Cannot connect to Docker daemon. Is the socket mounted?"
echo "Check that /var/run/docker.sock exists and is accessible."
exit 1
fi
}
function build_agent_image() {
check_docker
local dockerfile_dir="$CI_PROJECT_DIR/outcomes/Dockerfiles/agent"
if [ ! -d "$dockerfile_dir" ]; then
echo "ERROR: $dockerfile_dir not found. Run build_deb first."
exit 1
fi
local deb_file
deb_file=$(ls "$CI_PROJECT_DIR"/outcomes/pkg/stackstate-agent_*_${ARCH}.deb 2>/dev/null | head -1)
if [ -z "$deb_file" ]; then
echo "ERROR: No .deb found at outcomes/pkg/stackstate-agent_*_${ARCH}.deb"
exit 1
fi
echo "Copying $deb_file -> $dockerfile_dir/"
cp "$deb_file" "$dockerfile_dir/"
local local_tag="${AGENT_IMAGE_REPO}:${IMAGE_TAG}"
echo "Building agent image: $local_tag"
docker build --build-arg ARCH="${ARCH}" -t "$local_tag" "$dockerfile_dir"
if [ -n "$REGISTRY" ]; then
local remote_tag="${REGISTRY}/${ORG}/${local_tag}"
docker tag "$local_tag" "$remote_tag"
echo "Tagged: $remote_tag"
echo "Push with: docker push $remote_tag"
fi
}
function build_cluster_agent_image() {
check_docker
local dockerfile_dir="$CI_PROJECT_DIR/outcomes/Dockerfiles/cluster-agent"
if [ ! -d "$dockerfile_dir" ]; then
echo "ERROR: $dockerfile_dir not found. Run build_cluster_agent first."
exit 1
fi
# CI copies from bin/, local.sh build also puts it there
local binary="$CI_PROJECT_DIR/bin/stackstate-cluster-agent"
if [ ! -f "$binary" ]; then
binary="$SRC_PATH/bin/stackstate-cluster-agent"
fi
if [ ! -f "$binary" ]; then
echo "ERROR: stackstate-cluster-agent binary not found in bin/"
exit 1
fi
echo "Copying $binary -> $dockerfile_dir/"
cp "$binary" "$dockerfile_dir/"
local local_tag="${CLUSTER_IMAGE_REPO}:${IMAGE_TAG}"
echo "Building cluster-agent image: $local_tag"
docker build -t "$local_tag" "$dockerfile_dir"
if [ -n "$REGISTRY" ]; then
local remote_tag="${REGISTRY}/${ORG}/${local_tag}"
docker tag "$local_tag" "$remote_tag"
echo "Tagged: $remote_tag"
echo "Push with: docker push $remote_tag"
fi
}
if [ "${WHAT}" = "BUILD_AGENT_IMAGE" ]; then
build_agent_image
fi
if [ "${WHAT}" = "BUILD_CLUSTER_AGENT_IMAGE" ]; then
build_cluster_agent_image
fi
if [ "${WHAT}" = "BUILD_IMAGES" ]; then
build_agent_image
build_cluster_agent_image
fi
if [ "${WHAT}" = "CMD" ]; then
prepare
cd $SRC_PATH || exit
echo " --- ---"
echo " --- Running command `$2`"
echo " --- ---"
$2
cd "$CI_PROJECT_DIR" || exit
fi