# Path: postgraas_server/backends/docker/postgres_instance_driver.py
# def get_hostname():
# def _docker_client():
# def get_open_port():
# def get_container_by_name(postgraas_instance_name):
# def check_container_exists(postgraas_instance_name):
# def create_postgres_instance(postgraas_instance_name, connection_dict):
# def delete_postgres_instance(container_id):
# 
# Path: postgraas_server/configuration.py
# def get_config(config_filename=None, secrets_file='/secrets'):
#     config_filename = config_filename or get_default_config_filename()
#     logger.debug('config filename: {}'.format(config_filename))
#     with open(config_filename, 'r') as cfg:
#         config = json.load(cfg)
#     secrets = _load_secrets(filename=secrets_file)
#     if secrets: # pragma: no cover
#         try:
#             import secure_config.secrets as sec
#             config = sec.load_secret_dict(password=secrets['encryption_key'], config_dict=config)
#         except ImportError as e:
#             logger.debug('secure_config not installed')
# 
#     return config
# 
# def get_user(config):
#     try:
#         server = config['metadb']['server']
#         username = '@'.join([config['metadb']['db_username'], server])
#     except KeyError:
#         username = config['metadb']['db_username']
#     return username
# 
# def get_password(config):
#     try:
#         password = config['metadb']['db_pwd'].decrypt()
#     except AttributeError:
#         password = config['metadb']['db_pwd']
#     return password
# 
# Path: postgraas_server/utils.py
# def wait_for_postgres(dbname, user, password, host, port):
#     """
#     Try to connect to postgres every second, until it succeeds.
#     """
#     for i in range(540):
#         try:
#             psycopg2.connect(dbname=dbname, user=user, password=password, host=host, port=port)
#             return
#         except psycopg2.OperationalError as error:
#             print(i, " ..waiting for db, error: {}".format(error))
#             time.sleep(1)


from postgraas_server.backends.docker import postgres_instance_driver as pg
from postgraas_server.configuration import get_config, get_user, get_password
from postgraas_server.utils import wait_for_postgres


def create_db_container():
    config = get_config()
    print(config)
    db_credentials = {
        "db_name": config['metadb']['db_name'],
        "db_username": get_user(config),
        "db_pwd": get_password(config),
        "host": config['metadb']['host'],
        "port": config['metadb']['port']
    }
    if pg.check_container_exists('postgraas_master_db'):
        print("warning container already exists")

# Path: postgraas_server/backends/docker/postgres_instance_driver.py
# def get_hostname():
# def _docker_client():
# def get_open_port():
# def get_container_by_name(postgraas_instance_name):
# def check_container_exists(postgraas_instance_name):
# def create_postgres_instance(postgraas_instance_name, connection_dict):
# def delete_postgres_instance(container_id):
# 
# Path: postgraas_server/configuration.py
# def get_config(config_filename=None, secrets_file='/secrets'):
#     config_filename = config_filename or get_default_config_filename()
#     logger.debug('config filename: {}'.format(config_filename))
#     with open(config_filename, 'r') as cfg:
#         config = json.load(cfg)
#     secrets = _load_secrets(filename=secrets_file)
#     if secrets: # pragma: no cover
#         try:
#             import secure_config.secrets as sec
#             config = sec.load_secret_dict(password=secrets['encryption_key'], config_dict=config)
#         except ImportError as e:
#             logger.debug('secure_config not installed')
# 
#     return config
# 
# def get_user(config):
#     try:
#         server = config['metadb']['server']
#         username = '@'.join([config['metadb']['db_username'], server])
#     except KeyError:
#         username = config['metadb']['db_username']
#     return username
# 
# def get_password(config):
#     try:
#         password = config['metadb']['db_pwd'].decrypt()
#     except AttributeError:
#         password = config['metadb']['db_pwd']
#     return password
# 
# Path: postgraas_server/utils.py
# def wait_for_postgres(dbname, user, password, host, port):
#     """
#     Try to connect to postgres every second, until it succeeds.
#     """
#     for i in range(540):
#         try:
#             psycopg2.connect(dbname=dbname, user=user, password=password, host=host, port=port)
#             return
#         except psycopg2.OperationalError as error:
#             print(i, " ..waiting for db, error: {}".format(error))
#             time.sleep(1)


# Path: postgraas_server/backends/docker/create_db.py
from postgraas_server.backends.docker import postgres_instance_driver as pg
from postgraas_server.configuration import get_config, get_user, get_password
from postgraas_server.utils import wait_for_postgres



def create_db_container():
    config = get_config()
    print(config)
    db_credentials = {
        "db_name": config['metadb']['db_name'],
        "db_username": get_user(config),
        "db_pwd": get_password(config),
        "host": config['metadb']['host'],
        "port": config['metadb']['port']
    }
    if pg.check_container_exists('postgraas_master_db'):
        print("warning container already exists")

        postgraas_db = pg.get_container_by_name('postgraas_master_db')