Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to add persistent TEDAPI network routing. #520

Merged
merged 7 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# RELEASE NOTES

## v4.5.3 - TEDAPI Route Tool

* New `add_route.sh` tool to add persistent TEDAPI network routing by @SCHibbard in https://github.com/jasonacox/Powerwall-Dashboard/pull/520
* User can run `add_route.sh` to create a persistent route to the Powerwall TEDAPI endpoint. This can be run before `setup.sh` to allow [extended data local mode](https://github.com/jasonacox/Powerwall-Dashboard?tab=readme-ov-file#local-mode) for PW3 and PW2/+ systems. Existing user can also run this to set the route and re-run `setup.sh` to select the extended local mode.

## v4.5.2 - PW3 and FleetAPI Fixes

* Updates [pyPowerwall to v0.11.1](https://github.com/jasonacox/pypowerwall/pull/112) to fix a PW3 bug in TEDAPI and a site ID bug in FleetAPI.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.5.2
4.5.3
121 changes: 121 additions & 0 deletions add_route.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/bin/bash
#
# add_route.sh
# Script to setup persistant TEDAPI Routing for Powerwall Dashboard
# Version: 1.0.1
# By Scott Hibbard - 2024-09-15
#

CRONTAB="/var/spool/cron/crontabs/root"
PW_IP=""
SCRIPT_NAME="TEDAPI_routing"
DIR="/root/scripts"
LINUX_IP="192.168.91.0/24"
TARGET_IP="192.168.91.1"
NETMASK="255.255.255.255"
OS=$(uname -s)

echo "Setup script for persistant Powerall Dashboard TEDAPI Interface network routing"
echo "-------------------------------------------------------------------------------"
echo
echo "This script will require root privileges to read & set a startup cron task."
read -r -p "Do you want to run this script? [Y/n] " response
if [[ "$response" =~ ^([nN][oO]|[nN])$ ]]; then
echo "Cancel"
exit 1
fi

while [ "$PW_IP" == "" ]; do
read -p 'Enter Powerwall IP Address: ' PW_IP
done

# Detect OS and run commands accordingly
if [[ "${OS}" == "Linux" ]]; then
# Check if running under WSL
if grep -qi "microsoft" /proc/version; then
echo "WSL detected - unable to add route automatically."
echo "To add the route, open an Administrator Shell in Windows and run:"
echo " route -p add ${TARGET_IP} mask ${NETMASK} ${PW_IP}"
echo ""
read -p "Press Enter to exit..."
exit 1
else
echo "Native Linux detected"
if $(ip route | grep -qw ${LINUX_IP}); then
read -r -p "${LINUX_IP} routing already in routing table. Still want to run this? [y/N] " response
if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo "Cancel"
exit 1
fi
fi

sudo mkdir -p ${DIR}

if [ -f ${DIR}/${SCRIPT_NAME}.sh ]; then
echo "Boot script already exists (${DIR}/${SCRIPT_NAME}.sh)."
else
echo ""
cat > ${SCRIPT_NAME}.tmp << EOF
#!/bin/bash
#
# Add network route to Powerwall TEDAPI address on boot.
# This script is auto-generated by Powerwall Dashboard add_route.sh
#
declare -i i=0
while (( i < 15 )); do
RETURN="\$(ip route add ${LINUX_IP} via ${PW_IP} 2>&1)"
if [ "\$RETURN" != "RTNETLINK answers: File exists" ]; then
declare -i i=i+1
sleep 1
else
RETURN="Success"
break
fi
done

# Uncomment the lines below to log results of executing this script
# NOW="\$(date)"
# echo "\${NOW}: result=\${RETURN}, delay=\${i}" >> ${DIR}/${SCRIPT_NAME}.log

EOF
chmod 775 ${SCRIPT_NAME}.tmp
sudo chown 0 ${SCRIPT_NAME}.tmp
sudo mv ${SCRIPT_NAME}.tmp ${DIR}/${SCRIPT_NAME}.sh
fi

if ! (sudo test -f ${CRONTAB}) || ! (sudo grep -qw "${DIR}/${SCRIPT_NAME}.sh" ${CRONTAB}); then
(sudo crontab -u root -l 2>/dev/null; echo "@reboot ${DIR}/${SCRIPT_NAME}.sh") | sudo crontab -u root -
echo "Cron entry added."
else
echo "Cron line already exists."
fi
sudo /bin/bash ${DIR}/${SCRIPT_NAME}.sh
echo "Installation complete."
exit 0
fi
elif [[ "${OS}" == "Darwin" ]]; then
echo "macOS detected - adding permanent route for Wi-Fi" # TODO: Support for other network interfaces (Ethernet, etc.)
if sudo networksetup -setadditionalroutes Wi-Fi "${TARGET_IP}" "${NETMASK}" "${PW_IP}"; then
echo "Route added successfully."
else
echo "Failed to add the route. Please check your network configuration or permissions."
exit 1
fi
echo ""
exit 0
elif [[ "${OS}" =~ MINGW* || "${OS}" =~ CYGWIN* ]]; then
echo "Windows shell detected - attempting to add route automatically."
if route -p add "${TARGET_IP}" mask "${NETMASK}" "${PW_IP}"; then
echo "Route added successfully."
else
echo "Failed to add the route. Please ensure you are running as Administrator."
read -p "Press Enter to exit..."
exit 1
fi
echo ""
exit 0
else
echo "You are running '$OS', which is not supported in this script."
echo "Maybe you could add code to support '$OS'!"
SCHibbard marked this conversation as resolved.
Show resolved Hide resolved
exit 1
fi
2 changes: 1 addition & 1 deletion upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
set -e

# Set Globals
VERSION="4.5.2"
VERSION="4.5.3"
CURRENT="Unknown"
COMPOSE_ENV_FILE="compose.env"
INFLUXDB_ENV_FILE="influxdb.env"
Expand Down