# Path: src/myaas/backends/base.py
# class AbstractDatabase(PersistentContainerService, metaclass=ABCMeta):
#     """Abstract implementation for a database backend"""
# 
#     WAIT_MAX_TRIES = 30
#     not_found_exception_class = NonExistentDatabase
# 
#     def __init__(self, client, template, name, create=False, ttl=settings.CONTAINER_TTL):  # noqa
#         container_name = self._make_container_name(template, name)
# 
#         super().__init__(client, container_name)
# 
#         self.template = template
#         self.name = name
#         self.create = create
#         self.ttl = int(ttl)
# 
#         if not self.container:
#             if not self.create:
#                 raise self.not_found_exception_class()
#             self.container = self.create_container(self.image)
# 
#     @property
#     def user(self):
#         return ''
# 
#     @property
#     def password(self):
#         return ''
# 
#     @abstractproperty
#     def datadir(self):
#         pass
# 
#     @abstractproperty
#     def image(self):
#         pass
# 
#     @abstractproperty
#     def provider_name(self):
#         pass
# 
#     @abstractproperty
#     def service_port(self):
#         pass
# 
#     @property
#     def internal_ip(self):
#         return self.inspect()['NetworkSettings']['IPAddress']
# 
#     @property
#     def restart_policy(self):
#         return {"MaximumRetryCount": 5, "Name": "on-failure"}
# 
#     @property
#     def ports(self):
#         return [self.service_port]
# 
#     @property
#     def labels(self):
#         labels = {
#             'com.myaas.provider': self.provider_name,
#             'com.myaas.is_template': 'False',
#             'com.myaas.template': self.template,
#             'com.myaas.instance': self.name,
#             'com.myaas.username': self.user,
#             'com.myaas.password': self.password,
#         }
#         if self.ttl and self.ttl > 0:
#             expire_at = datetime.now() + timedelta(seconds=self.ttl)
#             labels.update({'com.myaas.expiresAt': str(expire_at.timestamp())})
#         return labels
# 
#     @property
#     def host_port(self):
#         port_name = f'{self.service_port}/tcp'
#         ports = self.inspect()['NetworkSettings']['Ports']
#         if not ports:
#             return None
#         elif port_name not in ports:
#             return None
#         return ports[port_name][0]["HostPort"]
# 
#     def test_connection(self, timeout=1):
#         """
#         Checks if the service running inside the container is accepting connections
#         """
#         if not self.service_port:
#             raise NotReachableException("Could not find container port")
#         if not self.internal_ip:
#             raise NotReachableException("Could not find container IP, is container running?")
# 
#         return test_tcp_connection(self.internal_ip, self.service_port)
# 
#     def wait_for_service_listening(self):
#         tries = 0
#         while tries < self.WAIT_MAX_TRIES:
#             if self.test_connection():
#                 return
#             sleep(5)
#             tries += 1
# 
#         raise DBTimeoutException("Could not connect with database, max retries reached")
# 
#     def _make_container_name(self, template, name):
#         container_name = settings.CONTAINER_PREFIX + template
#         if name:
#             container_name += '-' + name
#         return container_name
# 
# class AbstractDatabaseTemplate(AbstractDatabase):
#     """
#     Abstract implementation of a template database
#     """
#     WAIT_MAX_TRIES = 60
# 
#     not_found_exception_class = NonExistentTemplate
# 
#     def __init__(self, docker_client, template, create=False):
#         super().__init__(docker_client, template, None, create, ttl=0)
# 
#     def clone(self, name, ttl=None):
#         if self.running():
#             raise ImportInProgress
# 
#         database = self.database_backend(
#             self.client, self.template,
#             name, create=True, ttl=ttl)
# 
#         fs = FileSystem(settings.DATA_DIR)
# 
#         pre_existent_volume = fs.find_subvolume_by_name(database.container_name)
#         if pre_existent_volume:
#             pre_existent_volume.delete()
# 
#         subvolume = fs.find_subvolume_by_name(self.container_name)
#         subvolume.take_snapshot(database.host_datadir)
# 
#         return database
# 
#     @abstractmethod
#     def import_data(self, sql_file):
#         pass
# 
#     @abstractmethod
#     def get_engine_status(self):
#         pass
# 
#     @abstractproperty
#     def database_backend(self):
#         pass
# 
#     @property
#     def labels(self):
#         return {
#             'com.myaas.is_template': 'True',
#             'com.myaas.provider': self.provider_name,
#             'com.myaas.template': self.template,
#         }
# 
#     @property
#     def restart_policy(self):
#         return {"MaximumRetryCount": 0, "Name": "no"}
# 
#     def _run_command(self, command, stdin=None, env=None):
#         proc = subprocess.Popen(command,
#                                 stdin=stdin,
#                                 stderr=subprocess.PIPE,
#                                 stdout=subprocess.PIPE,
#                                 universal_newlines=True,
#                                 env=env)
#         out, err = proc.communicate()
#         return (out, err)
# 
# Path: src/myaas/backends/exceptions.py
# class ImportDataError(Exception):
#     pass


import os
from .. import settings
from .base import AbstractDatabase, AbstractDatabaseTemplate
from .exceptions import ImportDataError



class Database(AbstractDatabase):
    @property
    def provider_name(self):
        return "postgres"


# Path: src/myaas/backends/base.py
# class AbstractDatabase(PersistentContainerService, metaclass=ABCMeta):
#     """Abstract implementation for a database backend"""
# 
#     WAIT_MAX_TRIES = 30
#     not_found_exception_class = NonExistentDatabase
# 
#     def __init__(self, client, template, name, create=False, ttl=settings.CONTAINER_TTL):  # noqa
#         container_name = self._make_container_name(template, name)
# 
#         super().__init__(client, container_name)
# 
#         self.template = template
#         self.name = name
#         self.create = create
#         self.ttl = int(ttl)
# 
#         if not self.container:
#             if not self.create:
#                 raise self.not_found_exception_class()
#             self.container = self.create_container(self.image)
# 
#     @property
#     def user(self):
#         return ''
# 
#     @property
#     def password(self):
#         return ''
# 
#     @abstractproperty
#     def datadir(self):
#         pass
# 
#     @abstractproperty
#     def image(self):
#         pass
# 
#     @abstractproperty
#     def provider_name(self):
#         pass
# 
#     @abstractproperty
#     def service_port(self):
#         pass
# 
#     @property
#     def internal_ip(self):
#         return self.inspect()['NetworkSettings']['IPAddress']
# 
#     @property
#     def restart_policy(self):
#         return {"MaximumRetryCount": 5, "Name": "on-failure"}
# 
#     @property
#     def ports(self):
#         return [self.service_port]
# 
#     @property
#     def labels(self):
#         labels = {
#             'com.myaas.provider': self.provider_name,
#             'com.myaas.is_template': 'False',
#             'com.myaas.template': self.template,
#             'com.myaas.instance': self.name,
#             'com.myaas.username': self.user,
#             'com.myaas.password': self.password,
#         }
#         if self.ttl and self.ttl > 0:
#             expire_at = datetime.now() + timedelta(seconds=self.ttl)
#             labels.update({'com.myaas.expiresAt': str(expire_at.timestamp())})
#         return labels
# 
#     @property
#     def host_port(self):
#         port_name = f'{self.service_port}/tcp'
#         ports = self.inspect()['NetworkSettings']['Ports']
#         if not ports:
#             return None
#         elif port_name not in ports:
#             return None
#         return ports[port_name][0]["HostPort"]
# 
#     def test_connection(self, timeout=1):
#         """
#         Checks if the service running inside the container is accepting connections
#         """
#         if not self.service_port:
#             raise NotReachableException("Could not find container port")
#         if not self.internal_ip:
#             raise NotReachableException("Could not find container IP, is container running?")
# 
#         return test_tcp_connection(self.internal_ip, self.service_port)
# 
#     def wait_for_service_listening(self):
#         tries = 0
#         while tries < self.WAIT_MAX_TRIES:
#             if self.test_connection():
#                 return
#             sleep(5)
#             tries += 1
# 
#         raise DBTimeoutException("Could not connect with database, max retries reached")
# 
#     def _make_container_name(self, template, name):
#         container_name = settings.CONTAINER_PREFIX + template
#         if name:
#             container_name += '-' + name
#         return container_name
# 
# class AbstractDatabaseTemplate(AbstractDatabase):
#     """
#     Abstract implementation of a template database
#     """
#     WAIT_MAX_TRIES = 60
# 
#     not_found_exception_class = NonExistentTemplate
# 
#     def __init__(self, docker_client, template, create=False):
#         super().__init__(docker_client, template, None, create, ttl=0)
# 
#     def clone(self, name, ttl=None):
#         if self.running():
#             raise ImportInProgress
# 
#         database = self.database_backend(
#             self.client, self.template,
#             name, create=True, ttl=ttl)
# 
#         fs = FileSystem(settings.DATA_DIR)
# 
#         pre_existent_volume = fs.find_subvolume_by_name(database.container_name)
#         if pre_existent_volume:
#             pre_existent_volume.delete()
# 
#         subvolume = fs.find_subvolume_by_name(self.container_name)
#         subvolume.take_snapshot(database.host_datadir)
# 
#         return database
# 
#     @abstractmethod
#     def import_data(self, sql_file):
#         pass
# 
#     @abstractmethod
#     def get_engine_status(self):
#         pass
# 
#     @abstractproperty
#     def database_backend(self):
#         pass
# 
#     @property
#     def labels(self):
#         return {
#             'com.myaas.is_template': 'True',
#             'com.myaas.provider': self.provider_name,
#             'com.myaas.template': self.template,
#         }
# 
#     @property
#     def restart_policy(self):
#         return {"MaximumRetryCount": 0, "Name": "no"}
# 
#     def _run_command(self, command, stdin=None, env=None):
#         proc = subprocess.Popen(command,
#                                 stdin=stdin,
#                                 stderr=subprocess.PIPE,
#                                 stdout=subprocess.PIPE,
#                                 universal_newlines=True,
#                                 env=env)
#         out, err = proc.communicate()
#         return (out, err)
# 
# Path: src/myaas/backends/exceptions.py
# class ImportDataError(Exception):
#     pass


# Path: src/myaas/backends/postgres.py
import os
from .. import settings
from .base import AbstractDatabase, AbstractDatabaseTemplate
from .exceptions import ImportDataError




class Database(AbstractDatabase):
    @property
    def provider_name(self):
        return "postgres"


    @property