Commit b6b9d698 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Context compoent:

- progress on migration to CockroachDB (partial)
parent 177e96a8
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -36,14 +36,16 @@ cd $PROJECTDIR/src
#export REDIS_SERVICE_HOST=$(kubectl get node $TFS_K8S_HOSTNAME -o 'jsonpath={.status.addresses[?(@.type=="InternalIP")].address}')
#export REDIS_SERVICE_PORT=$(kubectl --namespace $TFS_K8S_NAMESPACE get service redis-tests -o 'jsonpath={.spec.ports[?(@.port==6379)].nodePort}')

export CRDB_URI="cockroachdb://tfs:tfs123@10.1.7.195:26257/tfs?sslmode=require"
#export CRDB_URI="cockroachdb://tfs:tfs123@127.0.0.1:26257/tfs_test?sslmode=require"
export CRDB_URI="cockroachdb://tfs:tfs123@10.1.7.195:26257/tfs_test?sslmode=require"
export PYTHONPATH=/home/tfs/tfs-ctrl/src

# Run unitary tests and analyze coverage of code at same time
#coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose --maxfail=1 \
#    context/tests/test_unitary.py

pytest --log-level=INFO --verbose -o log_cli=true --maxfail=1 \
# --log-level=INFO -o log_cli=true
pytest --verbose --maxfail=1 --durations=0 \
    context/tests/test_unitary.py

#kubectl --namespace $TFS_K8S_NAMESPACE delete service redis-tests
+272 −369

File changed.

Preview size limit exceeded, changes collapsed.

+15 −5
Original line number Diff line number Diff line
@@ -20,21 +20,31 @@ LOGGER = logging.getLogger(__name__)
APP_NAME = 'tfs'

class Engine:
    def get_engine(self) -> sqlalchemy.engine.Engine:
    @staticmethod
    def get_engine() -> sqlalchemy.engine.Engine:
        crdb_uri = get_setting('CRDB_URI')

        try:
            engine = sqlalchemy.create_engine(
                crdb_uri, connect_args={'application_name': APP_NAME}, echo=False, future=True)
                crdb_uri, connect_args={'application_name': APP_NAME}, echo=True, future=True)
        except: # pylint: disable=bare-except
            LOGGER.exception('Failed to connect to database: {:s}'.format(crdb_uri))
            return None

        try:
            if not sqlalchemy_utils.database_exists(engine.url):
                sqlalchemy_utils.create_database(engine.url)
            Engine.create_database(engine)
        except: # pylint: disable=bare-except
            LOGGER.exception('Failed to check/create to database: {:s}'.format(crdb_uri))
            LOGGER.exception('Failed to check/create to database: {:s}'.format(engine.url))
            return None

        return engine

    @staticmethod
    def create_database(engine : sqlalchemy.engine.Engine) -> None:
        if not sqlalchemy_utils.database_exists(engine.url):
            sqlalchemy_utils.create_database(engine.url)

    @staticmethod
    def drop_database(engine : sqlalchemy.engine.Engine) -> None:
        if sqlalchemy_utils.database_exists(engine.url):
            sqlalchemy_utils.drop_database(engine.url)
+1 −1
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ def main():
    metrics_port = get_metrics_port()
    start_http_server(metrics_port)

    db_engine = Engine().get_engine()
    db_engine = Engine.get_engine()
    rebuild_database(db_engine, drop_if_exists=False)

    # Get message broker instance
+13 −29
Original line number Diff line number Diff line
@@ -12,15 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from typing import Dict
from typing import Dict, List
from sqlalchemy import Column, Float, String
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
from ._Base import _Base

LOGGER = logging.getLogger(__name__)

class ContextModel(_Base):
    __tablename__ = 'context'
    context_uuid = Column(UUID(as_uuid=False), primary_key=True)
@@ -28,33 +25,20 @@ class ContextModel(_Base):
    created_at   = Column(Float)

    topology = relationship('TopologyModel', back_populates='context')
    #service  = relationship('ServiceModel', back_populates='context')
    #slice    = relationship('SliceModel', back_populates='context')

    def dump_id(self) -> Dict:
        return {'context_uuid': {'uuid': self.context_uuid}}

    #@staticmethod
    #def main_pk_name():
    #    return 'context_uuid'

    """    
    def dump_service_ids(self) -> List[Dict]:
        from .ServiceModel import ServiceModel # pylint: disable=import-outside-toplevel
        db_service_pks = self.references(ServiceModel)
        return [ServiceModel(self.database, pk).dump_id() for pk,_ in db_service_pks]

    def dump_topology_ids(self) -> List[Dict]:
        from .TopologyModel import TopologyModel # pylint: disable=import-outside-toplevel
        db_topology_pks = self.references(TopologyModel)
        return [TopologyModel(self.database, pk).dump_id() for pk,_ in db_topology_pks]
    """

    def dump(self,
        include_services : bool = True,     # pylint: disable=arguments-differ
        include_slices : bool = True,       # pylint: disable=arguments-differ
        include_topologies : bool = True    # pylint: disable=arguments-differ
    ) -> Dict:
        result = {'context_id': self.dump_id(), 'name': self.context_name}
        # if include_services: result['service_ids'] = self.dump_service_ids()
        # if include_slices: result['slice_ids'] = self.dump_slice_ids()
        # if include_topologies: result['topology_ids'] = self.dump_topology_ids()
        return result
        return 

    def dump(self) -> Dict:
        return {
            'context_id'  : self.dump_id(),
            'name'        : self.context_name,
            'topology_ids': [obj.dump_id() for obj in self.topology],
            #'service_ids' : [obj.dump_id() for obj in self.service ],
            #'slice_ids'   : [obj.dump_id() for obj in self.slice   ],
        }
Loading