diff --git a/bin/dcm b/bin/dcm index 2b35630..3795203 100755 --- a/bin/dcm +++ b/bin/dcm @@ -11,6 +11,43 @@ import os import re from sys import argv from time import time, sleep +from pprint import PrettyPrinter + + +class Dependecies: + def __init__(self, deps): + self.deps = deps + + @staticmethod + def load(yaml_filename): + deps = {} + with open(yaml_filename) as file: + dc = yaml.load(file, Loader=yaml.FullLoader) + for (name, conf) in dc['services'].items(): + depends_on = conf.get('depends_on', []) + deps[name] = depends_on + return deps + + def find(self, service): + all = set([service]) + if service in self.deps: + services = self.deps[service] + new = set(services) - all + if new: + print(f"{service}: {new}") + all.update(services) + del self.deps[service] + for one in new: + all.update(self.find(one)) + return all + + +def echo(text): + print(">> " + text) + + +def system(cmd): + return subprocess.call(cmd, shell=True) def split(text): @@ -40,8 +77,7 @@ def dc_cmd(args): def dc_run(args): cmd = dc_cmd(args) print(cmd) - returned_value = subprocess.call(cmd, shell=True) - exit(returned_value) + return system(cmd) def dc_log(args): @@ -60,8 +96,16 @@ def dump_services(files): print(service) -def service_dependencies(service, dc_files): - pass +def dc_deps(dc_files, service): + pp = PrettyPrinter(indent=2) + for dc_file in dc_files: + dep = Dependecies.load(dc_file) + if service in dep: + all = sorted(Dependecies(dep).find(service)) + print(all) + return + print(f"Service {service} not found") + def dump_files(files): @@ -73,7 +117,6 @@ def dump_files(files): # AQE: /opt/bin/aqe ping # md-api-new: curl -fs -o /dev/null http://localhost:9008/actuator/health/readiness # potgres: s6-setuidgid postgres; /usr/bin/pg_isready -# pulsar: curl -fs -o /dev/null http://localhost:8080/admin/v2/tenants/public # redis: /usr/bin/redis-cli ping # result-cache: curl -fs -o /dev/null http://localhost:9041/actuator/health/readiness # scan-model: curl -fs -o /dev/null http://localhost:9061/actuator/health/readiness @@ -82,8 +125,8 @@ def dump_files(files): def curl_check(uri): cmd = "curl -f -s -o /dev/null " + uri - print(cmd) - return subprocess.call(cmd, shell=True) == 0 + echo(cmd) + return system(cmd) == 0 def check_pulsar(): @@ -91,7 +134,7 @@ def check_pulsar(): def check_postgres(): - return False + return system("/usr/bin/pg_isready") == 0 def wait_for(check, delay, duration): @@ -99,12 +142,20 @@ def wait_for(check, delay, duration): while time() - start < duration: if check(): return True - else: - sleep(delay) + else: + echo(f"sleep for {delay} sec") + sleep(delay) + return False def up_and_wait(service, check, delay, duration): - pass + dc_run(["up", "-d", service]) + started = wait_for(check, delay, duration) + echo(f"WAIT FOR {service}") + if started: + echo(f"READY {service}") + else: + echo(f"BAD {service}") def start_tiger(): @@ -127,6 +178,12 @@ DEFAULT_DCF = ["docker-compose.yaml"] DC_FILES = try_load_list("DCF", DEFAULT_DCF) DC_FILES_ARGS = dc_files(DC_FILES) +SERVICES = { + "pulsar": lambda: up_and_wait("pulsar", check_pulsar, 10, 120), + "postgres": lambda: up_and_wait("postgres", check_postgres, 10, 120), + "data-loader": None, # co chci testovat? ze ma Exit code 0 +} + DEFAULT_TIGER_SERVICES = [ "afm-exec-api", "metadata-api-new", @@ -140,8 +197,10 @@ TIGER_SERVICES = try_load_list("SERVICES", DEFAULT_TIGER_SERVICES) COMMANDS = { "files": lambda args: dump_files(DC_FILES), "services": lambda args: dump_services(DC_FILES), - "log": dc_log - # "dep": + "log": dc_log, + "deps": lambda args: dc_deps(DC_FILES, args[0]), + "wait": None, + "up-wait": lambda args: SERVICES[args[0]](), } @@ -151,8 +210,11 @@ def main(): if action in COMMANDS: COMMANDS[action](args[1:]) else: - dc_run(args) + exit(dc_run(args)) if __name__ == '__main__': - main() + try: + main() + except KeyboardInterrupt: + pass