Browse Source

Waiting for database to come up.

Dan Helfman 1 year ago
parent
commit
a6178ee7e6
3 changed files with 84 additions and 0 deletions
  1. 1
    0
      Dockerfile
  2. 4
    0
      run.sh
  3. 79
    0
      wait-for

+ 1
- 0
Dockerfile View File

@@ -33,6 +33,7 @@ RUN install_packages \
33 33
 COPY mediagoblin_local.ini.template /app/mediagoblin_local.ini.template
34 34
 COPY uwsgi.yaml /etc/uwsgi/apps-enabled/mediagoblin.yaml
35 35
 COPY nginx.conf /etc/nginx/sites-enabled/mediagoblin.conf
36
+COPY wait-for /sbin/wait-for
36 37
 COPY run.sh /sbin/run.sh
37 38
 
38 39
 RUN addgroup --system mediagoblin \

+ 4
- 0
run.sh View File

@@ -7,6 +7,10 @@ envsubst < /app/mediagoblin_local.ini.template > /app/mediagoblin_local.ini
7 7
 chown mediagoblin.www-data /app/mediagoblin_local.ini
8 8
 su - mediagoblin --command "cd /app && gmg dbupdate"
9 9
 
10
+# Wait for the database to come up.
11
+database_host=$(echo $DATABASE_URL | cut -d @ -f 2 | cut -d / -f 1)
12
+/sbin/wait-for ${database_host}:5432
13
+
10 14
 # Run uWSGI and Nginx.
11 15
 chown mediagoblin.www-data /app/user_dev
12 16
 /usr/bin/uwsgi --uid mediagoblin --gid www-data --master --workers 3 /etc/uwsgi/apps-enabled/mediagoblin.yaml \

+ 79
- 0
wait-for View File

@@ -0,0 +1,79 @@
1
+#!/bin/sh
2
+
3
+TIMEOUT=15
4
+QUIET=0
5
+
6
+echoerr() {
7
+  if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
8
+}
9
+
10
+usage() {
11
+  exitcode="$1"
12
+  cat << USAGE >&2
13
+Usage:
14
+  $cmdname host:port [-t timeout] [-- command args]
15
+  -q | --quiet                        Do not output any status messages
16
+  -t TIMEOUT | --timeout=timeout      Timeout in seconds, zero for no timeout
17
+  -- COMMAND ARGS                     Execute command with args after the test finishes
18
+USAGE
19
+  exit "$exitcode"
20
+}
21
+
22
+wait_for() {
23
+  for i in `seq $TIMEOUT` ; do
24
+    nc -z "$HOST" "$PORT" > /dev/null 2>&1
25
+    
26
+    result=$?
27
+    if [ $result -eq 0 ] ; then
28
+      if [ $# -gt 0 ] ; then
29
+        exec "$@"
30
+      fi
31
+      exit 0
32
+    fi
33
+    sleep 1
34
+  done
35
+  echo "Operation timed out" >&2
36
+  exit 1
37
+}
38
+
39
+while [ $# -gt 0 ]
40
+do
41
+  case "$1" in
42
+    *:* )
43
+    HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
44
+    PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
45
+    shift 1
46
+    ;;
47
+    -q | --quiet)
48
+    QUIET=1
49
+    shift 1
50
+    ;;
51
+    -t)
52
+    TIMEOUT="$2"
53
+    if [ "$TIMEOUT" = "" ]; then break; fi
54
+    shift 2
55
+    ;;
56
+    --timeout=*)
57
+    TIMEOUT="${1#*=}"
58
+    shift 1
59
+    ;;
60
+    --)
61
+    shift
62
+    break
63
+    ;;
64
+    --help)
65
+    usage 0
66
+    ;;
67
+    *)
68
+    echoerr "Unknown argument: $1"
69
+    usage 1
70
+    ;;
71
+  esac
72
+done
73
+
74
+if [ "$HOST" = "" -o "$PORT" = "" ]; then
75
+  echoerr "Error: you need to provide a host and port to test."
76
+  usage 2
77
+fi
78
+
79
+wait_for "$@"

Loading…
Cancel
Save