env-wrapper 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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" -o "${OPT}" = '-h' ]; 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. echo ""
  42. for ARG in "${ARGV[@]}"; do
  43. shift
  44. if [ "${ARG}" = "--exec" ]; then
  45. lprint "Service already running, showing help text."
  46. lprint "Use: 'snap stop ${SNAP_NAME}' to stop the service and'"
  47. lprint "to execute the ozwdaemon manually"
  48. OPT_HELP=true
  49. continue
  50. fi
  51. set -- "$@" "${ARG}"
  52. done
  53. fi
  54. if [ ${OPT_HELP} = false ]; then
  55. if [ ! -c "${USB_PATH}" ]; then
  56. lprint "usb-path (${USB_PATH}) does not exist, or is not a Character Device."
  57. lprint "See: snap get ${SNAP_NAME} -d usb-path"
  58. exit 1
  59. fi
  60. OZW_ARGS+=(--serial-port "${USB_PATH}")
  61. if [ ${STOP_ON_FAILURE} == true ]; then
  62. OZW_ARGS+=(--stop-on-failure)
  63. elif [ ${STOP_ON_FAILURE} != false ]; then
  64. lprint "Invalid value for stop-on-failure (${STOP_ON_FAILURE})"
  65. lprint "See: snap get ${SNAP_NAME} -d stop-on-failure"
  66. exit 1
  67. fi
  68. OZW_ARGS+=(--mqtt-server "${MQTT_SERVER}")
  69. OZW_ARGS+=(--mqtt-port "${MQTT_PORT}")
  70. if [ ${MQTT_USERNAME} != false ]; then
  71. OZW_ARGS+=(--mqtt-username "${MQTT_USERNAME}")
  72. fi
  73. if [ ${MQTT_TLS} == true ]; then
  74. OZW_ARGS+=(--mqtt-tls)
  75. elif [ ${MQTT_TLS} != false ]; then
  76. lprint "Invalid value for mqtt.tls (${MQTT_TLS})"
  77. lprint "See: snap get ${SNAP_NAME} -d mqtt"
  78. exit 1
  79. fi
  80. OZW_ARGS+=(--mqtt-instance "${OZW_INSTANCE}")
  81. if [ ! -d "${OZW_CONFIG_DIR}" ] || [ ! -w "${OZW_CONFIG_DIR}" ]; then
  82. lprint "Directory: ${OZW_CONFIG_DIR}"
  83. lprint "- Does not exist or not writable."
  84. exit 125
  85. fi
  86. OZW_ARGS+=(--config-dir "${OZW_CONFIG_DIR}")
  87. if [ ! -d "${OZW_USER_DIR}" ] || [ ! -w "${OZW_USER_DIR}" ]; then
  88. lprint "Directory: ${OZW_USER_DIR}"
  89. lprint "- Does not exist or not writable."
  90. exit 126
  91. fi
  92. OZW_ARGS+=(--user-dir "${OZW_USER_DIR}")
  93. lprint "Waiting ${MQTT_CONNECT_TIMEOUT:=30} seconds for MQTT connection"
  94. if ! timeout "${MQTT_CONNECT_TIMEOUT:=30}" \
  95. bash -c "until "${SNAP}/bin/nc.openbsd" -zv "${MQTT_SERVER}" "${MQTT_PORT}"; do sleep 1; done" &>/dev/null; then
  96. lprint "Could not connect to MQTT on ${MQTT_SERVER}:${MQTT_PORT} after 30 seconds";
  97. exit 1;
  98. fi
  99. set -- "$@" "${OZW_ARGS[@]}"
  100. else
  101. echo "QT OpenZWave Remote Daemon ($($1 --version))"
  102. echo ""
  103. echo "IMPORTANT! The DAEMON/SERVICE is disabled by default after installation."
  104. echo "You have to manually 'daemonize' OpenZWave Daemon with executing the command"
  105. echo ""
  106. echo "$ ozwdaemon.enable"
  107. echo ""
  108. echo "But first set your configuration as descrived below and run ${SNAP_NAME}.exec"
  109. echo "until ${SNAP_NAME} boots and runs successfully. "
  110. echo ""
  111. echo "Base configuration values:"
  112. echo "- usb-path: $(snapctl get "usb-path")"
  113. echo "- stop-on-failure: $(snapctl get "stop-on-failure")"
  114. echo ""
  115. echo "MQTT-configuration values: $(snapctl get "mqtt")"
  116. echo ""
  117. echo "- mqtt.connect-timeout: Seconds to wait for MQTT to be up"
  118. echo "- mqtt.server: MQTT server hostname or IP"
  119. echo "- mqtt.port: MQTT server port"
  120. echo "- mqtt.tls: Enable TLF encryption to MQTT"
  121. echo "- mqtt.username: MQTT login username"
  122. echo "- mqtt.password: MQTT login password"
  123. echo ""
  124. echo "OpenZWave-configuration values: $(snapctl get "ozw")"
  125. echo ""
  126. echo "- ozw.auth-key: Remote management (ozw-admin) authorization key"
  127. echo "- ozw.config-dir: Directory containing the OZW Config Files"
  128. echo "- ozw.user-dir: Directory for the OZW User Files"
  129. echo "- ozw.instance: OpenZWave Instance Number"
  130. echo "- ozw.network-key: The Network Key to secure communications with your devices (that are included Securely)."
  131. echo " DO NOT LOSE THIS KEY OTHERWISE YOU WILL HAVE TO REINCLUDE YOUR SECURED DEVICES."
  132. echo " The default key is auto-generated on installation, and is unique."
  133. echo ""
  134. echo "Set options with: $ snap set ${SNAP_NAME} param=key"
  135. echo "For example: $ snap set ${SNAP_NAME} mqtt.server=localhost"
  136. echo ""
  137. echo "Also see 'snap info ${SNAP_NAME}' for information about secure devices."
  138. echo ""
  139. echo "Follow the log with"
  140. echo "$ snap logs ozwdaemon"
  141. echo ""
  142. echo "Other commands"
  143. echo "Disable the daemon: $ ozwdaemon.disable"
  144. echo "Restart the daemon: $ ozwdaemon.restart"
  145. exit 0
  146. fi
  147. exec "$@"