run.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env bash
  2. ## This script runs CT (and necessary dependencies) in docker container(s)
  3. set -euo pipefail
  4. # ensure dir
  5. cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")/../.."
  6. help() {
  7. echo
  8. echo "-h|--help: To display this usage info"
  9. echo "--app lib_dir/app_name: Print apps in json"
  10. echo "--console: Start EMQX in console mode"
  11. }
  12. WHICH_APP='novalue'
  13. CONSOLE='no'
  14. while [ "$#" -gt 0 ]; do
  15. case $1 in
  16. -h|--help)
  17. help
  18. exit 0
  19. ;;
  20. --app)
  21. WHICH_APP="$2"
  22. shift 2
  23. ;;
  24. --console)
  25. CONSOLE='yes'
  26. shift 1
  27. ;;
  28. *)
  29. echo "unknown option $1"
  30. exit 1
  31. ;;
  32. esac
  33. done
  34. if [ "${WHICH_APP}" = 'novalue' ]; then
  35. echo "must provide --app arg"
  36. exit 1
  37. fi
  38. ERLANG_CONTAINER='erlang24'
  39. DOCKER_CT_ENVS_FILE="${WHICH_APP}/docker-ct"
  40. if [ -f "$DOCKER_CT_ENVS_FILE" ]; then
  41. # shellcheck disable=SC2002
  42. CT_DEPS="$(cat "$DOCKER_CT_ENVS_FILE" | xargs)"
  43. fi
  44. CT_DEPS="${ERLANG_CONTAINER} ${CT_DEPS}"
  45. FILES=( )
  46. for dep in ${CT_DEPS}; do
  47. case "${dep}" in
  48. erlang24)
  49. FILES+=( '.ci/docker-compose-file/docker-compose.yaml' )
  50. ;;
  51. mongo)
  52. FILES+=( '.ci/docker-compose-file/docker-compose-mongo-single-tcp.yaml'
  53. '.ci/docker-compose-file/docker-compose-mongo-single-tls.yaml' )
  54. ;;
  55. redis)
  56. FILES+=( '.ci/docker-compose-file/docker-compose-redis-single-tcp.yaml'
  57. '.ci/docker-compose-file/docker-compose-redis-single-tls.yaml'
  58. '.ci/docker-compose-file/docker-compose-redis-sentinel-tcp.yaml'
  59. '.ci/docker-compose-file/docker-compose-redis-sentinel-tls.yaml' )
  60. ;;
  61. mysql)
  62. FILES+=( '.ci/docker-compose-file/docker-compose-mysql-tcp.yaml'
  63. '.ci/docker-compose-file/docker-compose-mysql-tls.yaml' )
  64. ;;
  65. pgsql)
  66. FILES+=( '.ci/docker-compose-file/docker-compose-pgsql-tcp.yaml'
  67. '.ci/docker-compose-file/docker-compose-pgsql-tls.yaml' )
  68. ;;
  69. *)
  70. echo "unknown_ct_dependency $dep"
  71. exit 1
  72. ;;
  73. esac
  74. done
  75. F_OPTIONS=""
  76. for file in "${FILES[@]}"; do
  77. F_OPTIONS="$F_OPTIONS -f $file"
  78. done
  79. # Passing $UID to docker-compose to be used in erlang container
  80. # as owner of the main process to avoid git repo permissions issue.
  81. # Permissions issue happens because we are mounting local filesystem
  82. # where files are owned by $UID to docker container where it's using
  83. # root (UID=0) by default, and git is not happy about it.
  84. # shellcheck disable=2086 # no quotes for F_OPTIONS
  85. UID_GID="$UID:$UID" docker-compose $F_OPTIONS up -d --build
  86. # /emqx is where the source dir is mounted to the Erlang container
  87. # in .ci/docker-compose-file/docker-compose.yaml
  88. TTY=''
  89. if [[ -t 1 ]]; then
  90. TTY='-t'
  91. fi
  92. # rebar and hex cache directory need to be writable by $UID
  93. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "mkdir /.cache && chown $UID:$UID /.cache"
  94. # need to initialize .erlang.cookie manually here because / is not writable by $UID
  95. docker exec -i $TTY -u root:root "$ERLANG_CONTAINER" bash -c "openssl rand -base64 16 > /.erlang.cookie && chown $UID:$UID /.erlang.cookie && chmod 0400 /.erlang.cookie"
  96. if [ "$CONSOLE" = 'yes' ]; then
  97. docker exec -i $TTY "$ERLANG_CONTAINER" bash -c "make run"
  98. else
  99. set +e
  100. docker exec -i $TTY "$ERLANG_CONTAINER" bash -c "make ${WHICH_APP}-ct"
  101. RESULT=$?
  102. # shellcheck disable=2086 # no quotes for F_OPTIONS
  103. UID_GID="$UID:$UID" docker-compose $F_OPTIONS down
  104. exit $RESULT
  105. fi