Skip to content

Commit 62b4135

Browse files
committed
Initial commit
0 parents  commit 62b4135

File tree

3 files changed

+286
-0
lines changed

3 files changed

+286
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

scripts/entrypoint.sh

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#! /usr/bin/env bash
2+
3+
set -e
4+
set -o errexit
5+
set -o nounset
6+
set -o pipefail
7+
# set -o xtrace # Uncomment this line for debugging purposes
8+
9+
#Variables
10+
: "${MYSQL_HOST:=}"
11+
: "${POSTGRESS_HOST:=}"
12+
: "${ELASTICSEARCH_HOST:=}"
13+
14+
15+
: "${WORKING_DIR:=""}"
16+
17+
VERSION="0.0.1"
18+
INIT="false"
19+
20+
case "$1" in
21+
-v | --version)
22+
echo "Version: $VERSION"
23+
exit
24+
;;
25+
--init)
26+
INIT="true"
27+
;;
28+
29+
esac
30+
31+
Check_TCP_service() {
32+
HOST=$1
33+
PORT=$2
34+
/scripts/wait_for.sh $HOST:$PORT -t 1
35+
}
36+
37+
Check_HTTP_service() {
38+
HOST=$1
39+
/scripts/wait_for.sh $HOST -t 1
40+
}
41+
42+
if [[ $MYSQL_HOST != "" ]]; then
43+
: "${MYSQL_PORT:=3306}"
44+
45+
Check_TCP_service $MYSQL_HOST $MYSQL_PORT
46+
echo "MYSQL_HOST is ok"
47+
fi
48+
49+
if [[ $POSTGRESS_HOST != "" ]]; then
50+
: "${POSTGRES_PORT:=5432}"
51+
52+
Check_TCP_service $POSTGRES_HOST $POSTGRES_PORT
53+
echo "POSTGRES_HOST is ok"
54+
fi
55+
56+
if [[ $ELASTICSEARCH_HOST != "" ]]; then
57+
: "${ELASTICSEARCH_PORT:=9200}"
58+
59+
LINK_TO_CHECK="http://$ELASTICSEARCH_HOST:$ELASTICSEARCH_PORT/_cluster/health"
60+
61+
Check_HTTP_service $LINK_TO_CHECK
62+
echo "ELASTICSEARCH_HOST is ok"
63+
fi
64+
65+
if [ -z "$WORKING_DIR" ]; then
66+
echo "WORKING_DIR is not defined"
67+
exit 1
68+
fi
69+
70+
71+
72+
# Only execute init script once
73+
if [[ ! -f "$WORKING_DIR/.initialized" ]]; then
74+
case "$INIT" in
75+
true)
76+
echo "Initilizing app"
77+
/scripts/init.sh
78+
echo "App was initialized"
79+
touch "$WORKING_DIR/.initialized"
80+
;;
81+
*)
82+
echo "Waiting for initialization"
83+
echo "If you want run this container as init, use --init flag"
84+
exit 1
85+
;;
86+
esac
87+
fi
88+
89+
if [[ $1 == '--init' ]]; then
90+
shift
91+
fi
92+
93+
exec "$@"

scripts/wait_for.sh

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
#!/bin/sh
2+
3+
# The MIT License (MIT)
4+
#
5+
# Copyright (c) 2017 Eficode Oy
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
25+
VERSION="2.2.4"
26+
27+
set -- "$@" -- "$TIMEOUT" "$QUIET" "$PROTOCOL" "$HOST" "$PORT" "$result"
28+
TIMEOUT=15
29+
QUIET=0
30+
# The protocol to make the request with, either "tcp" or "http"
31+
PROTOCOL="tcp"
32+
33+
echoerr() {
34+
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
35+
}
36+
37+
usage() {
38+
exitcode="$1"
39+
cat << USAGE >&2
40+
Usage:
41+
$0 host:port|url [-t timeout] [-- command args]
42+
-q | --quiet Do not output any status messages
43+
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
44+
Defaults to 15 seconds
45+
-v | --version Show the version of this tool
46+
-- COMMAND ARGS Execute command with args after the test finishes
47+
USAGE
48+
exit "$exitcode"
49+
}
50+
51+
wait_for() {
52+
case "$PROTOCOL" in
53+
tcp)
54+
if ! command -v nc >/dev/null; then
55+
echoerr 'nc command is missing!'
56+
exit 1
57+
fi
58+
;;
59+
http)
60+
if ! command -v wget >/dev/null; then
61+
echoerr 'wget command is missing!'
62+
exit 1
63+
fi
64+
;;
65+
esac
66+
67+
TIMEOUT_END=$(($(date +%s) + TIMEOUT))
68+
69+
while :; do
70+
case "$PROTOCOL" in
71+
tcp)
72+
nc -w 1 -z "$HOST" "$PORT" > /dev/null 2>&1
73+
;;
74+
http)
75+
wget --timeout=1 --tries=1 -q "$HOST" -O /dev/null > /dev/null 2>&1
76+
;;
77+
*)
78+
echoerr "Unknown protocol '$PROTOCOL'"
79+
exit 1
80+
;;
81+
esac
82+
83+
result=$?
84+
85+
if [ $result -eq 0 ] ; then
86+
if [ $# -gt 7 ] ; then
87+
for result in $(seq $(($# - 7))); do
88+
result=$1
89+
shift
90+
set -- "$@" "$result"
91+
done
92+
93+
TIMEOUT=$2 QUIET=$3 PROTOCOL=$4 HOST=$5 PORT=$6 result=$7
94+
shift 7
95+
exec "$@"
96+
fi
97+
exit 0
98+
fi
99+
100+
if [ $TIMEOUT -ne 0 -a $(date +%s) -ge $TIMEOUT_END ]; then
101+
echo "Operation timed out. host: $HOST is unreachable" >&2
102+
exit 1
103+
fi
104+
105+
sleep 1
106+
done
107+
}
108+
109+
while :; do
110+
case "$1" in
111+
http://*|https://*)
112+
HOST="$1"
113+
PROTOCOL="http"
114+
shift 1
115+
;;
116+
*:* )
117+
HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
118+
PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
119+
shift 1
120+
;;
121+
-v | --version)
122+
echo $VERSION
123+
exit
124+
;;
125+
-q | --quiet)
126+
QUIET=1
127+
shift 1
128+
;;
129+
-q-*)
130+
QUIET=0
131+
echoerr "Unknown option: $1"
132+
usage 1
133+
;;
134+
-q*)
135+
QUIET=1
136+
result=$1
137+
shift 1
138+
set -- -"${result#-q}" "$@"
139+
;;
140+
-t | --timeout)
141+
TIMEOUT="$2"
142+
shift 2
143+
;;
144+
-t*)
145+
TIMEOUT="${1#-t}"
146+
shift 1
147+
;;
148+
--timeout=*)
149+
TIMEOUT="${1#*=}"
150+
shift 1
151+
;;
152+
--)
153+
shift
154+
break
155+
;;
156+
--help)
157+
usage 0
158+
;;
159+
-*)
160+
QUIET=0
161+
echoerr "Unknown option: $1"
162+
usage 1
163+
;;
164+
*)
165+
QUIET=0
166+
echoerr "Unknown argument: $1"
167+
usage 1
168+
;;
169+
esac
170+
done
171+
172+
if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then
173+
echoerr "Error: invalid timeout '$TIMEOUT'"
174+
usage 3
175+
fi
176+
177+
case "$PROTOCOL" in
178+
tcp)
179+
if [ "$HOST" = "" ] || [ "$PORT" = "" ]; then
180+
echoerr "Error: you need to provide a host and port to test."
181+
usage 2
182+
fi
183+
;;
184+
http)
185+
if [ "$HOST" = "" ]; then
186+
echoerr "Error: you need to provide a host to test."
187+
usage 2
188+
fi
189+
;;
190+
esac
191+
192+
wait_for "$@"

0 commit comments

Comments
 (0)