2020-04-28 20:15:59 +00:00
|
|
|
import socket
|
|
|
|
import time
|
2021-12-10 08:53:00 +00:00
|
|
|
|
2020-07-10 17:45:36 +00:00
|
|
|
from constants import (
|
|
|
|
DB_HOST_KEY,
|
2021-12-10 08:52:40 +00:00
|
|
|
DB_PORT,
|
2021-12-10 08:53:00 +00:00
|
|
|
DB_PORT_KEY,
|
|
|
|
REDIS_CACHE_KEY,
|
|
|
|
REDIS_QUEUE_KEY,
|
|
|
|
REDIS_SOCKETIO_KEY,
|
2020-07-10 17:45:36 +00:00
|
|
|
)
|
2021-12-10 08:53:00 +00:00
|
|
|
from six.moves.urllib.parse import urlparse
|
|
|
|
from utils import get_config
|
2020-03-14 17:17:35 +00:00
|
|
|
|
2020-04-28 20:15:59 +00:00
|
|
|
|
2020-02-10 08:00:54 +00:00
|
|
|
def is_open(ip, port, timeout=30):
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
s.settimeout(timeout)
|
|
|
|
try:
|
|
|
|
s.connect((ip, int(port)))
|
|
|
|
s.shutdown(socket.SHUT_RDWR)
|
|
|
|
return True
|
2020-04-28 20:15:59 +00:00
|
|
|
except Exception:
|
2020-02-10 08:00:54 +00:00
|
|
|
return False
|
|
|
|
finally:
|
|
|
|
s.close()
|
|
|
|
|
2020-04-28 20:15:59 +00:00
|
|
|
|
2020-03-14 17:17:35 +00:00
|
|
|
def check_host(ip, port, retry=10, delay=3, print_attempt=True):
|
2020-02-10 08:00:54 +00:00
|
|
|
ipup = False
|
|
|
|
for i in range(retry):
|
2020-03-14 17:17:35 +00:00
|
|
|
if print_attempt:
|
2021-12-10 08:51:47 +00:00
|
|
|
print(f"Attempt {i+1} to connect to {ip}:{port}")
|
2020-02-10 08:00:54 +00:00
|
|
|
if is_open(ip, port):
|
|
|
|
ipup = True
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
time.sleep(delay)
|
|
|
|
return ipup
|
|
|
|
|
2020-04-28 20:15:59 +00:00
|
|
|
|
2020-07-01 14:31:47 +00:00
|
|
|
# Check service
|
|
|
|
def check_service(
|
2021-12-10 08:52:40 +00:00
|
|
|
retry=10, delay=3, print_attempt=True, service_name=None, service_port=None
|
|
|
|
):
|
2020-07-01 14:31:47 +00:00
|
|
|
|
2020-03-14 17:17:35 +00:00
|
|
|
config = get_config()
|
2020-07-01 14:31:47 +00:00
|
|
|
if not service_name:
|
2021-12-10 08:52:40 +00:00
|
|
|
service_name = config.get(DB_HOST_KEY, "mariadb")
|
2020-07-01 14:31:47 +00:00
|
|
|
if not service_port:
|
|
|
|
service_port = config.get(DB_PORT_KEY, DB_PORT)
|
|
|
|
|
2020-06-29 17:58:10 +00:00
|
|
|
is_db_connected = False
|
|
|
|
is_db_connected = check_host(
|
2021-12-10 08:52:40 +00:00
|
|
|
service_name, service_port, retry, delay, print_attempt
|
|
|
|
)
|
2020-06-29 17:58:10 +00:00
|
|
|
if not is_db_connected:
|
2021-12-10 08:52:40 +00:00
|
|
|
print(
|
|
|
|
"Connection to {service_name}:{service_port} timed out".format(
|
|
|
|
service_name=service_name,
|
|
|
|
service_port=service_port,
|
|
|
|
)
|
|
|
|
)
|
2020-03-14 17:17:35 +00:00
|
|
|
exit(1)
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-04-28 20:15:59 +00:00
|
|
|
|
2020-02-10 08:00:54 +00:00
|
|
|
# Check redis queue
|
2020-03-14 17:17:35 +00:00
|
|
|
def check_redis_queue(retry=10, delay=3, print_attempt=True):
|
|
|
|
check_redis_queue = False
|
|
|
|
config = get_config()
|
2021-12-10 08:52:40 +00:00
|
|
|
redis_queue_url = urlparse(
|
|
|
|
config.get(REDIS_QUEUE_KEY, "redis://redis-queue:6379")
|
|
|
|
).netloc
|
2020-03-14 17:17:35 +00:00
|
|
|
redis_queue, redis_queue_port = redis_queue_url.split(":")
|
|
|
|
check_redis_queue = check_host(
|
2021-12-10 08:52:40 +00:00
|
|
|
redis_queue, redis_queue_port, retry, delay, print_attempt
|
|
|
|
)
|
2020-03-14 17:17:35 +00:00
|
|
|
if not check_redis_queue:
|
|
|
|
print("Connection to redis queue timed out")
|
|
|
|
exit(1)
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-04-28 20:15:59 +00:00
|
|
|
|
2020-02-10 08:00:54 +00:00
|
|
|
# Check redis cache
|
2020-03-14 17:17:35 +00:00
|
|
|
def check_redis_cache(retry=10, delay=3, print_attempt=True):
|
|
|
|
check_redis_cache = False
|
|
|
|
config = get_config()
|
2021-12-10 08:52:40 +00:00
|
|
|
redis_cache_url = urlparse(
|
|
|
|
config.get(REDIS_CACHE_KEY, "redis://redis-cache:6379")
|
|
|
|
).netloc
|
2020-03-14 17:17:35 +00:00
|
|
|
redis_cache, redis_cache_port = redis_cache_url.split(":")
|
|
|
|
check_redis_cache = check_host(
|
2021-12-10 08:52:40 +00:00
|
|
|
redis_cache, redis_cache_port, retry, delay, print_attempt
|
|
|
|
)
|
2020-03-14 17:17:35 +00:00
|
|
|
if not check_redis_cache:
|
|
|
|
print("Connection to redis cache timed out")
|
|
|
|
exit(1)
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-04-28 20:15:59 +00:00
|
|
|
|
2020-02-10 08:00:54 +00:00
|
|
|
# Check redis socketio
|
2020-03-14 17:17:35 +00:00
|
|
|
def check_redis_socketio(retry=10, delay=3, print_attempt=True):
|
|
|
|
check_redis_socketio = False
|
|
|
|
config = get_config()
|
2021-12-10 08:52:40 +00:00
|
|
|
redis_socketio_url = urlparse(
|
|
|
|
config.get(REDIS_SOCKETIO_KEY, "redis://redis-socketio:6379")
|
|
|
|
).netloc
|
2020-03-14 17:17:35 +00:00
|
|
|
redis_socketio, redis_socketio_port = redis_socketio_url.split(":")
|
|
|
|
check_redis_socketio = check_host(
|
2021-12-10 08:52:40 +00:00
|
|
|
redis_socketio, redis_socketio_port, retry, delay, print_attempt
|
|
|
|
)
|
2020-03-14 17:17:35 +00:00
|
|
|
if not check_redis_socketio:
|
|
|
|
print("Connection to redis socketio timed out")
|
|
|
|
exit(1)
|
|
|
|
|
2020-04-28 20:15:59 +00:00
|
|
|
|
2020-03-14 17:17:35 +00:00
|
|
|
def main():
|
2020-07-01 14:31:47 +00:00
|
|
|
check_service()
|
2020-03-14 17:17:35 +00:00
|
|
|
check_redis_queue()
|
|
|
|
check_redis_cache()
|
|
|
|
check_redis_socketio()
|
2021-12-10 08:52:40 +00:00
|
|
|
print("Connections OK")
|
2020-02-10 08:00:54 +00:00
|
|
|
|
2020-04-28 20:15:59 +00:00
|
|
|
|
2020-03-14 17:17:35 +00:00
|
|
|
if __name__ == "__main__":
|
2020-03-20 15:26:23 +00:00
|
|
|
main()
|