Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deb: improve process timing for safety and simplicity #762

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ migration_from_v4_post_process() {
fi
}

local_base_plugins=/tmp/<%= package_dir %>/.local_base_plugins
local_base_plugins="/tmp/<%= package_dir %>/.previous_plugin_list"
install_missing_plugins() {
# Install missing gems (even though systemd is not available, it works)
if [ -f $local_base_plugins ]; then
Expand All @@ -168,31 +168,17 @@ install_missing_plugins() {
fi
}

zero_downtime_restart_supported=/tmp/<%= package_dir %>/.zero_downtime_restart_supported
pid_for_auto_restart="/tmp/<%= package_dir %>/.pid_for_auto_restart"
fluentd_auto_restart() {
if [ ! -e "$zero_downtime_restart_supported" ]; then
if [ ! -f "$pid_for_auto_restart" ]; then
return
fi

if [ -d /run/systemd/system ]; then
pid=$(systemctl show <%= service_name %> --property=MainPID --value)
if [ $pid -ne 0 ]; then
. /etc/default/<%= service_name %>
case "$FLUENT_PACKAGE_SERVICE_RESTART" in
auto)
echo "Kick auto service upgrade mode to MainPID:$pid"
kill -USR2 $pid
;;
manual)
echo "No need to restart service in manual mode..."
;;
*)
echo "Nothing to be done..."
;;
esac
fi
fi
rm -rf $zero_downtime_restart_supported
pid=$(cat "$pid_for_auto_restart")
echo "Kick auto restart to MainPID:$pid"
kill -USR2 $pid

rm -f "$pid_for_auto_restart"
}

case "$1" in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ purge_bin_symlinks() {
fi
}

purge_tmp_files_for_upgrade() {
rm -f "/tmp/<%= package_dir %>/.plugin_list"
rm -f "/tmp/<%= package_dir %>/.main_pid"
}

case $1 in
remove)
purge_var_run
Expand All @@ -72,6 +77,9 @@ case $1 in
purge_users
purge_bin_symlinks
;;
upgrade)
purge_tmp_files_for_upgrade
;;
*)
# nothing to do for upgrade, failed-upgrade, abort-install, abort-upgrade
;;
Expand Down
41 changes: 13 additions & 28 deletions fluent-package/templates/package-scripts/fluent-package/deb/preinst
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,25 @@ set -e
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package.

local_base_plugins=/tmp/<%= package_dir %>/.local_base_plugins
migrate_local_plugins() {
if [ "$FLUENT_PACKAGE_SERVICE_RESTART" != auto ]; then
return
mark_auto_restart_ready() {
# Copy tmp files made by FROM-side because they should be cleaned postrm of FROM-side.
# (To ensure cleanup, tmp files should be cleaned by its own-side).
# The sequence of these tmp files is as follows:
# 1. FROM-prerm(upgrade): Leave tmp files if need.
# 2. TO-preinst(upgrade): Copy tmp files for TO-side.
# 3. FROM-postrm(upgrade): Clean tmp files of FROM-side(1.).
# 4. TO-postinst(configure): Use and clean tmp files of TO-side(2.).
if [ -f "/tmp/<%= package_dir %>/.plugin_list" ]; then
cp "/tmp/<%= package_dir %>/.plugin_list" "/tmp/<%= package_dir %>/.previous_plugin_list"
daipom marked this conversation as resolved.
Show resolved Hide resolved
fi
if [ ! -d /run/systemd/system ]; then
# tmpfiles.d owns /tmp/<%= package_dir %>, but not created without systemd
mkdir -p /tmp/<%= package_dir %>
fi
# collect list of gems
# We don't use fluent-diagtool here because it depends on systemd and piuparts fails
/usr/sbin/fluent-gem list '^fluent-plugin-' --no-version --no-verbose > $local_base_plugins
}

zero_downtime_restart_supported_version="1.18.0"
zero_downtime_restart_supported=/tmp/<%= package_dir %>/.zero_downtime_restart_supported
check_version() {
printf '%s\n' "$zero_downtime_restart_supported_version" "$1" | sort --check=quiet --version-sort
}
check_whether_zero_downtime_restart_supported() {
current_version=$(/usr/sbin/fluentd --version | cut -d' ' -f 4)
if check_version $current_version; then
echo "Fluentd $current_version supports zero downtime restart."
mkdir -p /tmp/<%= package_dir %>
touch $zero_downtime_restart_supported
if [ -f "/tmp/<%= package_dir %>/.main_pid" ]; then
cp "/tmp/<%= package_dir %>/.main_pid" "/tmp/<%= package_dir %>/.pid_for_auto_restart"
fi
}

case "$1" in
upgrade)
. /etc/default/<%= service_name %>
echo "preinst FLUENT_PACKAGE_SERVICE_RESTART: $FLUENT_PACKAGE_SERVICE_RESTART"
migrate_local_plugins
check_whether_zero_downtime_restart_supported
mark_auto_restart_ready
;;
abort-upgrade)
;;
Expand Down
46 changes: 40 additions & 6 deletions fluent-package/templates/package-scripts/fluent-package/deb/prerm
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,52 @@ set -e
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package.

make_sure_working_dir_exists() {
# This func is for CI ONLY.
# Some CI cases don't provide env with systemd.
# tmpfiles.d owns /tmp/<%= package_dir %>, but not created without systemd
if [ -d /run/systemd/system ]; then
return
fi
mkdir -p "/tmp/<%= package_dir %>"
}

case "$1" in
remove|upgrade|deconfigure)
;;
leave_info_for_auto_restart_if_need() {
# Some CI cases that don't provide env with systemd, so disable auto restart feature in those cases.
if [ ! -d /run/systemd/system ]; then
return
fi

failed-upgrade)
;;
pid="$(systemctl show "<%= service_name %>" --property=MainPID --value)"
if [ $pid -eq 0 ]; then
echo "Do not use auto restart because the service is not active"
return
fi

. "/etc/default/<%= service_name %>"
echo "FLUENT_PACKAGE_SERVICE_RESTART: $FLUENT_PACKAGE_SERVICE_RESTART"
if [ "$FLUENT_PACKAGE_SERVICE_RESTART" != auto ]; then
return
fi
kenhys marked this conversation as resolved.
Show resolved Hide resolved

/usr/sbin/fluent-gem list '^fluent-plugin-' --no-version --no-verbose > "/tmp/<%= package_dir %>/.plugin_list"

echo "$pid" > "/tmp/<%= package_dir %>/.main_pid"
}

case "$1" in
upgrade)
make_sure_working_dir_exists
leave_info_for_auto_restart_if_need
;;
remove|deconfigure)
;;
failed-upgrade)
;;
*)
echo "prerm called with unknown argument '$1'" >&2
exit 1
;;
;;
esac

# dh_installdeb will replace this with shell code automatically
Expand Down
Loading