Skip to content

Commit 20a6bd2

Browse files
committed
Makefile: support for golang debugging
1 parent 1e5b924 commit 20a6bd2

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

Makefile

+9-4
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ WORKER_SRC_DEPS := $(SRC_DEPS)
316316
COMPOSER_SRC_DEPS := $(SRC_DEPS)
317317

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

320325
USE_BTRFS ?= yes
321326

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

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

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

containers/osbuild-composer/entrypoint.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,10 @@ def handler(signum, frame):
401401
liveness.touch()
402402

403403
try:
404-
if self.args.builtin_worker:
404+
should_launch_composer = any([self.args.weldr_api, self.args.composer_api, self.args.local_worker_api, self.args.remote_worker_api])
405+
if self.args.builtin_worker or not should_launch_composer:
406+
if not should_launch_composer:
407+
print(f"NOTE: launching worker only - no API for composer enabled")
405408
proc_worker = self._spawn_worker()
406409

407410
if self.args.dnf_json:
@@ -410,6 +413,28 @@ def handler(signum, frame):
410413
if should_launch_composer:
411414
proc_composer = self._spawn_composer(sockets)
412415

416+
debug_port = os.environ.get('GODEBUG_PORT')
417+
debugger = None
418+
419+
if debug_port:
420+
# only debug one - either composer or worker if there is no composer
421+
child_pid = proc_composer.pid if proc_composer else proc_worker.pid
422+
debug_target_name = "image-builder-composer" if proc_composer else "image-builder-worker"
423+
424+
debugger_cmd = [
425+
"/usr/bin/dlv",
426+
"attach",
427+
"--headless=true",
428+
"--api-version", "2",
429+
"--listen", f":{debug_port}",
430+
str(child_pid),
431+
"/usr/libexec/osbuild-composer/osbuild-composer"
432+
]
433+
434+
print(f"NOTE: you HAVE to attach the debugger NOW otherwise { debug_target_name } "
435+
f"will not continue running", file=sys.stderr)
436+
debugger = subprocess.Popen(debugger_cmd)
437+
413438
if proc_composer:
414439
res = proc_composer.wait()
415440

@@ -418,6 +443,10 @@ def handler(signum, frame):
418443
proc_worker.terminate()
419444
proc_worker.wait()
420445

446+
if debugger:
447+
debugger.wait()
448+
449+
421450
except KeyboardInterrupt:
422451
if proc_composer:
423452
proc_composer.terminate()

distribution/Dockerfile-composer

+3-2
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

+6-2
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)