Skip to content
Snippets Groups Projects
Commit 3dbb8475 authored by Nikolay Stanchev's avatar Nikolay Stanchev
Browse files

Implements database connectivity using PostgreSQL for the CLMC service

parent 7025a194
No related branches found
No related tags found
No related merge requests found
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
""" """
from pyramid.config import Configurator from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from clmcservice.models import DBSession, Base
from clmcservice.aggregationapi.utilities import validate_conf_file, RUNNING_FLAG, MALFORMED_FLAG, CONF_FILE_ATTRIBUTE, CONF_OBJECT, AGGREGATOR_CONFIG_SECTION from clmcservice.aggregationapi.utilities import validate_conf_file, RUNNING_FLAG, MALFORMED_FLAG, CONF_FILE_ATTRIBUTE, CONF_OBJECT, AGGREGATOR_CONFIG_SECTION
...@@ -31,6 +33,10 @@ def main(global_config, **settings): ...@@ -31,6 +33,10 @@ def main(global_config, **settings):
This function returns a Pyramid WSGI application. This function returns a Pyramid WSGI application.
""" """
engine = engine_from_config(settings, 'sqlalchemy.') # initialise a database engine by using the 'sqlalchemy' setting in the configuration .ini file
DBSession.configure(bind=engine) # bind the engine to a DB session
Base.metadata.bind = engine # bind the engine to the Base class metadata
# validate and use (if valid) the configuration file # validate and use (if valid) the configuration file
conf_file_path = settings[CONF_FILE_ATTRIBUTE] conf_file_path = settings[CONF_FILE_ATTRIBUTE]
conf = validate_conf_file(conf_file_path) # if None returned here, service is in unconfigured state conf = validate_conf_file(conf_file_path) # if None returned here, service is in unconfigured state
......
import os
import sys
from sqlalchemy import engine_from_config
from pyramid.paster import get_appsettings, setup_logging
from clmcservice.models import DBSession, Base
def usage(argv):
"""
A method to be called when the script has been used in an incorrect way.
:param argv: cmd arguments
"""
cmd = os.path.basename(argv[0])
print('usage: %s <config_uri>\n'
'(example: "%s development.ini")' % (cmd, cmd))
sys.exit(1)
def main(argv=sys.argv):
"""
Main method of the script - initialises the database by creating all tables declared in the models.py module
:param argv: command line arguments - expects a configuration .ini file from which it retrieves the URL with which to connect to postgresql
"""
if len(argv) != 2:
usage(argv) # in case of wrong usage
config_uri = argv[1]
setup_logging(config_uri)
settings = get_appsettings(config_uri) # get application specific settings
engine = engine_from_config(settings, 'sqlalchemy.') # create the db engine from the sqlalchemy setting configured in the .ini file
DBSession.configure(bind=engine)
Base.metadata.create_all(engine) # creates all model tables
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
from zope.sqlalchemy import ZopeTransactionExtension
from sqlalchemy import Column, String, Integer, UniqueConstraint
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension())) # initialise a ORM session, ought to be reused across the different modules
Base = declarative_base() # initialise a declarative Base instance to use for the web app models
class ServiceFunctionEndpoint(Base):
"""
This class defines the main model of the WHOAMI API, declaring the global tags for a specific service function on a specific endpoint.
"""
__tablename__ = 'sfendpoints' # table name in the PostgreSQL database
__table_args__ = (UniqueConstraint('sf_i', 'sf_endpoint', 'sr'),) # defines a unique constraint across 3 columns - sf_i, sf_endpoint, sr
uid = Column(Integer, primary_key=True, autoincrement=True) # a primary key integer field (auto incremented)
location = Column(String) # cluster label
sfc = Column(String) # service function chain label
sfc_i = Column(String) # service function chain instance identifier
sf = Column(String) # service function label
sf_i = Column(String) # service function identifier (potentially FQDN)
sf_endpoint = Column(String) # service function endpoint (potentially IP address)
sr = Column(String) # service router ID - service router that connects the VM to FLAME
...@@ -14,9 +14,11 @@ pyramid.default_locale_name = en ...@@ -14,9 +14,11 @@ pyramid.default_locale_name = en
pyramid.includes = pyramid_debugtoolbar pyramid_exclog pyramid.includes = pyramid_debugtoolbar pyramid_exclog
exclog.ignore = exclog.ignore =
## Configuration file path # Configuration file path
configuration_file_path = /etc/flame/clmc/service.conf configuration_file_path = /etc/flame/clmc/service.conf
# PostgreSQL connection url
sqlalchemy.url = postgresql://clmc:clmc_service@localhost:5432/whoamidb
# By default, the toolbar only appears for clients from IP addresses # By default, the toolbar only appears for clients from IP addresses
# '127.0.0.1' and '::1'. # '127.0.0.1' and '::1'.
...@@ -36,7 +38,7 @@ listen = localhost:9080 ...@@ -36,7 +38,7 @@ listen = localhost:9080
### ###
[loggers] [loggers]
keys = root, exc_logger, clmcservice keys = root, exc_logger, service_logger, sqlalchemy.engine.base.Engine
[handlers] [handlers]
keys = console, filelog, exc_handler keys = console, filelog, exc_handler
...@@ -48,7 +50,12 @@ keys = generic, exc_formatter ...@@ -48,7 +50,12 @@ keys = generic, exc_formatter
level = INFO level = INFO
handlers = console handlers = console
[logger_clmcservice] [logger_sqlalchemy.engine.base.Engine]
level = INFO
handlers =
qualname = sqlalchemy.engine.base.Engine
[logger_service_logger]
level = INFO level = INFO
handlers = filelog handlers = filelog
qualname = service_logger qualname = service_logger
......
...@@ -14,9 +14,11 @@ pyramid.default_locale_name = en ...@@ -14,9 +14,11 @@ pyramid.default_locale_name = en
pyramid.includes = pyramid_exclog pyramid.includes = pyramid_exclog
exclog.ignore = exclog.ignore =
## Configuration file path # Configuration file path
configuration_file_path = /etc/flame/clmc/service.conf configuration_file_path = /etc/flame/clmc/service.conf
# PostgreSQL connection url
sqlalchemy.url = postgresql://clmc:clmc_service@localhost:5432/whoamidb
### ###
# wsgi server configuration # wsgi server configuration
...@@ -32,7 +34,7 @@ listen = *:9080 ...@@ -32,7 +34,7 @@ listen = *:9080
### ###
[loggers] [loggers]
keys = root, exc_logger, service_logger keys = root, exc_logger, service_logger, sqlalchemy.engine.base.Engine
[handlers] [handlers]
keys = console, filelog, exc_handler keys = console, filelog, exc_handler
...@@ -44,6 +46,11 @@ keys = generic, exc_formatter ...@@ -44,6 +46,11 @@ keys = generic, exc_formatter
level = INFO level = INFO
handlers = console handlers = console
[logger_sqlalchemy.engine.base.Engine]
level = INFO
handlers =
qualname = sqlalchemy.engine.base.Engine
[logger_service_logger] [logger_service_logger]
level = INFO level = INFO
handlers = filelog handlers = filelog
......
...@@ -46,6 +46,9 @@ requires = [ ...@@ -46,6 +46,9 @@ requires = [
'pyramid_debugtoolbar', 'pyramid_debugtoolbar',
'pyramid_exclog', 'pyramid_exclog',
'waitress', 'waitress',
'sqlalchemy',
'zope.sqlalchemy',
'psycopg2',
'influxdb', 'influxdb',
'pytest', 'pytest',
] ]
...@@ -80,5 +83,8 @@ setup( ...@@ -80,5 +83,8 @@ setup(
'paste.app_factory': [ 'paste.app_factory': [
'main = clmcservice:main', 'main = clmcservice:main',
], ],
'console_scripts': [
'initialize_clmcservice_db = clmcservice.initialize_db:main',
]
}, },
) )
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment