wait4it
A simple go application to test whether a port is ready to accept a connection or check
MySQL, PostgreSQL, MongoDB or Redis server is ready or not, Also you can do Http call and check
the response code and text in response.
It also supports timeout so it can wait for a particular time and then fail.
Supported Modules
- TCP port
- MySQL
- PostgresQL
- Http
- MongoDB
- Redis
Install
You can download the latest release, or you can build it yourself.
To build just run go build -o wait4it
Configuration
Environment variables
The following environment variables are supported:
W4IT_TYPE
(check type [http, tcp, mysql, postgres])W4IT_TIMEOUT
(timeout in seconds)W4IT_HOST
(the host to check)W4IT_PORT
(port to check on the host)W4IT_USERNAME
(username for the services that needs username)W4IT_PASSWORD
(password for the services that needs password)W4IT_DBNAME
(database name for MySQL or PostgreSQL)W4IT_SSL_MODE
(whether to enable or disable ssl-mode for Postgres [disable, enable])W4IT_OPERATION_MODE
(to select operation mode for redis [standalone, cluster])W4IT_HTTP_STATUS_CODE
(for Http check, which status code to expect)W4IT_HTTP_TEXT
(for Http check, find substring inside the response)
Command Line Args
The following command-line flags are supported
-type
(check type [http, tcp, mysql, postgres])-h
(the host to check, default is 127.0.0.1)-p
(port to check on the host)-t
(timeout in seconds, time to wait before considering the operation as failed. default is 30)-u
(username for the services that needs username)-P
(password for the services that needs password)-n
(database name for MySQL or PostgreSQL)-ssl
(whether to enable or disable ssl-mode for Postgres [disable, enable])-operation-mode
(to select operation mode for redis [standalone, cluster])-http-status
(for Http check, which status code to expect)-http-text
(for Http check, find substring inside the response)
Sample
Check a TCP port
./wait4it -h=127.0.0.1 -p=8080 -t=60
Check a MySQL instance
./wait4it -h=127.0.0.1 -p=3306 -t=60 -u=root -P=secret -n=app
Check a PostgresQL instance
./wait4it -type=postgres -h=127.0.0.1 -p=5432 -t=60 -u=postgres -P=secret -ssl=disable
Check HTTP response and text
./wait4it -type=http -h=https://farshad.nematdoust.com -t=60 -status-code=200 -http-text="Software Engineer"
Check a MongoDB instance
./wait4it -type=mongo -p=27017 -t=60 -u=mongoadmin -P=secret -h=127.0.0.1
Check a Redis instance
./wait4it -type=redis -p=6379 -t=60 -P=secret -h=127.0.0.1
# if your redis is not password protected
./wait4it -type=redis -p=6379 -t=60 -h=127.0.0.1
# if you use redis cluster
./wait4it -type=redis -p=6379 -t=60 -P=secret -operation-mode=cluster -h=127.0.0.1
Docker
You can run this wait4it
inside a docker container, and it's possible to run this container as init container inside
K8s and Openshift.
You can use the following image in Docker Hub:
docker pull ph4r5h4d/wait4it
Inside container you can do the following:
Sample
Check a TCP port
docker run ph4r5h4d/wait4it -h=127.0.0.1 -p=8080 -t=60
Check a MySQL instance
docker run ph4r5h4d/wait4it -h=127.0.0.1 -p=3306 -t=60 -u=root -P=secret -n=app
Check a PostgresQL instance
docker run ph4r5h4d/wait4it -type=postgres -h=127.0.0.1 -p=5432 -t=60 -u=postgres -P=secret -ssl=disable
Check HTTP response and text
docker run ph4r5h4d/wait4it -type=http -h=https://farshad.nematdoust.com -t=60 -status-code=200 -http-text="Software Engineer"
Check a MongoDB instance
docker run ph4r5h4d/wait4it -type=mongo -p=32768 -t=60 -u=mongoadmin -P=secret -h=127.0.0.1
Check a Redis instance
docker run ph4r5h4d/wait4it -type=redis -p=6379 -t=60 -P=secret -h=127.0.0.1
# if your redis is not password protected
docker run ph4r5h4d/wait4it -type=redis -p=6379 -t=60 -h=127.0.0.1
# if you use redis cluster
docker run ph4r5h4d/wait4it -type=redis -p=6379 -t=60 -P=secret -operation-mode=cluster -h=127.0.0.1
Notes
Configuration
- note that environment variables have higher priority than command-line arguments.
This means if you define both
W4IT_TYPE
and-type
, the application takes the value of the environment variable into account.
Exit codes
- 0: connection established successfully
- 1: timed out
- 2: mostly means a validation error or something wrong with the input data
Http check
- for the Http check if you do not define status code it will check for 200 status code
- if
http-text
is not defined then wait4it will check the status code
Postgres check
- if
ssl
is not defined then it'sdisable
by default
MongoDB check
- for the moment only username/password authentication mechanism is supported.
Redis check
- if
operation-mode
is not defined then it'sstandalone
by default. Redis Sentinel is not supported yet. - this version can only check one host within Redis cluster, using multiple hosts to check cluster status for Redis will be added in the next version.