From 80af500858d7e73408b34fcbfa2c43a7b703d0b4 Mon Sep 17 00:00:00 2001 From: explosion-mental Date: Sun, 19 Sep 2021 09:50:32 -0500 Subject: [PATCH] Code refactor (improved readability) of auto-cpufreq-installer (#238) --- auto-cpufreq-installer | 273 ++++++++++++++------------------ scripts/auto-cpufreq-install.sh | 68 ++++---- scripts/auto-cpufreq-remove.sh | 27 +++- 3 files changed, 180 insertions(+), 188 deletions(-) diff --git a/auto-cpufreq-installer b/auto-cpufreq-installer index c983aab..525d862 100755 --- a/auto-cpufreq-installer +++ b/auto-cpufreq-installer @@ -1,5 +1,5 @@ #!/bin/bash -# + # auto-cpufreq-installer: # auto-cpufreq source code based installer @@ -10,15 +10,17 @@ cd "${SCRIPT_DIR}" distro="$(lsb_release -is)" release="$(lsb_release -rs)" codename="$(lsb_release -cs)" +sep="\n-------------------------------------------------------------------------------" +# functions + +#separator function separator { - sep="\n-------------------------------------------------------------------------------" echo -e $sep } - # root check function root_check { - if (($EUID != 0)); then + if ((EUID != 0)); then separator echo -e "\nMust be run as root (i.e: 'sudo $0')." separator @@ -31,11 +33,6 @@ function pip_pkg_install { python3 -m pip install -r requirements.txt } -function complete_msg { - echo -e "\nauto-cpufreq tool successfully installed.\n" - echo -e "For list of options, run:\nauto-cpufreq" -} - # tool install function install { python3 setup.py install --record files.txt @@ -44,141 +41,108 @@ function install { } function update_service_file { - sed -i 's|ExecStart=/usr/local/bin/auto-cpufreq|ExecStart=/usr/bin/auto-cpufreq|' /usr/local/share/auto-cpufreq/scripts/auto-cpufreq.service + echo -e "\nUpdating service file (/usr/local/bin/auto-cpufreq -> /usr/bin/auto-cpu-freq)" + sed -i 's|ExecStart=/usr/local/bin/auto-cpufreq|ExecStart=/usr/bin/auto-cpufreq|' \ + /usr/local/share/auto-cpufreq/scripts/auto-cpufreq.service +} + +# First argument is the distro +function detected_distro() { + echo -e "\nDetected $1 distribution" + separator + echo -e "\nSetting up Python environment\n" +} + +# merged functions pip - install - complete_msg, since it repeats +function completed () { + echo -e "\nInstalling necessary Python packages\n" + pip_pkg_install + separator + echo -e "\ninstalling auto-cpufreq tool\n" + install + separator + echo -e "\nauto-cpufreq tool successfully installed.\n" + echo -e "For list of options, run:\nauto-cpufreq" + separator +} + +function manual_install { + echo -e "\nDidn't detect Debian or RedHat based distro.\n" + echo -e "To complete installation, you need to:" + echo -e "Install: python3, pip3, python3-setuptools\n" + echo -e "Install necessary Python packages:" + echo -e "pip3 install psutil click distro power" + echo -e "\nRun following sequence of lines:" + echo -e "\n-----" + echo -e "\npython3 setup.py install --record files.txt" + echo -e "mkdir -p /usr/local/share/auto-cpufreq/" + echo -e "cp -r scripts/ /usr/local/share/auto-cpufreq/" + echo -e "\n-----" + echo -e "\nAfter which tool is installed, for full list of options run:" + echo -e "auto-cpufreq" + separator + echo -e "\nConsider creating a feature request to add support for your distro:" + echo -e "https://github.com/AdnanHodzic/auto-cpufreq/issues/new" + echo -e "\nMake sure to include following information:\n" + echo -e "Distribution: $distro" + echo -e "Release: $release" + echo -e "Codename: $codename" + separator } function tool_install { - # Debian - if [ -f /etc/debian_version ]; then - separator - echo -e "\nDetected Debian based distribution" - separator - echo -e "\nSetting up Python environment\n" - apt install python3-dev python3-pip python3-setuptools dmidecode -y - separator - echo -e "\nInstalling necessary Python packages\n" - pip_pkg_install - separator - echo -e "\ninstalling auto-cpufreq tool\n" - install - separator - complete_msg - separator - # RedHat - elif [ -f /etc/redhat-release ]; then - separator - echo -e "\nDetected RedHat based distribution\n" - echo -e "\nSetting up Python environment\n" - # CentOS exception - if [ -f /etc/centos-release ]; then - yum install platform-python-devel dmidecode - else - yum install python-devel dmidecode - fi - echo -e "\nInstalling necessary Python packages\n" - pip_pkg_install - separator - echo -e "\ninstalling auto-cpufreq tool\n" - install - separator - complete_msg - separator - # Solus - elif [ -f /etc/solus-release ]; then - separator - echo -e "\nDetected Solus distribution\n" - echo -e "\nSetting up Python environment\n" - eopkg install pip python3 python3-devel dmidecode - eopkg install -c system.devel - echo -e "\nInstalling necessary Python packages\n" - pip_pkg_install - separator - echo -e "\ninstalling auto-cpufreq tool\n" - install - update_service_file - separator - complete_msg - separator - # OpenSUSE - elif [ -f /etc/os-release ] && eval "$(cat /etc/os-release)" && [[ $ID == "opensuse"* ]]; then - separator - echo -e "\nDetected an OpenSUSE distribution\n" - echo -e "\nSetting up Python environment\n" - if [[ $ID == "opensuse-leap" ]]; then - zypper install -y python3 python3-pip python3-setuptools python3-devel gcc dmidecode - else - zypper install -y python38 python3-pip python3-setuptools python3-devel gcc dmidecode - fi - echo -e "\nInstalling necessary Python packages\n" - pip_pkg_install - separator - echo -e "\ninstalling auto-cpufreq tool\n" - install - separator - complete_msg - separator - # Manjaro/Arch - elif [ -f /etc/os-release ] && eval "$(cat /etc/os-release)" && [[ $ID == "manjaro" || $ID == "arch" || $ID == "endeavouros" || $ID == "garuda" || $ID == "artix" ]]; then - separator - echo -e "\nDetected an Arch Linux based distribution\n" - echo -e "\nSetting up Python environment\n" - pacman -S --noconfirm --needed python python-pip python-setuptools base-devel dmidecode - echo -e "\nInstalling necessary Python packages\n" - pip_pkg_install - separator - echo -e "\ninstalling auto-cpufreq tool\n" - install - if [[ $ID != "artix" ]]; then - update_service_file - fi - separator - complete_msg - separator - # Void Linux - elif [ -f /etc/os-release ] && eval "$(cat /etc/os-release)" && [[ $ID == "void"* ]]; then - separator - echo -e "\nDetected Void Linux ditribution\n" - separator - echo -e "\nSetting up Python environment\n" - xbps-install -Suy python3 python3-pip python3-devel python3-setuptools base-devel dmidecode - separator - echo -e "\nInstalling necessary Python packages\n" - pip_pkg_install - separator - echo -e "\ninstalling auto-cpufreq tool\n" - install - separator - complete_msg - separator - # Other - else - separator - echo -e "\nDidn't detect Debian or RedHat based distro.\n" - echo -e "To complete installation, you need to:" - echo -e "Install: python3, pip3, python3-setuptools\n" - echo -e "Install necessary Python packages:" - echo -e "pip3 install psutil click distro power" - echo -e "\nRun following sequence of lines:" - echo -e "\n-----" - echo -e "\npython3 setup.py install --record files.txt" - echo -e "mkdir -p /usr/local/share/auto-cpufreq/" - echo -e "cp -r scripts/ /usr/local/share/auto-cpufreq/" - echo -e "\n-----" - echo -e "\nAfter which tool is installed, for full list of options run:" - echo -e "auto-cpufreq" - separator - echo -e "\nConsider creating a feature request to add support for your distro:" - echo -e "https://github.com/AdnanHodzic/auto-cpufreq/issues/new" - echo -e "\nMake sure to include following information:\n" - echo -e "Distribution: $distro" - echo -e "Release: $release" - echo -e "Codename: $codename" - separator - fi +separator +if [ -f /etc/debian_version ]; then + detected_distro "Debian based" + apt install python3-dev python3-pip python3-setuptools dmidecode -y + completed +elif [ -f /etc/redhat-release ]; then + detected_distro "RedHat based" + if [ -f /etc/centos-release ]; then + yum install platform-python-devel dmidecode + else + yum install python-devel dmidecode + fi + completed +elif [ -f /etc/solus-release ]; then + detected_distro "Solus" + eopkg install pip python3 python3-devel dmidecode + eopkg install -c system.devel + update_service_file + completed +elif [ -f /etc/os-release ];then + eval "$(cat /etc/os-release)" + separator + case $ID in + opensuse-leap) + detected_distro "OpenSUSE" + zypper install -y python3 python3-pip python3-setuptools python3-devel gcc dmidecode + ;; + opensuse) + detected_distro "OpenSUSE" + echo -e "\nDetected an OpenSUSE ditribution\n\nSetting up Python environment\n" + zypper install -y python38 python3-pip python3-setuptools python3-devel gcc dmidecode + ;; + arch|manjaro|endeavouros|garuda|artix) + detected_distro "Arch Linux based" + pacman -S --noconfirm --needed python python-pip python-setuptools base-devel dmidecode + [ $ID != "artix" ] && update_service_file + ;; + void) + detected_distro "Void Linux" + xbps-install -Suy python3 python3-pip python3-devel python3-setuptools base-devel dmidecode + ;; + *) #Any other distro + manual_install + ;; + esac + completed +else # In case /etc/os-release doesn't exist + manual_install +fi } function tool_remove { - files="files.txt" share_dir="/usr/local/share/auto-cpufreq/" srv_install="/usr/bin/auto-cpufreq-install" @@ -217,12 +181,14 @@ function ask_operation { echo -e "\n-------------------------- auto-cpufreq installer -----------------------------" echo -e "\nWelcome to auto-cpufreq tool installer." echo -e "\nOptions:\n" - read -p "[I]nstall + read -p \ +"[I]nstall [R]emove [Q]uit Select a key: [i/r/q]: " answer } +# End of functions root_check @@ -242,18 +208,17 @@ else esac fi -if [[ $answer == [Ii] ]]; then - root_check - tool_install -elif [[ $answer == [Rr] ]]; then - root_check - tool_remove -elif [[ $answer == [Qq] ]]; then - separator - echo "" - exit 0 -else - separator - echo -e "\nUnknown key, aborting ...\n" - exit 1 -fi +case $answer in + I|i) + tool_install ;; + R|r) + tool_remove ;; + Q|q) + separator + echo "" + exit 0 ;; + *) + separator + echo -e "\nUnknown key, aborting ...\n" + exit 1 ;; +esac diff --git a/scripts/auto-cpufreq-install.sh b/scripts/auto-cpufreq-install.sh index 6ffe8d4..4a3f3a2 100755 --- a/scripts/auto-cpufreq-install.sh +++ b/scripts/auto-cpufreq-install.sh @@ -11,40 +11,50 @@ then exit 1 fi +# First argument is the "sv" path, second argument is the "service" path this +# only exist because the path between distros may vary +runit_ln() { + echo -e "\n* Deploy auto-cpufreq runit unit file" + mkdir "$1"/sv/auto-cpufreq + cp /usr/local/share/auto-cpufreq/scripts/run "$1"/sv/auto-cpufreq + chmod +x "$1"/sv/auto-cpufreq/run -# Install script for runit -if [ -f /etc/os-release ] && eval "$(cat /etc/os-release)" && [[ $ID == "void"* ]]; then - echo -e "\n* Deploy auto-cpufreq runit unit file" - mkdir /etc/sv/auto-cpufreq - cp /usr/local/share/auto-cpufreq/scripts/run /etc/sv/auto-cpufreq - chmod +x /etc/sv/auto-cpufreq/run + echo -e "\n* Creating symbolic link ($2/service/auto-cpufreq -> $1/sv/auto-cpufreq)" + ln -s "$1"/sv/auto-cpufreq "$2"/service +} - echo -e "\n* Creating symbolic link (/var/service/auto-cpufreq -> /etc/sv/auto-cpufreq)" - ln -s /etc/sv/auto-cpufreq /var/service +# sv commands +sv_cmd() { + echo -e "\n* Stopping auto-cpufreq daemon (runit) service" + sv stop auto-cpufreq + echo -e "\n* Starting auto-cpufreq daemon (runit) service" + sv start auto-cpufreq + sv up auto-cpufreq +} - echo -e "\n* Stopping auto-cpufreq daemon (runit) service" - sv stop auto-cpufreq +# Installation for runit, we still look for the distro because of the path may +# vary. +if [ "$(ps h -o comm 1)" = "runit" ];then + if [ -f /etc/os-release ];then + eval "$(cat /etc/os-release)" + separator + case $ID in + void) + runit_ln /etc /var + sv_cmd + ;; + artix) + # Note: Artix supports other inits than runnit + runit_ln /etc/runit /run/runit + sv_cmd + ;; + *) + echo -e "\n* Runit init detected but your distro is not supported\n" + echo -e "\n* Please open an issue on https://github.com/AdnanHodzic/auto-cpufreq\n" - echo -e "\n* Starting auto-cpufreq daemon (runit) service" - sv start auto-cpufreq - sv up auto-cpufreq - -elif [ -f /etc/os-release ] && eval "$(cat /etc/os-release)" && [[ $ID == "artix"* ]]; then - echo -e "\n* Deploy auto-cpufreq runit unit file" - mkdir /etc/runit/sv/auto-cpufreq - cp /usr/local/share/auto-cpufreq/scripts/run /etc/runit/sv/auto-cpufreq - chmod +x /etc/runit/sv/auto-cpufreq/run - - echo -e "\n* Creating symbolic link (/run/runit/service/auto-cpufreq -> /etc/runit/sv/auto-cpufreq)" - ln -s /etc/runit/sv/auto-cpufreq /run/runit/service - - echo -e "\n* Stopping auto-cpufreq daemon (runit) service" - sv stop auto-cpufreq - - echo -e "\n* Starting auto-cpufreq daemon (runit) service" - sv start auto-cpufreq - sv up auto-cpufreq + esac + fi # Install script for systemd else echo -e "\n* Deploy auto-cpufreq systemd unit file" diff --git a/scripts/auto-cpufreq-remove.sh b/scripts/auto-cpufreq-remove.sh index 704ba4e..3e88c2e 100755 --- a/scripts/auto-cpufreq-remove.sh +++ b/scripts/auto-cpufreq-remove.sh @@ -5,21 +5,38 @@ echo -e "\n------------------ Running auto-cpufreq daemon removal script ------------------" -if [[ $EUID != 0 ]]; -then +if [[ $EUID != 0 ]]; then echo -e "\nERROR\nMust be run as root (i.e: 'sudo $0')\n" exit 1 fi -if [ -f /etc/os-release ] && eval "$(cat /etc/os-release)" && [[ $ID == "void"* ]]; then +# First argument is the "sv" path, second argument is the "service" path +rm_sv() { echo -e "\n* Stopping auto-cpufreq daemon (runit) service" sv stop auto-cpufreq echo -e "\n* Removing auto-cpufreq daemon (runit) unit files" - rm -rf /etc/sv/auto-cpufreq - rm -rf /var/service/auto-cpufreq + rm -rf "$1"/sv/auto-cpufreq + rm -rf "$2"/service/auto-cpufreq +} +# Remove service for runit +if [ "$(ps h -o comm 1)" = "runit" ];then + if [ -f /etc/os-release ];then + eval "$(cat /etc/os-release)" + separator + case $ID in + void) + rm_sv /etc /var ;; + artix) + rm_sv /etc/runit /run/runit ;; + *) + echo -e "\n* Runit init detected but your distro is not supported\n" + echo -e "\n* Please open an issue on https://github.com/AdnanHodzic/auto-cpufreq\n" + + esac + fi else echo -e "\n* Stopping auto-cpufreq daemon (systemd) service" systemctl stop auto-cpufreq