Skip to content

Commit f2b24ab

Browse files
daipomkenhys
authored andcommitted
rpm: improve process timing for safety (#764)
Points * FROM-package just leaves tmp files if it supports the features. * TO-package trigger the features if there are those tmp files. * Thus, can ensure that both FROM and TO support the feature. * Make installing plugin and restarting the same condition. * Disable `%systemd_postun_with_restart` completely to align specifications with DEB. Before 1. to-pre(2): Collect plugin-list. 2. Install TO-package 3. to-post(2): Install plugin. 4. from-preun(1): Do nothing. 5. Uninstall FROM-package 6. from-postun(1): Restart if need. After 1. to-pre(2): Collect plugin-list. 2. Install TO-package 3. to-post(2): Do nothing. 4. from-preun(1): * Check auto or not. * Leave plugin-install flag and pid if need. 5. Uninstall FROM-package 6. from-postun(1): Disable `%systemd_postun_with_restart`. 7. to-posttrans: Install plugin and restart if need. Signed-off-by: Daijiro Fukuda <fukuda@clear-code.com>
1 parent 66b8947 commit f2b24ab

File tree

5 files changed

+101
-56
lines changed

5 files changed

+101
-56
lines changed

.github/workflows/yum.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,12 @@ jobs:
153153
- "update-to-next-version-service-status.sh disabled active"
154154
- "update-to-next-version-service-status.sh disabled inactive"
155155
- "update-to-next-version-with-auto-and-manual.sh"
156-
- "update-to-next-major-version.sh auto"
157-
- "update-to-next-major-version.sh manual"
158-
- "update-to-next-major-version.sh etc"
156+
- "update-to-next-major-version.sh auto active"
157+
- "update-to-next-major-version.sh auto inactive"
158+
- "update-to-next-major-version.sh manual active"
159+
- "update-to-next-major-version.sh manual inactive"
160+
- "update-to-next-major-version.sh etc active"
161+
- "update-to-next-major-version.sh etc inactive"
159162
- "update-without-data-lost.sh v5 v6"
160163
- "update-without-data-lost.sh v6 v5"
161164
include:
@@ -229,9 +232,12 @@ jobs:
229232
- "update-to-next-version-service-status.sh disabled active"
230233
- "update-to-next-version-service-status.sh disabled inactive"
231234
- "update-to-next-version-with-auto-and-manual.sh"
232-
- "update-to-next-major-version.sh auto"
233-
- "update-to-next-major-version.sh manual"
234-
- "update-to-next-major-version.sh etc"
235+
- "update-to-next-major-version.sh auto active"
236+
- "update-to-next-major-version.sh auto inactive"
237+
- "update-to-next-major-version.sh manual active"
238+
- "update-to-next-major-version.sh manual inactive"
239+
- "update-to-next-major-version.sh etc active"
240+
- "update-to-next-major-version.sh etc inactive"
235241
- "update-without-data-lost.sh v5 v6"
236242
- "update-without-data-lost.sh v6 v5"
237243
include:

fluent-package/apt/systemd-test/downgrade-to-v5-lts.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ systemctl status --no-pager fluentd
2525
systemctl status --no-pager td-agent
2626

2727
# Fluentd should be restarted.
28+
# NOTE: Unlike RPM, the restart behavior depends on TO-side. So, it restarts.
2829
test $main_pid -ne $(eval $(systemctl show fluentd --property=MainPID) && echo $MainPID)

fluent-package/yum/fluent-package.spec.in

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
%define v4migration_old_rotate_config_saved /tmp/@PACKAGE_DIR@/.old_rotate_config
3434
%define v4migration_enabled_service /tmp/@PACKAGE_DIR@/.v4migration_enabled_service
3535
%define local_base_plugins /tmp/@PACKAGE_DIR@/.local_base_plugins
36+
%define install_plugins /tmp/@PACKAGE_DIR@/.install_plugins
37+
%define pid_for_auto_restart /tmp/@PACKAGE_DIR@/.pid_for_auto_restart
3638

3739
# Omit the brp-python-bytecompile automagic because post hook for ffi fails on AmazonLinux 2.
3840
%if %{_amazon_ver} == 2
@@ -160,6 +162,14 @@ mkdir -p %{buildroot}%{_sysconfdir}/@PACKAGE_DIR@/plugin
160162
mkdir -p %{buildroot}/tmp/@PACKAGE_DIR@
161163

162164
%pre
165+
# Make sure the previous tmp files for auto restart does not remain.
166+
# Note:
167+
# %preun (FROM-side) can create these files, but they will be removed in %posttrans (TO-side).
168+
# This means that these files may not be removed depending on the version of TO-side.
169+
# In the future, want to figure out a more secure way to manage tmp files...
170+
rm -f %{install_plugins}
171+
rm -f %{pid_for_auto_restart}
172+
163173
if ! getent group @COMPAT_SERVICE_NAME@ >/dev/null; then
164174
if ! getent group @SERVICE_NAME@ >/dev/null; then
165175
/usr/sbin/groupadd --system @SERVICE_NAME@
@@ -188,16 +198,35 @@ else
188198
fi
189199
fi
190200
if [ $1 -eq 2 ]; then
191-
. %{_sysconfdir}/sysconfig/@SERVICE_NAME@
192-
echo "pre FLUENT_PACKAGE_SERVICE_RESTART: $FLUENT_PACKAGE_SERVICE_RESTART"
193-
if [ "$FLUENT_PACKAGE_SERVICE_RESTART" = auto ]; then
194-
# collect installed gems during upgrading
201+
# Collect plugin-list.
202+
# Note:
203+
# This should be done in %preun(1) of FROM-side, but we have no choice but to do this here.
204+
# %preun(1) of FROM-side is executed after TO-side installs the files and replaces embedded Ruby.
205+
# So, this needs to be done before it.
206+
if [ -e /usr/sbin/fluent-gem ]; then
195207
/usr/sbin/fluent-gem list '^fluent-plugin-' --no-versions --no-verbose > %{local_base_plugins}
196208
fi
197209
fi
198210

199211
%preun
200212
%systemd_preun @SERVICE_NAME@.service
213+
if [ $1 -eq 1 ]; then
214+
# systemctl ... --property=MainPID --value is available since systemd 230 or later.
215+
# thus for amazonlinux:2, it can not be used.
216+
pid="$(systemctl show "@SERVICE_NAME@" --property=MainPID | cut -d'=' -f2)"
217+
if [ "$pid" -eq 0 ]; then
218+
echo "Do not use auto restart because the service is not active"
219+
else
220+
. %{_sysconfdir}/sysconfig/@SERVICE_NAME@
221+
echo "FLUENT_PACKAGE_SERVICE_RESTART: $FLUENT_PACKAGE_SERVICE_RESTART"
222+
if [ "$FLUENT_PACKAGE_SERVICE_RESTART" = auto ]; then
223+
# Present that FROM-side wants auto installing plugins and restarting.
224+
# Note: Wants to collect plugin-list here, but we need to do it in %pre (see comments in %pre).
225+
touch %{install_plugins}
226+
echo "$pid" > %{pid_for_auto_restart}
227+
fi
228+
fi
229+
fi
201230

202231
%post
203232
%systemd_post @SERVICE_NAME@.service
@@ -291,51 +320,9 @@ if [ -f "%{_sysconfdir}/prelink.conf" ]; then
291320
%{__sed} -i"" %{_sysconfdir}/prelink.conf -e "/\/opt\/td-agent\/bin\/ruby/d"
292321
fi
293322
fi
294-
if [ $1 -eq 2 ]; then
295-
# install missing plugins during upgrading package
296-
if [ -f %{local_base_plugins} ]; then
297-
local_current_plugins=$(/usr/sbin/fluent-gem list '^fluent-plugin-' --no-versions --no-verbose)
298-
if ! grep --fixed-strings --line-regexp --invert-match "$local_current_plugins" %{local_base_plugins}; then
299-
echo "No missing plugins to install"
300-
else
301-
if ! curl --fail --silent -O https://rubygems.org/specs.4.8.gz; then
302-
echo "No network connectivity..."
303-
else
304-
grep --fixed-strings --line-regexp --invert-match "$local_current_plugins" %{local_base_plugins} | while read missing_gem
305-
do
306-
if ! /usr/sbin/fluent-gem install --no-document $missing_gem; then
307-
echo "Can't install missing plugin automatically: please install $missing_gem manually."
308-
fi
309-
done
310-
fi
311-
fi
312-
rm -f %{local_base_plugins}
313-
fi
314-
fi
315323

316324
%postun
317-
if [ $1 -eq 1 ]; then
318-
# Control service during upgrading
319-
. %{_sysconfdir}/sysconfig/@SERVICE_NAME@
320-
echo "postun FLUENT_PACKAGE_SERVICE_RESTART: $FLUENT_PACKAGE_SERVICE_RESTART"
321-
if [ "$FLUENT_PACKAGE_SERVICE_RESTART" = "auto" ]; then
322-
# systemctl ... --property=MainPID --value is available since systemd 230 or later.
323-
# thus for amazonlinux:2, it can not be used.
324-
pid=$(systemctl show fluentd --property=MainPID | cut -d'=' -f2)
325-
if [ $pid -gt 0 ]; then
326-
echo "Kick auto service upgrade mode to MainPID:$pid"
327-
kill -USR2 $pid
328-
else
329-
# no running fluentd service
330-
echo "Suppress auto service upgrade mode to MainPID:$pid"
331-
fi
332-
elif [ "$FLUENT_PACKAGE_SERVICE_RESTART" = "manual" ]; then
333-
echo "No need to restart service in manual mode..."
334-
else
335-
# no support for upgrading without downtime
336-
%systemd_postun_with_restart @SERVICE_NAME@.service
337-
fi
338-
fi
325+
# Disable systemd_postun_with_restart to manage restart on the package side.
339326
if [ $1 -eq 0 ]; then
340327
# Uninstall
341328
# Without this uninstall conditional guard block ($1 -eq 0), symlink
@@ -400,6 +387,31 @@ if [ -f %{v4migration} ]; then
400387
rm -f %{v4migration_with_restart}
401388
fi
402389
fi
390+
if [ -f %{install_plugins} ] && [ -f %{local_base_plugins} ]; then
391+
local_current_plugins=$(/usr/sbin/fluent-gem list '^fluent-plugin-' --no-versions --no-verbose)
392+
if ! grep --fixed-strings --line-regexp --invert-match "$local_current_plugins" %{local_base_plugins}; then
393+
echo "No missing plugins to install"
394+
else
395+
if ! curl --fail --silent -O https://rubygems.org/specs.4.8.gz; then
396+
echo "No network connectivity..."
397+
else
398+
grep --fixed-strings --line-regexp --invert-match "$local_current_plugins" %{local_base_plugins} | while read missing_gem
399+
do
400+
if ! /usr/sbin/fluent-gem install --no-document $missing_gem; then
401+
echo "Can't install missing plugin automatically: please install $missing_gem manually."
402+
fi
403+
done
404+
fi
405+
fi
406+
fi
407+
rm -f %{install_plugins}
408+
rm -f %{local_base_plugins}
409+
if [ -f %{pid_for_auto_restart} ]; then
410+
pid=$(cat %{pid_for_auto_restart})
411+
echo "Kick auto restart to MainPID:$pid"
412+
kill -USR2 $pid
413+
rm -f %{pid_for_auto_restart}
414+
fi
403415

404416
%files
405417
%doc README.md

fluent-package/yum/systemd-test/downgrade-to-v5-lts.sh

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ sudo $DNF remove -y fluent-package
2727
sudo $DNF install -y \
2828
/host/${distribution}/${DISTRIBUTION_VERSION}/x86_64/Packages/fluent-package-[0-9]*.rpm
2929

30+
# Customize the env file to prevent replacing by downgrade.
31+
# Need this to test the case where some tmp files is left.
32+
echo "FOO=foo" >> /etc/sysconfig/fluentd
33+
3034
sudo systemctl enable --now fluentd
3135
systemctl status --no-pager fluentd
3236
systemctl status --no-pager td-agent
@@ -42,5 +46,22 @@ systemctl is-enabled fluentd
4246
systemctl status --no-pager fluentd
4347
systemctl status --no-pager td-agent
4448

45-
# Fluentd should be restarted.
46-
test $main_pid -ne $(eval $(systemctl show fluentd --property=MainPID) && echo $MainPID)
49+
# Fluentd should NOT be restarted.
50+
# NOTE: Unlike DEB, the restart behavior depends on FROM-side. So, it does not restart.
51+
# (it should restarts only when triggering zerodowntime-restart).
52+
test $main_pid -eq $(eval $(systemctl show fluentd --property=MainPID) && echo $MainPID)
53+
54+
# === Test: Remained tmp files should not affect to next upgrade ===
55+
# (This happens when env file was customized but the FLUENT_PACKAGE_SERVICE_RESTART was still `auto`)
56+
57+
# Some tmp files remains, though it is not happy.
58+
test -e /tmp/fluent/.install_plugins
59+
test -e /tmp/fluent/.pid_for_auto_restart
60+
61+
sudo $DNF install -y \
62+
/host/${distribution}/${DISTRIBUTION_VERSION}/x86_64/Packages/fluent-package-[0-9]*.rpm | tee upgrade.log
63+
64+
# zerodowntime-restart should NOT be triggered.
65+
(! grep "Kick auto restart" upgrade.log)
66+
67+
# ======

fluent-package/yum/systemd-test/update-to-next-major-version.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ set -exu
55
. $(dirname $0)/commonvar.sh
66

77
service_restart=$1
8+
status_before_update=$2 # active / inactive
89

910
# Install the current
1011
package="/host/${distribution}/${DISTRIBUTION_VERSION}/x86_64/Packages/fluent-package-*.rpm"
1112
sudo $DNF install -y $package
1213

14+
if [ "$status_before_update" = active ]; then
15+
sudo systemctl start fluentd
16+
fi
17+
1318
# Set FLUENT_PACKAGE_SERVICE_RESTART
1419
sed -i "s/=auto/=$service_restart/" /etc/sysconfig/fluentd
1520

@@ -22,7 +27,7 @@ package="/host/v6-test/${distribution}/${DISTRIBUTION_VERSION}/x86_64/Packages/f
2227
sudo $DNF install -y $package
2328

2429
# Test: Check whether plugin/gem were installed during upgrading
25-
if [ "$service_restart" = auto ]; then
30+
if [ "$service_restart" = auto ] && [ "$status_before_update" = active ]; then
2631
# plugin gem should be installed automatically
2732
/opt/fluent/bin/fluent-gem list | grep fluent-plugin-concat
2833
# Non fluent-plugin- prefix gem should not be installed automatically

0 commit comments

Comments
 (0)