setup_env 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/bin/bash
  2. set -e
  3. export LC_ALL=en_US.UTF-8
  4. export HOME=/app
  5. cd
  6. if [ -f /app/tmp/pids/delayed_job.pid ]; then
  7. rm /app/tmp/pids/delayed_job.pid
  8. fi
  9. # DATABASE_URL takes the highest precedence.
  10. if [[ -n "$DATABASE_URL" ]]; then
  11. case "${DATABASE_URL%%://*}" in
  12. mysql)
  13. DATABASE_ADAPTER=mysql2
  14. DATABASE_URL="$DATABASE_ADAPTER${DATABASE_URL#mysql}" ;;
  15. postgres)
  16. DATABASE_ADAPTER=postgresql ;;
  17. *)
  18. DATABASE_ADAPTER="${DATABASE_URL%%://*}" ;;
  19. esac
  20. eval "$(ruby -ruri -rshellwords -e 'u = URI(ARGV[0]); {
  21. HOST: u.hostname,
  22. PORT: u.port,
  23. USERNAME: u.user,
  24. PASSWORD: u.password,
  25. NAME: u.path[%r{[^/]+}]
  26. }.each {|k,v| puts "HUGINN_DATABASE_#{k}=#{v.to_s.shellescape}" }' "$DATABASE_URL")"
  27. # is a mysql or postgresql database linked?
  28. # requires that the mysql or postgresql containers have exposed
  29. # port 3306 and 5432 respectively.
  30. elif [[ -n "${MYSQL_PORT_3306_TCP_ADDR}" || ("${DATABASE_ADAPTER}" == "mysql2" && -n "${DATABASE_HOST}") ]]; then
  31. DATABASE_ADAPTER=${DATABASE_ADAPTER:-mysql2}
  32. DATABASE_HOST=${DATABASE_HOST:-${MYSQL_PORT_3306_TCP_ADDR}}
  33. DATABASE_PORT=${DATABASE_PORT:-${MYSQL_PORT_3306_TCP_PORT:-3306}}
  34. elif [[ -n "${POSTGRES_PORT_5432_TCP_ADDR}" || ("${DATABASE_ADAPTER}" == "postgresql" && -n "${DATABASE_HOST}") ]]; then
  35. DATABASE_ADAPTER=${DATABASE_ADAPTER:-postgresql}
  36. DATABASE_HOST=${DATABASE_HOST:-${POSTGRES_PORT_5432_TCP_ADDR}}
  37. DATABASE_PORT=${DATABASE_PORT:-${POSTGRES_PORT_5432_TCP_PORT:-5432}}
  38. else
  39. START_MYSQL=${START_MYSQL:-true}
  40. DATABASE_ADAPTER=${DATABASE_ADAPTER:-mysql2}
  41. fi
  42. USE_GRAPHVIZ_DOT=${HUGINN_USE_GRAPHVIZ_DOT:-${USE_GRAPHVIZ_DOT:-dot}}
  43. DATABASE_HOST=${HUGINN_DATABASE_HOST:-${DATABASE_HOST:-localhost}}
  44. DATABASE_PORT=${HUGINN_DATABASE_PORT:-${DATABASE_PORT}}
  45. case "$DATABASE_ADAPTER" in
  46. mysql*)
  47. DATABASE_ENCODING=${HUGINN_DATABASE_ENCODING:-${DATABASE_ENCODING:-utf8mb4}} ;;
  48. *)
  49. DATABASE_ENCODING=${HUGINN_DATABASE_ENCODING:-${DATABASE_ENCODING:-utf8}} ;;
  50. esac
  51. DATABASE_USERNAME=${HUGINN_DATABASE_USERNAME:-${DATABASE_USERNAME}}
  52. DATABASE_PASSWORD=${HUGINN_DATABASE_PASSWORD:-${DATABASE_PASSWORD:-password}}
  53. DATABASE_NAME=${HUGINN_DATABASE_NAME:-${DATABASE_NAME:-huginn_production}}
  54. RAILS_ENV=${HUGINN_RAILS_ENV:-${RAILS_ENV:-production}}
  55. RAILS_LOG_TO_STDOUT=${RAILS_LOG_TO_STDOUT:-true}
  56. RAILS_SERVE_STATIC_FILES=${RAILS_SERVE_STATIC_FILES:-true}
  57. IP="${IP:-0.0.0.0}"
  58. PORT=${HUGINN_PORT:-${PORT:-3000}}
  59. PORT="$(echo $PORT|awk -F ':' '{print $NF}')"
  60. HUGINN_PORT=${PORT}
  61. cp -a /app/.env.example /tmp/
  62. # Do we have a Rails secret yet?
  63. # If not, generate one, but persist it for the container's lifetime.
  64. if [ -z "$APP_SECRET_TOKEN" ] && grep -q ^APP_SECRET_TOKEN=REPLACE_ME_NOW /tmp/.env.example; then
  65. [ -e '/tmp/secret' ] && secret="$(cat /tmp/secret)"
  66. if [ -z "$secret" ]; then
  67. echo 'Generating random APP_SECRET_TOKEN.'
  68. secret=$(dd if=/dev/urandom bs=36 count=1 | openssl base64)
  69. echo "$secret" > /tmp/secret
  70. fi
  71. sed -i "s:REPLACE_ME_NOW!:$secret:" /tmp/.env.example
  72. fi
  73. # Add ENV variables to .env.example which are not present in it but usable
  74. cat >> /tmp/.env.example <<EOF
  75. ASSET_HOST=
  76. DEFAULT_SCENARIO_FILE=
  77. IP=
  78. RAILS_ENV=
  79. RAILS_LOG_TO_STDOUT=
  80. RAILS_SERVE_STATIC_FILES=
  81. SEED_EMAIL=
  82. SEED_PASSWORD=
  83. SEED_USERNAME=
  84. SMTP_OPENSSL_CA_FILE=
  85. SMTP_OPENSSL_CA_PATH=
  86. SMTP_OPENSSL_VERIFY_MODE=
  87. START_MYSQL=
  88. EOF
  89. [ -e '/app/.env' ] && cat /app/.env >> /tmp/.env.example
  90. quote_if_necessary() {
  91. if grep -q -E -e '^".*" *$' -e "^'.*' *$" <<<"$*"; then
  92. # preserve single or double quotes if they are provided
  93. echo "$*";
  94. else
  95. # otherwise double quote the string after escaping \, $, `
  96. echo "\"$(sed -e 's/\\/\\\\/g' -e 's/\$/\\\$/g' -e 's/`/\\`/g' <<<"$*")\""
  97. fi
  98. }
  99. # Default to the environment variable values set in .env.example
  100. sed -n -r -e 's/^#?([A-Za-z0-9_]+=.*)/\1/p' /tmp/.env.example | \
  101. while IFS='=' read var value ; do
  102. huginn_var="HUGINN_$var"
  103. val="${!huginn_var:-${!var:-$value}}"
  104. echo "$var=$(quote_if_necessary $val)"
  105. done > /tmp/.env
  106. source /tmp/.env
  107. if [ "${START_MYSQL}" = "true" ]; then
  108. echo 'DATABASE_SOCKET=/app/tmp/sockets/mysqld.sock' >> /tmp/.env
  109. DATABASE_SOCKET='/app/tmp/sockets/mysqld.sock'
  110. fi
  111. [ -e '/app/.env' ] || cp -a /tmp/.env /app/
  112. chmod ugo+r /app/.env
  113. rm -f /tmp/.env.example
  114. # use default port number if it is still not set
  115. case "${DATABASE_ADAPTER}" in
  116. mysql2) DATABASE_PORT=${DATABASE_PORT:-3306} ;;
  117. postgresql) DATABASE_PORT=${DATABASE_PORT:-5432} ;;
  118. *) echo "Unsupported database adapter. Available adapters are mysql2, and postgresql." && exit 1 ;;
  119. esac
  120. bundle config set --local path vendor/bundle
  121. bundle config set --local without 'test development'
  122. bundle install
  123. # Configure the unicorn server
  124. sed -r config/unicorn.rb.example \
  125. -e "s/^ *listen .+/listen '$IP:$PORT'/" \
  126. -e 's;^ *stderr_path .+;stderr_path "/dev/stderr";' \
  127. -e 's;^ *stdout_path .+;stdout_path "/dev/stdout";' \
  128. -e '/^ *pid .+/d' > config/unicorn.rb
  129. mkdir -p tmp/pids tmp/cache tmp/sockets log