The main Bubble source repository. Contains the Bubble API server, the web UI, documentation and utilities. https://getbubblenow.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

205 regels
6.8 KiB

  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2020 Bubble, Inc. All rights reserved. For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
  4. #
  5. # Run Bubble server or CLI commands. A wrapper for starting a JVM to run Bubble programs.
  6. #
  7. # Usage: run.sh [debug [debug-port]] [command] [args]
  8. #
  9. # All arguments are optional:
  10. #
  11. # debug : if the first argument is the literal string 'debug' then immediately after starting,
  12. # the Java process will wait for a debugger to attach. Default is not to enable debugging.
  13. # debug-port : the port that will be listened on for the debugger. Default port is 5005
  14. # command : the CLI command to run, or 'server' to run BUBBLE API server. Default is to run Bubble API server
  15. # args : depends on the command. Use '-h' to request help for a command
  16. #
  17. # Environment variables
  18. #
  19. # BUBBLE_LISTEN_ALL : if set to true and running a Bubble server, listen on all addresses (bind to 0.0.0.0)
  20. # if not set, server will only listen on 127.0.0.1
  21. # BUBBLE_ENV : env file to load, used when performing handlebars substitutions on entities marked
  22. # with `"_subst": true` JSON attribute. Default is ~/.bubble.env
  23. # BUBBLE_JVM_OPTS : Java options. Defaults to either "-Xmx512m -Xms512m" when no command is set, else "-Xmx64m -Xms2m"
  24. # BUBBLE_JAR : location of bubble uberjar. Default is to assume there is exactly one bubble-server*.jar file in a
  25. # directory named "target" that is in the same directory as this script
  26. #
  27. # Environment variables for API commands
  28. #
  29. # BUBBLE_API : which API to use. Default is local (http://127.0.0.1:PORT, where PORT is found in .bubble.env)
  30. # BUBBLE_USER : account to use. Default is root@local.local
  31. # BUBBLE_PASS : password for account. Default is password
  32. #
  33. #
  34. SCRIPT="${0}"
  35. SCRIPT_DIR="$(cd "$(dirname "${SCRIPT}")" && pwd)"
  36. . "${SCRIPT_DIR}"/bubble_common
  37. # fail on any command error
  38. set -e
  39. function print_auth_env_info () {
  40. if [[ -z "${BUBBLE_QUIET_AUTH_CACHE}" || "${BUBBLE_QUIET_AUTH_CACHE}" != "true" ]] ; then
  41. echo 1>&2 "Using default user ${BUBBLE_USER} from ${DEFAULT_USER_LINK}
  42. - Set env var BUBBLE_DISABLE_AUTH_CACHE=true to disable this behavior
  43. - Set env var BUBBLE_QUIET_AUTH_CACHE=true to hide this warning
  44. "
  45. fi
  46. }
  47. function read_default_user () {
  48. if [[ -z "${BUBBLE_DISABLE_AUTH_CACHE}" || "${BUBBLE_DISABLE_AUTH_CACHE}" == "false" ]] ; then
  49. BUBBLE_AUTH="${HOME}/.bubble_auth"
  50. if [[ -n "${BUBBLE_API}" && -d "${BUBBLE_AUTH}" ]] ; then
  51. API_HOST="$(echo -n "${BUBBLE_API}" | awk -F '/' '{print $3}')"
  52. AUTH_DIR="${BUBBLE_AUTH}/${API_HOST}"
  53. DEFAULT_USER_LINK="${AUTH_DIR}/default"
  54. if [[ -L "${DEFAULT_USER_LINK}" ]] ; then
  55. print_auth_env_info
  56. basename "$(readlink "${DEFAULT_USER_LINK}")"
  57. fi
  58. fi
  59. fi
  60. }
  61. function read_cached_pass () {
  62. if [[ -z "${BUBBLE_DISABLE_AUTH_CACHE}" || "${BUBBLE_DISABLE_AUTH_CACHE}" == "false" ]] ; then
  63. BUBBLE_AUTH="${HOME}/.bubble_auth"
  64. if [[ -n "${BUBBLE_API}" && -d "${BUBBLE_AUTH}" ]] ; then
  65. API_HOST="$(echo -n "${BUBBLE_API}" | awk -F '/' '{print $3}')"
  66. AUTH_DIR="${BUBBLE_AUTH}/${API_HOST}"
  67. PASS_FILE="${AUTH_DIR}/${BUBBLE_USER}"
  68. if [[ -n "${BUBBLE_USER}" && -f "${PASS_FILE}" ]] ; then
  69. print_auth_env_info
  70. cat "${PASS_FILE}" | awk '{$1=$1}1'
  71. fi
  72. fi
  73. fi
  74. }
  75. BASE="$(cd "$(dirname "${0}")" && pwd)"
  76. if [[ $(basename "${BASE}") != "bubble-server" && -d "${BASE}/bubble-server" ]] ; then
  77. BASE="${BASE}/bubble-server"
  78. fi
  79. if [[ $(basename "${BASE}") == "bin" && -d "${BASE}/../bubble-server" ]] ; then
  80. BASE="$(cd "${BASE}"/../bubble-server && pwd)"
  81. fi
  82. # save explicitly set key, if we have one
  83. SAVED_DB_KEY=""
  84. if [[ -n "${BUBBLE_DB_ENCRYPTION_KEY}" ]] ; then
  85. SAVED_DB_KEY="${BUBBLE_DB_ENCRYPTION_KEY}"
  86. fi
  87. if [[ -z "${BUBBLE_ENV}" ]] ; then
  88. BUBBLE_ENV="${HOME}/.bubble.env"
  89. if [[ ! -f "${BUBBLE_ENV}" ]] ; then
  90. BUBBLE_ENV="/home/bubble/api/bubble.env"
  91. fi
  92. fi
  93. if [[ -f ${BUBBLE_ENV} ]] ; then
  94. if [[ -z "${BUBBLE_QUIET}" || ${BUBBLE_QUIET} != 1 ]] ; then
  95. echo 1>&2 "Loading env: ${BUBBLE_ENV}"
  96. fi
  97. . ${BUBBLE_ENV}
  98. fi
  99. if [[ -n "${SAVED_DB_KEY}" ]] ; then
  100. export BUBBLE_DB_ENCRYPTION_KEY="${SAVED_DB_KEY}"
  101. fi
  102. debug="${1}"
  103. if [[ "x${debug}" == "xdebug" ]] ; then
  104. shift
  105. ARG_LEN=$(echo -n "${1}" | wc -c)
  106. ARG_NUMERIC_LEN=$(echo -n "${1}" | tr -dc [:digit:] | wc -c) # strip all non-digits
  107. if [[ -n "${ARG_NUMERIC_LEN}" && ${ARG_LEN} -eq ${ARG_NUMERIC_LEN} ]] ; then
  108. # Second arg is the debug port
  109. DEBUG_PORT="${1}"
  110. shift || :
  111. fi
  112. if [[ -z "${DEBUG_PORT}" ]] ; then
  113. DEBUG_PORT=5005
  114. fi
  115. debug="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=${DEBUG_PORT}"
  116. else
  117. debug=""
  118. fi
  119. command="${1}"
  120. server=0
  121. DEFAULT_JVM_OPTS=""
  122. if [[ -z "${command}" ]] ; then
  123. server=1
  124. CLASS=bubble.server.BubbleServer
  125. DEFAULT_JVM_OPTS="-Xmx512m -Xms512m"
  126. else
  127. CLASS=bubble.main.BubbleMain
  128. DEFAULT_JVM_OPTS="-Xmx64m -Xms2m"
  129. shift
  130. fi
  131. if [[ -z "${BUBBLE_JAR}" ]] ; then
  132. die "API jar file not found"
  133. fi
  134. if [[ -z "${BUBBLE_JVM_OPTS}" ]] ; then
  135. BUBBLE_JVM_OPTS="${DEFAULT_JVM_OPTS}"
  136. fi
  137. BUBBLE_JVM_OPTS="${BUBBLE_JVM_OPTS} -Djava.net.preferIPv4Stack=true"
  138. # Choose appropriate log config
  139. if [[ ${server} -eq 1 ]] ; then
  140. LOG_CONFIG="-Dlogback.configurationFile=logback.xml"
  141. if [[ -f ${BUBBLE_ENV} ]] ; then
  142. command="${BUBBLE_ENV}"
  143. fi
  144. else
  145. LOG_CONFIG="-Dlogback.configurationFile=logback-client.xml"
  146. fi
  147. if [[ -z "${BUBBLE_ADDITIONAL_CLASSPATH}" ]] ; then
  148. BUBBLE_CP="${BUBBLE_JAR}"
  149. else
  150. BUBBLE_CP="${BUBBLE_JAR}:${BUBBLE_ADDITIONAL_CLASSPATH}"
  151. fi
  152. # Default user if none set
  153. if [[ -z "${BUBBLE_USER}" ]] ; then
  154. if [[ -n "${BUBBLE_API}" ]] ; then
  155. BUBBLE_USER=$(read_default_user)
  156. fi
  157. if [[ -z "${BUBBLE_USER}" ]] ; then
  158. if [[ -n "${REQUIRE_BUBBLE_USER}" ]] ; then
  159. die "No BUBBLE_USER env var defined"
  160. fi
  161. echo 1>&2 "*** Warning: BUBBLE_USER env var was not defined, using default user (probable authentication failure)"
  162. BUBBLE_USER=root@local.local
  163. fi
  164. fi
  165. # Default password if none set
  166. if [[ -z "${BUBBLE_PASS}" ]] ; then
  167. if [[ -n "${BUBBLE_API}" ]] ; then
  168. BUBBLE_PASS=$(read_cached_pass)
  169. fi
  170. if [[ -z "${BUBBLE_PASS}" ]] ; then
  171. if [[ -n "${REQUIRE_BUBBLE_PASS}" ]] ; then
  172. die "No BUBBLE_PASS env var defined"
  173. fi
  174. echo 1>&2 "*** Warning: BUBBLE_PASS env var was not defined, using default password (probable authentication failure)"
  175. BUBBLE_PASS=password
  176. fi
  177. fi
  178. LISTEN_ALL=""
  179. if [[ -n "${BUBBLE_LISTEN_ALL}" && "${BUBBLE_LISTEN_ALL}" == "true" ]] ; then
  180. LISTEN_ALL="-Dbubble.listenAll=true"
  181. fi
  182. # Run!
  183. BUBBLE_JAR="${BUBBLE_JAR}" java ${LOG_CONFIG} ${BUBBLE_JVM_OPTS} \
  184. -Xlog:class+load=info:/tmp/bubble_classes_$(date +%s).txt \
  185. ${debug} -server -cp "${BUBBLE_CP}" ${LISTEN_ALL} ${CLASS} ${command} "${@}"