From 1589a538b63966d59d368099425180ff943575d9 Mon Sep 17 00:00:00 2001 From: Manuel Date: Tue, 29 Oct 2024 15:51:32 +0100 Subject: [PATCH] Made install and upgrade easier --- README.md | 109 +++++++++++---------- dbus-mqtt-grid/restart.sh | 8 +- download.sh | 197 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 258 insertions(+), 56 deletions(-) create mode 100644 download.sh diff --git a/README.md b/README.md index 8802cfa..67c507d 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,10 @@ 1. [JSON structure](#json-structure) 1. [Tasmota](#tasmota) 1. [Home Assistant](#home-assistant) -1. [Install](#install) +1. [Install / Update](#install--update) 1. [Uninstall](#uninstall) 1. [Restart](#restart) 1. [Debugging](#debugging) -1. [Multiple instances](#multiple-instances) 1. [Compatibility](#compatibility) 1. [Screenshots](#screenshots) @@ -243,63 +242,86 @@ config.ini topic = SML/SENSOR Additional information can be found in this [issue](https://github.com/mr-manuel/venus-os_dbus-mqtt-grid/issues/13#issue-2045377392). -## Install +## Install / Update 1. Login to your Venus OS device via SSH. See [Venus OS:Root Access](https://www.victronenergy.com/live/ccgx:root_access#root_access) for more details. -2. Execute this commands to download and extract the files: +2. Execute this commands to download and copy the files: ```bash - # change to temp folder - cd /tmp + wget -O /tmp/download_dbus-mqtt-grid.sh https://raw.githubusercontent.com/mr-manuel/venus-os_dbus-mqtt-grid/master/download.sh - # download driver - wget -O /tmp/venus-os_dbus-mqtt-grid.zip https://github.com/mr-manuel/venus-os_dbus-mqtt-grid/archive/refs/heads/master.zip + bash /tmp/download_dbus-mqtt-grid.sh + ``` - # If updating: cleanup old folder - rm -rf /tmp/venus-os_dbus-mqtt-grid-master +3. Select the version you want to install. - # unzip folder - unzip venus-os_dbus-mqtt-grid.zip +4. Press enter for a single instance. For multiple instances, enter a number and press enter. - # If updating: backup existing config file - mv /data/etc/dbus-mqtt-grid/config.ini /data/etc/dbus-mqtt-grid_config.ini + Example: - # If updating: cleanup existing driver - rm -rf /data/etc/dbus-mqtt-grid + - Pressing enter or entering `1` will install the driver to `/data/etc/dbus-mqtt-grid`. + - Entering `2` will install the driver to `/data/etc/dbus-mqtt-grid-2`. - # copy files - cp -R /tmp/venus-os_dbus-mqtt-grid-master/dbus-mqtt-grid/ /data/etc/ +### Extra steps for your first installation - # If updating: restore existing config file - mv /data/etc/dbus-mqtt-grid_config.ini /data/etc/dbus-mqtt-grid/config.ini - ``` +5. Edit the config file to fit your needs. The correct command for your installation is shown after the installation. -3. Copy the sample config file, if you are installing the driver for the first time and edit it to your needs. + - If you pressed enter or entered `1` during installation: + ```bash + nano /data/etc/dbus-mqtt-grid/config.ini + ``` + - If you entered `2` during installation: ```bash - # copy default config file - cp /data/etc/dbus-mqtt-grid/config.sample.ini /data/etc/dbus-mqtt-grid/config.ini + nano /data/etc/dbus-mqtt-grid-2/config.ini + ``` - # edit the config file with nano - nano /data/etc/dbus-mqtt-grid/config.ini +6. Install the driver as a service. The correct command for your installation is shown after the installation. + + - If you pressed enter or entered `1` during installation: + ```bash + bash /data/etc/dbus-mqtt-grid/install.sh ``` -4. Run `bash /data/etc/dbus-mqtt-grid/install.sh` to install the driver as service. + - If you entered `2` during installation: + ```bash + bash /data/etc/dbus-mqtt-grid-2/install.sh + ``` - The daemon-tools should start this service automatically within seconds. + The daemon-tools should start this service automatically within seconds. ## Uninstall -Run `/data/etc/dbus-mqtt-grid/uninstall.sh` +⚠️ If you have multiple instances, ensure you choose the correct one. For example: + +- To uninstall the default instance: + ```bash + bash /data/etc/dbus-mqtt-grid/uninstall.sh + ``` + +- To uninstall the second instance: + ```bash + bash /data/etc/dbus-mqtt-grid-2/uninstall.sh + ``` ## Restart -Run `/data/etc/dbus-mqtt-grid/restart.sh` +⚠️ If you have multiple instances, ensure you choose the correct one. For example: + +- To restart the default instance: + ```bash + bash /data/etc/dbus-mqtt-grid/restart.sh + ``` + +- To restart the second instance: + ```bash + bash /data/etc/dbus-mqtt-grid-2/restart.sh + ``` ## Debugging -The logs can be checked with `tail -n 100 -F /data/log/dbus-mqtt-grid/current | tai64nlocal` +⚠️ If you have multiple instances, ensure you choose the correct one. The service status can be checked with svstat `svstat /service/dbus-mqtt-grid` @@ -309,32 +331,9 @@ If the seconds are under 5 then the service crashes and gets restarted all the t If the script stops with the message `dbus.exceptions.NameExistsException: Bus name already exists: com.victronenergy.grid.mqtt_grid"` it means that the service is still running or another service is using that bus name. -## Multiple instances - -It's possible to have multiple instances, but it's not automated. Follow these steps to achieve this: - -1. Save the new name to a variable `driverclone=dbus-mqtt-grid-2` - -2. Copy current folder `cp -r /data/etc/dbus-mqtt-grid/ /data/etc/$driverclone/` - -3. Rename the main script `mv /data/etc/$driverclone/dbus-mqtt-grid.py /data/etc/$driverclone/$driverclone.py` - -4. Fix the script references for service and log - ``` - sed -i 's:dbus-mqtt-grid:'$driverclone':g' /data/etc/$driverclone/service/run - sed -i 's:dbus-mqtt-grid:'$driverclone':g' /data/etc/$driverclone/service/log/run - ``` - -5. Change the `device_name`, increase the `device_instance` and update the `topic` in the `config.ini` - -Now you can install and run the cloned driver. Should you need another instance just increase the number in step 1 and repeat all steps. - ## Compatibility -It was tested on Venus OS Large `v2.92` on the following devices: - -* RaspberryPi 4b -* MultiPlus II (GX Version) +This software supports the latest three stable versions of Venus OS. It may also work on older versions, but this is not guaranteed. ## Screenshots diff --git a/dbus-mqtt-grid/restart.sh b/dbus-mqtt-grid/restart.sh index ad27894..2f7c751 100644 --- a/dbus-mqtt-grid/restart.sh +++ b/dbus-mqtt-grid/restart.sh @@ -2,4 +2,10 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) SERVICE_NAME=$(basename $SCRIPT_DIR) -kill $(pgrep -f "python $SCRIPT_DIR/$SERVICE_NAME.py") +pid=$(pgrep -f "python $SCRIPT_DIR/$SERVICE_NAME.py") +if [ -n "$pid" ]; then + kill $pid + echo "** Driver restarted **" +else + echo "** Driver is not running **" +fi diff --git a/download.sh b/download.sh new file mode 100644 index 0000000..757aaa4 --- /dev/null +++ b/download.sh @@ -0,0 +1,197 @@ +#!/bin/bash + +driver_path="/data/etc" +driver_name="dbus-mqtt-grid" + +echo "" +echo "" + +# fetch version numbers for different versions +echo -n "Fetch current version numbers..." + +# latest release +latest_release_stable=$(curl -s https://api.github.com/repos/mr-manuel/venus-os_${driver_name}/releases/latest | grep "tag_name" | cut -d : -f 2,3 | tr -d "\ " | tr -d \" | tr -d \,) + +# nightly build +latest_release_nightly=$(curl -s https://raw.githubusercontent.com/mr-manuel/venus-os_${driver_name}/master/${driver_name}/${driver_name}.py | grep FirmwareVersion | awk -F'"' '{print $4}') + + +echo +PS3=$'\nSelect which version you want to install and enter the corresponding number: ' + +# create list of versions +version_list=( + "latest release \"$latest_release_stable\"" + "nightly build \"v$latest_release_nightly\"" + "quit" +) + +select version in "${version_list[@]}" +do + case $version in + "latest release \"$latest_release_stable\"") + break + ;; + "nightly build \"v$latest_release_nightly\"") + break + ;; + "quit") + exit 0 + ;; + *) + echo "> Invalid option: $REPLY. Please enter a number!" + ;; + esac +done + +echo "> Selected: $version" +echo "" + + +# Which driver instance do you want to install? +echo "Which driver instance do you want to install/update?" +read -p "Enter the driver instance number you want to install/update. If you don't know just press enter [1]: " driver_instance + +if [ -n "$driver_instance" ] && [ "$driver_instance" != "1" ]; then + driver_name_instance="${driver_name}-${driver_instance}" +else + driver_name_instance=${driver_name} +fi + + +echo "" +if [ -d ${driver_path}/${driver_name_instance} ]; then + echo "Updating driver '$driver_name' as '$driver_name_instance'..." +else + echo "Installing driver '$driver_name' as '$driver_name_instance'..." +fi + + +# change to temp folder +cd /tmp + + +# download driver +echo "" +echo "Downloading driver..." + + +## latest release +if [ "$version" = "latest release \"$latest_release_stable\"" ]; then + # download latest release + url=$(curl -s https://api.github.com/repos/mr-manuel/venus-os_${driver_name}/releases/latest | grep "zipball_url" | sed -n 's/.*"zipball_url": "\([^"]*\)".*/\1/p') +fi + +## nightly build +if [ "$version" = "nightly build \"v$latest_release_nightly\"" ]; then + # download nightly build + url="https://github.com/mr-manuel/venus-os_${driver_name}/archive/refs/heads/master.zip" +fi + +echo "Downloading from: $url" +wget -O /tmp/venus-os_${driver_name}.zip "$url" + +# check if download was successful +if [ ! -f /tmp/venus-os_${driver_name}.zip ]; then + echo "" + echo "Download failed. Exiting..." + exit 1 +fi + + +# If updating: cleanup old folder +if [ -d /tmp/venus-os_${driver_name}-master ]; then + rm -rf /tmp/venus-os_${driver_name}-master +fi + + +# unzip folder +echo "Unzipping driver..." +unzip venus-os_${driver_name}.zip + +# Find and rename the extracted folder to be always the same +extracted_folder=$(find /tmp/ -maxdepth 1 -type d -name "*${driver_name}-*") + +if [ -n "$extracted_folder" ]; then + mv "$extracted_folder" /tmp/venus-os_${driver_name}-master +else + echo "Error: Could not find extracted folder. Exiting..." + exit 1 +fi + + +# If updating: backup existing config file +if [ -f ${driver_path}/${driver_name_instance}/config.ini ]; then + echo "" + echo "Backing up existing config file..." + mv ${driver_path}/${driver_name_instance}/config.ini ${driver_path}/${driver_name_instance}_config.ini +fi + + +# If updating: cleanup existing driver +if [ -d ${driver_path}/${driver_name_instance} ]; then + echo "" + echo "Cleaning up existing driver..." + rm -rf ${driver_path}/${driver_name_instance} +fi + + +# copy files +echo "" +echo "Copying new driver files..." +cp -R /tmp/venus-os_${driver_name}-master/${driver_name}/ ${driver_path}/${driver_name_instance}/ + +# remove temp files +echo "" +echo "Cleaning up temp files..." +rm -rf /tmp/venus-os_${driver_name}.zip +rm -rf /tmp/venus-os_${driver_name}-master + + +# check if driver_name is no equal to driver_name_instance +if [ "$driver_name" != "$driver_name_instance" ]; then + echo "" + echo "Renaming internal driver files..." + # rename the driver_name.py file to driver_name_instance.py + mv ${driver_path}/${driver_name_instance}/${driver_name}.py ${driver_path}/${driver_name_instance}/${driver_name_instance}.py + # rename the driver_name in the run file to driver_name_instance + sed -i 's:'${driver_name}':'${driver_name_instance}':g' ${driver_path}/${driver_name_instance}/service/run + # rename the driver_name in the log run file to driver_name_instance + sed -i 's:'${driver_name}':'${driver_name_instance}':g' ${driver_path}/${driver_name_instance}/service/log/run +fi + + +# If updating: restore existing config file +if [ -f ${driver_path}/${driver_name_instance}_config.ini ]; then + echo "" + echo "Restoring existing config file..." + mv ${driver_path}/${driver_name_instance}_config.ini ${driver_path}/${driver_name_instance}/config.ini +fi + + +# copy default config file +if [ ! -f ${driver_path}/${driver_name_instance}/config.ini ]; then + echo "" + echo "" + echo "First installation detected. Copying default config file..." + echo "" + echo "** Do not forget to edit the config file with your settings! **" + echo "You can edit the config file with the following command:" + echo "nano ${driver_path}/${driver_name_instance}/config.ini" + cp ${driver_path}/${driver_name_instance}/config.sample.ini ${driver_path}/${driver_name_instance}/config.ini + echo "" + echo "** Execute the install.sh script after you have edited the config file! **" + echo "You can execute the install.sh script with the following command:" + echo "bash ${driver_path}/${driver_name_instance}/install.sh" + echo "" +else + echo "" + echo "Restaring driver to apply new version..." + /bin/bash ${driver_path}/${driver_name_instance}/restart.sh +fi + + +echo +echo "Done." +echo +echo