From 185f519f74cde89f805f308e51f688daaed61eab Mon Sep 17 00:00:00 2001 From: Kentaro Hayashi Date: Fri, 9 Feb 2024 17:21:48 +0900 Subject: [PATCH] deb rpm: quote target files There was a case that missing quote about file name causes migration failures. * No quote under /etc/td-agent/'PATH_WITH_SPACE' * No quote under /var/log/td-agent/'PATH_WITH_SPACE' Usually such files should not be generated, but need to care it in practical use case. Signed-off-by: Kentaro Hayashi --- .../apt/systemd-test/update-from-v4.sh | 13 +++++++++ ...ext-version-with-backward-compat-for-v4.sh | 12 ++++++++ .../fluent-package/deb/postinst | 28 +++++++++++-------- fluent-package/yum/fluent-package.spec.in | 26 +++++++++-------- .../yum/systemd-test/update-from-v4.sh | 14 ++++++++++ ...ext-version-with-backward-compat-for-v4.sh | 13 +++++++++ 6 files changed, 82 insertions(+), 24 deletions(-) diff --git a/fluent-package/apt/systemd-test/update-from-v4.sh b/fluent-package/apt/systemd-test/update-from-v4.sh index 14657a05e..e0973d221 100755 --- a/fluent-package/apt/systemd-test/update-from-v4.sh +++ b/fluent-package/apt/systemd-test/update-from-v4.sh @@ -19,6 +19,14 @@ sudo apt install -V -y td-agent=${td_agent_version}-1 systemctl status --no-pager td-agent +# Generate garbage files +touch /etc/td-agent/a\ b\ c +touch /var/log/td-agent/a\ b\ c.log +touch /etc/td-agent/plugin/in_fake.rb +for d in $(seq 1 10); do + touch /var/log/td-agent/$d.log +done + # Install the current sudo apt install -V -y \ /host/${distribution}/pool/${code_name}/${channel}/*/*/fluent-package_*_${architecture}.deb @@ -40,10 +48,15 @@ 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 -e /etc/fluent/a\ b\ c +test -e /etc/fluent/plugin/in_fake.rb # Test: log file migration test -h /var/log/td-agent test -e /var/log/td-agent/td-agent.log +for d in $(seq 1 10); do + test -e /var/log/fluent/$d.log +done # Test: bin file migration test -h /usr/sbin/td-agent diff --git a/fluent-package/apt/systemd-test/update-to-next-version-with-backward-compat-for-v4.sh b/fluent-package/apt/systemd-test/update-to-next-version-with-backward-compat-for-v4.sh index b8436f773..762e70bda 100755 --- a/fluent-package/apt/systemd-test/update-to-next-version-with-backward-compat-for-v4.sh +++ b/fluent-package/apt/systemd-test/update-to-next-version-with-backward-compat-for-v4.sh @@ -19,6 +19,14 @@ sudo apt install -V -y td-agent=${td_agent_version}-1 systemctl status --no-pager td-agent +# Generate garbage files +touch /etc/td-agent/a\ b\ c +touch /var/log/td-agent/a\ b\ c.log +touch /etc/td-agent/plugin/in_fake.rb +for d in $(seq 1 10); do + touch /var/log/td-agent/$d.log +done + # Install the current sudo apt install -V -y \ /host/${distribution}/pool/${code_name}/${channel}/*/*/fluent-package_*_${architecture}.deb @@ -49,9 +57,13 @@ 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 -e /etc/fluent/plugin/in_fake.rb # Test: keep compatibility with v4: symlinks for log files test -h /var/log/td-agent +for d in $(seq 1 10); do + test -e /var/log/fluent/$d.log +done # Test: keep compatibility with v4: symlinks for bin files test -h /usr/sbin/td-agent diff --git a/fluent-package/templates/package-scripts/fluent-package/deb/postinst b/fluent-package/templates/package-scripts/fluent-package/deb/postinst index 4a8a3607a..8e1423221 100755 --- a/fluent-package/templates/package-scripts/fluent-package/deb/postinst +++ b/fluent-package/templates/package-scripts/fluent-package/deb/postinst @@ -74,12 +74,15 @@ migration_from_v4_main_process() { 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 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 [ -n "$(find /etc/<%= compat_package_dir %> -mindepth 1 -maxdepth 1 \ + -not -path /etc/<%= compat_package_dir %>/plugin \ + -not -path /etc/<%= compat_package_dir %>/<%= compat_service_name %>.conf)" ]; then + echo "Migrating from /etc/<%= compat_service_name %>/* to /etc/<%= package_dir %>/..." + find /etc/<%= compat_package_dir %> -mindepth 1 -maxdepth 1 \ + -not -path /etc/<%= compat_package_dir %>/plugin \ + -not -path /etc/<%= compat_package_dir %>/<%= compat_service_name %>.conf \ + -print0 | xargs --null mv -t /etc/<%= package_dir %>/ + fi 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 @@ -103,12 +106,13 @@ migration_from_v4_main_process() { mv -f /var/log/<%= compat_package_dir %>/buffer/* /var/log/<%= package_dir %>/buffer/ fi fi - for d in $(ls /var/log/<%= compat_package_dir %>); do - if [ ! "$d" = "buffer" ]; then - # except /var/log/<%= compat_package_dir %>/buffer must be migrated - mv -f /var/log/<%= compat_package_dir %>/$d /var/log/<%= package_dir %>/ - fi - done + # except /var/log/<%= compat_package_dir %>/buffer must be migrated + if [ -n "$(find /var/log/<%= compat_package_dir %> -mindepth 1 -maxdepth 1 \ + -not -path /var/log/<%= compat_package_dir %>/buffer)" ]; then + echo "Migrating from /var/log/<%= compat_service_name %>/* to /var/log/<%= package_dir %>/..." + find /var/log/<%= compat_package_dir %> -mindepth 1 -maxdepth 1 \ + -not -path /var/log/<%= compat_package_dir %>/buffer -print0 | xargs --null mv -t /var/log/<%= package_dir %>/ + fi fi } diff --git a/fluent-package/yum/fluent-package.spec.in b/fluent-package/yum/fluent-package.spec.in index ef5e91af4..dc415ff45 100644 --- a/fluent-package/yum/fluent-package.spec.in +++ b/fluent-package/yum/fluent-package.spec.in @@ -196,12 +196,13 @@ if [ $1 -eq 1 ]; then 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 [ -n "$(find /etc/@COMPAT_PACKAGE_DIR@ -mindepth 1 -maxdepth 1 \ + -not -path /etc/@COMPAT_PACKAGE_DIR@/plugin)" ]; then + echo "Migrating from /etc/@COMPAT_PACKAGE_DIR@/* to /etc/@PACKAGE_DIR@/..." + find /etc/@COMPAT_PACKAGE_DIR@ -mindepth 1 -maxdepth 1 \ + -not -path /etc/@COMPAT_PACKAGE_DIR@/plugin \ + -print0 | xargs --null mv -t /etc/@PACKAGE_DIR@/ + fi 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 @@ -230,12 +231,13 @@ if [ $1 -eq 1 ]; then if [ -d /var/log/@COMPAT_PACKAGE_DIR@/buffer -a -n "$(ls /var/log/@COMPAT_PACKAGE_DIR@/buffer)" ]; then mv -f /var/log/@COMPAT_PACKAGE_DIR@/buffer/* /var/log/@PACKAGE_DIR@/buffer/ fi - for d in /var/log/@COMPAT_PACKAGE_DIR@/*; do - if [ ! "$d" == "/var/log/@COMPAT_PACKAGE_DIR@/buffer" ]; then - # /var/log/@COMPAT_PACKAGE_DIR@/buffer will be removed from td-agent - mv -f $d /var/log/@PACKAGE_DIR@/ - fi - done + # migrate log files except owned buffer + if [ -n "$(find /var/log/@COMPAT_PACKAGE_DIR@ -mindepth 1 -maxdepth 1 \ + -not -path /var/log/@COMPAT_PACKAGE_DIR@/buffer)" ]; then + echo "Migrating from /var/log/@COMPAT_PACKAGE_DIR@/* to /var/log/@PACKAGE_DIR@/..." + find /var/log/@COMPAT_PACKAGE_DIR@ -mindepth 1 -maxdepth 1 \ + -not -path /var/log/@COMPAT_PACKAGE_DIR@/buffer -print0 | xargs --null mv -t /var/log/@PACKAGE_DIR@/ + fi if [ -f /var/log/@PACKAGE_DIR@/@COMPAT_SERVICE_NAME@.log ]; then # Backup the config since RPM removes the file if it is not edited. if [ -f /etc/logrotate.d/@COMPAT_SERVICE_NAME@ ]; then diff --git a/fluent-package/yum/systemd-test/update-from-v4.sh b/fluent-package/yum/systemd-test/update-from-v4.sh index b63807722..54f992b85 100755 --- a/fluent-package/yum/systemd-test/update-from-v4.sh +++ b/fluent-package/yum/systemd-test/update-from-v4.sh @@ -34,6 +34,14 @@ sudo $DNF install -y td-agent-${td_agent_version}-1.*.x86_64 sudo systemctl enable --now td-agent systemctl status --no-pager td-agent +# Generate garbage files +touch /etc/td-agent/a\ b\ c +touch /var/log/td-agent/a\ b\ c.log +touch /etc/td-agent/plugin/in_fake.rb +for d in $(seq 1 10); do + touch /var/log/td-agent/$d.log +done + # Install the current sudo $DNF install -y \ /host/${distribution}/${DISTRIBUTION_VERSION}/x86_64/Packages/fluent-package-[0-9]*.rpm @@ -48,10 +56,16 @@ 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 -e /etc/fluent/a\ b\ c +test -e /etc/fluent/plugin/in_fake.rb # Test: log file migration test -h /var/log/td-agent test -e /var/log/td-agent/td-agent.log +test -e /var/log/fluent/a\ b\ c.log +for d in $(seq 1 10); do + test -e /var/log/fluent/$d.log +done # Test: bin file migration test -h /usr/sbin/td-agent diff --git a/fluent-package/yum/systemd-test/update-to-next-version-with-backward-compat-for-v4.sh b/fluent-package/yum/systemd-test/update-to-next-version-with-backward-compat-for-v4.sh index f1b3d342e..007c1ebca 100755 --- a/fluent-package/yum/systemd-test/update-to-next-version-with-backward-compat-for-v4.sh +++ b/fluent-package/yum/systemd-test/update-to-next-version-with-backward-compat-for-v4.sh @@ -34,6 +34,14 @@ sudo $DNF install -y td-agent-${td_agent_version}-1.*.x86_64 sudo systemctl enable --now td-agent systemctl status --no-pager td-agent +# Generate garbage files +touch /etc/td-agent/a\ b\ c +touch /var/log/td-agent/a\ b\ c.log +touch /etc/td-agent/plugin/in_fake.rb +for d in $(seq 1 10); do + touch /var/log/td-agent/$d.log +done + # Install the current package="/host/${distribution}/${DISTRIBUTION_VERSION}/x86_64/Packages/fluent-package-[0-9]*.rpm" sudo $DNF install -y $package @@ -79,9 +87,14 @@ 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 -e /etc/fluent/a\ b\ c +test -e /etc/fluent/plugin/in_fake.rb # Test: keep compatibility with v4: symlinks for log files test -h /var/log/td-agent +for d in $(seq 1 10); do + test -e /var/log/fluent/$d.log +done # Test: keep compatibility with v4: symlinks for bin files test -h /usr/sbin/td-agent