Skip to content

Commit 969a11a

Browse files
committed
Makefile: support for golang debugging
1 parent 2a3aafe commit 969a11a

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

Makefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ WORKER_SRC_DEPS := $(SRC_DEPS)
317317
COMPOSER_SRC_DEPS := $(SRC_DEPS)
318318

319319
GOMODARGS ?= -modfile=go.local.mod
320+
# gcflags "-N -l" for golang to allow debugging
321+
GCFLAGS ?= -gcflags=all=-N -gcflags=all=-l
322+
323+
CONTAINER_DEPS_COMPOSER := ./containers/osbuild-composer/entrypoint.py
324+
CONTAINER_DEPS_WORKER := ./distribution/osbuild-worker-entrypoint.sh
320325

321326
USE_BTRFS ?= yes
322327

@@ -330,13 +335,13 @@ go.local.mod go.local.sum: $(SRC_DEPS_EXTERNAL_DIRS) go.mod $(SRC_DEPS_EXTERNAL)
330335
env GOPROXY=$(GOPROXY) go mod tidy $(GOMODARGS)
331336
env GOPROXY=$(GOPROXY) go mod vendor $(GOMODARGS)
332337

333-
container_worker_built.info: go.local.mod $(WORKER_SRC_DEPS) $(DOCKERFILE_WORKER)
334-
$(CONTAINER_EXECUTABLE) build -t $(DOCKER_IMAGE_WORKER) -f $(DOCKERFILE_WORKER) --build-arg GOMODARGS=$(GOMODARGS) --build-arg USE_BTRFS=$(USE_BTRFS) .
338+
container_worker_built.info: go.local.mod $(WORKER_SRC_DEPS) $(DOCKERFILE_WORKER) $(CONTAINER_DEPS_WORKER)
339+
$(CONTAINER_EXECUTABLE) build -t $(DOCKER_IMAGE_WORKER) -f $(DOCKERFILE_WORKER) --build-arg GOMODARGS="$(GOMODARGS)" --build-arg GCFLAGS="$(GCFLAGS)" --build-arg USE_BTRFS=$(USE_BTRFS) .
335340
echo "Worker last built on" > $@
336341
date >> $@
337342

338-
container_composer_built.info: go.local.mod $(COMPOSER_SRC_DEPS) $(DOCKERFILE_COMPOSER)
339-
$(CONTAINER_EXECUTABLE) build -t $(DOCKER_IMAGE_COMPOSER) -f $(DOCKERFILE_COMPOSER) --build-arg GOMODARGS=$(GOMODARGS) .
343+
container_composer_built.info: go.local.mod $(COMPOSER_SRC_DEPS) $(DOCKERFILE_COMPOSER) $(CONTAINER_DEPS_COMPOSER)
344+
$(CONTAINER_EXECUTABLE) build -t $(DOCKER_IMAGE_COMPOSER) -f $(DOCKERFILE_COMPOSER) --build-arg GOMODARGS="$(GOMODARGS)" --build-arg GCFLAGS="$(GCFLAGS)" .
340345
echo "Composer last built on" > $@
341346
date >> $@
342347

containers/osbuild-composer/entrypoint.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,37 @@ def handler(signum, frame):
333333
liveness.touch()
334334

335335
try:
336-
if self.args.builtin_worker:
336+
should_launch_composer = any([self.args.weldr_api, self.args.composer_api, self.args.local_worker_api, self.args.remote_worker_api])
337+
if self.args.builtin_worker or not should_launch_composer:
338+
if not should_launch_composer:
339+
print(f"NOTE: launching worker only - no API for composer enabled")
337340
proc_worker = self._spawn_worker()
338341

339-
if any([self.args.weldr_api, self.args.composer_api, self.args.local_worker_api, self.args.remote_worker_api]):
342+
if should_launch_composer:
340343
proc_composer = self._spawn_composer(sockets)
341344

345+
debug_port = os.environ.get('GODEBUG_PORT')
346+
debugger = None
347+
348+
if debug_port:
349+
# only debug one - either composer or worker if there is no composer
350+
child_pid = proc_composer.pid if proc_composer else proc_worker.pid
351+
debug_target_name = "image-builder-composer" if proc_composer else "image-builder-worker"
352+
353+
debugger_cmd = [
354+
"/usr/bin/dlv",
355+
"attach",
356+
"--headless=true",
357+
"--api-version", "2",
358+
"--listen", f":{debug_port}",
359+
str(child_pid),
360+
"/usr/libexec/osbuild-composer/osbuild-composer"
361+
]
362+
363+
print(f"NOTE: you HAVE to attach the debugger NOW otherwise { debug_target_name } "
364+
f"will not continue running", file=sys.stderr)
365+
debugger = subprocess.Popen(debugger_cmd)
366+
342367
if proc_composer:
343368
res = proc_composer.wait()
344369

@@ -347,6 +372,10 @@ def handler(signum, frame):
347372
proc_worker.terminate()
348373
proc_worker.wait()
349374

375+
if debugger:
376+
debugger.wait()
377+
378+
350379
except KeyboardInterrupt:
351380
if proc_composer:
352381
proc_composer.terminate()

distribution/Dockerfile-composer

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ ARG GOPROXY=https://proxy.golang.org,direct
1111
RUN go env -w GOPROXY=$GOPROXY
1212

1313
ARG GOMODARGS=""
14+
ARG GCFLAGS=""
1415

15-
RUN go install $GOMODARGS ./cmd/osbuild-composer/
16+
RUN go install $GOMODARGS $GCFLAGS ./cmd/osbuild-composer/
1617

1718
FROM registry.access.redhat.com/ubi9/go-toolset:latest AS builder2
1819
RUN go install github.com/jackc/tern@latest
1920

2021
FROM fedora:39
2122

22-
RUN dnf install -y python3 python3-dnf gpgme libassuan device-mapper-libs
23+
RUN dnf install -y python3 python3-dnf gpgme libassuan device-mapper-libs delve
2324
RUN mkdir -p "/usr/libexec/osbuild-composer"
2425
RUN mkdir -p "/etc/osbuild-composer/"
2526
RUN mkdir -p "/run/osbuild-composer/"

distribution/Dockerfile-worker_srcinstall

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@ ARG GOPROXY=https://proxy.golang.org,direct
1818
RUN go env -w GOPROXY=$GOPROXY
1919

2020
ARG GOMODARGS=""
21+
ARG GCFLAGS=""
2122

22-
RUN go install $GOMODARGS ./cmd/osbuild-worker
23+
RUN go install $GOMODARGS $GCFLAGS ./cmd/osbuild-worker
2324

2425
FROM osbuild_devel
2526

27+
RUN dnf install -y delve
28+
2629
RUN mkdir -p "/usr/libexec/osbuild-composer"
2730
RUN mkdir -p "/etc/osbuild-composer/"
2831
RUN mkdir -p "/run/osbuild-composer/"
2932
RUN mkdir -p "/var/cache/osbuild-worker/"
3033
RUN mkdir -p "/var/lib/osbuild-composer/"
3134
RUN mkdir -p "/var/cache/osbuild-composer/output"
3235
COPY --from=builder /opt/app-root/src/go/bin/osbuild-worker /usr/libexec/osbuild-composer/
36+
COPY distribution/osbuild-worker-entrypoint.sh /usr/libexec/osbuild-composer/
3337

34-
ENTRYPOINT ["/usr/libexec/osbuild-composer/osbuild-worker"]
38+
ENTRYPOINT ["/usr/libexec/osbuild-composer/osbuild-worker-entrypoint.sh"]

0 commit comments

Comments
 (0)