|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +## Install the Grott proxy server on the current system |
| 4 | +## |
| 5 | +## install_grott(String openhab-ip-address, String username, String interactive) |
| 6 | +## |
| 7 | +install_grott() { |
| 8 | + # Validate install type argument |
| 9 | + if [ "$#" -lt 1 ]; then |
| 10 | + echo "Error: Please supply install type." |
| 11 | + echo "Usage: $0 <install|remove> <username> (<openhab-ip>)" |
| 12 | + exit 1 |
| 13 | + elif [[ "$1" != "install" && "$1" != "remove" ]]; then |
| 14 | + echo "Error: Invalid install type '$1'. Use 'install' or 'remove'." |
| 15 | + exit 1 |
| 16 | + fi |
| 17 | + INSTALL_TYPE="$1" |
| 18 | + |
| 19 | + # Validate user name argument |
| 20 | + if [ "$#" -lt 2 ]; then |
| 21 | + echo "Error: Please supply user name." |
| 22 | + echo "Usage: $0 <install|remove> <username> (<openhab-ip>)" |
| 23 | + exit 1 |
| 24 | + elif ! id "$2" &>/dev/null; then |
| 25 | + echo "Error: User '$2' does not exist." |
| 26 | + exit 1 |
| 27 | + fi |
| 28 | + USERNAME="$2" |
| 29 | + INSTALL_DIR="/home/$USERNAME/grott" |
| 30 | + |
| 31 | + SERVICE_FILE="/etc/systemd/system/grott.service" |
| 32 | + |
| 33 | + if [[ $INSTALL_TYPE == "install" ]]; then |
| 34 | + # Validate openhab ip address argument |
| 35 | + if [ "$#" -lt 3 ]; then |
| 36 | + echo "Error: Please supply openHAB IP address." |
| 37 | + echo "Usage: $0 <install|remove> <username> <openhab-ip>" |
| 38 | + exit 1 |
| 39 | + elif ! [[ "$3" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 40 | + echo "Error: Invalid IP address format: $3" |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + EXT_URL="http://$3:8080/growatt" |
| 44 | + |
| 45 | + echo -n "[openHABian] Installing Grott Proxy with extension URL: $EXT_URL " |
| 46 | + |
| 47 | + # Update system and install dependencies |
| 48 | + sudo apt update |
| 49 | + sudo apt install -y python3 python3-pip |
| 50 | + sudo pip3 install paho-mqtt requests # paho-mqtt is a required dependency (even if disabled) |
| 51 | + |
| 52 | + # Prepare installation directory and pouplate it with Grott files |
| 53 | + sudo -u "$USERNAME" mkdir -p "$INSTALL_DIR" |
| 54 | + if [ ! -d "$INSTALL_DIR" ]; then |
| 55 | + echo "Error: Installation directory $INSTALL_DIR does not exist." |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | + |
| 59 | + download_grott_files || { |
| 60 | + echo "Failed to download Grott files. Exiting." |
| 61 | + return 1 |
| 62 | + } |
| 63 | + |
| 64 | + # Create grott.service systemd file from template |
| 65 | + if ! sed -e "s|%INSTALL_DIR|$INSTALL_DIR|g" \ |
| 66 | + -e "s|%USERNAME|$USERNAME|g" \ |
| 67 | + "${BASEDIR:-/opt/openhabian}/includes/grott.service" > "$SERVICE_FILE"; then |
| 68 | + echo "FAILED (sed substitution)" |
| 69 | + return 1 |
| 70 | + fi |
| 71 | + |
| 72 | + # Create grott.ini file from template |
| 73 | + if ! sed -e "s|%EXT_URL|$EXT_URL|g" \ |
| 74 | + "${BASEDIR:-/opt/openhabian}/includes/grott.ini" > "$INSTALL_DIR/grott.ini"; then |
| 75 | + echo "FAILED (sed substitution)" |
| 76 | + return 1 |
| 77 | + fi |
| 78 | + |
| 79 | + # Enable and start service |
| 80 | + sudo systemctl daemon-reexec |
| 81 | + sudo systemctl enable grott |
| 82 | + sudo systemctl start grott |
| 83 | + |
| 84 | + if [[ -n "$INTERACTIVE" ]]; then |
| 85 | + whiptail --title "Grott Proxy installed" --msgbox "We installed Grott Proxy on your system." 7 80 |
| 86 | + fi |
| 87 | + |
| 88 | + return 0 |
| 89 | + |
| 90 | + elif [[ $INSTALL_TYPE == "remove" ]]; then |
| 91 | + echo -n "[openHABian] Removing Grott Proxy... " |
| 92 | + |
| 93 | + # Stop and disable systemd service |
| 94 | + if sudo systemctl is-active --quiet grott; then |
| 95 | + sudo systemctl stop grott |
| 96 | + fi |
| 97 | + |
| 98 | + if sudo systemctl is-enabled --quiet grott; then |
| 99 | + sudo systemctl disable grott |
| 100 | + fi |
| 101 | + |
| 102 | + # Remove systemd service file |
| 103 | + if [ -f "$SERVICE_FILE" ]; then |
| 104 | + sudo rm "$SERVICE_FILE" |
| 105 | + sudo systemctl daemon-reload |
| 106 | + fi |
| 107 | + |
| 108 | + # Remove installation directory |
| 109 | + if [ -d "$INSTALL_DIR" ]; then |
| 110 | + sudo rm -rf "$INSTALL_DIR" |
| 111 | + echo "Removed $INSTALL_DIR" |
| 112 | + else |
| 113 | + echo "Directory $INSTALL_DIR not found." |
| 114 | + fi |
| 115 | + |
| 116 | + if [[ -n "$INTERACTIVE" ]]; then |
| 117 | + whiptail --title "Grott Proxy removed" --msgbox "We removed Grott Proxy from your system." 7 80 |
| 118 | + fi |
| 119 | + |
| 120 | + return 0 |
| 121 | + fi |
| 122 | +} |
| 123 | + |
| 124 | +download_grott_files() { |
| 125 | + GROTT_FILE_URL="https://raw.githubusercontent.com/johanmeijer/grott/master" |
| 126 | + GROTT_PY_FILES=( |
| 127 | + "grott.py" |
| 128 | + "grottconf.py" |
| 129 | + "grottdata.py" |
| 130 | + "grottproxy.py" |
| 131 | + "grottserver.py" |
| 132 | + "grottsniffer.py" |
| 133 | + ) |
| 134 | + |
| 135 | + for file in "${GROTT_PY_FILES[@]}"; do |
| 136 | + FILE_URL="${GROTT_FILE_URL}/${file}" |
| 137 | + echo "Downloading $file..." |
| 138 | + curl -fsSL "$FILE_URL" -o "${INSTALL_DIR}/${file}" || { |
| 139 | + echo "Failed to download $file" |
| 140 | + return 1 |
| 141 | + } |
| 142 | + done |
| 143 | +} |
0 commit comments