functions 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "Install 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. plug_connected "raw-usb"
  33. if [ $? -ne 0 ]; then
  34. MISSING=1
  35. fi
  36. plug_connected "hardware-observe"
  37. if [ $? -ne 0 ]; then
  38. MISSING=1
  39. fi
  40. if [ $MISSING -ne 0 ]; then
  41. return 1
  42. fi
  43. return 0
  44. }
  45. function is_root {
  46. if [ ${EUID:-$(id -u)} -eq 0 ]; then
  47. return 0
  48. fi
  49. return 1
  50. }
  51. function require_root {
  52. is_root
  53. if [ $? -eq 1 ]; then
  54. echo "Running as root is required." >&2
  55. echo "Re-run with sudo."
  56. exit 1
  57. fi
  58. }
  59. function testnset_config {
  60. logger "${SNAP_NAME}: Testing ${1}, or setting ${2}"
  61. RES=$(snapctl get ${1})
  62. if [ $? -ne 0 ] || [ -z "${RES}" ]; then
  63. logger "${SNAP_NAME}: Setting ${1}=${2}"
  64. RES=$(snapctl set ${1}=${2})
  65. if [ $? -ne 0 ]; then
  66. logger "${RES}"
  67. exit 1
  68. fi
  69. return 0
  70. else
  71. logger "${SNAP_NAME}: ${1} has ${RES}, leaving as is"
  72. fi
  73. return 0
  74. }
  75. function test_default_config {
  76. testnset_config "server.ssl" false
  77. testnset_config "server.host" "localhost"
  78. testnset_config "server.port" 8091
  79. testnset_config "session.cookie-secure" false
  80. testnset_config "session.secret" $(uuid)
  81. }