Skip to content

Commit

Permalink
Bugfix for different user than openhabian in zigbee2mqtt-installation (
Browse files Browse the repository at this point in the history
…#1836)

* Raspberry-Imager Devices tag added

Raspberry Imager now needs a devices tag
Signed-off-by: Carsten Mogge <[email protected]>
  • Loading branch information
Larsen-Locke authored Feb 4, 2024
1 parent eb9bfbb commit 4e8f731
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
23 changes: 13 additions & 10 deletions functions/nodejs-apps.bash
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ nodejs_setup() {

echo -n "$(timestamp) [openHABian] Installing NodeJS... "
if [[ -n $PREOFFLINE ]]; then
if cond_redirect apt-get --quiet install --download-only --yes nodejs; then echo "OK"; else echo "FAILED"; return 1; fi
if cond_redirect apt-get --quiet install --download-only --yes nodejs npm; then echo "OK"; else echo "FAILED"; return 1; fi
else
if cond_redirect apt-get install --yes -o DPkg::Lock::Timeout="$APTTIMEOUT" nodejs; then echo "OK"; else echo "FAILED"; return 1; fi
if cond_redirect apt-get install --yes -o DPkg::Lock::Timeout="$APTTIMEOUT" nodejs npm; then echo "OK"; else echo "FAILED"; return 1; fi
fi
fi
}
Expand Down Expand Up @@ -214,8 +214,7 @@ nodered_setup() {
zigbee2mqtt_download() {
echo -n "$(timestamp) [openHABian] Downloading Zigbee2MQTT... "
if ! cond_redirect mkdir -p /opt/zigbee2mqtt; then echo "FAILED (mkdir -p /opt/zigbee2mqtt)"; fi
if ! cond_redirect chown openhabian /opt/zigbee2mqtt; then echo "FAILED (chown /opt/zigbee2mqtt)"; fi
if ! cond_redirect chgrp openhab /opt/zigbee2mqtt; then echo "FAILED (chgrp /opt/zigbee2mqtt)"; fi
if ! cond_redirect chown "${username:-openhabian}:openhab" /opt/zigbee2mqtt; then echo "FAILED (chown /opt/zigbee2mqtt)"; fi
if ! cond_redirect sudo -u "${username:-openhabian}" git clone https://github.com/Koenkk/zigbee2mqtt.git "/opt/zigbee2mqtt"; then echo "FAILED (git clone)"; return 1; fi
}

Expand All @@ -234,7 +233,7 @@ zigbee2mqtt_setup() {
local mqttPWText="\\nIf your MQTT-server requires a password, please enter it here:"
local my_adapters
local by_path_or_id
local mqttDefaultUser="openhabian"
local mqttDefaultUser="${username:-openhabian}"
local mqttUser
local serverIP
local installSuccessText
Expand All @@ -250,7 +249,7 @@ zigbee2mqtt_setup() {
if ! (whiptail --title "Zigbee2MQTT Uninstall" --yes-button "Continue" --no-button "Cancel" --yesno "$uninstallText" 7 80); then echo "CANCELED"; return 0; fi
fi
echo -n "$(timestamp) [openHABian] Removing Zigbee2MQTT service... "
if ! cond_redirect systemctl stop zigbee2mqtt.service; then echo "FAILED (disable service)"; return 1; fi
systemctl stop zigbee2mqtt.service
if ! rm -f /etc/systemd/system/zigbee2mqtt.service; then echo "FAILED (remove service)"; return 1; fi
if cond_redirect systemctl -q daemon-reload; then echo "OK"; else echo "FAILED (daemon-reload)"; return 1; fi

Expand Down Expand Up @@ -331,22 +330,26 @@ zigbee2mqtt_setup() {

echo -n "$(timestamp) [openHABian] Creating log directory... "
mkdir -p /var/log/zigbee2mqtt || (echo "FAILED (create log-directory)"; return 1)
chown openhabian /var/log/zigbee2mqtt || (echo "FAILED (create log-directory)"; return 1)
chgrp openhab /var/log/zigbee2mqtt || (echo "FAILED (create log-directory)"; return 1)
chown "${username:-openhabian}:openhab" /var/log/zigbee2mqtt || (echo "FAILED (create log-directory)"; return 1)
echo "OK"

echo -n "$(timestamp) [openHABian] Zigbee2MQTT install & config... "
cd /opt/zigbee2mqtt || (echo "FAILED (cd)"; return 1)
if ! cond_redirect sudo -u "${username:-openhabian}" npm ci ; then echo "FAILED (npm ci)"; return 1; fi
sed -e "s|%adapter|$by_path_or_id/$selectedAdapter|g" /opt/openhabian/includes/zigbee2mqtt/configuration.yaml | sudo -u "${username:-openhabian}" dd status=none of=/opt/zigbee2mqtt/data/configuration.yaml

if ! cond_redirect install -o "${username:-openhabian}" -g openhab -m 644 "${BASEDIR:-/opt/openhabian}/includes/zigbee2mqtt/configuration.yaml" /opt/zigbee2mqtt/data/; then echo "FAILED (install configuration.yaml)"; return 1; fi
sed -i -e "s|%adapter|$by_path_or_id/$selectedAdapter|g" /opt/zigbee2mqtt/data/configuration.yaml
sed -i -e "s|%user%|$mqttUser|g" /opt/zigbee2mqtt/data/configuration.yaml
sed -i -e "s|%password%|$mqttPW|g" /opt/zigbee2mqtt/data/configuration.yaml

cd /opt || (echo "FAILED (cd)"; return 1)
echo "OK"

echo -n "$(timestamp) [openHABian] Setting up Zigbee2MQTT service... "
if ! cond_redirect install -m 644 "${BASEDIR:-/opt/openhabian}"/includes/zigbee2mqtt/zigbee2mqtt.service /etc/systemd/system/; then echo "FAILED (install service)"; return 1; fi

if ! cond_redirect install -o "${username:-openhabian}" -g openhab -m 644 "${BASEDIR:-/opt/openhabian}/includes/zigbee2mqtt/zigbee2mqtt.service" /etc/systemd/system/; then echo "FAILED (install service)"; return 1; fi
sed -i -e "s|%user%|${username:-openhabian}|g" "/etc/systemd/system/zigbee2mqtt.service"

if ! cond_redirect systemctl -q daemon-reload; then echo "FAILED (daemon-reload)"; return 1; fi
if ! cond_redirect systemctl enable --now zigbee2mqtt.service; then echo "FAILED (enable service)"; return 1; fi
echo "OK"
Expand Down
12 changes: 8 additions & 4 deletions functions/packages.bash
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,12 @@ mqtt_setup() {
local mosquittoConf="/etc/mosquitto/mosquitto.conf"
local mosquittoPasswd="/etc/mosquitto/passwd"
local mqttPasswd
local mqttUser="openhabian"
local introText="The MQTT broker Eclipse Mosquitto will be installed from the official repository.\\n\\nIn addition, you can activate username:password authentication."
local questionText="\\nDo you want to secure your MQTT broker by a username:password combination? Every client will need to provide these upon connection.\\n\\nUsername will be '${mqttUser}', please provide a password (consisting of ASCII printable characters except space). Leave blank for no authentication, run setup again to change."
local mqttUser
local mqttDefaultUser="openhabian"
local introText="\\nThe MQTT broker Eclipse Mosquitto will be installed from the official repository."
local mqttUserText="\\nSecure your MQTT broker by a username:password combination. Every client will need to provide these upon connection.\\nPlease enter your MQTT-User (default = openhabian):"
local mqttPasswordText="\\nPlease provide a password (consisting of ASCII printable characters except space). Run setup again to change."

local successText="Setup was successful.\\n\\nEclipse Mosquitto is now up and running in the background. You should be able to make a first connection.\\n\\nTo continue your integration in openHAB, please follow the instructions under: https://www.openhab.org/addons/bindings/mqtt/"

echo -n "$(timestamp) [openHABian] Beginning the MQTT broker Eclipse Mosquitto installation... "
Expand All @@ -287,7 +290,8 @@ mqtt_setup() {

echo -n "$(timestamp) [openHABian] Configuring MQTT... "
if [[ -n $INTERACTIVE ]]; then
if ! mqttPasswd="$(whiptail --title "MQTT Authentication" --passwordbox "$questionText" 14 80 3>&1 1>&2 2>&3)"; then echo "CANCELED"; return 0; fi
if ! mqttUser=$(whiptail --title "MQTT User" --inputbox "$mqttUserText" 10 80 "$mqttDefaultUser" 3>&1 1>&2 2>&3); then return 0; fi
if ! mqttPasswd="$(whiptail --title "MQTT Authentication" --passwordbox "$mqttPasswordText" 14 80 3>&1 1>&2 2>&3)"; then echo "CANCELED"; return 0; fi
fi
if ! grep -qs "listener" ${mosquittoConf}; then
printf "\\n\\nlistener 1883" >> ${mosquittoConf}
Expand Down
2 changes: 1 addition & 1 deletion functions/system.bash
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ permissions_corrections() {
if ! cond_redirect fix_permissions /var/log/mosquitto "mosquitto:${username:-openhabian}" 644 755; then echo "FAILED (mosquitto log permissions)"; retval=1; fi
fi
if zigbee2mqtt_is_installed; then
if ! cond_redirect fix_permissions /var/log/zigbee2mqtt "${username:-openhabian}:${username:-openhabian}" 644 755; then echo "FAILED (zigbee2mqtt log permissions)"; retval=1; fi
if ! cond_redirect fix_permissions /var/log/zigbee2mqtt "${username:-openhabian}:openhab" 644 755; then echo "FAILED (zigbee2mqtt log permissions)"; retval=1; fi
fi
if zram_is_installed; then
if influxdb_is_installed; then
Expand Down
2 changes: 1 addition & 1 deletion includes/zigbee2mqtt/zigbee2mqtt.service
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ StandardOutput=inherit
# Or use StandardOutput=null if you don't want Zigbee2MQTT messages filling syslog, for more options see systemd.exec(5)
StandardError=inherit
Restart=always
User=openhabian
User=%user%

[Install]
WantedBy=multi-user.target

0 comments on commit 4e8f731

Please sign in to comment.