Skip to content

Commit

Permalink
deb:rpm: stop editing settings for migration (#546)
Browse files Browse the repository at this point in the history
This fix stops editing `/etc/default/fluentd` or
`/etc/sysconfig/fluentd` for migration.
We shouldn't touch the settings for migration.

For migration of the config file, we make a symlink:

* `/etc/fluent/fluent.conf` => `/etc/fluent/td-agent.conf`

We stop migrating the log filename.
If need to output logs to the old filename `td-agent.log`, user needs to
set the environmental variable.

---------

Signed-off-by: Daijiro Fukuda <fukuda@clear-code.com>
  • Loading branch information
daipom authored Jul 27, 2023
1 parent 29c9e92 commit 30939fc
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 137 deletions.
28 changes: 8 additions & 20 deletions fluent-package/apt/systemd-test/update-from-v4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ systemctl status --no-pager td-agent
systemctl status --no-pager fluentd

# Test: config migration
test -L /etc/td-agent
test -h /etc/td-agent
test -h /etc/fluent/fluentd.conf
test $(readlink "/etc/fluent/fluentd.conf") = "/etc/fluent/td-agent.conf"
test -e /etc/td-agent/td-agent.conf

# Test: log file migration
test -L /var/log/td-agent
test -h /var/log/td-agent
test -e /var/log/td-agent/td-agent.log

# Test: bin file migration
Expand All @@ -53,30 +55,16 @@ env_vars=$(sudo sed -e 's/\x0/\n/g' /proc/$pid/environ)
test $(eval $env_vars && echo $HOME) = "/var/lib/fluent"
test $(eval $env_vars && echo $LOGNAME) = "_fluentd"
test $(eval $env_vars && echo $USER) = "_fluentd"
test $(eval $env_vars && echo $FLUENT_CONF) = "/etc/fluent/td-agent.conf"
test $(eval $env_vars && echo $FLUENT_PACKAGE_LOG_FILE) = "/var/log/fluent/td-agent.log"
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: No error logs
# (v4 default config outputs 'warn' log, so we should check only 'error' and 'fatal' logs)
sleep 3
(! grep -q -e '\[error\]' -e '\[fatal\]' /var/log/td-agent/td-agent.log)

# Test: logrotate config migration
test -e /etc/logrotate.d/td-agent
test -e /var/log/fluent/td-agent.log

sudo logrotate -f /etc/logrotate.d/td-agent
sleep 1

test -e /var/log/fluent/td-agent.log.1
sudo cp /var/log/fluent/td-agent.log.1 saved_rotated_logfile

sudo systemctl stop td-agent
# Check that SIGUSR1 is sent to Fluentd and Fluentd reopens the logfile
# not to log to the rotated old file.
sudo diff --report-identical-files /var/log/fluent/td-agent.log.1 saved_rotated_logfile
test -e /var/log/fluent/fluentd.log
(! grep -e '\[error\]' -e '\[fatal\]' /var/log/fluent/fluentd.log)

# Uninstall
sudo apt remove -y fluent-package
Expand Down
4 changes: 0 additions & 4 deletions fluent-package/templates/etc/systemd/fluentd.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
FLUENT_PACKAGE_OPTIONS=""
# The following environment variable will be modified via package maintainer's migration script
FLUENT_CONF=/etc/<%= package_dir %>/<%= service_name %>.conf
FLUENT_PLUGIN=/etc/<%= package_dir %>/plugin
FLUENT_PACKAGE_LOG_FILE=/var/log/<%= package_dir %>/<%= service_name %>.log
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,23 @@ migration_from_v4_main_process() {
echo "Migrating from /etc/<%= compat_service_name %>/plugin/ to /etc/<%= package_dir %>/plugin/..."
mv -f /etc/<%= compat_service_name %>/plugin/* /etc/<%= package_dir %>/plugin/
fi
create_link_for_config=n
if [ -f /etc/<%= compat_package_dir %>/<%= compat_service_name %>.conf -a ! -e /etc/<%= compat_package_dir %>/<%= service_name %>.conf ]; then
create_link_for_config=y
fi
if [ -f /etc/<%= compat_package_dir %>/<%= compat_service_name %>.conf ]; then
echo "Migrating from /etc/<%= compat_package_dir %>/<%= compat_service_name %>.conf to /etc/<%= package_dir %>/<%= compat_service_name %>.conf"
cp -f /etc/<%= compat_package_dir %>/<%= compat_service_name %>.conf /etc/<%= package_dir %>/<%= compat_service_name %>.conf
echo "Refer previous configuration <%= compat_service_name %>.conf ..."
sed -i"" /etc/default/<%= service_name %> -e "/FLUENT_CONF/c FLUENT_CONF=/etc/<%= package_dir %>/<%= compat_service_name %>.conf"
for d in $(ls /etc/<%= compat_package_dir %>); do
if [ ! "$d" = "plugin" -a ! "$d" = "<%= compat_service_name %>.conf" ]; then
# except managed under deb files must be migrated
mv -f /etc/<%= compat_package_dir %>/$d /etc/<%= package_dir %>/
fi
done
fi
for d in $(ls /etc/<%= compat_package_dir %>); do
if [ ! "$d" = "plugin" -a ! "$d" = "<%= compat_service_name %>.conf" ]; then
# except managed under deb files must be migrated
mv -f /etc/<%= compat_package_dir %>/$d /etc/<%= package_dir %>/
fi
done
if [ $create_link_for_config = "y" ]; then
rm -f /etc/<%= package_dir %>/<%= service_name %>.conf
ln -sf /etc/<%= package_dir %>/<%= compat_service_name %>.conf /etc/<%= package_dir %>/<%= service_name %>.conf
fi
fi
if [ -h /etc/systemd/system/td-agent.service ]; then
Expand All @@ -103,14 +109,6 @@ migration_from_v4_main_process() {
mv -f /var/log/<%= compat_package_dir %>/$d /var/log/<%= package_dir %>/
fi
done
if [ -f /var/log/<%= package_dir %>/<%= compat_service_name %>.log ]; then
echo "Keep logging to <%= compat_service_name %>.log ..."
sed -i"" /etc/default/<%= service_name %> -e "/FLUENT_PACKAGE_LOG_FILE/c FLUENT_PACKAGE_LOG_FILE=/var/log/<%= package_dir %>/<%= compat_service_name %>.log"
fi
if [ -f /etc/logrotate.d/<%= compat_service_name %> ]; then
echo "Migrating path of pid ..."
sed -i"" /etc/logrotate.d/<%= compat_service_name %> -e "s,/var/run/<%= compat_package_dir %>/<%= compat_service_name %>.pid,/var/run/<%= package_dir %>/<%= service_name %>.pid,"
fi
fi
}

Expand Down
28 changes: 13 additions & 15 deletions fluent-package/yum/fluent-package.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,19 @@ if [ $1 -eq 1 ]; then
echo "Migrating from /etc/@COMPAT_PACKAGE_DIR@/plugin/ to /etc/@PACKAGE_DIR@/plugin/..."
mv -f /etc/@COMPAT_PACKAGE_DIR@/plugin/* /etc/@PACKAGE_DIR@/plugin/
fi
if [ -f /etc/@COMPAT_PACKAGE_DIR@/@COMPAT_SERVICE_NAME@.conf ]; then
for d in /etc/@COMPAT_PACKAGE_DIR@/*; do
if [ ! "$d" == "/etc/@COMPAT_PACKAGE_DIR@/plugin" ]; then
echo "Migrating from $d to /etc/@PACKAGE_DIR@/..."
mv -f $d /etc/@PACKAGE_DIR@/
fi
done
echo "Refer previous configuration @COMPAT_SERVICE_NAME@.conf ..."
%{__sed} -i"" %{_sysconfdir}/sysconfig/@SERVICE_NAME@ -e "/FLUENT_CONF/c FLUENT_CONF=/etc/@PACKAGE_DIR@/@COMPAT_SERVICE_NAME@.conf"
create_link_for_config=n
if [ -f /etc/@COMPAT_PACKAGE_DIR@/@COMPAT_SERVICE_NAME@.conf -a ! -e /etc/@COMPAT_PACKAGE_DIR@/@SERVICE_NAME@.conf ]; then
create_link_for_config=y
fi
for d in /etc/@COMPAT_PACKAGE_DIR@/*; do
if [ ! "$d" == "/etc/@COMPAT_PACKAGE_DIR@/plugin" ]; then
echo "Migrating from $d to /etc/@PACKAGE_DIR@/..."
mv -f $d /etc/@PACKAGE_DIR@/
fi
done
if [ $create_link_for_config = "y" ]; then
rm -f /etc/@PACKAGE_DIR@/@SERVICE_NAME@.conf
ln -sf /etc/@PACKAGE_DIR@/@COMPAT_SERVICE_NAME@.conf /etc/@PACKAGE_DIR@/@SERVICE_NAME@.conf
fi
fi
if systemctl is-active @COMPAT_SERVICE_NAME@; then
Expand Down Expand Up @@ -233,8 +237,6 @@ if [ $1 -eq 1 ]; then
fi
done
if [ -f /var/log/@PACKAGE_DIR@/@COMPAT_SERVICE_NAME@.log ]; then
echo "Keep logging to @COMPAT_SERVICE_NAME@.log ..."
%{__sed} -i"" %{_sysconfdir}/sysconfig/@SERVICE_NAME@ -e "/FLUENT_PACKAGE_LOG_FILE/c FLUENT_PACKAGE_LOG_FILE=/var/log/@PACKAGE_DIR@/@COMPAT_SERVICE_NAME@.log"
# Backup the config since RPM removes the file if it is not edited.
if [ -f /etc/logrotate.d/@COMPAT_SERVICE_NAME@ ]; then
cp /etc/logrotate.d/@COMPAT_SERVICE_NAME@ %{v4migration_old_rotate_config_saved}
Expand Down Expand Up @@ -327,10 +329,6 @@ if [ -f %{v4migration} ]; then
echo "Restores logrotate config from backup"
mv -f %{v4migration_old_rotate_config_saved} /etc/logrotate.d/@COMPAT_SERVICE_NAME@
fi
if [ -f /etc/logrotate.d/@COMPAT_SERVICE_NAME@ ]; then
echo "Migrating path of pid in the logrotate config /etc/logrotate.d/@COMPAT_SERVICE_NAME@ ..."
%{__sed} -i"" /etc/logrotate.d/@COMPAT_SERVICE_NAME@ -e "s,/var/run/@COMPAT_PACKAGE_DIR@/@COMPAT_SERVICE_NAME@.pid,/var/run/@PACKAGE_DIR@/@SERVICE_NAME@.pid,"
fi
rm -f %{v4migration}
if [ -f %{v4migration_with_restart} ]; then
# When upgrading from v4, td-agent.service will be removed
Expand Down
1 change: 0 additions & 1 deletion fluent-package/yum/systemd-test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ test_filenames=(
install-newly.sh
update-from-v4.sh
update-to-next-version.sh
update-from-v4-logrotation-config-edited.sh
)

for test_filename in ${test_filenames[@]}; do
Expand Down

This file was deleted.

28 changes: 8 additions & 20 deletions fluent-package/yum/systemd-test/update-from-v4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ sudo systemctl enable --now fluentd
systemctl status --no-pager td-agent

# Test: config migration
test -L /etc/td-agent
test -h /etc/td-agent
test -h /etc/fluent/fluentd.conf
test $(readlink "/etc/fluent/fluentd.conf") = "/etc/fluent/td-agent.conf"
test -e /etc/td-agent/td-agent.conf

# Test: log file migration
test -L /var/log/td-agent
test -h /var/log/td-agent
test -e /var/log/td-agent/td-agent.log

# Test: bin file migration
Expand All @@ -61,30 +63,16 @@ env_vars=$(sudo sed -e 's/\x0/\n/g' /proc/$pid/environ)
test $(eval $env_vars && echo $HOME) = "/var/lib/fluent"
test $(eval $env_vars && echo $LOGNAME) = "fluentd"
test $(eval $env_vars && echo $USER) = "fluentd"
test $(eval $env_vars && echo $FLUENT_CONF) = "/etc/fluent/td-agent.conf"
test $(eval $env_vars && echo $FLUENT_PACKAGE_LOG_FILE) = "/var/log/fluent/td-agent.log"
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: No error logs
# (v4 default config outputs 'warn' log, so we should check only 'error' and 'fatal' logs)
sleep 3
(! grep -q -e '\[error\]' -e '\[fatal\]' /var/log/td-agent/td-agent.log)

# Test: logrotate config migration
test -e /etc/logrotate.d/td-agent
test -e /var/log/fluent/td-agent.log

sudo $DNF install -y logrotate # rockylinux-8 needs to install logrotate
sudo logrotate -f /etc/logrotate.d/td-agent
sleep 1

test -e /var/log/fluent/td-agent.log.1
sudo cp /var/log/fluent/td-agent.log.1 saved_rotated_logfile
sudo systemctl stop td-agent
# Check that SIGUSR1 is sent to Fluentd and Fluentd reopens the logfile
# not to log to the rotated old file.
sudo diff --report-identical-files /var/log/fluent/td-agent.log.1 saved_rotated_logfile
test -e /var/log/fluent/fluentd.log
(! grep -e '\[error\]' -e '\[fatal\]' /var/log/fluent/fluentd.log)

# Uninstall
sudo $DNF remove -y fluent-package
Expand Down

0 comments on commit 30939fc

Please sign in to comment.