From 969a11ac99d9aa0f376939c813412e22518d8d26 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 | 13 ++++++--- containers/osbuild-composer/entrypoint.py | 33 +++++++++++++++++++++-- distribution/Dockerfile-composer | 5 ++-- distribution/Dockerfile-worker_srcinstall | 8 ++++-- 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 40e67bac96e..6a1005842c2 100644 --- a/Makefile +++ b/Makefile @@ -317,6 +317,11 @@ 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 +CONTAINER_DEPS_WORKER := ./distribution/osbuild-worker-entrypoint.sh USE_BTRFS ?= yes @@ -330,13 +335,13 @@ go.local.mod go.local.sum: $(SRC_DEPS_EXTERNAL_DIRS) go.mod $(SRC_DEPS_EXTERNAL) env GOPROXY=$(GOPROXY) go mod tidy $(GOMODARGS) 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_worker_built.info: go.local.mod $(WORKER_SRC_DEPS) $(DOCKERFILE_WORKER) $(CONTAINER_DEPS_WORKER) + $(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..9d83d650cd3 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", file=sys.stderr) + 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..7a1e70682d2 100644 --- a/distribution/Dockerfile-worker_srcinstall +++ b/distribution/Dockerfile-worker_srcinstall @@ -18,11 +18,14 @@ 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 +RUN dnf install -y delve + RUN mkdir -p "/usr/libexec/osbuild-composer" RUN mkdir -p "/etc/osbuild-composer/" RUN mkdir -p "/run/osbuild-composer/" @@ -30,5 +33,6 @@ RUN mkdir -p "/var/cache/osbuild-worker/" RUN mkdir -p "/var/lib/osbuild-composer/" RUN mkdir -p "/var/cache/osbuild-composer/output" COPY --from=builder /opt/app-root/src/go/bin/osbuild-worker /usr/libexec/osbuild-composer/ +COPY distribution/osbuild-worker-entrypoint.sh /usr/libexec/osbuild-composer/ -ENTRYPOINT ["/usr/libexec/osbuild-composer/osbuild-worker"] +ENTRYPOINT ["/usr/libexec/osbuild-composer/osbuild-worker-entrypoint.sh"]