From ec5bb0a4d28424c520c1ff925cf2090c853708f2 Mon Sep 17 00:00:00 2001 From: Jeff Cheng <83052155+jcheng-splunk@users.noreply.github.com> Date: Fri, 10 Dec 2021 10:20:36 -0500 Subject: [PATCH] Add journalctl to docker image (#1026) --- .github/workflows/build-and-test.yml | 2 + Makefile | 2 + cmd/otelcol/Dockerfile | 10 +++ .../buildscripts/packaging/collect-libs.sh | 80 +++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100755 internal/buildscripts/packaging/collect-libs.sh diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index e414941c933..28106b4a1d3 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -486,6 +486,8 @@ jobs: echo "Failing job execution: fail to start otelcol docker container in 10 seconds." exit 1 fi + # ensure journalctl can run with the collected libraries + docker exec otelcol /bin/journalctl docker-otelcol-windows: name: docker-otelcol-windows diff --git a/Makefile b/Makefile index dfdeddd2228..00ee713713c 100644 --- a/Makefile +++ b/Makefile @@ -211,11 +211,13 @@ endif cp ./bin/otelcol_linux_$(ARCH) ./cmd/otelcol/otelcol cp ./bin/translatesfx_linux_$(ARCH) ./cmd/otelcol/translatesfx cp ./bin/migratecheckpoint_linux_$(ARCH) ./cmd/otelcol/migratecheckpoint + cp ./internal/buildscripts/packaging/collect-libs.sh ./cmd/otelcol/collect-libs.sh docker buildx build --platform linux/$(ARCH) -o type=image,name=otelcol:$(ARCH),push=false --build-arg ARCH=$(ARCH) --build-arg SMART_AGENT_RELEASE=$(SMART_AGENT_RELEASE) ./cmd/otelcol/ docker tag otelcol:$(ARCH) otelcol:latest rm ./cmd/otelcol/otelcol rm ./cmd/otelcol/translatesfx rm ./cmd/otelcol/migratecheckpoint + rm ./cmd/otelcol/collect-libs.sh .PHONY: binaries-all-sys binaries-all-sys: binaries-darwin_amd64 binaries-linux_amd64 binaries-linux_arm64 binaries-windows_amd64 diff --git a/cmd/otelcol/Dockerfile b/cmd/otelcol/Dockerfile index a1300d3a2cc..74b756fef9a 100644 --- a/cmd/otelcol/Dockerfile +++ b/cmd/otelcol/Dockerfile @@ -37,6 +37,12 @@ RUN if [ "$ARCH" = "amd64" ]; then \ fi RUN find /usr/lib/splunk-otel-collector/agent-bundle -wholename "*test*.key" -delete -or -wholename "*test*.pem" -delete +FROM debian:11.1 as journalctl +RUN apt update +RUN apt install -y systemd +COPY collect-libs.sh /collect-libs.sh +RUN /collect-libs.sh /opt/journalctl /bin/journalctl + FROM scratch ENV SPLUNK_BUNDLE_DIR=/usr/lib/splunk-otel-collector/agent-bundle ENV SPLUNK_COLLECTD_DIR=${SPLUNK_BUNDLE_DIR}/run/collectd @@ -50,11 +56,15 @@ COPY --from=otelcol --chown=999 /otel /etc/otel COPY --from=otelcol --chown=999 /otel/collector /etc/otel/collector COPY --from=smartagent --chown=999 /usr/lib/splunk-otel-collector /usr/lib/splunk-otel-collector +COPY --from=journalctl --chown=999 /bin/journalctl /bin/journalctl +COPY --from=journalctl --chown=999 /opt/journalctl / + COPY --chown=999 config/collector/gateway_config.yaml /etc/otel/collector/gateway_config.yaml COPY --chown=999 config/collector/otlp_config_linux.yaml /etc/otel/collector/otlp_config_linux.yaml COPY --chown=999 config/collector/agent_config.yaml /etc/otel/collector/agent_config.yaml COPY --chown=999 config/collector/fargate_config.yaml /etc/otel/collector/fargate_config.yaml COPY --chown=999 config/collector/ecs_ec2_config.yaml /etc/otel/collector/ecs_ec2_config.yaml + USER splunk-otel-collector ENTRYPOINT ["/otelcol"] EXPOSE 13133 14250 14268 4317 4318 6060 8888 9411 9443 9080 diff --git a/internal/buildscripts/packaging/collect-libs.sh b/internal/buildscripts/packaging/collect-libs.sh new file mode 100755 index 00000000000..0277d5b364a --- /dev/null +++ b/internal/buildscripts/packaging/collect-libs.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +set -e + +usage() { + echo 'Usage: $1 output_dir binary_path ...' +} + +output_dir=$1 +if [[ -e "$output_dir" ]]; then + echo "$output_dir exists!" >&2 + exit 1 +fi +mkdir -p $output_dir + +shift +binary_paths=$@ + +if [[ ${#binary_paths[@]} == 0 ]] +then + usage + exit 1 +fi + +echo "Copying dependent libs to $output_dir" + +find_deps() { + local paths=$@ + find $paths -type f -o -type l -and -executable -or -name "*.so*" | \ + xargs ldd | \ + grep -o '/.*' | awk '{print $1}' | grep -v ':$' | sort -u +} + +copy_lib_and_links() { + local lib=$1 + local output_dir=$2 + + while [ 0 ]; do + file=$(basename $lib) + dir=$(dirname $lib) + mkdir -p ${output_dir}/$dir + cp -a $lib ${output_dir}/$dir + lib=$(readlink "${dir}/$file" || true) + if [[ -z "$lib" ]]; then + break + fi + libdir=$(dirname $lib) + if [[ "${libdir:0:1}" != "/" ]]; then + lib=${dir}/$lib + fi + done +} + +libs="$(find_deps $binary_paths)" +transitive_libs="$(find_deps $libs)" + +for lib in $libs $transitive_libs +do + if [[ ! -e ${output_dir}/$lib ]]; then + copy_lib_and_links $lib $output_dir + echo "Pulled in $lib" + fi +done + +echo "Processed $(wc -w <<< $libs) libraries" + +echo "Checking for missing lib dependencies..." + +# Look for all of the deps now in the output_dir and make sure we have them +new_deps=$(find_deps $output_dir) +for dep in $new_deps +do + stat ${dep} >/dev/null + if [[ $? != 0 ]]; then + echo "Missing dependency in target dir: $dep" >&2 + exit 1 + fi +done + +echo "Everything is there!"