Skip to content

Commit 78be1b3

Browse files
committed
initial commit
0 parents  commit 78be1b3

File tree

3 files changed

+355
-0
lines changed

3 files changed

+355
-0
lines changed

.gitignore

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
target/
76+
77+
# Jupyter Notebook
78+
.ipynb_checkpoints
79+
80+
# IPython
81+
profile_default/
82+
ipython_config.py
83+
84+
# pyenv
85+
.python-version
86+
87+
# pipenv
88+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
90+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
91+
# install all needed dependencies.
92+
#Pipfile.lock
93+
94+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
95+
__pypackages__/
96+
97+
# Celery stuff
98+
celerybeat-schedule
99+
celerybeat.pid
100+
101+
# SageMath parsed files
102+
*.sage.py
103+
104+
# Environments
105+
.env
106+
.venv
107+
env/
108+
venv/
109+
ENV/
110+
env.bak/
111+
venv.bak/
112+
113+
# Spyder project settings
114+
.spyderproject
115+
.spyproject
116+
117+
# Rope project settings
118+
.ropeproject
119+
120+
# mkdocs documentation
121+
/site
122+
123+
# mypy
124+
.mypy_cache/
125+
.dmypy.json
126+
dmypy.json
127+
128+
# Pyre type checker
129+
.pyre/
130+
131+
# IDE
132+
.idea/
133+
.vscode/

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## r-u-ready to accept connections pgsql/mysql or any web service?
2+
3+
`./r-u-ready` is a script created to wait for services like pgsql/mysql/django/flask in docker containers. It was inspired by [eficode/wait-for](https://github.com/eficode/wait-for) and [vishnubob/wait-for-it](https://github.com/vishnubob/wait-for-it).
4+
5+
When using this tool, you only need to pick the `r-u-ready` file as part of your project.
6+
7+
## Usage
8+
9+
```
10+
./r-u-ready service [-h host] [-P port] [-u username] [-p password] [-t timeout] [-- command args]
11+
12+
service REQUIRED pgsql | mysql | web
13+
14+
-h HOST | --host=HOST Domain/IP host (default: 127.0.0.1)
15+
-P PORT | --port=PORT Port (default: 5432 for psql, 3306 for mysql, 80 for web)
16+
17+
-u USERNAME | --username=USERNAME MySQL username (required for mysql only)
18+
-p PASSWORD | --password=PASSWORD MySQL password for the given username (required for mysql only)
19+
20+
-q | --quiet Do not output any status messages
21+
-t TIMEOUT | --timeout=TIMEOUT Timeout in seconds, zero for no timeout (default: 15 seconds)
22+
-- COMMAND ARGS Execute command with args after the test finishes
23+
```
24+
25+
## Examples
26+
27+
To check if [google.com](https://google.com) is available:
28+
29+
```
30+
$ ./r-u-ready web -h google.com -- echo "Google.com site is up"
31+
32+
Waiting for web server at google.com: to be ready...
33+
web server at google.com: is ready for connection
34+
35+
Google.com site is up
36+
```
37+
38+
To wait for mysql database to accept connections:
39+
40+
```
41+
$ ./r-u-ready mysql -h 127.0.0.1 -P 3306 -u root -p <password> -t 300
42+
43+
Waiting for mysql server at 127.0.0.1:3306 to be ready...
44+
mysql server at 127.0.0.1:3306 is ready for connection
45+
```
46+
47+
To wait for database container to become available:
48+
49+
```
50+
version: '3.7'
51+
52+
services:
53+
db:
54+
image: postgres:alpine
55+
56+
backend:
57+
build: backend
58+
command: sh -c './r-u-ready pgsql -h db -t 60'
59+
depends_on:
60+
- db
61+
```
62+
63+
## Note
64+
65+
Make sure postgresql-client or mariadb-client or curl is installed in your Dockerfile before running the command.
66+
```
67+
RUN apk add --update curl
68+
RUN apk add postgresql-client
69+
RUN apk add mariadb-client
70+
```

r-u-ready

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/bin/sh
2+
3+
TIMEOUT=15
4+
QUIET=0
5+
HOST="127.0.0.1"
6+
7+
echoerr() {
8+
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
9+
}
10+
11+
usage() {
12+
exitcode="$1"
13+
cat << USAGE >&2
14+
Usage:
15+
$cmdname service [-h host] [-P port] [-u username] [-p password] [-t timeout] [-- command args]
16+
17+
service REQUIRED pgsql | mysql | web
18+
19+
-h HOST | --host=HOST Domain/IP host (default: 127.0.0.1)
20+
-P PORT | --port=PORT Port (default: 5432 for psql, 3306 for mysql, 80 for web)
21+
22+
-u USERNAME | --username=USERNAME MySQL username (required for mysql only)
23+
-p PASSWORD | --password=PASSWORD MySQL password for the given username (required for mysql only)
24+
25+
-q | --quiet Do not output any status messages
26+
-t TIMEOUT | --timeout=TIMEOUT Timeout in seconds, zero for no timeout (default: 15 seconds)
27+
-- COMMAND ARGS Execute command with args after the test finishes
28+
USAGE
29+
exit "$exitcode"
30+
}
31+
32+
get_params() {
33+
if [ "$SERVICE" = pgsql ]; then
34+
if [ -z "$PORT" ]; then PORT=5432; fi
35+
PARAMS="pg_isready -h ${HOST} -p ${PORT}"
36+
37+
elif [ "$SERVICE" = mysql ]; then
38+
if [ -z "$PORT" ]; then PORT=3306; fi
39+
PARAMS="mysqladmin ping -h ${HOST} -P ${PORT} -u ${USERNAME} -p${PASSWORD}"
40+
41+
else
42+
if [ -z "$PORT" ]; then PORT=80; fi
43+
PARAMS="curl ${HOST}:${PORT} --connect-timeout 1"
44+
fi
45+
46+
echo $PARAMS
47+
}
48+
49+
wait_for() {
50+
echo "Waiting for ${SERVICE} server at ${HOST}:${PORT} to be ready..."
51+
52+
for i in `seq $TIMEOUT` ; do
53+
eval $(get_params) > /dev/null 2>&1
54+
55+
result=$?
56+
if [ $result -eq 0 ] ; then
57+
echo "${SERVICE} server at ${HOST}:${PORT} is ready for connection"
58+
if [ $# -gt 0 ] ; then
59+
exec "$@"
60+
fi
61+
exit 0
62+
fi
63+
sleep 1
64+
done
65+
echo "Operation timed out" >&2
66+
exit 1
67+
}
68+
69+
while [ $# -gt 0 ]
70+
do
71+
case "$1" in
72+
pgsql | mysql | web)
73+
SERVICE=$1
74+
if [ "$SERVICE" = "" ]; then break; fi
75+
shift 1
76+
;;
77+
-h)
78+
HOST="$2"
79+
if [ "$HOST" = "" ]; then break; fi
80+
shift 2
81+
;;
82+
--host=*)
83+
HOST="${1#*=}"
84+
shift 1
85+
;;
86+
-P)
87+
PORT="$2"
88+
if [ "$PORT" = "" ]; then break; fi
89+
shift 2
90+
;;
91+
--port=*)
92+
PORT="${1#*=}"
93+
shift 1
94+
;;
95+
-u)
96+
USERNAME="$2"
97+
if [ "$USERNAME" = "" ]; then break; fi
98+
shift 2
99+
;;
100+
--username=*)
101+
USERNAME="${1#*=}"
102+
shift 1
103+
;;
104+
-p)
105+
PASSWORD="$2"
106+
if [ "$PASSWORD" = "" ]; then break; fi
107+
shift 2
108+
;;
109+
--password=*)
110+
PASSWORD="${1#*=}"
111+
shift 1
112+
;;
113+
-q | --quiet)
114+
QUIET=1
115+
shift 1
116+
;;
117+
-t)
118+
TIMEOUT="$2"
119+
if [ "$TIMEOUT" = "" ]; then break; fi
120+
shift 2
121+
;;
122+
--timeout=*)
123+
TIMEOUT="${1#*=}"
124+
shift 1
125+
;;
126+
--)
127+
shift
128+
break
129+
;;
130+
--help)
131+
usage 0
132+
;;
133+
*)
134+
echoerr "Unknown argument: $1"
135+
usage 1
136+
;;
137+
esac
138+
done
139+
140+
if [ "$SERVICE" = "" ]; then
141+
echoerr "Error: you must provide a service name: pgsql | mysql | web"
142+
usage 2
143+
fi
144+
145+
if [ "$SERVICE" = mysql ]; then
146+
if [ "$USERNAME" = "" ] || [ "$PASSWORD" = "" ]; then
147+
echoerr "Error: you must provide username and password for mysql service"
148+
usage 2
149+
fi
150+
fi
151+
152+
wait_for "$@"

0 commit comments

Comments
 (0)