Skip to content

Commit

Permalink
Added install_dir to trace agent
Browse files Browse the repository at this point in the history
  • Loading branch information
mellon85 committed Nov 28, 2023
1 parent a597be5 commit 435c12b
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 12 deletions.
11 changes: 10 additions & 1 deletion pkg/version/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ var AgentVersion string
// Commit is populated with the short commit hash from which the Agent was built
var Commit string

var agentVersionDefault = "6.0.0"
// AgentPath is the path to the installation
var AgentPath string

const agentVersionDefault = "6.0.0"
const agentPathDefault = "/opt/datadog-agent"

func init() {
if AgentVersion == "" {
AgentVersion = agentVersionDefault
}

if AgentPath == "" {
AgentPath = agentPathDefault
}
panic(AgentPath)
}
3 changes: 3 additions & 0 deletions tasks/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sys
import tempfile
from distutils.dir_util import copy_tree
from typing import Optional

from invoke import task
from invoke.exceptions import Exit, ParseError
Expand Down Expand Up @@ -117,6 +118,7 @@ def build(
go_mod="mod",
windows_sysprobe=False,
cmake_options='',
install_dir: Optional[str] = None,
):
"""
Build the agent. If the bits to include in the build are not specified,
Expand All @@ -141,6 +143,7 @@ def build(
python_home_3=python_home_3,
major_version=major_version,
python_runtimes=python_runtimes,
install_dir=install_dir,
)

if sys.platform == 'win32':
Expand Down
4 changes: 3 additions & 1 deletion tasks/cluster_agent_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import shutil
from distutils.dir_util import copy_tree
from typing import Optional

from .build_tags import filter_incompatible_tags, get_build_tags
from .utils import REPO_PATH, bin_name, get_build_flags, get_version
Expand All @@ -23,6 +24,7 @@ def build_common(
skip_assets,
go_mod="mod",
arch="x64",
install_dir : Optional[str] = None,
):
"""
Build Cluster Agent
Expand All @@ -35,7 +37,7 @@ def build_common(
build_tags = get_build_tags(build_include, build_exclude)

# We rely on the go libs embedded in the debian stretch image to build dynamically
ldflags, gcflags, env = get_build_flags(ctx, static=False)
ldflags, gcflags, env = get_build_flags(ctx, static=False, install_dir=install_dir)

cmd = "go build -mod={go_mod} {race_opt} {build_type} -tags '{build_tags}' -o {bin_name} "
cmd += "-gcflags=\"{gcflags}\" -ldflags=\"{ldflags}\" {REPO_PATH}/cmd/cluster-agent{suffix}"
Expand Down
4 changes: 3 additions & 1 deletion tasks/cws_instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import platform
import shutil
from typing import Optional

from invoke import task
from invoke.exceptions import Exit
Expand Down Expand Up @@ -37,11 +38,12 @@ def build(
arch=CURRENT_ARCH, # noqa: U100
go_mod="mod",
static=False,
install_dir : Optional[str] = None,
):
"""
Build cws-instrumentation
"""
ldflags, gcflags, env = get_build_flags(ctx, major_version=major_version, python_runtimes='3', static=static)
ldflags, gcflags, env = get_build_flags(ctx, major_version=major_version, python_runtimes='3', static=static, install_dir=install_dir)

# TODO use pkg/version for this
main = "main."
Expand Down
4 changes: 3 additions & 1 deletion tasks/dogstatsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import shutil
import sys
from distutils.dir_util import copy_tree
from typing import Optional

from invoke import task
from invoke.exceptions import Exit
Expand Down Expand Up @@ -35,6 +36,7 @@ def build(
major_version='7',
arch="x64",
go_mod="mod",
install_dir : Optional[str] =None,
):
"""
Build Dogstatsd
Expand All @@ -46,7 +48,7 @@ def build(
)
build_exclude = [] if build_exclude is None else build_exclude.split(",")
build_tags = get_build_tags(build_include, build_exclude)
ldflags, gcflags, env = get_build_flags(ctx, static=static, major_version=major_version)
ldflags, gcflags, env = get_build_flags(ctx, static=static, major_version=major_version, install_dir=install_dir)
bin_path = DOGSTATSD_BIN_PATH

# generate windows resources
Expand Down
4 changes: 3 additions & 1 deletion tasks/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import shutil
import textwrap
from pathlib import Path
from typing import Optional

from invoke import task
from invoke.exceptions import Exit
Expand Down Expand Up @@ -41,6 +42,7 @@ def run_golangci_lint(
verbose=False,
golangci_lint_kwargs="",
headless_mode: bool = False,
install_dir: Optional[str] = None,
):
if isinstance(targets, str):
# when this function is called from the command line, targets are passed
Expand All @@ -54,7 +56,7 @@ def run_golangci_lint(
# Always add `test` tags while linting as test files are also linted
tags.extend(UNIT_TEST_TAGS)

_, _, env = get_build_flags(ctx, rtloader_root=rtloader_root, headless_mode=headless_mode)
_, _, env = get_build_flags(ctx, rtloader_root=rtloader_root, headless_mode=headless_mode, install_dir=install_dir)
verbosity = "-v" if verbose else ""
# we split targets to avoid going over the memory limit from circleCI
results = []
Expand Down
3 changes: 2 additions & 1 deletion tasks/process_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ def build(
python_runtimes='3',
arch="x64",
go_mod="mod",
install_dir : Optional[str] = None,
):
"""
Build the process agent
"""
flavor = AgentFlavor[flavor]
ldflags, gcflags, env = get_build_flags(ctx, major_version=major_version, python_runtimes=python_runtimes)
ldflags, gcflags, env = get_build_flags(ctx, major_version=major_version, python_runtimes=python_runtimes, install_dir=install_dir)

# generate windows resources
if sys.platform == 'win32':
Expand Down
7 changes: 5 additions & 2 deletions tasks/security_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import tarfile
import tempfile
from subprocess import check_output
from typing import Optional

from invoke import task
from invoke.exceptions import Exit
Expand Down Expand Up @@ -59,11 +60,12 @@ def build(
go_mod="mod",
skip_assets=False,
static=False,
install_dir : Optional[str] = None,
):
"""
Build the security agent
"""
ldflags, gcflags, env = get_build_flags(ctx, major_version=major_version, python_runtimes='3', static=static)
ldflags, gcflags, env = get_build_flags(ctx, major_version=major_version, python_runtimes='3', static=static, install_dir=install_dir)

# TODO use pkg/version for this
main = "main."
Expand Down Expand Up @@ -338,6 +340,7 @@ def build_functional_tests(
race=False,
kernel_release=None,
debug=False,
install_dir : Optional[str] = None,
):
build_cws_object_files(
ctx,
Expand All @@ -349,7 +352,7 @@ def build_functional_tests(

build_embed_syscall_tester(ctx)

ldflags, gcflags, env = get_build_flags(ctx, major_version=major_version, static=static)
ldflags, gcflags, env = get_build_flags(ctx, major_version=major_version, static=static, install_dir=install_dir)

env["CGO_ENABLED"] = "1"
if arch == "x86":
Expand Down
3 changes: 3 additions & 0 deletions tasks/system_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import tempfile
from pathlib import Path
from subprocess import check_output
from typing import Optional

import requests
from invoke import task
Expand Down Expand Up @@ -549,11 +550,13 @@ def build_sysprobe_binary(
binary=BIN_PATH,
bundle_ebpf=False,
strip_binary=False,
install_dir : Optional[str] =None,
):
ldflags, gcflags, env = get_build_flags(
ctx,
major_version=major_version,
python_runtimes=python_runtimes,
install_dir=install_dir,
)

build_tags = get_default_build_tags(build="system-probe", arch=arch)
Expand Down
4 changes: 3 additions & 1 deletion tasks/trace_agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
from typing import Optional

from invoke import Exit, task

Expand All @@ -24,13 +25,14 @@ def build(
python_runtimes='3',
arch="x64",
go_mod="mod",
install_dir : Optional[str] =None,
):
"""
Build the trace agent.
"""

flavor = AgentFlavor[flavor]
ldflags, gcflags, env = get_build_flags(ctx, major_version=major_version, python_runtimes=python_runtimes)
ldflags, gcflags, env = get_build_flags(ctx, major_version=major_version, python_runtimes=python_runtimes, install_dir=install_dir)

# generate windows resources
if sys.platform == 'win32':
Expand Down
15 changes: 12 additions & 3 deletions tasks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import time
from subprocess import check_output
from types import SimpleNamespace
from typing import Optional

from invoke import task
from invoke.exceptions import Exit
Expand Down Expand Up @@ -111,6 +112,8 @@ def get_win_py_runtime_var(python_runtimes):
return "PY2_RUNTIME" if '2' in python_runtimes else "PY3_RUNTIME"


DEFAULT_AGENT_PATH = "/opt/datadog-agent"

def get_build_flags(
ctx,
static=False,
Expand All @@ -123,6 +126,7 @@ def get_build_flags(
python_runtimes='3',
headless_mode=False,
race=False,
install_dir : Optional[str] =None, # I have no idea how this would interact with embedded_path
):
"""
Build the common value for both ldflags and gcflags, and return an env accordingly.
Expand All @@ -136,6 +140,9 @@ def get_build_flags(
extldflags = ""
env = {"GO111MODULE": "on"}

# replace the install dir with the deafult path if it's empty or None
install_dir = install_dir or DEFAULT_AGENT_PATH

if sys.platform == 'win32':
env["CGO_LDFLAGS_ALLOW"] = "-Wl,--allow-multiple-definition"
else:
Expand Down Expand Up @@ -170,6 +177,8 @@ def get_build_flags(

ldflags += f"-X {REPO_PATH}/pkg/config.DefaultPython={get_default_python(python_runtimes)} "

ldflags += f"-X {REPO_PATH}/pkg/version.AgentPath={install_dir} "

# adding rtloader libs and headers to the env
if rtloader_lib:
if not headless_mode:
Expand Down Expand Up @@ -215,13 +224,13 @@ def get_build_flags(
extldflags += "-Wl,-bind_at_load "

if extldflags:
ldflags += f"'-extldflags={extldflags}' "
ldflags += f"'-extldflags={extldflags}'"

# Needed to fix an issue when using -race + gcc 10.x on Windows
# https://github.com/bazelbuild/rules_go/issues/2614
if race:
if sys.platform == 'win32':
ldflags += " -linkmode=external"
ldflags += ["-linkmode=external"]

return ldflags, gcflags, env

Expand Down Expand Up @@ -502,7 +511,7 @@ def generate_config(ctx, build_type, output_file, env=None):


##
## release.json entry mapping functions
# release.json entry mapping functions
##


Expand Down

0 comments on commit 435c12b

Please sign in to comment.