Browse Source

version bump and add new env.vars + tests

Joachim M. Giæver 3 years ago
parent
commit
b095a8ce72
6 changed files with 43 additions and 10 deletions
  1. 3 1
      snap/snapcraft.yaml
  2. 7 0
      src/helper/env-wrapper
  3. 26 0
      src/helper/functions
  4. 2 1
      src/hooks/configure
  5. 3 8
      src/hooks/install
  6. 2 0
      src/hooks/post-refresh

+ 3 - 1
snap/snapcraft.yaml

@@ -1,6 +1,6 @@
 name: zwavejs2mqtt
 base: core20
-version: 'v1.4.0'
+version: 'v2.0.0'
 summary: Fully configurable Zwave to MQTT Gateway and Control Panel. 
 description: |
   - Configurable Zwave to Mqtt Gateway
@@ -109,5 +109,7 @@ parts:
   local:
     plugin: dump
     source: src/
+    stage-packages:
+      - uuid
     organize:
       hooks: snap/hooks

+ 7 - 0
src/helper/env-wrapper

@@ -83,11 +83,18 @@ export GIT_DIR="${SNAP}/usr/lib/zwavejs2mqtt/.git"
 
 export HTTPS="${SERVER_SSL}"
 
+export SESSION_SECRET=$(snapctl get session.secret)
+export USE_SECURE_COOKIE=$(snapctl get session.cookie-secure)
+
 if [ "${HTTPS}" != true ]; then
     unset HTTPS
     unset SERVER_SSL
 fi
 
+if [ "${USE_SECURE_COOKIE}" != true ]; then
+    unset USE_SECURE_COOKIE
+fi
+
 export HOST="${SERVER_HOST}"
 export PORT="${SERVER_PORT}"
 

+ 26 - 0
src/helper/functions

@@ -67,3 +67,29 @@ function require_root {
         exit 1
     fi
 }
+
+function testnset_config {
+    logger "${SNAP_NAME}: Testing ${1}, or setting ${2}"
+    RES=$(snapctl get ${1})
+    if [ $? -ne 0 ] || [ -z "${RES}" ]; then
+        logger "${SNAP_NAME}: Setting ${1}=${2}"
+        RES=$(snapctl set ${1}=${2})
+        if [ $? -ne 0 ]; then
+            logger "${RES}"
+            exit 1
+        fi
+        return 0
+    else
+        logger "${SNAP_NAME}: ${1} has ${RES}, leaving as is"
+    fi
+    return 0
+}
+
+function test_default_config {
+    testnset_config "server.ssl" false
+    testnset_config "server.host" "localhost"
+    testnset_config "server.port" 8091
+
+    testnset_config "session.cookie-secure" false
+    testnset_config "session.secret" $(uuid)
+}

+ 2 - 1
src/hooks/configure

@@ -26,9 +26,10 @@ jq --arg networkKey "${NETWORK_KEY}" '.zwave.networkKey = $networkKey' $SNAP_DAT
 cat ${SNAP_DATA}/tmp.json > ${SNAP_DATA}/settings.json
 rm ${SNAP_DATA}/tmp.json
 
+test_default_config
+
 RUNNING=$(snapctl services | grep "${SNAP_NAME}" | grep inactive | wc -l)
 if [ ${RUNNING} -eq 0 ]; then
     echo "Restarting ${SNAP_NAME}"
     snapctl restart "${SNAP_NAME}"
 fi
-

+ 3 - 8
src/hooks/install

@@ -4,18 +4,13 @@ source $SNAP/helper/functions
 
 set -e 
 
-logger "SNAP_DATA: $(ls -al ${SNAP_DATA})"
-
 SETTINGS_KEY=$(same_network_key "")
 
 if [ $? -eq 0 ]; then
-    snapctl set network.key="$(cat /dev/urandom | LC_ALL=C tr -dc '0-9A-F' | fold -w 32 | head -n 1)"
-else
-    snapctl set network.key="${SETTINGS_KEY}"
+    SETTINGS_KEY=$(cat /dev/urandom | LC_ALL=C tr -dc '0-9A-F' | fold -w 32 | head -n 1)
 fi
 
-snapctl set server.ssl=false
-snapctl set server.host="localhost"
-snapctl set server.port="8091"
+testnset_config "network.key" "${SETTINGS_KEY}"
+test_default_config
 
 snapctl stop --disable "${SNAP_NAME}.${SNAP_NAME}"

+ 2 - 0
src/hooks/post-refresh

@@ -3,3 +3,5 @@
 source $SNAP/helper/functions
 
 ensure_zwavejs_config
+
+test_default_config