#!/bin/bash do_configureLogrotate() { local IS_SYSTEMD=0 local packageName="amd-smi-lib" local logPath=/var/log/amd_smi_lib local logFile="${logPath}/AMD-SMI-lib.log" local logrotateConfFile=/etc/logrotate.d/amd_smi.conf mkdir -p "${logPath}" touch "${logFile}" chmod -R a+rw "${logPath}" chmod a+rw "${logFile}" if ! command -v logrotate &>/dev/null; then echo "[WARNING] Detected logrotate is not installed."\ "$packageName logs (when turned on) will not rotate properly." return fi if [ ! -f $logrotateConfFile ]; then touch "${logrotateConfFile}" chmod 644 "${logrotateConfFile}" # root r/w, all others read # AMD 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. AMD-SMI-lib.log.2023-05-09_16:51:42 cat << EOF > "${logrotateConfFile}" ${logFile} { su root root hourly missingok notifempty rotate 4 size 1M copytruncate dateext dateformat .%%Y-%%m-%%d_%H:%%M:%%S } EOF # Fix for % S argument not found (now we escape with %%) # 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 sed -i s/%%/%/g "${logrotateConfFile}" # workaround: remove extra 'OURCE' text # from amd_smi.conf. Unsure if CMAKE, # bash, or here document # issue (only seen on RHEL 8.7) 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 if [ -d /run/systemd/system ]; then systemctl list-timers | grep -iq logrotate if [ $? -eq 0 ]; then IS_SYSTEMD=1 fi 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 cp /lib/systemd/system/logrotate.timer /lib/systemd/system/logrotate.timer.backup fi cat << EOF > /lib/systemd/system/logrotate.timer [Unit] Description=Hourly rotation of log files Documentation=man:logrotate(8) man:logrotate.conf(5) [Timer] OnCalendar= OnCalendar=hourly AccuracySec=1m Persistent=true [Install] WantedBy=timers.target EOF 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 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 } do_ldconfig() { # left-hand term originates from ENABLE_LDCONFIG = ON/OFF at package build if [ "@ENABLE_LDCONFIG@" == "ON" ]; then echo @CPACK_PACKAGING_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@ > /etc/ld.so.conf.d/x86_64-libamd_smi_lib.conf ldconfig fi } do_install_amdsmi_python_lib() { # get python version local python3_minor_version python3_minor_version=$(python3 -c 'import sys;print(sys.version_info.minor)') if [ $? -ne 0 ]; then echo "[WARNING] Could not determine python version. "\ "AMD-SMI python library will not be installed." return fi # check if python version is supported if [ "$python3_minor_version" -lt 6 ]; then echo "[WARNING] AMD-SMI python library is not "\ "supported on python version 3.$python3_minor_version. "\ "AMD-SMI python library will not be installed." return fi local PREVIOUS_PIP_ROOT_USER_ACTION="$PIP_ROOT_USER_ACTION" export PIP_ROOT_USER_ACTION=ignore # python3.11 requires --break-system-packages local PREVIOUS_PIP_BREAK_SYSTEM_PACKAGES="$PIP_BREAK_SYSTEM_PACKAGES" export PIP_BREAK_SYSTEM_PACKAGES=1 # Remove old python library local pip_list_output pip_list_output=$(python3 -m pip list --format=columns --disable-pip-version-check) # check pip list output for amdsmi if [[ $pip_list_output == *"amdsmi"* ]]; then echo "Detected old AMD-SMI python library (amdsmi)..." python3 -m pip uninstall amdsmi --yes --quiet --disable-pip-version-check echo "Removed old AMD-SMI python library (amdsmi)..." fi # upgrade pip if it's an ancient version # otherwise the amdsmi install will fail local pip_version pip_version=$(python3 -m pip --version | grep -Eo '^[^\ ]+ ([0-9]+)' | grep -Eo '[0-9]+$') if [[ "$pip_version" -lt 19 ]]; then echo "Detected ancient pip version ($pip_version)... Upgrading..." python3 -m pip install --upgrade pip --quiet --disable-pip-version-check fi unset pip_version # install PyYAML dependency python3 -m pip install 'PyYAML>=5.1' --quiet --disable-pip-version-check --ignore-installed # install python library at @CPACK_PACKAGING_INSTALL_PREFIX@/@SHARE_INSTALL_PREFIX@/amdsmi local python_lib_path=@CPACK_PACKAGING_INSTALL_PREFIX@/@SHARE_INSTALL_PREFIX@ python3 -m pip install "$python_lib_path" --quiet --disable-pip-version-check export PIP_ROOT_USER_ACTION="$PREVIOUS_PIP_ROOT_USER_ACTION" export PIP_BREAK_SYSTEM_PACKAGES="$PREVIOUS_PIP_BREAK_SYSTEM_PACKAGES" # only try to activate argcomplete if such command exists if command -v activate-global-python-argcomplete &>/dev/null; then activate-global-python-argcomplete else # try older argcomplete3 version if command -v activate-global-python-argcomplete3 &>/dev/null; then activate-global-python-argcomplete3 else echo "[WARNING] Could not find argcomplete or argcomplete3. "\ "Argument completion will not work..." fi fi } # post install or upgrade, $i is 1 or 2 -> do these actions if [ "$1" -ge 1 ]; then do_install_amdsmi_python_lib do_ldconfig do_configureLogrotate || exit 0 fi