Browse Source

Fix bug with missing config

Joachim M. Giæver 3 năm trước cách đây
mục cha
commit
5ec76c6843
7 tập tin đã thay đổi với 48 bổ sung31 xóa
  1. 1 3
      snap/snapcraft.yaml
  2. 0 1
      src/bin/restart
  3. 8 9
      src/helper/env-wrapper
  4. 31 9
      src/helper/functions
  5. 4 3
      src/hooks/configure
  6. 4 4
      src/hooks/install
  7. 0 2
      src/hooks/post-refresh

+ 1 - 3
snap/snapcraft.yaml

@@ -59,10 +59,7 @@ apps:
     command: bin/restart
 
 layout:
-  /usr/src/app/store:
-    bind: $SNAP_DATA/zwavejs2mqtt/store
   $SNAP/usr/lib/zwavejs2mqtt/node_modules/@zwave-js/config/config:
-    # bind-file: $SNAP_DATA/config/devices/index.json
     bind: $SNAP/usr/lib/zwavejs2mqtt/node_modules/@zwave-js/config/config
 
 parts:
@@ -80,6 +77,7 @@ parts:
     stage-packages:
       - git
       - udev
+      - rsync
     override-build: |
       set -ex
       npm config set unsafe-perm true

+ 0 - 1
src/bin/restart

@@ -1,5 +1,4 @@
 #!/usr/bin/env bash
-set -e
 
 source $SNAP/helper/functions
 

+ 8 - 9
src/helper/env-wrapper

@@ -1,9 +1,8 @@
 #!/usr/bin/env bash
 
-set -e
 source $SNAP/helper/functions
 
-if [ -z ${DAEMONIZED} ]; then
+if [ -z "${DAEMONIZED}" ]; then
     DAEMONIZED=0
 fi
 
@@ -17,12 +16,10 @@ function lprint {
 
 export NETWORK_KEY=$(snapctl get network.key)
 
-if [ -f "${SNAP_DATA}/settings.json" ]; then
-    SETTINGS_KEY=$(jq '.zwave.networkKey' -r ${SNAP_DATA}/settings.json)
-    if [ "${SETTINGS_KEY}" != "" ] && [ "${NETWORK_KEY}" != "${SETTINGS_KEY}" ]; then
-        snapctl set network.key="${SETTINGS_KEY}"
-        export NETWORK_KEY="${SETTINGS_KEY}"
-    fi
+SETTINGS_KEY=$(same_network_key "${NETWORK_KEY}")
+if [ $? -ne 0 ]; then
+    snapctl set network.key="${SETTINGS_KEY}"
+    export NETWORK_KEY="${SETTINGS_KEY}"
 fi
 
 OPT_HELP=false
@@ -38,7 +35,7 @@ if [ ${OPT_HELP} = true ]; then
         echo "NOTE! NOTE! NOTE!"
         echo ""
     fi
-    echo "Zwavejs2mqtt ($ ($ 1 --version))"
+    echo "${SNAP_NAME} ($ ($ 1 --version))"
     echo ""
     echo "IMPORTANT! The DAEMON/SERVICE is disabled by default after installation." 
     echo "You have to manually 'daemonize' it with executing the command"
@@ -76,6 +73,8 @@ if [ $? -ne 0 ]; then
     exit 1
 fi
 
+ensure_zwavejs_config
+
 export SERVER_SSL=$(snapctl get server.ssl)
 export SERVER_HOST=$(snapctl get server.host)
 export SERVER_PORT=$(snapctl get server.port)

+ 31 - 9
src/helper/functions

@@ -2,30 +2,52 @@
 
 function ensure_zwavejs_config {
     ZWAVE_JS_CONF="${SNAP}/usr/lib/zwavejs2mqtt/node_modules/@zwave-js/config/config"
-
     if [ "$(find ${ZWAVE_JS_CONF} -maxdepth 0 -empty -exec echo empty \;)" == "empty" ]; then
-        logger "Config directory is empty, copying data"
-        cp -rf $SNAP/snap/zwave-js/config/* "${ZWAVE_JS_CONF}" 
+        logger "Config directory is empty, copying data from ${SNAP}/snap/zwave-js/config"
+        rsync -raz "${SNAP}/snap/zwave-js/config" "${ZWAVE_JS_CONF}/.."
+    fi
+    return 0
+}
+
+function same_network_key {
+    if [ -f "${SNAP_DATA}/settings.json" ]; then
+        SETTINGS_KEY=$(jq '.zwave.networkKey' -r ${SNAP_DATA}/settings.json)
+        if [ "${SETTINGS_KEY}" != "" ] && [ "${1}" != "${SETTINGS_KEY}" ]; then
+            echo "${SETTINGS_KEY}"
+            return 1
+        fi
+        return 0
+    fi
+    return 0
+}
+
+function plug_connected {
+    if ! snapctl is-connected "${1}"; then
+        echo "Missing plug: «${1}»" >&2
+        echo "Install with:"
+        echo "$ sudo snap connect ${SNAP_NAME}:${1}"
+        return 1
     fi
+    return 0
 }
 
 function plugs_connected {
     MISSING=0
-    if ! snapctl is-connected raw-usb; then
-        echo "Missing plug «raw-usb" >&2
+
+    plug_connected "raw-usb"
+    if [ $? -ne 0 ]; then
         MISSING=1
     fi
 
-    if ! snapctl is-connected hardware-observe; then
-        echo "Missing plug «hardware-observe»" >&2
+    plug_connected "hardware-observe"
+    if [ $? -ne 0 ]; then
         MISSING=1
     fi
 
     if [ $MISSING -ne 0 ]; then
-        echo "Plug with:" >&2
-        echo "$ sudo snap connect ${SNAP_NAME}:PLUG-NAME" >&2
         return 1
     fi
+
     return 0
 }
 

+ 4 - 3
src/hooks/configure

@@ -1,18 +1,18 @@
 #!/usr/bin/env bash
 
 set -e
-# silence is golden.
+source $SNAP/helper/functions
 
 SERVER_SSL=$(snapctl get server.ssl)
 SERVER_PORT=$(snapctl get server.port)
 
 if [ $(echo "${SERVER_PORT}" | grep -E "^\-?[0-9]+$") = "" ]; then
-    echo "Port must be numeric, got ${SERVER_PORT}" >&2
+    echo "!! server.port must be numeric, got ${SERVER_PORT}" >&2
     exit 1
 fi
 
 if [ ${SERVER_SSL} != true -a ${SERVER_SSL} != false ]; then
-    echo "server.ssl must be boolean value" >&2
+    echo "!! server.ssl must be boolean value" >&2
     exit 1
 fi
 
@@ -31,3 +31,4 @@ if [ ${RUNNING} -eq 0 ]; then
     echo "Restarting ${SNAP_NAME}"
     snapctl restart "${SNAP_NAME}"
 fi
+

+ 4 - 4
src/hooks/install

@@ -1,13 +1,13 @@
 #!/usr/bin/env bash
 
-set -e
-source $SNAP/helper/functions
+#source $SNAP/helper/functions
+
+set -e 
 
 snapctl set network.key="$(cat /dev/urandom | LC_ALL=C tr -dc '0-9A-F' | fold -w 32 | head -n 1)"
+
 snapctl set server.ssl=false
 snapctl set server.host="localhost"
 snapctl set server.port="8091"
 
 snapctl stop --disable "${SNAP_NAME}.${SNAP_NAME}"
-
-ensure_zwavejs_config

+ 0 - 2
src/hooks/post-refresh

@@ -1,7 +1,5 @@
 #!/usr/bin/env bash
 
-set -e
-
 source $SNAP/helper/functions
 
 ensure_zwavejs_config