Skip to content

Commit 743a85d

Browse files
committed
feat(build): Output metadata file when calling build command
1 parent 6e507ad commit 743a85d

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/dda/build/artifacts/binaries/core_agent.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,12 @@ def name(self) -> str:
100100
return "core-agent"
101101

102102
@override
103-
def build(self, app: Application, *args: Any, **kwargs: Any) -> None:
104-
from dda.utils.fs import Path
105-
103+
def build(self, app: Application, output: Path, *args: Any, **kwargs: Any) -> None:
106104
# TODO: Build rtloader first if needed
107105
# TODO: Make this build in a devenv ? Or at least add a flag
108106
app.tools.go.build(
109107
"github.com/DataDog/datadog-agent/cmd/agent",
110-
output=Path("./bin/agent"),
108+
output=output,
111109
build_tags=self.get_build_tags(),
112110
gcflags=self.get_gcflags(),
113111
ldflags=self.get_ldflags(app),

src/dda/cli/build/bin/core_agent/__init__.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,49 @@
55

66
from typing import TYPE_CHECKING
77

8+
import click
9+
10+
from dda.build.metadata.digests import ArtifactDigest, DigestType
811
from dda.cli.base import dynamic_command, pass_app
12+
from dda.utils.fs import Path
913

1014
if TYPE_CHECKING:
1115
from dda.cli.application import Application
1216

17+
DEFAULT_OUTPUT_PLACEHOLDER = Path("./bin/agent/canonical_filename")
18+
1319

1420
@dynamic_command(short_help="Build the `core-agent` binary.")
21+
@click.option(
22+
"--output",
23+
"-o",
24+
type=click.Path(file_okay=True, dir_okay=False, exists=False, writable=True),
25+
default=DEFAULT_OUTPUT_PLACEHOLDER,
26+
help="""
27+
The path on which to create the binary.
28+
Defaults to bin/agent/canonical_filename - the canonical filename of the built artifact.
29+
This filename contains some metadata about the built artifact, e.g. commit hash, build timestamp, etc.
30+
""",
31+
)
1532
@pass_app
16-
def cmd(app: Application) -> None:
33+
def cmd(app: Application, output: Path) -> None:
34+
import shutil
35+
1736
from dda.build.artifacts.binaries.core_agent import CoreAgent
37+
from dda.utils.fs import temp_file
1838

1939
artifact = CoreAgent()
2040
app.display_waiting("Building the `core-agent` binary...")
21-
artifact.build(app)
41+
with temp_file() as tf:
42+
artifact.build(app, output=tf)
43+
digest = ArtifactDigest(value=tf.hexdigest(), type=DigestType.FILE_SHA256)
44+
45+
metadata = artifact.compute_metadata(app, digest)
46+
47+
# Special case: if output is the default value, use the canonical filename from the metadata
48+
if output == DEFAULT_OUTPUT_PLACEHOLDER:
49+
output = Path("./bin/") / metadata.get_canonical_filename()
50+
51+
output.parent.mkdir(parents=True, exist_ok=True)
52+
shutil.move(tf, output)
53+
metadata.to_file(output.with_suffix(".json"))

0 commit comments

Comments
 (0)