# Path: zoe_lib/config.py
# def get_conf() -> Namespace:
#     """Returns the conf singleton."""
#     return _CONF
# 
# Path: zoe_lib/state/base.py
# class BaseTable:
#     """Common abstraction for all tables."""
#     def __init__(self, sql_manager, table_name):
#         self.table_name = table_name
#         self.sql_manager = sql_manager
#         self.cursor = self.sql_manager.cursor()
# 
#     def create(self):
#         """Create this table."""
#         raise NotImplementedError
# 
#     def insert(self, **kwargs):
#         """Create a new record."""
#         raise NotImplementedError
# 
#     def delete(self, record_id):
#         """Delete a record from this table."""
#         query = 'DELETE FROM "{}" WHERE id = %s'.format(self.table_name)
#         self.cursor.execute(query, (record_id,))
#         self.sql_manager.commit()
# 
#     def update(self, record_id, **kwargs):
#         """Update the state of an execution."""
#         arg_list = []
#         value_list = []
#         for key, value in kwargs.items():
#             arg_list.append('{} = %s'.format(key))
#             value_list.append(value)
#         set_q = ", ".join(arg_list)
#         value_list.append(record_id)
#         q_base = 'UPDATE "{}" SET '.format(self.table_name) + set_q + ' WHERE id=%s'
#         query = self.cursor.mogrify(q_base, value_list)
#         self.cursor.execute(query)
#         self.sql_manager.commit()
# 
#     def select(self, only_one=False, limit=-1, **kwargs):
#         """Select records."""
#         raise NotImplementedError
# 
# class BaseRecord:
#     """
#     :type sql_manager: SQLManager
#     """
#     def __init__(self, d, sql_manager):
#         """
#         :type sql_manager: SQLManager
#         """
#         self.sql_manager = sql_manager
#         self.id = d['id']
# 
#     def serialize(self):
#         """Generates a dictionary that can be serialized in JSON."""
#         raise NotImplementedError


import logging
from zoe_lib.config import get_conf
from zoe_lib.state.base import BaseTable, BaseRecord
        """The service could not be created/started."""
        self.sql_manager.services.update(self.id, status=self.ERROR_STATUS, error_message=error_message)
        self.status = self.ERROR_STATUS
        self.error_message = error_message

    def set_backend_status(self, new_status):
        """The backend status of the service has changed."""
        log.debug("service {}, backend status updated to {}".format(self.id, new_status))
        self.backend_status = new_status
        if self.is_dead():
            for port in self.ports:
                port.reset()
            self.ip_address = None
            self.sql_manager.services.update(self.id, backend_status=new_status, ip_address=None)
            if new_status == self.BACKEND_DESTROY_STATUS:
                self.backend_id = None
                self.sql_manager.services.update(self.id, backend_status=new_status, backend_id=None, ip_address=None)
            else:
                self.sql_manager.services.update(self.id, backend_status=new_status, ip_address=None)
        else:
            self.sql_manager.services.update(self.id, backend_status=new_status)

    def assign_backend_host(self, backend_host):
        """Assign this service to a host in particular."""
        self.sql_manager.services.update(self.id, backend_host=backend_host)
        self.backend_host = backend_host

    @property
    def dns_name(self):
        """Getter for the DNS name of this service as it will be registered in Docker's DNS."""

# Path: zoe_lib/config.py
# def get_conf() -> Namespace:
#     """Returns the conf singleton."""
#     return _CONF
# 
# Path: zoe_lib/state/base.py
# class BaseTable:
#     """Common abstraction for all tables."""
#     def __init__(self, sql_manager, table_name):
#         self.table_name = table_name
#         self.sql_manager = sql_manager
#         self.cursor = self.sql_manager.cursor()
# 
#     def create(self):
#         """Create this table."""
#         raise NotImplementedError
# 
#     def insert(self, **kwargs):
#         """Create a new record."""
#         raise NotImplementedError
# 
#     def delete(self, record_id):
#         """Delete a record from this table."""
#         query = 'DELETE FROM "{}" WHERE id = %s'.format(self.table_name)
#         self.cursor.execute(query, (record_id,))
#         self.sql_manager.commit()
# 
#     def update(self, record_id, **kwargs):
#         """Update the state of an execution."""
#         arg_list = []
#         value_list = []
#         for key, value in kwargs.items():
#             arg_list.append('{} = %s'.format(key))
#             value_list.append(value)
#         set_q = ", ".join(arg_list)
#         value_list.append(record_id)
#         q_base = 'UPDATE "{}" SET '.format(self.table_name) + set_q + ' WHERE id=%s'
#         query = self.cursor.mogrify(q_base, value_list)
#         self.cursor.execute(query)
#         self.sql_manager.commit()
# 
#     def select(self, only_one=False, limit=-1, **kwargs):
#         """Select records."""
#         raise NotImplementedError
# 
# class BaseRecord:
#     """
#     :type sql_manager: SQLManager
#     """
#     def __init__(self, d, sql_manager):
#         """
#         :type sql_manager: SQLManager
#         """
#         self.sql_manager = sql_manager
#         self.id = d['id']
# 
#     def serialize(self):
#         """Generates a dictionary that can be serialized in JSON."""
#         raise NotImplementedError


# Path: zoe_lib/state/service.py
import logging
from zoe_lib.config import get_conf
from zoe_lib.state.base import BaseTable, BaseRecord

        """The service could not be created/started."""
        self.sql_manager.services.update(self.id, status=self.ERROR_STATUS, error_message=error_message)
        self.status = self.ERROR_STATUS
        self.error_message = error_message

    def set_backend_status(self, new_status):
        """The backend status of the service has changed."""
        log.debug("service {}, backend status updated to {}".format(self.id, new_status))
        self.backend_status = new_status
        if self.is_dead():
            for port in self.ports:
                port.reset()
            self.ip_address = None
            self.sql_manager.services.update(self.id, backend_status=new_status, ip_address=None)
            if new_status == self.BACKEND_DESTROY_STATUS:
                self.backend_id = None
                self.sql_manager.services.update(self.id, backend_status=new_status, backend_id=None, ip_address=None)
            else:
                self.sql_manager.services.update(self.id, backend_status=new_status, ip_address=None)
        else:
            self.sql_manager.services.update(self.id, backend_status=new_status)

    def assign_backend_host(self, backend_host):
        """Assign this service to a host in particular."""
        self.sql_manager.services.update(self.id, backend_host=backend_host)
        self.backend_host = backend_host

    @property
    def dns_name(self):
        """Getter for the DNS name of this service as it will be registered in Docker's DNS."""

        return "{}-{}-{}".format(self.name, self.execution_id, get_conf().deployment_name)