Skip to content

Commit

Permalink
Embed fluent package info into environment variable
Browse files Browse the repository at this point in the history
It is aimed to detect current running process's fluentd version and
so on from /proc/id/environ.

This fix introduce the following environment variables which contains
similar information as same as /usr/sbin/fluentd -v

* FLUENT_PACKAGE_VERSION
* FLUENT_PACKAGE_FLUENTD_VERSION
* FLUENT_PACKAGE_FLUENTD_REVISION

Signed-off-by: Kentaro Hayashi <[email protected]>
  • Loading branch information
kenhys committed Oct 9, 2024
1 parent b0dde24 commit c13789b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/apt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ jobs:
test-file:
- "update-from-v4.sh"
- "update-to-next-version-with-backward-compat-for-v4.sh"
- "update-to-next-version.sh"
- "downgrade-to-v4.sh"
- "install-newly.sh local"
- "install-newly.sh v5"
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/yum.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ jobs:
test-file:
- "update-from-v4.sh"
- "update-to-next-version-with-backward-compat-for-v4.sh"
- "update-to-next-version.sh"
- "downgrade-to-v4.sh"
- "install-newly.sh local"
- "install-newly.sh v5"
Expand Down Expand Up @@ -153,6 +154,7 @@ jobs:
test-file:
- "update-from-v4.sh"
- "update-to-next-version-with-backward-compat-for-v4.sh"
- "update-to-next-version.sh"
- "downgrade-to-v4.sh"
- "install-newly.sh local"
- "install-newly.sh v5"
Expand Down
29 changes: 27 additions & 2 deletions fluent-package/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,35 @@ def template_params(params = nil)
end

if params
if params[:fluentd_version]
config.merge!({fluentd_version: params[:fluentd_version],
fluentd_revision: FLUENTD_REVISION})
end
config.merge(params)
else
config
end
end

def fluent_package_info(version)
"#{PACKAGE_NAME} #{PACKAGE_VERSION} fluentd #{version} (#{FLUENTD_REVISION})"
end

def extract_fluentd_version(archive_path)
puts "::group::Extract fluentd version from: #{archive_path}" if ENV["CI"]
fluentd_version = ""
sh("tar", "xvf", archive_path, "-C", File.dirname(archive_path), "--no-same-owner")
archive_dir = File.join(File.dirname(archive_path),
File.basename(archive_path, ".tar.gz"))
Dir.chdir(archive_dir) do
IO.popen("git tag --contains #{FLUENTD_REVISION}") do |tags|
fluentd_version = tags.readlines.last.chomp.delete_prefix("v")
end
end
puts "::endgroup::" if ENV["CI"]
fluentd_version
end

def template_path(*path_parts)
File.join('templates', *path_parts)
end
Expand Down Expand Up @@ -586,15 +609,17 @@ class BuildTask
desc "Create systemd unit file for Red Hat like systems"
task :rpm_systemd do
dest = File.join(STAGING_DIR, 'usr', 'lib', 'systemd', 'system', SERVICE_NAME + ".service")
params = {pkg_type: "rpm"}
version = extract_fluentd_version(@download_task.file_fluentd_archive)
params = {pkg_type: "rpm", fluentd_version: version}
render_systemd_unit_file(dest, template_params(params))
render_systemd_environment_file(template_params(params))
end

desc "Create systemd unit file for Debian like systems"
task :deb_systemd do
dest = File.join(STAGING_DIR, 'lib', 'systemd', 'system', SERVICE_NAME + ".service")
params = {pkg_type: "deb"}
version = extract_fluentd_version(@download_task.file_fluentd_archive)
params = {pkg_type: "deb", fluentd_version: version}
render_systemd_unit_file(dest, template_params(params))
render_systemd_environment_file(template_params(params))
end
Expand Down
5 changes: 5 additions & 0 deletions fluent-package/apt/systemd-test/update-to-next-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ sudo apt install -V -y \
dpkg-deb -R /host/${distribution}/pool/${code_name}/${channel}/*/*/fluent-package_*_${architecture}.deb tmp
last_ver=$(cat tmp/DEBIAN/control | grep "Version: " | sed -E "s/Version: ([0-9.]+)-([0-9]+)/\2/g")
sed -i -E "s/Version: ([0-9.]+)-([0-9]+)/Version: \1-$(($last_ver+1))/g" tmp/DEBIAN/control
# Bump up x.y.w.z to check whether FLUENT_PACKAGE_VERSION was updated correctly later
sed -i -E "s/FLUENT_PACKAGE_VERSION=([0-9.]+)/FLUENT_PACKAGE_VERSION=\1.1/g" tmp/lib/systemd/system/fluentd.service
next_package_ver=$(cat tmp/lib/systemd/system/fluentd.service | grep "FLUENT_PACKAGE_VERSION" | sed -E "s/Environment=FLUENT_PACKAGE_VERSION=(.+)/\1/")
echo "repacked next fluent-package version: $next_package_ver"
dpkg-deb --build tmp next_version.deb

# Install the dummy package
Expand All @@ -38,6 +42,7 @@ test $(eval $env_vars && echo $FLUENT_CONF) = "/etc/fluent/fluentd.conf"
test $(eval $env_vars && echo $FLUENT_PACKAGE_LOG_FILE) = "/var/log/fluent/fluentd.log"
test $(eval $env_vars && echo $FLUENT_PLUGIN) = "/etc/fluent/plugin"
test $(eval $env_vars && echo $FLUENT_SOCKET) = "/var/run/fluent/fluentd.sock"
test $(eval $env_vars && echo $FLUENT_PACKAGE_VERSION) = "$next_package_ver"

# Test: fluent-diagtool
sudo fluent-gem install fluent-plugin-concat
Expand Down
3 changes: 3 additions & 0 deletions fluent-package/templates/etc/systemd/fluentd.service.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Environment=FLUENT_CONF=/etc/<%= package_dir %>/<%= service_name %>.conf
Environment=FLUENT_PLUGIN=/etc/<%= package_dir %>/plugin
Environment=FLUENT_SOCKET=/var/run/<%= package_dir %>/<%= service_name %>.sock
Environment=FLUENT_PACKAGE_LOG_FILE=/var/log/<%= package_dir %>/<%= service_name %>.log
Environment=FLUENT_PACKAGE_VERSION=<%= version %>
Environment=FLUENT_PACKAGE_FLUENTD_VERSION=<%= fluentd_version %>
Environment=FLUENT_PACKAGE_FLUENTD_REVISION=<%= fluentd_revision %>
<% if pkg_type == 'deb' %>
EnvironmentFile=-/etc/default/<%= service_name %>
<% else %>
Expand Down
17 changes: 16 additions & 1 deletion fluent-package/yum/systemd-test/update-to-next-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ release=$(rpmquery --queryformat="%{Release}" -p $package)
release_ver=$(echo $release | cut -d . -f1)
# Example: "2.el9"
next_release=$(($release_ver+1)).$(echo $release | cut -d. -f2)
rpmrebuild --release=$next_release --package $package
rpmrebuild --release=$next_release --modify="find $HOME -name fluentd.service | xargs sed -i -E 's/FLUENT_PACKAGE_VERSION=([0-9.]+)/FLUENT_PACKAGE_VERSION=\1.1/g'" --package $package
next_package=$(find rpmbuild -name "*.rpm")
rpm2cpio $next_package | cpio -id ./usr/lib/systemd/system/fluentd.service
next_package_ver=$(cat ./usr/lib/systemd/system/fluentd.service | grep "FLUENT_PACKAGE_VERSION" | sed -E "s/Environment=FLUENT_PACKAGE_VERSION=(.+)/\1/")
echo "repacked next fluent-package version: $next_package_ver"

# Install the dummy package of the next version
sudo $DNF install -y ./$next_package
Expand All @@ -62,6 +65,18 @@ test $(eval $env_vars && echo $FLUENT_CONF) = "/etc/fluent/fluentd.conf"
test $(eval $env_vars && echo $FLUENT_PACKAGE_LOG_FILE) = "/var/log/fluent/fluentd.log"
test $(eval $env_vars && echo $FLUENT_PLUGIN) = "/etc/fluent/plugin"
test $(eval $env_vars && echo $FLUENT_SOCKET) = "/var/run/fluent/fluentd.sock"
cpe_name=$(grep CPE_NAME /etc/os-release | cut -d'=' -f2)
case $cpe_name in
*rocky:8*)
# RHEL8 %systemd_postun_with_restart doesn't restart when already service is running
# thus, FLUENT_PACKAGE_VERSION will be kept.
release=$(rpmquery --queryformat="%{Version}" -p $package)
test $(eval $env_vars && echo $FLUENT_PACKAGE_VERSION) = "$release"
;;
*)
test $(eval $env_vars && echo $FLUENT_PACKAGE_VERSION) = "$next_package_ver"
;;
esac

# Test: logs
sleep 3
Expand Down

0 comments on commit c13789b

Please sign in to comment.