From 4387d40f5f8985e99a9ea565691c45dd120a709f Mon Sep 17 00:00:00 2001 From: Masoko Date: Sat, 12 Apr 2025 13:48:49 +0300 Subject: [PATCH] Fix/installation (#215) * fix installation when using service - no need for ctrl+c * Improve uninstallatino process --- README.md | 11 ++++++++++- install.sh | 1 - remote_install.sh | 24 +++++++++++++++++++----- src/rpi-cpu2mqtt.py | 25 ++++++++++++++++++++++++- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c57cf98..6e0894c 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ The easiest way to track your Raspberry Pi or Ubuntu computer system health and * Remotely restart / shutdown your system and control your monitors. * Automatic HASS configuration: Supports discovery messages, so no manual configuration in [Home Assistant](https://www.home-assistant.io/) configuration.yaml is needed. * Automated installation and configuration: you can install it and schedule it with a service or cron with just one command from shell. +* Easy uninstallation, just run rpi-mqtt-monitor --uninstall * Configurable: You can select what is monitored and how the message(s) is send (separately or as one csv message). * Easy update: You can update the script by calling it with command line "rpi-mqtt-monitor --update" or via Home Assistant UI. * Support multiple languages: English, German and Bulgarian @@ -63,7 +64,7 @@ The easiest way to track your Raspberry Pi or Ubuntu computer system health and ## CLI arguments ``` -usage: rpi-mqtt-monitor [-h] [-H] [-d] [-s] [-v] [-u] [-w] +usage: rpi-mqtt-monitor [-h] [-H] [-d] [-s] [-v] [-u] [-w] [--uninstall] Monitor CPU load, temperature, frequency, free space, etc., and publish the data to an MQTT server or Home Assistant API. @@ -75,6 +76,7 @@ options: -v, --version display installed version and exit -u, --update update script and config then exit -w, --hass_wake display Home assistant wake on lan configuration + --uninstall uninstall rpi-mqtt-monitor and remove all related files ``` @@ -106,6 +108,13 @@ It is recommended to run the script as a service, this way you can use the resta ### Manual [moved to wiki](../../wiki/Manual-Installation) +## Uninstallation + +To uninstall Raspberry Pi MQTT Monitor, run the following command: + +'''rpi-mqtt-monitor --uninstall''' + + ## Home Assistant Integration If you are using discovery_messages, then this step is not required as a new MQTT device will be automatically created in Home Assistant and all you need to do is add it to a dashboard. diff --git a/install.sh b/install.sh index 41979ac..c6584f2 100755 --- a/install.sh +++ b/install.sh @@ -237,7 +237,6 @@ set_service(){ sudo systemctl enable rpi-mqtt-monitor.service sudo systemctl start rpi-mqtt-monitor.service sudo service rpi-mqtt-monitor restart - sudo service rpi-mqtt-monitor status print_green "+ Service is enabled and started" git config --global --add safe.directory ${cwd} } diff --git a/remote_install.sh b/remote_install.sh index 54e5f19..a7b3361 100755 --- a/remote_install.sh +++ b/remote_install.sh @@ -24,15 +24,29 @@ welcome(){ uninstall(){ printm "Uninstalling rpi-mqtt-monitor" - - # Remove the rpi-mqtt-monitor directory - if [ -d "rpi-mqtt-monitor" ]; then - rm -rf rpi-mqtt-monitor + + # Ask for confirmation before proceeding + read -r -p "Are you sure you want to uninstall rpi-mqtt-monitor? [y/N] " response + if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then + echo "Uninstallation canceled." + exit + fi + + # Get the absolute path of the script + script_dir=$(dirname "$(realpath "$0")") + + # Remove the rpi-mqtt-monitor directory if it exists + if [ -d "$script_dir" ]; then + if [ "$(realpath rpi-mqtt-monitor)" == "$script_dir" ]; then + # If the script is running from the installation directory, navigate out of it + cd .. + fi + sudo rm -rf "$script_dir" echo "Removed rpi-mqtt-monitor directory." else echo "rpi-mqtt-monitor directory not found." fi - + # Remove the cron job if it exists if crontab -l | grep -q rpi-cpu2mqtt.py; then crontab -l | grep -v rpi-cpu2mqtt.py | crontab - diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 429db63..9493f33 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -869,7 +869,7 @@ def parse_arguments(): parser.add_argument('-v', '--version', action='store_true', help='display installed version and exit', default=False) parser.add_argument('-u', '--update', action='store_true', help='update script and config then exit', default=False) parser.add_argument('-w', '--hass_wake', action='store_true', help='display Home assistant wake on lan configuration', default=False) - + parser.add_argument('--uninstall', action='store_true', help='uninstall rpi-mqtt-monitor and remove all related files') args = parser.parse_args() if args.update: @@ -1015,6 +1015,24 @@ def update_status(): break +def uninstall_script(): + """Call the remote_install.sh script to uninstall the application.""" + script_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../remote_install.sh") + + if not os.path.exists(script_path): + print("Error: remote_install.sh script not found.") + return + + try: + # Run the uninstall command + subprocess.run(["bash", script_path, "uninstall"], check=True) + print("Uninstallation process completed.") + except subprocess.CalledProcessError as e: + print(f"Error during uninstallation: {e}") + except Exception as e: + print(f"Unexpected error: {e}") + + def on_message(client, userdata, msg): global exit_flag, thread1, thread2 print("Received message: ", msg.payload.decode()) @@ -1060,6 +1078,11 @@ else: if __name__ == '__main__': args = parse_arguments(); + + if args.uninstall: + uninstall_script() + sys.exit(0) + if args.service: if not args.hass_api: client = paho.Client()