setup_env 4.3 KB

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