From 83e14f4dcde82eda7dafc497107349a628dc90f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Sch=C3=BCller?= Date: Thu, 11 Apr 2024 18:18:30 +0200 Subject: [PATCH] Makefile: support for golang debugging --- Makefile | 10 ++++--- containers/osbuild-composer/entrypoint.py | 33 +++++++++++++++++++++-- distribution/Dockerfile-composer | 5 ++-- distribution/Dockerfile-worker_srcinstall | 3 ++- 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 40e67bac96e..ca3d747f703 100644 --- a/Makefile +++ b/Makefile @@ -317,6 +317,10 @@ WORKER_SRC_DEPS := $(SRC_DEPS) COMPOSER_SRC_DEPS := $(SRC_DEPS) GOMODARGS ?= -modfile=go.local.mod +# gcflags "-N -l" for golang to allow debugging +GCFLAGS ?= -gcflags=all=-N -gcflags=all=-l + +CONTAINER_DEPS_COMPOSER := ./containers/osbuild-composer/entrypoint.py USE_BTRFS ?= yes @@ -331,12 +335,12 @@ go.local.mod go.local.sum: $(SRC_DEPS_EXTERNAL_DIRS) go.mod $(SRC_DEPS_EXTERNAL) env GOPROXY=$(GOPROXY) go mod vendor $(GOMODARGS) container_worker_built.info: go.local.mod $(WORKER_SRC_DEPS) $(DOCKERFILE_WORKER) - $(CONTAINER_EXECUTABLE) build -t $(DOCKER_IMAGE_WORKER) -f $(DOCKERFILE_WORKER) --build-arg GOMODARGS=$(GOMODARGS) --build-arg USE_BTRFS=$(USE_BTRFS) . + $(CONTAINER_EXECUTABLE) build -t $(DOCKER_IMAGE_WORKER) -f $(DOCKERFILE_WORKER) --build-arg GOMODARGS="$(GOMODARGS)" --build-arg GCFLAGS="$(GCFLAGS)" --build-arg USE_BTRFS=$(USE_BTRFS) . echo "Worker last built on" > $@ date >> $@ -container_composer_built.info: go.local.mod $(COMPOSER_SRC_DEPS) $(DOCKERFILE_COMPOSER) - $(CONTAINER_EXECUTABLE) build -t $(DOCKER_IMAGE_COMPOSER) -f $(DOCKERFILE_COMPOSER) --build-arg GOMODARGS=$(GOMODARGS) . +container_composer_built.info: go.local.mod $(COMPOSER_SRC_DEPS) $(DOCKERFILE_COMPOSER) $(CONTAINER_DEPS_COMPOSER) + $(CONTAINER_EXECUTABLE) build -t $(DOCKER_IMAGE_COMPOSER) -f $(DOCKERFILE_COMPOSER) --build-arg GOMODARGS="$(GOMODARGS)" --build-arg GCFLAGS="$(GCFLAGS)" . echo "Composer last built on" > $@ date >> $@ diff --git a/containers/osbuild-composer/entrypoint.py b/containers/osbuild-composer/entrypoint.py index 8fc0b093b73..21ff06a4366 100644 --- a/containers/osbuild-composer/entrypoint.py +++ b/containers/osbuild-composer/entrypoint.py @@ -333,12 +333,37 @@ def handler(signum, frame): liveness.touch() try: - if self.args.builtin_worker: + should_launch_composer = any([self.args.weldr_api, self.args.composer_api, self.args.local_worker_api, self.args.remote_worker_api]) + if self.args.builtin_worker or not should_launch_composer: + if not should_launch_composer: + print(f"NOTE: launching worker only - no API for composer enabled") proc_worker = self._spawn_worker() - if any([self.args.weldr_api, self.args.composer_api, self.args.local_worker_api, self.args.remote_worker_api]): + if should_launch_composer: proc_composer = self._spawn_composer(sockets) + debug_port = os.environ.get('GODEBUG_PORT') + debugger = None + + if debug_port: + # only debug one - either composer or worker if there is no composer + child_pid = proc_composer.pid if proc_composer else proc_worker.pid + debug_target_name = "image-builder-composer" if proc_composer else "image-builder-worker" + + debugger_cmd = [ + "/usr/bin/dlv", + "attach", + "--headless=true", + "--api-version", "2", + "--listen", f":{debug_port}", + str(child_pid), + "/usr/libexec/osbuild-composer/osbuild-composer" + ] + + print(f"NOTE: you HAVE to attach the debugger NOW otherwise { debug_target_name } " + f"will not continue running") + debugger = subprocess.Popen(debugger_cmd) + if proc_composer: res = proc_composer.wait() @@ -347,6 +372,10 @@ def handler(signum, frame): proc_worker.terminate() proc_worker.wait() + if debugger: + debugger.wait() + + except KeyboardInterrupt: if proc_composer: proc_composer.terminate() diff --git a/distribution/Dockerfile-composer b/distribution/Dockerfile-composer index 7975a5327a0..819999b41f8 100644 --- a/distribution/Dockerfile-composer +++ b/distribution/Dockerfile-composer @@ -11,15 +11,16 @@ ARG GOPROXY=https://proxy.golang.org,direct RUN go env -w GOPROXY=$GOPROXY ARG GOMODARGS="" +ARG GCFLAGS="" -RUN go install $GOMODARGS ./cmd/osbuild-composer/ +RUN go install $GOMODARGS $GCFLAGS ./cmd/osbuild-composer/ FROM registry.access.redhat.com/ubi9/go-toolset:latest AS builder2 RUN go install github.com/jackc/tern@latest FROM fedora:39 -RUN dnf install -y python3 python3-dnf gpgme libassuan device-mapper-libs +RUN dnf install -y python3 python3-dnf gpgme libassuan device-mapper-libs delve RUN mkdir -p "/usr/libexec/osbuild-composer" RUN mkdir -p "/etc/osbuild-composer/" RUN mkdir -p "/run/osbuild-composer/" diff --git a/distribution/Dockerfile-worker_srcinstall b/distribution/Dockerfile-worker_srcinstall index 6eb78d4ffe2..613f0b63c48 100644 --- a/distribution/Dockerfile-worker_srcinstall +++ b/distribution/Dockerfile-worker_srcinstall @@ -18,8 +18,9 @@ ARG GOPROXY=https://proxy.golang.org,direct RUN go env -w GOPROXY=$GOPROXY ARG GOMODARGS="" +ARG GCFLAGS="" -RUN go install $GOMODARGS ./cmd/osbuild-worker +RUN go install $GOMODARGS $GCFLAGS ./cmd/osbuild-worker FROM osbuild_devel