Skip to content
Snippets Groups Projects
Select Git revision
  • 74162333fc5b80f3670ee1bd6997f80320d655a2
  • master default protected
  • exponential-backoff-login
  • v1.9.2
  • v1.9.0
  • v1.8.8
  • v1.8.7
  • v1.8.5
  • v1.8.4
  • v1.8.2
  • v1.8
  • v1.7
  • v1.6
  • v1.5
  • v1.4
  • v1.3
  • v1.2
  • v1.1
  • v1.0.1
  • v1.0
  • v0.2.80
  • v0.2.79
  • v0.2.78
23 results

db_finder.py

Blame
  • db_finder.py 1.36 KiB
    import os
    import subprocess
    
    
    def get_db_ip():
        result = subprocess.run([
            "docker",
            "ps",
            "-f",
            "name=%s" % get_guessed_container_name(),
            "-q",
        ], stdout=subprocess.PIPE)
        ids = result.stdout.decode('utf-8').strip().split('\n')
        if len(ids) > 1:
            raise Exception("Can't find the DB, too much match")
        if len(ids) == 0 or len(ids[0]) == 0:
            result = subprocess.run([
                "docker",
                "ps",
                "-f",
                "name=_db",
                "-q",
            ], stdout=subprocess.PIPE)
            ids = result.stdout.decode('utf-8').strip().split('\n')
            if len(ids) > 1:
                raise Exception(
                    "Can't find the DB, couldn't guess container name (tried '%s'), "
                    "and too much match with '_db'" % get_guessed_container_name())
            if len(ids) == 0 or len(ids[0]) == 0:
                raise Exception("Can't find the DB")
        result = subprocess.run([
            "docker",
            "inspect",
            "-f",
            "'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'",
            ids[0],
        ], stdout=subprocess.PIPE)
        return result.stdout.decode('utf-8').strip().replace("'", "")
    
    
    def get_guessed_container_name():
        return str(os.path.dirname(__file__).split(os.path.sep)[-2]).lower().replace('-', '') + "_db"
    
    
    if __name__ == "__main__":
        print(get_db_ip())