BGP-Ranking/bin/run_backend.py

145 lines
4.5 KiB
Python
Raw Normal View History

2018-07-13 14:51:00 +02:00
#!/usr/bin/env python3
2018-03-29 22:37:28 +02:00
# -*- coding: utf-8 -*-
2021-12-06 14:30:08 +01:00
import argparse
import os
2018-03-29 22:37:28 +02:00
import time
from pathlib import Path
2021-12-06 14:30:08 +01:00
from subprocess import Popen
from typing import Optional, Dict
2018-03-29 22:37:28 +02:00
2021-12-06 14:30:08 +01:00
from redis import Redis
from redis.exceptions import ConnectionError
2018-03-29 22:37:28 +02:00
2021-12-06 14:30:08 +01:00
from bgpranking.default import get_homedir, get_socket_path, get_config
2018-03-29 22:37:28 +02:00
2021-12-06 14:30:08 +01:00
def check_running(name: str) -> bool:
if name == "storage":
r = Redis(get_config('generic', 'storage_db_hostname'), get_config('generic', 'storage_db_port'))
elif name == "ranking":
r = Redis(get_config('generic', 'ranking_db_hostname'), get_config('generic', 'ranking_db_port'))
else:
socket_path = get_socket_path(name)
if not os.path.exists(socket_path):
return False
r = Redis(unix_socket_path=socket_path)
try:
return True if r.ping() else False
except ConnectionError:
return False
def launch_cache(storage_directory: Optional[Path]=None):
2018-03-29 22:37:28 +02:00
if not storage_directory:
storage_directory = get_homedir()
if not check_running('cache'):
2018-03-30 14:33:33 +02:00
Popen(["./run_redis.sh"], cwd=(storage_directory / 'cache'))
2018-03-29 22:37:28 +02:00
2021-12-06 14:30:08 +01:00
def shutdown_cache(storage_directory: Optional[Path]=None):
2018-03-29 22:37:28 +02:00
if not storage_directory:
storage_directory = get_homedir()
2021-12-06 14:30:08 +01:00
r = Redis(unix_socket_path=get_socket_path('cache'))
r.shutdown(save=True)
print('Redis cache database shutdown.')
2018-03-29 22:37:28 +02:00
2021-12-06 14:30:08 +01:00
def launch_temp(storage_directory: Optional[Path]=None):
2018-03-29 22:37:28 +02:00
if not storage_directory:
storage_directory = get_homedir()
if not check_running('intake') and not check_running('prepare'):
2018-03-30 14:33:33 +02:00
Popen(["./run_redis.sh"], cwd=(storage_directory / 'temp'))
2018-03-29 22:37:28 +02:00
2021-12-06 14:30:08 +01:00
def shutdown_temp(storage_directory: Optional[Path]=None):
2018-03-29 22:37:28 +02:00
if not storage_directory:
storage_directory = get_homedir()
2021-12-06 14:30:08 +01:00
r = Redis(unix_socket_path=get_socket_path('intake'))
r.shutdown(save=True)
print('Redis intake database shutdown.')
r = Redis(unix_socket_path=get_socket_path('prepare'))
r.shutdown(save=True)
print('Redis prepare database shutdown.')
2018-03-29 22:37:28 +02:00
2021-12-06 14:30:08 +01:00
def launch_storage(storage_directory: Optional[Path]=None):
2018-03-29 22:37:28 +02:00
if not storage_directory:
storage_directory = get_homedir()
if not check_running('storage'):
2021-12-06 14:30:08 +01:00
Popen(["./run_kvrocks.sh"], cwd=(storage_directory / 'storage'))
2018-03-29 22:37:28 +02:00
2021-12-06 14:30:08 +01:00
def shutdown_storage(storage_directory: Optional[Path]=None):
redis = Redis(get_config('generic', 'storage_db_hostname'), get_config('generic', 'storage_db_port'))
redis.shutdown()
2018-03-29 22:37:28 +02:00
2021-12-06 14:30:08 +01:00
def launch_ranking(storage_directory: Optional[Path]=None):
2018-03-29 22:37:28 +02:00
if not storage_directory:
storage_directory = get_homedir()
2021-12-06 14:30:08 +01:00
if not check_running('ranking'):
Popen(["./run_kvrocks.sh"], cwd=(storage_directory / 'ranking'))
def shutdown_ranking(storage_directory: Optional[Path]=None):
redis = Redis(get_config('generic', 'ranking_db_hostname'), get_config('generic', 'ranking_db_port'))
redis.shutdown()
2018-03-29 22:37:28 +02:00
def launch_all():
launch_cache()
launch_temp()
launch_storage()
2021-12-06 14:30:08 +01:00
launch_ranking()
2018-03-29 22:37:28 +02:00
2021-12-06 14:30:08 +01:00
def check_all(stop: bool=False):
backends: Dict[str, bool] = {'cache': False, 'storage': False, 'ranking': False,
'intake': False, 'prepare': False}
2018-03-29 22:37:28 +02:00
while True:
2021-12-06 14:30:08 +01:00
for db_name in backends.keys():
print(backends[db_name])
2018-03-29 22:37:28 +02:00
try:
2021-12-06 14:30:08 +01:00
backends[db_name] = check_running(db_name)
2018-03-29 22:37:28 +02:00
except Exception:
2021-12-06 14:30:08 +01:00
backends[db_name] = False
2018-03-29 22:37:28 +02:00
if stop:
2021-12-06 14:30:08 +01:00
if not any(running for running in backends.values()):
2018-03-29 22:37:28 +02:00
break
else:
2021-12-06 14:30:08 +01:00
if all(running for running in backends.values()):
2018-03-29 22:37:28 +02:00
break
2021-12-06 14:30:08 +01:00
for db_name, running in backends.items():
if not stop and not running:
print(f"Waiting on {db_name} to start")
if stop and running:
print(f"Waiting on {db_name} to stop")
2018-03-29 22:37:28 +02:00
time.sleep(1)
def stop_all():
shutdown_cache()
shutdown_temp()
shutdown_storage()
2021-12-06 14:30:08 +01:00
shutdown_ranking()
2018-03-29 22:37:28 +02:00
2021-12-06 14:30:08 +01:00
def main():
2018-03-29 22:37:28 +02:00
parser = argparse.ArgumentParser(description='Manage backend DBs.')
parser.add_argument("--start", action='store_true', default=False, help="Start all")
parser.add_argument("--stop", action='store_true', default=False, help="Stop all")
parser.add_argument("--status", action='store_true', default=True, help="Show status")
args = parser.parse_args()
if args.start:
launch_all()
if args.stop:
stop_all()
if not args.stop and args.status:
check_all()
2021-12-06 14:30:08 +01:00
if __name__ == '__main__':
main()