functions 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env bash
  2. function ensure_zwavejs_config {
  3. ZWAVE_JS_CONF="${SNAP}/usr/lib/zwavejs2mqtt/node_modules/@zwave-js/config/config"
  4. if [ "$(find ${ZWAVE_JS_CONF} -maxdepth 0 -empty -exec echo empty \;)" == "empty" ]; then
  5. logger "Config directory is empty, copying data from ${SNAP}/snap/zwave-js/config"
  6. rsync -raz "${SNAP}/snap/zwave-js/config" "${ZWAVE_JS_CONF}/.."
  7. fi
  8. return 0
  9. }
  10. function same_network_key {
  11. if [ -f "${SNAP_DATA}/settings.json" ]; then
  12. SETTINGS_KEY=$(jq '.zwave.networkKey' -r ${SNAP_DATA}/settings.json)
  13. if [ "${SETTINGS_KEY}" != "" ] && [ "${1}" != "${SETTINGS_KEY}" ]; then
  14. echo "${SETTINGS_KEY}"
  15. return 1
  16. fi
  17. return 0
  18. fi
  19. return 0
  20. }
  21. function plug_connected {
  22. if ! snapctl is-connected "${1}"; then
  23. echo "Missing plug: «${1}»" >&2
  24. echo "Connect with:"
  25. echo "$ sudo snap connect ${SNAP_NAME}:${1}"
  26. return 1
  27. fi
  28. return 0
  29. }
  30. function plugs_connected {
  31. MISSING=0
  32. RAW_OUT=$(plug_connected "raw-usb")
  33. RAW_RES=$?
  34. SERIAL_OUT=$(plug_connected "serial-port")
  35. SERIAL_RES=$?
  36. if [ $RAW_RES -ne 0 -a $SERIAL_RES -ne 0 ]; then
  37. echo $RAW_OUT
  38. echo "----- OR -----"
  39. echo $SERIAL_OUT
  40. MISSING=1
  41. elif [ $RAW_RES -ne 0 ]; then
  42. echo "OPTIONAL plug:"
  43. echo $RAW_OUT
  44. elif [ $SERIAL_RES -ne 0 ]; then
  45. echo "OPTIONAL plug"
  46. echo $SERIAL_OUT
  47. fi
  48. plug_connected "hardware-observe"
  49. if [ $? -ne 0 ]; then
  50. MISSING=1
  51. fi
  52. if [ $MISSING -ne 0 ]; then
  53. return 1
  54. fi
  55. return 0
  56. }
  57. function is_root {
  58. if [ ${EUID:-$(id -u)} -eq 0 ]; then
  59. return 0
  60. fi
  61. return 1
  62. }
  63. function require_root {
  64. is_root
  65. if [ $? -eq 1 ]; then
  66. echo "Running as root is required." >&2
  67. echo "Re-run with sudo."
  68. exit 1
  69. fi
  70. }
  71. function testnset_config {
  72. logger "${SNAP_NAME}: Testing ${1}, or setting ${2}"
  73. RES=$(snapctl get ${1})
  74. if [ $? -ne 0 ] || [ -z "${RES}" ]; then
  75. logger "${SNAP_NAME}: Setting ${1}=${2}"
  76. RES=$(snapctl set ${1}=${2})
  77. if [ $? -ne 0 ]; then
  78. logger "${RES}"
  79. exit 1
  80. fi
  81. return 0
  82. else
  83. logger "${SNAP_NAME}: ${1} has ${RES}, leaving as is"
  84. fi
  85. return 0
  86. }
  87. function test_default_config {
  88. testnset_config "server.ssl" false
  89. testnset_config "server.host" "localhost"
  90. testnset_config "server.port" 8091
  91. testnset_config "session.cookie-secure" false
  92. testnset_config "session.secret" $(uuid)
  93. }