env-wrapper 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env bash
  2. set -e
  3. OZW_LIB=$(dirname $(find "${SNAP}/usr/local" -name libopenzwave.so -print -quit))
  4. export LD_LIBRARY_PATH="${OZW_LIB}:${LD_LIBRARY_PATH}"
  5. function lprint {
  6. echo ${1}
  7. logger "${SNAP_NAME}: ${1}"
  8. }
  9. if ! snapctl is-connected raw-usb; then
  10. lprint "Please connect raw-usb interface!"
  11. lprint "Run: snap connect ${SNAP_NAME}:raw-usb"
  12. exit 1
  13. fi
  14. USB_PATH=$(snapctl get usb-path)
  15. STOP_ON_FAILURE=$(snapctl get stop-on-failure)
  16. MQTT_SERVER=$(snapctl get mqtt.server)
  17. MQTT_PORT=$(snapctl get mqtt.port)
  18. MQTT_USERNAME=$(snapctl get mqtt.username)
  19. MQTT_PASSWORD=$(snapctl get mqtt.password)
  20. if [ ${MQTT_PASSWORD:=false} != false ]; then
  21. export MQTT_PASSWORD;
  22. fi
  23. MQTT_TLS=$(snapctl get mqtt.tls)
  24. OZW_INSTANCE=$(snapctl get ozw.instance)
  25. export OZW_NETWORK_KEY=$(snapctl get ozw.network-key)
  26. OZW_CONFIG_DIR=$(snapctl get ozw.config-dir)
  27. OZW_USER_DIR=$(snapctl get ozw.user-dir)
  28. OZW_AUTH_KEY=$(snapctl get ozw.auth-key)
  29. if [ ${OZW_AUTH_KEY:=false} != false ]; then
  30. export OZW_AUTH_KEY
  31. fi
  32. OPT_HELP=false
  33. ARGV=( "$@" )
  34. for ((i = 0; i < $#; i++)); do
  35. OPT="${ARGV[$i]}"
  36. if [ "${OPT}" = "--help" ]; then
  37. OPT_HELP=true
  38. fi
  39. done
  40. if [ $(snapctl services ozwdaemon | grep inactive | wc -l) -eq 0 -a ${OPT_HELP} = false ]; then
  41. lprint "Service already running, showing help text."
  42. lprint "Use: 'snap stop ${SNAP_NAME}' to stop the service and'"
  43. lprint "to execute the ozwdaemon manually"
  44. echo ""
  45. OPT_HELP=true
  46. fi
  47. if [ ${OPT_HELP} = false ]; then
  48. if [ ! -c "${USB_PATH}" ]; then
  49. lprint "usb-path (${USB_PATH}) does not exist,"
  50. lprint "or is not a Character Device"
  51. lprint "See: snap get ${SNAP_NAME} -d usb-path"
  52. exit 1
  53. fi
  54. OZW_ARGS+=(--serial-port "${USB_PATH}")
  55. if [ ${STOP_ON_FAILURE} == true ]; then
  56. OZW_ARGS+=(--stop-on-failure)
  57. elif [ ${STOP_ON_FAILURE} != false ]; then
  58. lprint "Invalid value for stop-on-failure (${STOP_ON_FAILURE})"
  59. lprint "See: snap get ${SNAP_NAME} -d stop-on-failure"
  60. exit 1
  61. fi
  62. OZW_ARGS+=(--mqtt-server "${MQTT_SERVER}")
  63. OZW_ARGS+=(--mqtt-port "${MQTT_PORT}")
  64. if [ ${MQTT_USERNAME} != false ]; then
  65. OZW_ARGS+=(--mqtt-username "${MQTT_USERNAME}")
  66. fi
  67. if [ ${MQTT_TLS} == true ]; then
  68. OZW_ARGS+=(--mqtt-tls)
  69. elif [ ${MQTT_TLS} != false ]; then
  70. lprint "Invalid value for mqtt.tls (${MQTT_TLS})"
  71. lprint "See: snap get ${SNAP_NAME} -d mqtt"
  72. exit 1
  73. fi
  74. OZW_ARGS+=(--mqtt-instance "${OZW_INSTANCE}")
  75. if [ ! -d "${OZW_CONFIG_DIR}" ] || [ ! -w "${OZW_CONFIG_DIR}" ]; then
  76. lprint "Directory: ${OZW_CONFIG_DIR}"
  77. lprint "- Does not exist or not writable."
  78. exit 125
  79. fi
  80. OZW_ARGS+=(--config-dir "${OZW_CONFIG_DIR}")
  81. if [ ! -d "${OZW_USER_DIR}" ] || [ ! -w "${OZW_USER_DIR}" ]; then
  82. lprint "Directory: ${OZW_USER_DIR}"
  83. lprint "- Does not exist or not writable."
  84. exit 126
  85. fi
  86. OZW_ARGS+=(--user-dir "${OZW_USER_DIR}")
  87. lprint "Waiting ${MQTT_CONNECT_TIMEOUT:=30} seconds for MQTT connection"
  88. if ! timeout "${MQTT_CONNECT_TIMEOUT:=30}" \
  89. bash -c "until "${SNAP}/bin/nc.openbsd" -zv "${MQTT_SERVER}" "${MQTT_PORT}"; do sleep 1; done" &>/dev/null; then
  90. lprint "Could not connect to MQTT on ${MQTT_SERVER}:${MQTT_PORT} after 30 seconds";
  91. exit 1;
  92. fi
  93. lprint "MQTT seems to be up on ${MQTT_SERVER}:${MQTT_PORT}"
  94. set -- "$@" "${OZW_ARGS[@]}"
  95. else
  96. echo "QT OpenZWave Remote Daemon ($($SNAP/usr/local/bin/ozwdaemon --version))"
  97. echo ""
  98. echo "- usb-path: $(snapctl get "usb-path")"
  99. echo "- stop-on-failure: $(snapctl get "stop-on-failure")"
  100. echo ""
  101. echo "MQTT-configuration values: $(snapctl get "mqtt")"
  102. echo ""
  103. echo "- mqtt.connect-timeout: Seconds to wait for MQTT to be up"
  104. echo "- mqtt.server: MQTT server hostname or IP"
  105. echo "- mqtt.port: MQTT server port"
  106. echo "- mqtt.tls: Enable TLF encryption to MQTT"
  107. echo "- mqtt.username: MQTT login username"
  108. echo "- mqtt.password: MQTT login password"
  109. echo ""
  110. echo "OpenZWave-configuration values: $(snapctl get "ozw")"
  111. echo ""
  112. echo "- ozw.auth-key: Remote management (ozw-admin) authorization key"
  113. echo "- ozw.config-dir: Directory containing the OZW Config Files"
  114. echo "- ozw.user-dir: Directory for the OZW User Files"
  115. echo "- ozw.instance: OpenZWave Instance Number"
  116. echo "- ozw.network-key: The Network Key to secure communications with your devices (that are included Securely)."
  117. echo " DO NOT LOSE THIS KEY OTHERWISE YOU WILL HAVE TO REINCLUDE YOUR SECURED DEVICES."
  118. echo " The default key is auto-generated on installation, and is unique."
  119. echo ""
  120. echo "Set options with: snap set ${SNAP_NAME} param=key"
  121. echo "For example: snap set ${SNAP_NAME} mqtt.server=localhost"
  122. echo ""
  123. echo "Also see 'snap info ${SNAP_NAME}' for information about secure devices."
  124. echo ""
  125. echo "Follow log with"
  126. echo "'sudo journalctl -xef --identifier=${SNAP_NAME}.${SNAP_NAME}'"
  127. exit 0
  128. fi
  129. lprint "Exec: $@"
  130. exec "$@"