From 20a6bd281635bd3a68933bc5249e0950067ca1ec 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 | 31 ++++++++++++++++++++++- distribution/Dockerfile-composer | 5 ++-- distribution/Dockerfile-worker_srcinstall | 8 ++++-- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 6c7eb4e403e..b1825c0e839 100644 --- a/Makefile +++ b/Makefile @@ -316,6 +316,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 @@ -329,13 +334,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 4bba6cfa4d7..f69bd74f046 100644 --- a/containers/osbuild-composer/entrypoint.py +++ b/containers/osbuild-composer/entrypoint.py @@ -401,7 +401,10 @@ 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 self.args.dnf_json: @@ -410,6 +413,28 @@ def handler(signum, frame): 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() @@ -418,6 +443,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"]