diff --git a/projects/rocm-smi-lib/DEBIAN/postinst.in b/projects/rocm-smi-lib/DEBIAN/postinst.in index ab2f640553..9e0fe4457c 100755 --- a/projects/rocm-smi-lib/DEBIAN/postinst.in +++ b/projects/rocm-smi-lib/DEBIAN/postinst.in @@ -1,21 +1,18 @@ #!/bin/bash -#set -x - -packageName="rocm-smi-lib" -logPath=/var/log/rocm_smi_lib -logName=ROCm-SMI-lib.log -logFile="${logPath}/${logName}" -logrotateConfFile=/etc/logrotate.d/rocm_smi.conf - -do_addLogFolder() { - sudo mkdir -p "${logPath}" - sudo touch "${logFile}" - sudo chmod -R a+rw "${logPath}" - sudo chmod a+rw "${logFile}" -} do_configureLogrotate() { - logrotate --version &>/dev/null + local IS_SYSTEMD=0 + local packageName="rocm-smi-lib" + local logPath=/var/log/rocm_smi_lib + local logFile="${logPath}/ROCm-SMI-lib.log" + local logrotateConfFile=/etc/logrotate.d/rocm_smi.conf + + mkdir -p "${logPath}" + touch "${logFile}" + chmod -R a+rw "${logPath}" + chmod a+rw "${logFile}" + + command -v logrotate &>/dev/null if [ $? -ne 0 ]; then echo "[WARNING] Detected logrotate is not installed."\ "$packageName logs (when turned on) will not rotate properly." @@ -23,14 +20,14 @@ do_configureLogrotate() { fi if [ ! -f $logrotateConfFile ]; then - sudo touch "${logrotateConfFile}" - sudo chmod 644 "${logrotateConfFile}" # root r/w, all others read + touch "${logrotateConfFile}" + chmod 644 "${logrotateConfFile}" # root r/w, all others read # ROCm SMI logging rotation, rotates files using root user/group # Hourly logrotation check # Only rotates if size grew larger than 1MB # Max of 4 rotation files, oldest will be removed # Rotated files use date extention of ex. ROCm-SMI-lib.log.2023-05-09_16:51:42 - cat << EOF | sudo tee "${logrotateConfFile}" >/dev/null + cat << EOF > "${logrotateConfFile}" ${logFile} { su root root hourly @@ -47,43 +44,29 @@ EOF # issue was RPM build thought we were using macros # https://gitlab.kitware.com/cmake/cmake/-/issues/22965 # https://rpm-software-management.github.io/rpm/manual/spec.html - sudo sed -i s/%%/%/g "${logrotateConfFile}" + sed -i s/%%/%/g "${logrotateConfFile}" # workaround: remove extra 'OURCE' text # from rocm_smi.conf. Unsure if CMAKE, # bash, or here document # issue (only seen on RHEL 8.7) - sudo sed -i s/OURCE//g "${logrotateConfFile}" + sed -i s/OURCE//g "${logrotateConfFile}" fi # check if logrotate uses system timers, Ubuntu/modern OS's do # Several older OS's like RHEL 8.7, do not. Instead defaults # to use daily cron jobs - see https://stackoverflow.com/a/69465677 - sudo systemctl list-timers|grep -iq logrotate - if [ $? -ne 0 ]; then - # confirm logrotate file exists in daily - if [ -f /etc/cron.daily/logrotate ]; then - # move logrotate daily to hourly - if [ -d /etc/cron.hourly ]; then - sudo mv /etc/cron.daily/logrotate /etc/cron.hourly/logrotate - else - echo "[WARNING] Could find and configure hourly cron for $packageName's"\ - " logrotate. $packageName logs (when turned on) will not rotate properly." - fi - else - # confirm that it's already been moved to hourly - sudo find /etc/cron.* -iname logrotate -print -quit |grep -iq hourly - if [ $? -ne 0 ]; then - echo "[WARNING] Could not configure an hourly cron for $packageName's logrotate."\ - "$packageName logs (when turned on) may not rotate properly." - fi + if [ -d /run/systemd/system ]; then + systemctl list-timers | grep -iq logrotate + if [ $? -ne 0 ]; then + IS_SYSTEMD=1 fi - return #done configuring for non-systemd timers - else + fi + if [ "$IS_SYSTEMD" -eq 1 ]; then # Configure systemd timers - the typical setup for modern Linux logrotation setups if [ -f /lib/systemd/system/logrotate.timer ]; then if [ ! -f /lib/systemd/system/logrotate.timer.backup ]; then - sudo cp /lib/systemd/system/logrotate.timer /lib/systemd/system/logrotate.timer.backup + cp /lib/systemd/system/logrotate.timer /lib/systemd/system/logrotate.timer.backup fi - cat <<'EOF' | sudo tee /lib/systemd/system/logrotate.timer >/dev/null + cat << EOF > /lib/systemd/system/logrotate.timer [Unit] Description=Hourly rotation of log files Documentation=man:logrotate(8) man:logrotate.conf(5) @@ -97,12 +80,19 @@ Persistent=true [Install] WantedBy=timers.target EOF - sudo systemctl reenable --now logrotate.timer + systemctl reenable --now logrotate.timer else echo "[WARNING] Could not configure systemd timer for $packageName's logrotate."\ "$packageName logs (when turned on) will not rotate properly." fi - return #done configuring for systemd timers + else + # $IS_SYSTEMD -eq 0 + if [ -f /etc/cron.daily/logrotate ]; then + # move logrotate daily to hourly + if [ -d /etc/cron.hourly ]; then + mv /etc/cron.daily/logrotate /etc/cron.hourly/logrotate + fi + fi fi } @@ -117,7 +107,6 @@ do_ldconfig() { case "$1" in ( configure ) do_ldconfig - do_addLogFolder do_configureLogrotate ;; ( abort-upgrade | abort-remove | abort-deconfigure ) diff --git a/projects/rocm-smi-lib/DEBIAN/prerm.in b/projects/rocm-smi-lib/DEBIAN/prerm.in index 9b4efa9899..7bf61f02d1 100755 --- a/projects/rocm-smi-lib/DEBIAN/prerm.in +++ b/projects/rocm-smi-lib/DEBIAN/prerm.in @@ -1,29 +1,4 @@ #!/bin/bash -set -e - -packageName="rocm-smi-lib" -logPath=/var/log/rocm_smi_lib -logName=ROCm-SMI-lib.log -logFile="${logPath}/${logName}" -logrotateConfFile=/etc/logrotate.d/rocm_smi.conf - -rm_logFolder() { - sudo rm -rf "$logPath" -} - -return_logrotateToOrigConfig() { - if [ -f $logrotateConfFile ]; then - sudo rm -rf "${logrotateConfFile}" - fi - if [ -f /etc/cron.hourly/logrotate ]; then - sudo mv /etc/cron.hourly/logrotate /etc/cron.daily/logrotate - fi - if [ -f /lib/systemd/system/logrotate.timer.backup ]; then - sudo cp /lib/systemd/system/logrotate.timer.backup /lib/systemd/system/logrotate.timer - sudo rm -rf /lib/systemd/system/logrotate.timer.backup - sudo systemctl reenable --now logrotate.timer - fi -} rm_ldconfig() { # left-hand term originates from ENABLE_LDCONFIG = ON/OFF at package build @@ -34,8 +9,27 @@ rm_ldconfig() { } rm_pyc() { - # remove pyc file generated by python - rm -rf @CPACK_PACKAGING_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBEXECDIR@/rocm_smi/__pycache__ + # remove pyc file generated by python + rm -rf @CPACK_PACKAGING_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBEXECDIR@/rocm_smi/__pycache__ +} + +rm_logFolder() { + rm -rf /var/log/rocm_smi_lib +} + +return_logrotateToOrigConfig() { + local logrotateConfFile=/etc/logrotate.d/rocm_smi.conf + if [ -f $logrotateConfFile ]; then + rm -rf "$logrotateConfFile" + fi + if [ -f /etc/cron.hourly/logrotate ]; then + mv /etc/cron.hourly/logrotate /etc/cron.daily/logrotate + fi + if [ -f /lib/systemd/system/logrotate.timer.backup ]; then + cp /lib/systemd/system/logrotate.timer.backup /lib/systemd/system/logrotate.timer + rm -rf /lib/systemd/system/logrotate.timer.backup + systemctl reenable --now logrotate.timer + fi } diff --git a/projects/rocm-smi-lib/RPM/post.in b/projects/rocm-smi-lib/RPM/post.in index b04e31c9f9..ccf1f814a5 100755 --- a/projects/rocm-smi-lib/RPM/post.in +++ b/projects/rocm-smi-lib/RPM/post.in @@ -1,21 +1,18 @@ #!/bin/bash -#set -x - -packageName="rocm-smi-lib" -logPath=/var/log/rocm_smi_lib -logName=ROCm-SMI-lib.log -logFile="${logPath}/${logName}" -logrotateConfFile=/etc/logrotate.d/rocm_smi.conf - -do_addLogFolder() { - sudo mkdir -p "${logPath}" - sudo touch "${logFile}" - sudo chmod -R a+rw "${logPath}" - sudo chmod a+rw "${logFile}" -} do_configureLogrotate() { - logrotate --version &>/dev/null + local IS_SYSTEMD=0 + local packageName="rocm-smi-lib" + local logPath=/var/log/rocm_smi_lib + local logFile="${logPath}/ROCm-SMI-lib.log" + local logrotateConfFile=/etc/logrotate.d/rocm_smi.conf + + mkdir -p "${logPath}" + touch "${logFile}" + chmod -R a+rw "${logPath}" + chmod a+rw "${logFile}" + + command -v logrotate &>/dev/null if [ $? -ne 0 ]; then echo "[WARNING] Detected logrotate is not installed."\ "$packageName logs (when turned on) will not rotate properly." @@ -23,14 +20,14 @@ do_configureLogrotate() { fi if [ ! -f $logrotateConfFile ]; then - sudo touch "${logrotateConfFile}" - sudo chmod 644 "${logrotateConfFile}" # root r/w, all others read + touch "${logrotateConfFile}" + chmod 644 "${logrotateConfFile}" # root r/w, all others read # ROCm SMI logging rotation, rotates files using root user/group # Hourly logrotation check # Only rotates if size grew larger than 1MB # Max of 4 rotation files, oldest will be removed # Rotated files use date extention of ex. ROCm-SMI-lib.log.2023-05-09_16:51:42 - cat << EOF | sudo tee "${logrotateConfFile}" >/dev/null + cat << EOF > "${logrotateConfFile}" ${logFile} { su root root hourly @@ -47,43 +44,29 @@ EOF # issue was RPM build thought we were using macros # https://gitlab.kitware.com/cmake/cmake/-/issues/22965 # https://rpm-software-management.github.io/rpm/manual/spec.html - sudo sed -i s/%%/%/g "${logrotateConfFile}" + sed -i s/%%/%/g "${logrotateConfFile}" # workaround: remove extra 'OURCE' text # from rocm_smi.conf. Unsure if CMAKE, # bash, or here document # issue (only seen on RHEL 8.7) - sudo sed -i s/OURCE//g "${logrotateConfFile}" + sed -i s/OURCE//g "${logrotateConfFile}" fi # check if logrotate uses system timers, Ubuntu/modern OS's do # Several older OS's like RHEL 8.7, do not. Instead defaults # to use daily cron jobs - see https://stackoverflow.com/a/69465677 - sudo systemctl list-timers|grep -iq logrotate - if [ $? -ne 0 ]; then - # confirm logrotate file exists in daily - if [ -f /etc/cron.daily/logrotate ]; then - # move logrotate daily to hourly - if [ -d /etc/cron.hourly ]; then - sudo mv /etc/cron.daily/logrotate /etc/cron.hourly/logrotate - else - echo "[WARNING] Could find and configure hourly cron for $packageName's"\ - " logrotate. $packageName logs (when turned on) will not rotate properly." - fi - else - # confirm that it's already been moved to hourly - sudo find /etc/cron.* -iname logrotate -print -quit |grep -iq hourly - if [ $? -ne 0 ]; then - echo "[WARNING] Could not configure an hourly cron for $packageName's logrotate."\ - "$packageName logs (when turned on) may not rotate properly." - fi + if [ -d /run/systemd/system ]; then + systemctl list-timers | grep -iq logrotate + if [ $? -ne 0 ]; then + IS_SYSTEMD=1 fi - return #done configuring for non-systemd timers - else + fi + if [ "$IS_SYSTEMD" -eq 1 ]; then # Configure systemd timers - the typical setup for modern Linux logrotation setups if [ -f /lib/systemd/system/logrotate.timer ]; then if [ ! -f /lib/systemd/system/logrotate.timer.backup ]; then - sudo cp /lib/systemd/system/logrotate.timer /lib/systemd/system/logrotate.timer.backup + cp /lib/systemd/system/logrotate.timer /lib/systemd/system/logrotate.timer.backup fi - cat <<'EOF' | sudo tee /lib/systemd/system/logrotate.timer >/dev/null + cat << EOF > /lib/systemd/system/logrotate.timer [Unit] Description=Hourly rotation of log files Documentation=man:logrotate(8) man:logrotate.conf(5) @@ -97,12 +80,19 @@ Persistent=true [Install] WantedBy=timers.target EOF - sudo systemctl reenable --now logrotate.timer + systemctl reenable --now logrotate.timer else echo "[WARNING] Could not configure systemd timer for $packageName's logrotate."\ "$packageName logs (when turned on) will not rotate properly." fi - return #done configuring for systemd timers + else + # $IS_SYSTEMD -eq 0 + if [ -f /etc/cron.daily/logrotate ]; then + # move logrotate daily to hourly + if [ -d /etc/cron.hourly ]; then + mv /etc/cron.daily/logrotate /etc/cron.hourly/logrotate + fi + fi fi } @@ -114,14 +104,8 @@ do_ldconfig() { fi } -# left-hand term originates from ENABLE_LDCONFIG = ON/OFF at package build -if [ "@ENABLE_LDCONFIG@" == "ON" ]; then - echo -e "@CPACK_PACKAGING_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@" > /etc/ld.so.conf.d/x86_64-librocm_smi_lib.conf - ldconfig -fi - # post install or upgrade, $i is 1 or 2 -> do these actions -if [ $1 -ge 1 ]; then - do_addLogFolder +if [ "$1" -ge 1 ]; then + do_ldconfig do_configureLogrotate -fi \ No newline at end of file +fi diff --git a/projects/rocm-smi-lib/RPM/postun.in b/projects/rocm-smi-lib/RPM/postun.in index 0dd41d82d8..b42b5b90b9 100755 --- a/projects/rocm-smi-lib/RPM/postun.in +++ b/projects/rocm-smi-lib/RPM/postun.in @@ -1,7 +1,7 @@ #!/bin/bash # second term originates from ENABLE_LDCONFIG = ON/OFF at package build -if [ $1 -le 1 ] && [ "@ENABLE_LDCONFIG@" == "ON" ]; then +if [ "$1" -le 1 ] && [ "@ENABLE_LDCONFIG@" == "ON" ]; then # perform the below actions for rpm remove($1=0) or upgrade($1=1) operations rm -f /etc/ld.so.conf.d/x86_64-librocm_smi_lib.conf ldconfig diff --git a/projects/rocm-smi-lib/RPM/preun.in b/projects/rocm-smi-lib/RPM/preun.in index b9c2eda6b3..b72c39465c 100755 --- a/projects/rocm-smi-lib/RPM/preun.in +++ b/projects/rocm-smi-lib/RPM/preun.in @@ -1,34 +1,32 @@ #!/bin/bash -#set -x -packageName="rocm-smi-lib" -logPath=/var/log/rocm_smi_lib -logName=ROCm-SMI-lib.log -logFile="${logPath}/${logName}" -logrotateConfFile=/etc/logrotate.d/rocm_smi.conf +rm_pyc() { + # remove pyc file generated by python + rm -rf @CPACK_PACKAGING_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBEXECDIR@/rocm_smi/__pycache__ +} rm_logFolder() { - sudo rm -rf "$logPath" + rm -rf /var/log/rocm_smi_lib } return_logrotateToOrigConfig() { + local logrotateConfFile=/etc/logrotate.d/rocm_smi.conf if [ -f $logrotateConfFile ]; then - sudo rm -rf "${logrotateConfFile}" + rm -rf "$logrotateConfFile" fi if [ -f /etc/cron.hourly/logrotate ]; then - sudo mv /etc/cron.hourly/logrotate /etc/cron.daily/logrotate + mv /etc/cron.hourly/logrotate /etc/cron.daily/logrotate fi if [ -f /lib/systemd/system/logrotate.timer.backup ]; then - sudo cp /lib/systemd/system/logrotate.timer.backup /lib/systemd/system/logrotate.timer - sudo rm -rf /lib/systemd/system/logrotate.timer.backup - sudo systemctl reenable --now logrotate.timer + cp /lib/systemd/system/logrotate.timer.backup /lib/systemd/system/logrotate.timer + rm -rf /lib/systemd/system/logrotate.timer.backup + systemctl reenable --now logrotate.timer fi } -if [ $1 -le 1 ]; then +if [ "$1" -le 1 ]; then # perform the below actions for rpm remove($1=0) or upgrade($1=1) operations - # remove pyc file generated by python - rm -rf @CPACK_PACKAGING_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBEXECDIR@/rocm_smi/__pycache__ + rm_pyc rm_logFolder return_logrotateToOrigConfig fi