Skip to content

Commit de24d25

Browse files
committed
Migrate redhat init scripts to bazel.
1 parent 3f204ca commit de24d25

File tree

9 files changed

+124
-50
lines changed

9 files changed

+124
-50
lines changed

bazel/rules/dd_agent_expand_template.bzl

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""dd_agent_expand_template. Expand a template, splicing in agent specific flags."""
22

3+
load("@bazel_skylib//lib:paths.bzl", "paths")
34
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
5+
load("@rules_pkg//pkg:providers.bzl", "PackageFilesInfo")
46

57
# The place we will install to if we run bazel pkg_install without a destdir
68
# We use /tmp for lack of a better safe space.
@@ -26,7 +28,9 @@ def _dd_agent_expand_template_impl(ctx):
2628

2729
# TODO: decide if we should default etc to the output base or relative to install_dir.
2830
# There are use cases for either. For now, we are relative to the output base.
29-
subs["{etc_dir}"] = subs["{output_config_dir}"] + "/etc"
31+
# Note that the choice of /etc/datadog-agent seems odd, in that /etc is usually /etc,
32+
# but this matches what is done in the current product packaging.
33+
subs["{etc_dir}"] = subs["{output_config_dir}"] + "/etc/datadog-agent"
3034

3135
# Now let local substitutions override the flags.
3236
subs.update(ctx.attr.substitutions)
@@ -42,7 +46,30 @@ def _dd_agent_expand_template_impl(ctx):
4246
output = ctx.outputs.out,
4347
substitutions = subs,
4448
)
45-
return [DefaultInfo(files = depset([ctx.outputs.out]))]
49+
50+
# Now build an output path relative to this package.
51+
workspace_root = paths.join("..", ctx.label.workspace_name) if ctx.label.workspace_name else ""
52+
package_path = paths.join(workspace_root, ctx.outputs.out.owner.package)
53+
dest_path = paths.relativize(ctx.outputs.out.short_path, package_path)
54+
destination = paths.join(ctx.attr.prefix, dest_path)
55+
return [
56+
DefaultInfo(files = depset([ctx.outputs.out])),
57+
PackageFilesInfo(
58+
dest_src_map = {destination: ctx.outputs.out},
59+
attributes = json.decode(ctx.attr.attributes),
60+
),
61+
]
62+
63+
OUT_DOC = """The destination of the expanded file.
64+
65+
When this target is consummed by pkg_* rules, the output destinaion
66+
of the file is computed relative to the package. This is effectively
67+
the equivalent to using `strip_prefix=strip_prefix.from_pkg()` of a
68+
`pkg_files` rule.
69+
70+
If `prefix` is also specified, that is applied after computing the
71+
package relative path.
72+
"""
4673

4774
dd_agent_expand_template = rule(
4875
implementation = _dd_agent_expand_template_impl,
@@ -70,10 +97,12 @@ explicitly add delimiters to the key strings, for example "{KEY}" or "@KEY@"."""
7097
"substitutions": attr.string_dict(
7198
doc = "A dictionary mapping strings to their substitutions. These take precedence over flags.",
7299
),
73-
"out": attr.output(
74-
mandatory = True,
75-
doc = "The destination of the expanded file.",
100+
"out": attr.output(mandatory = True, doc = OUT_DOC),
101+
"attributes": attr.string(
102+
doc = """See @rules_pkg for documentation.""",
103+
default = "{}", # Empty JSON
76104
),
105+
"prefix": attr.string(doc = """See @rules_pkg for documentation."""),
77106
"_install_dir": attr.label(default = "@@//:install_dir"),
78107
"_output_config_dir": attr.label(default = "@@//:output_config_dir"),
79108
},

omnibus/config/software/init-scripts-agent.rb

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
etc_dir = "#{output_config_dir}/etc/datadog-agent"
1313
mkdir "/etc/init"
1414
if debian_target?
15-
# building into / is not acceptable. We'll continue to to that for now,
16-
# but the replacement has to build to a build output tree.
17-
command_on_repo_root "bazelisk run --//:output_config_dir='#{output_config_dir}' --//:install_dir=#{install_dir} -- //packages/debian/etc:install --verbose --destdir=#{destdir}"
18-
command_on_repo_root "cp bazel-bin/packages/debian/etc/etc/init/datadog-agent.conf /etc/init"
19-
2015
# sysvinit support for debian only for now
2116
mkdir "/etc/init.d"
2217

18+
# building into / is not acceptable. We'll continue to to that for now,
19+
# but the replacement has to build to a build output tree.
20+
command_on_repo_root "bazelisk run --//:output_config_dir='#{output_config_dir}' --//:install_dir=#{install_dir} -- //packages/debian/etc:install --verbose --destdir=#{destdir}"
21+
# command_on_repo_root "cp bazel-bin/packages/debian/etc/etc/init/datadog-agent.conf /etc/init"
2322
erb source: "upstart_debian.process.conf.erb",
2423
dest: "/etc/init/datadog-agent-process.conf",
2524
mode: 0644,
@@ -61,38 +60,14 @@
6160
mode: 0755,
6261
vars: { install_dir: install_dir, etc_dir: etc_dir }
6362

63+
project.extra_package_file '/etc/init/datadog-agent.conf'
6464
project.extra_package_file '/etc/init.d/datadog-agent'
6565
project.extra_package_file '/etc/init.d/datadog-agent-process'
6666
project.extra_package_file '/etc/init.d/datadog-agent-trace'
6767
project.extra_package_file '/etc/init.d/datadog-agent-security'
6868
project.extra_package_file '/etc/init.d/datadog-agent-data-plane'
6969
elsif redhat_target? || suse_target?
70-
# Ship a different upstart job definition on RHEL to accommodate the old
71-
# version of upstart (0.6.5) that RHEL 6 provides.
72-
erb source: "upstart_redhat.conf.erb",
73-
dest: "/etc/init/datadog-agent.conf",
74-
mode: 0644,
75-
vars: { install_dir: install_dir, etc_dir: etc_dir }
76-
erb source: "upstart_redhat.process.conf.erb",
77-
dest: "/etc/init/datadog-agent-process.conf",
78-
mode: 0644,
79-
vars: { install_dir: install_dir, etc_dir: etc_dir }
80-
erb source: "upstart_redhat.sysprobe.conf.erb",
81-
dest: "/etc/init/datadog-agent-sysprobe.conf",
82-
mode: 0644,
83-
vars: { install_dir: install_dir, etc_dir: etc_dir }
84-
erb source: "upstart_redhat.trace.conf.erb",
85-
dest: "/etc/init/datadog-agent-trace.conf",
86-
mode: 0644,
87-
vars: { install_dir: install_dir, etc_dir: etc_dir }
88-
erb source: "upstart_redhat.security.conf.erb",
89-
dest: "/etc/init/datadog-agent-security.conf",
90-
mode: 0644,
91-
vars: { install_dir: install_dir, etc_dir: etc_dir }
92-
erb source: "upstart_redhat.data-plane.conf.erb",
93-
dest: "/etc/init/datadog-agent-data-plane.conf",
94-
mode: 0644,
95-
vars: { install_dir: install_dir, etc_dir: etc_dir }
70+
command_on_repo_root "bazelisk run --//:output_config_dir='#{output_config_dir}' --//:install_dir=#{install_dir} -- //packages/redhat/etc:install --verbose --destdir=#{destdir}"
9671
end
9772
project.extra_package_file '/etc/init/datadog-agent.conf'
9873
project.extra_package_file '/etc/init/datadog-agent-process.conf'

packages/redhat/etc/BUILD.bazel

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""Rules to build /etc for Debian distributions."""
2+
3+
load("@rules_pkg//pkg:install.bzl", "pkg_install")
4+
load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_filegroup")
5+
load("//bazel/rules:dd_agent_expand_template.bzl", "dd_agent_expand_template")
6+
7+
package(default_visibility = ["//packages:__subpackages__"])
8+
9+
# Ship a different upstart job definition from debian on RHEL to accommodate
10+
# the old version of upstart (0.6.5) that RHEL 6 provides.
11+
dd_agent_expand_template(
12+
name = "upstart_redhat_conf_gen",
13+
out = "etc/init/datadog-agent.conf",
14+
attributes = pkg_attributes(mode = "0644"),
15+
template = "upstart_redhat.conf.in",
16+
)
17+
18+
dd_agent_expand_template(
19+
name = "upstart_redhat_process_conf_gen",
20+
out = "etc/init/datadog-agent-process.conf",
21+
attributes = pkg_attributes(mode = "0644"),
22+
template = "upstart_redhat.process.conf.in",
23+
)
24+
25+
dd_agent_expand_template(
26+
name = "upstart_redhat_sysprobe_conf_gen",
27+
out = "etc/init/datadog-agent-sysprobe.conf",
28+
attributes = pkg_attributes(mode = "0644"),
29+
template = "upstart_redhat.sysprobe.conf.in",
30+
)
31+
32+
dd_agent_expand_template(
33+
name = "upstart_redhat_trace_conf_gen",
34+
out = "etc/init/datadog-agent-trace.conf",
35+
attributes = pkg_attributes(mode = "0644"),
36+
template = "upstart_redhat.trace.conf.in",
37+
)
38+
39+
dd_agent_expand_template(
40+
name = "upstart_redhat_security_conf_gen",
41+
out = "etc/init/datadog-agent-security.conf",
42+
attributes = pkg_attributes(mode = "0644"),
43+
template = "upstart_redhat.security.conf.in",
44+
)
45+
46+
dd_agent_expand_template(
47+
name = "upstart_redhat_data_plane_conf_gen",
48+
out = "etc/init/datadog-agent-data-plane.conf",
49+
attributes = pkg_attributes(mode = "0644"),
50+
template = "upstart_redhat.data-plane.conf.in",
51+
)
52+
53+
pkg_filegroup(
54+
name = "all_files",
55+
srcs = [
56+
":upstart_redhat_conf_gen",
57+
":upstart_redhat_data_plane_conf_gen",
58+
":upstart_redhat_process_conf_gen",
59+
":upstart_redhat_security_conf_gen",
60+
":upstart_redhat_sysprobe_conf_gen",
61+
":upstart_redhat_trace_conf_gen",
62+
],
63+
)
64+
65+
pkg_install(
66+
name = "install",
67+
srcs = [
68+
":all_files",
69+
],
70+
)

omnibus/config/templates/init-scripts-agent/upstart_redhat.conf.erb renamed to packages/redhat/etc/upstart_redhat.conf.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ script
1616
#
1717
# setuid is not available in versions of upstart before 1.4. CentOS/RHEL6 use an earlier version of upstart.
1818
# This is the best way to set the user in the absence of setuid.
19-
exec su -s /bin/sh -c 'DD_LOG_TO_CONSOLE=false exec "$0" "$@"' dd-agent -- <%= install_dir %>/bin/agent/agent run -p <%= install_dir %>/run/agent.pid &>> /var/log/datadog/errors.log
19+
exec su -s /bin/sh -c 'DD_LOG_TO_CONSOLE=false exec "$0" "$@"' dd-agent -- {install_dir}/bin/agent/agent run -p {install_dir}/run/agent.pid &>> /var/log/datadog/errors.log
2020
end script
2121

2222
pre-start script
@@ -29,5 +29,5 @@ pre-start script
2929
end script
3030

3131
post-stop script
32-
rm -f <%= install_dir %>/run/agent.pid
32+
rm -f {install_dir}/run/agent.pid
3333
end script

omnibus/config/templates/init-scripts-agent/upstart_redhat.data-plane.conf.erb renamed to packages/redhat/etc/upstart_redhat.data-plane.conf.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ script
1616
#
1717
# setuid is not available in versions of upstart before 1.4. CentOS/RHEL6 use an earlier version of upstart.
1818
# This is the best way to set the user in the absence of setuid.
19-
exec su -s /bin/sh -c 'DD_LOG_TO_CONSOLE=false exec "$0" "$@"' dd-agent -- <%= install_dir %>/embedded/bin/agent-data-plane run --config <%= etc_dir %>/datadog.yaml --pidfile <%= install_dir %>/run/agent-data-plane.pid &>> /var/log/datadog/data-plane-errors.log
19+
exec su -s /bin/sh -c 'DD_LOG_TO_CONSOLE=false exec "$0" "$@"' dd-agent -- {install_dir}/embedded/bin/agent-data-plane run --config {etc_dir}/datadog.yaml --pidfile {install_dir}/run/agent-data-plane.pid &>> /var/log/datadog/data-plane-errors.log
2020
end script
2121

2222
pre-start script
@@ -29,5 +29,5 @@ pre-start script
2929
end script
3030

3131
post-stop script
32-
rm -f <%= install_dir %>/run/agent-data-plane.pid
32+
rm -f {install_dir}/run/agent-data-plane.pid
3333
end script

omnibus/config/templates/init-scripts-agent/upstart_redhat.process.conf.erb renamed to packages/redhat/etc/upstart_redhat.process.conf.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ script
1616
#
1717
# setuid is not available in versions of upstart before 1.4. CentOS/RHEL6 use an earlier version of upstart.
1818
# This is the best way to set the user in the absence of setuid.
19-
exec su -s /bin/sh -c 'DD_LOG_TO_CONSOLE=false exec "$0" "$@"' dd-agent -- <%= install_dir %>/embedded/bin/process-agent --cfgpath=<%= etc_dir %>/datadog.yaml --sysprobe-config=<%= etc_dir %>/system-probe.yaml --pid=<%= install_dir %>/run/process-agent.pid &>> /var/log/datadog/process-errors.log
19+
exec su -s /bin/sh -c 'DD_LOG_TO_CONSOLE=false exec "$0" "$@"' dd-agent -- {install_dir}/embedded/bin/process-agent --cfgpath={etc_dir}/datadog.yaml --sysprobe-config={etc_dir}/system-probe.yaml --pid={install_dir}/run/process-agent.pid &>> /var/log/datadog/process-errors.log
2020
end script
2121

2222
pre-start script
@@ -29,5 +29,5 @@ pre-start script
2929
end script
3030

3131
post-stop script
32-
rm -f <%= install_dir %>/run/process-agent.pid
32+
rm -f {install_dir}/run/process-agent.pid
3333
end script

omnibus/config/templates/init-scripts-agent/upstart_redhat.security.conf.erb renamed to packages/redhat/etc/upstart_redhat.security.conf.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ script
1313
# Logging to console from the agent is disabled since the agent already logs using file or
1414
# syslog depending on its configuration. We then redirect the stdout/stderr of the agent process
1515
# to log panic/crashes.
16-
exec <%= install_dir %>/embedded/bin/security-agent start -c <%= etc_dir %>/datadog.yaml -c <%= etc_dir %>/security-agent.yaml --sysprobe-config <%= etc_dir %>/system-probe.yaml -p <%= install_dir %>/run/security-agent.pid &>> /var/log/datadog/security-errors.log
16+
exec {install_dir}/embedded/bin/security-agent start -c {etc_dir}/datadog.yaml -c {etc_dir}/security-agent.yaml --sysprobe-config {etc_dir}/system-probe.yaml -p {install_dir}/run/security-agent.pid &>> /var/log/datadog/security-errors.log
1717
end script
1818

1919
pre-start script
2020
# Do not run if security-agent.yaml does not exist
21-
test -f <%= etc_dir %>/security-agent.yaml || { stop ; exit 0; }
21+
test -f {etc_dir}/security-agent.yaml || { stop ; exit 0; }
2222

2323
# Manual rotation of errors log
2424
log_file_size=`du -b /var/log/datadog/security-errors.log | cut -f1`
@@ -29,5 +29,5 @@ pre-start script
2929
end script
3030

3131
post-stop script
32-
rm -f <%= install_dir %>/run/security-agent.pid
32+
rm -f {install_dir}/run/security-agent.pid
3333
end script

omnibus/config/templates/init-scripts-agent/upstart_redhat.sysprobe.conf.erb renamed to packages/redhat/etc/upstart_redhat.sysprobe.conf.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ console output
1111

1212
pre-start script
1313
# Do not run if config file doesn't exist
14-
test -f <%= etc_dir %>/system-probe.yaml || { stop ; exit 0; }
14+
test -f {etc_dir}/system-probe.yaml || { stop ; exit 0; }
1515

1616
# Manual rotation of errors log
1717
log_file_size=`du -b /var/log/datadog/system-probe-errors.log | cut -f1`
@@ -32,9 +32,9 @@ script
3232
# Logging to console from the agent is disabled since the agent already logs using file or
3333
# syslog depending on its configuration. We then redirect the stdout/stderr of the agent process
3434
# to log panic/crashes.
35-
exec <%= install_dir %>/embedded/bin/system-probe run --config=<%= etc_dir %>/system-probe.yaml --pid=<%= install_dir %>/run/system-probe.pid &>> /var/log/datadog/system-probe-errors.log
35+
exec {install_dir}/embedded/bin/system-probe run --config={etc_dir}/system-probe.yaml --pid={install_dir}/run/system-probe.pid &>> /var/log/datadog/system-probe-errors.log
3636
end script
3737

3838
post-stop script
39-
rm -f <%= install_dir %>/run/system-probe.pid
39+
rm -f {install_dir}/run/system-probe.pid
4040
end script

omnibus/config/templates/init-scripts-agent/upstart_redhat.trace.conf.erb renamed to packages/redhat/etc/upstart_redhat.trace.conf.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ script
1616
#
1717
# setuid is not available in versions of upstart before 1.4. CentOS/RHEL6 use an earlier version of upstart.
1818
# This is the best way to set the user in the absence of setuid.
19-
exec su -s /bin/sh -c 'DD_LOG_TO_CONSOLE=false exec "$0" "$@"' dd-agent -- <%= install_dir %>/embedded/bin/trace-loader <%= etc_dir %>/datadog.yaml <%= install_dir %>/embedded/bin/trace-agent --config <%= etc_dir %>/datadog.yaml --pidfile <%= install_dir %>/run/trace-agent.pid &>> /var/log/datadog/trace-errors.log
19+
exec su -s /bin/sh -c 'DD_LOG_TO_CONSOLE=false exec "$0" "$@"' dd-agent -- {install_dir}/embedded/bin/trace-loader {etc_dir}/datadog.yaml {install_dir}/embedded/bin/trace-agent --config {etc_dir}/datadog.yaml --pidfile {install_dir}/run/trace-agent.pid &>> /var/log/datadog/trace-errors.log
2020
end script
2121

2222
pre-start script
@@ -29,5 +29,5 @@ pre-start script
2929
end script
3030

3131
post-stop script
32-
rm -f <%= install_dir %>/run/trace-agent.pid
32+
rm -f {install_dir}/run/trace-agent.pid
3333
end script

0 commit comments

Comments
 (0)