-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathprocess_agent.py
159 lines (128 loc) · 4.96 KB
/
process_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
import shutil
import sys
import tempfile
from invoke import task
from invoke.exceptions import Exit
from tasks.build_tags import add_fips_tags, filter_incompatible_tags, get_build_tags, get_default_build_tags
from tasks.flavor import AgentFlavor
from tasks.libs.common.utils import REPO_PATH, bin_name, get_build_flags
from tasks.system_probe import copy_ebpf_and_related_files
from tasks.windows_resources import build_messagetable, build_rc, versioninfo_vars
BIN_DIR = os.path.join(".", "bin", "process-agent")
BIN_PATH = os.path.join(BIN_DIR, bin_name("process-agent"))
@task
def build(
ctx,
race=False,
build_include=None,
build_exclude=None,
install_path=None,
flavor=AgentFlavor.base.name,
rebuild=False,
major_version='7',
go_mod="readonly",
):
"""
Build the process agent
"""
flavor = AgentFlavor[flavor]
if flavor.is_ot():
flavor = AgentFlavor.base
fips_mode = flavor.is_fips()
ldflags, gcflags, env = get_build_flags(
ctx,
install_path=install_path,
major_version=major_version,
)
# generate windows resources
if sys.platform == 'win32':
build_messagetable(ctx)
vars = versioninfo_vars(ctx, major_version=major_version)
build_rc(
ctx,
"cmd/process-agent/windows_resources/process-agent.rc",
vars=vars,
out="cmd/process-agent/rsrc.syso",
)
goenv = {}
# extend PATH from gimme with the one from get_build_flags
if "PATH" in os.environ and "PATH" in goenv:
goenv["PATH"] += ":" + os.environ["PATH"]
env.update(goenv)
build_include = (
get_default_build_tags(build="process-agent", flavor=flavor)
if build_include is None
else filter_incompatible_tags(build_include.split(","))
)
build_exclude = [] if build_exclude is None else build_exclude.split(",")
build_tags = get_build_tags(build_include, build_exclude)
build_tags = add_fips_tags(build_tags, fips_mode)
if os.path.exists(BIN_PATH):
os.remove(BIN_PATH)
# TODO static option
cmd = 'go build -mod={go_mod} {race_opt} {build_type} -tags "{go_build_tags}" '
cmd += '-o {agent_bin} -gcflags="{gcflags}" -ldflags="{ldflags}" {REPO_PATH}/cmd/process-agent'
args = {
"go_mod": go_mod,
"race_opt": "-race" if race else "",
"build_type": "-a" if rebuild else "",
"go_build_tags": " ".join(build_tags),
"agent_bin": BIN_PATH,
"gcflags": gcflags,
"ldflags": ldflags,
"REPO_PATH": REPO_PATH,
}
ctx.run(cmd.format(**args), env=env)
class TempDir:
def __enter__(self):
self.fname = tempfile.mkdtemp()
print(f"created tempdir: {self.fname}")
return self.fname
# The _ in front of the unused arguments are needed to pass lint check
def __exit__(self, _exception_type, _exception_value, _exception_traceback):
print(f"deleting tempdir: {self.fname}")
shutil.rmtree(self.fname)
@task
def build_dev_image(ctx, image=None, push=False, base_image="datadog/agent:latest", include_agent_binary=False):
"""
Build a dev image of the process-agent based off an existing datadog-agent image
image: the image name used to tag the image
push: if true, run a docker push on the image
base_image: base the docker image off this already build image (default: datadog/agent:latest)
include_agent_binary: if true, use the agent binary in bin/agent/agent as opposite to the base image's binary
"""
if image is None:
raise Exit(message="image was not specified")
with TempDir() as docker_context:
ctx.run(f"cp tools/ebpf/Dockerfiles/Dockerfile-process-agent-dev {docker_context + '/Dockerfile'}")
ctx.run(f"cp bin/process-agent/process-agent {docker_context + '/process-agent'}")
ctx.run(f"cp bin/system-probe/system-probe {docker_context + '/system-probe'}")
if include_agent_binary:
ctx.run(f"cp bin/agent/agent {docker_context + '/agent'}")
core_agent_dest = "/opt/datadog-agent/bin/agent/agent"
else:
# this is necessary so that the docker build doesn't fail while attempting to copy the agent binary
ctx.run(f"touch {docker_context}/agent")
core_agent_dest = "/dev/null"
copy_ebpf_and_related_files(ctx, docker_context)
with ctx.cd(docker_context):
# --pull in the build will force docker to grab the latest base image
ctx.run(
f"docker build --pull --tag {image} --build-arg AGENT_BASE={base_image} --build-arg CORE_AGENT_DEST={core_agent_dest} ."
)
if push:
ctx.run(f"docker push {image}")
@task
def go_generate(ctx):
"""
Run the go generate directives inside the /pkg/process directory
"""
with ctx.cd("./pkg/process/events/model"):
ctx.run("go generate ./...")
@task
def gen_mocks(ctx):
"""
Generate mocks
"""
ctx.run("mockery")