Guest User

Untitled

a guest
May 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. TRY_LOOP="20"
  4.  
  5. : "${REDIS_HOST:="REDIS-HOST"}"
  6. : "${REDIS_PORT:="6379"}"
  7. : "${REDIS_PASSWORD:=""}"
  8.  
  9. : "${POSTGRES_HOST:="POSTGRESQL-HOST"}"
  10. : "${POSTGRES_PORT:="5432"}"
  11. : "${POSTGRES_USER:="USER"}"
  12. : "${POSTGRES_PASSWORD:="********"}"
  13. : "${POSTGRES_DB:="DataLabAirflowDB"}"
  14.  
  15. # Defaults and back-compat
  16. : "${AIRFLOW__CORE__FERNET_KEY:=${FERNET_KEY:=$(python -c "from cryptography.fernet import Fernet; FERNET_KEY = Fernet.generate_key().decode(); print(FERNET_KEY)")}}"
  17. #: "${AIRFLOW__CORE__EXECUTOR:=${EXECUTOR:-Sequential}Executor}"
  18. : "${AIRFLOW__CORE__EXECUTOR:=CeleryExecutor}"
  19.  
  20. export \
  21. AIRFLOW__CELERY__BROKER_URL \
  22. AIRFLOW__CELERY__CELERY_RESULT_BACKEND \
  23. AIRFLOW__CORE__EXECUTOR \
  24. AIRFLOW__CORE__FERNET_KEY \
  25. AIRFLOW__CORE__LOAD_EXAMPLES \
  26. AIRFLOW__CORE__SQL_ALCHEMY_CONN \
  27.  
  28.  
  29. # Load DAGs exemples (default: Yes)
  30. if [[ -z "$AIRFLOW__CORE__LOAD_EXAMPLES" && "${LOAD_EX:=n}" == n ]]
  31. then
  32. AIRFLOW__CORE__LOAD_EXAMPLES=False
  33. fi
  34.  
  35. # Install custom python package if requirements.txt is present
  36. if [ -e "/requirements.txt" ]; then
  37. $(which pip) install --user -r /requirements.txt
  38. fi
  39.  
  40. if [ -n "$REDIS_PASSWORD" ]; then
  41. REDIS_PREFIX=:${REDIS_PASSWORD}@
  42. else
  43. REDIS_PREFIX=
  44. fi
  45.  
  46. wait_for_port() {
  47. local name="$1" host="$2" port="$3"
  48. local j=0
  49. while ! nc -z "$host" "$port" >/dev/null 2>&1 < /dev/null; do
  50. j=$((j+1))
  51. if [ $j -ge $TRY_LOOP ]; then
  52. echo >&2 "$(date) - $host:$port still not reachable, giving up"
  53. exit 1
  54. fi
  55. echo "$(date) - waiting for $name... $j/$TRY_LOOP"
  56. sleep 5
  57. done
  58. }
  59.  
  60. wait_for_redis() {
  61. # Wait for Redis if we are using it
  62. if [ "$AIRFLOW__CORE__EXECUTOR" = "CeleryExecutor" ]
  63. then
  64. wait_for_port "Redis" "$REDIS_HOST" "$REDIS_PORT"
  65. fi
  66. }
  67.  
  68. AIRFLOW__CORE__SQL_ALCHEMY_CONN="postgresql+psycopg2://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB"
  69. AIRFLOW__CELERY__BROKER_URL="redis://$REDIS_PREFIX$REDIS_HOST:$REDIS_PORT/1"
  70. AIRFLOW__CELERY__CELERY_RESULT_BACKEND="db+postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB"
  71.  
  72. case "$1" in
  73. webserver)
  74. wait_for_port "Postgres" "$POSTGRES_HOST" "$POSTGRES_PORT"
  75. wait_for_redis
  76. airflow initdb
  77. python /create-user.py
  78. if [ "$AIRFLOW__CORE__EXECUTOR" = "LocalExecutor" ];
  79. then
  80. # With the "Local" executor it should all run in one container.
  81. airflow scheduler &
  82. fi
  83. exec airflow webserver
  84. ;;
  85. worker|scheduler)
  86. wait_for_port "Postgres" "$POSTGRES_HOST" "$POSTGRES_PORT"
  87. wait_for_redis
  88. # To give the webserver time to run initdb.
  89. sleep 10
  90. exec airflow "$@"
  91. ;;
  92. flower)
  93. wait_for_redis
  94. exec airflow "$@"
  95. ;;
  96. version)
  97. exec airflow "$@"
  98. ;;
  99. *)
  100. # The command is something like bash, not an airflow subcommand. Just run it in the right environment.
  101. exec "$@"
  102. ;;
  103. esac
Add Comment
Please, Sign In to add comment