Skip to content
Snippets Groups Projects
Commit 5ceac689 authored by sveseli's avatar sveseli
Browse files

add first functional DS web service with user, experiment and auth controllers...

add first functional DS web service with user, experiment and auth controllers that allow retrieving user and experiment information from the db, start/stop experiment, and principal authorization; added corresponding API and CLI classes; added storage manager skeleton
parent 56e604fc
No related branches found
No related tags found
No related merge requests found
Showing
with 636 additions and 0 deletions
#!/usr/bin/env python
from dm.common.constants import dmRole
from dm.common.objects.authorizationPrincipal import AuthorizationPrincipal
from dm.common.db.api.userDbApi import UserDbApi
from dm.common.service.auth.authorizationPrincipalRetriever import AuthorizationPrincipalRetriever
from dm.ds_web_service.api.dsRestApiFactory import DsRestApiFactory
class DsAuthPrincipalRetriever(AuthorizationPrincipalRetriever):
def __init__(self):
AuthorizationPrincipalRetriever.__init__(self, self.__class__.__name__)
self.authApi = DsRestApiFactory.getAuthRestApi()
def getAuthorizationPrincipal(self, username):
principal = self.authApi.getAuthorizationPrincipal(username)
return principal
#######################################################################
# Testing.
if __name__ == '__main__':
pass
#!/usr/bin/env python
#
# Auth route descriptor.
#
from dm.common.utility.configurationManager import ConfigurationManager
from authSessionController import AuthSessionController
class AuthRouteDescriptor:
@classmethod
def getRoutes(cls):
contextRoot = ConfigurationManager.getInstance().getContextRoot()
authSessionController = AuthSessionController()
routes = [
# Get authorization principal
{
'name' : 'getAuthorizationPrincipal',
'path' : '%s/authorizationPrincipals/:(username)' % contextRoot,
'controller' : authSessionController,
'action' : 'getAuthorizationPrincipal',
'method' : [ 'GET' ]
},
]
return routes
#authenticateUser!/usr/bin/env python
import cherrypy
from dm.common.service.dmSessionController import DmSessionController
from dm.ds_web_service.service.impl.authSessionControllerImpl import AuthSessionControllerImpl
class AuthSessionController(DmSessionController):
def __init__(self):
DmSessionController.__init__(self)
self.authSessionControllerImpl = AuthSessionControllerImpl()
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getAuthorizationPrincipal(self, username, **kwargs):
if not len(username):
raise InvalidRequest('Invalid username provided.')
response = self.authSessionControllerImpl.getAuthorizationPrincipal(username).getFullJsonRep()
self.logger.debug('Returning authorization principal for %s' % (username))
return response
#!/usr/bin/env python
#
# DM DS Web Service
#
####################################################################
from dm.common.service.dmRestWebServiceBase import DmRestWebServiceBase
from dm.common.utility.dmModuleManager import DmModuleManager
from dm.common.utility.configurationManager import ConfigurationManager
from dm.ds_web_service.service.impl.storageManager import StorageManager
from dsWebServiceRouteMapper import DsWebServiceRouteMapper
####################################################################
class DsWebService(DmRestWebServiceBase):
def __init__(self):
DmRestWebServiceBase.__init__(self, DsWebServiceRouteMapper)
def initDmModules(self):
self.logger.debug('Initializing dm modules')
# Add modules that will be started.
moduleManager = DmModuleManager.getInstance()
moduleManager.addModule(StorageManager.getInstance())
self.logger.debug('Initialized dm modules')
def getDefaultServerHost(self):
return ConfigurationManager.getInstance().getServiceHost()
def getDefaultServerPort(self):
return ConfigurationManager.getInstance().getServicePort()
####################################################################
# Run service
if __name__ == '__main__':
ConfigurationManager.getInstance().setServiceName('ds-web-service')
service = DsWebService();
service.run()
#!/usr/bin/env python
#
# Route mapper for DM DS web service.
#
import sys
import os
import cherrypy
from dm.common.utility.loggingManager import LoggingManager
from dm.common.utility.configurationManager import ConfigurationManager
from dm.common.service.loginRouteDescriptor import LoginRouteDescriptor
from userRouteDescriptor import UserRouteDescriptor
from experimentRouteDescriptor import ExperimentRouteDescriptor
from authRouteDescriptor import AuthRouteDescriptor
class DsWebServiceRouteMapper:
@classmethod
def setupRoutes(cls):
""" Setup RESTFul routes. """
logger = LoggingManager.getInstance().getLogger(cls.__name__)
contextRoot = ConfigurationManager.getInstance().getContextRoot()
logger.debug('Using context root: %s' % contextRoot)
# Get routes.
routes = LoginRouteDescriptor.getRoutes()
routes += AuthRouteDescriptor.getRoutes()
routes += UserRouteDescriptor.getRoutes()
routes += ExperimentRouteDescriptor.getRoutes()
# Add routes to dispatcher.
d = cherrypy.dispatch.RoutesDispatcher()
for route in routes:
logger.debug('Connecting route: %s' % route)
d.connect(route['name'], route['path'], action=route['action'], controller=route['controller'], conditions=dict(method=route['method']))
return d
#!/usr/bin/env python
#
# User route descriptor.
#
from dm.common.utility.configurationManager import ConfigurationManager
from experimentSessionController import ExperimentSessionController
class ExperimentRouteDescriptor:
@classmethod
def getRoutes(cls):
contextRoot = ConfigurationManager.getInstance().getContextRoot()
# Static instances shared between different routes
experimentSessionController = ExperimentSessionController()
# Define routes.
routes = [
# Get experiment types
{
'name' : 'getExperimentTypes',
'path' : '%s/experimentTypes' % contextRoot,
'controller' : experimentSessionController,
'action' : 'getExperimentTypes',
'method' : ['GET']
},
# Get experiments
{
'name' : 'getExperiments',
'path' : '%s/experiments' % contextRoot,
'controller' : experimentSessionController,
'action' : 'getExperiments',
'method' : ['GET']
},
# Get experiment
{
'name' : 'getExperimentById',
'path' : '%s/experiments/:(id)' % contextRoot,
'controller' : experimentSessionController,
'action' : 'getExperimentById',
'method' : ['GET']
},
# Get experiment
{
'name' : 'getExperimentByName',
'path' : '%s/experimentsByName/:(name)' % contextRoot,
'controller' : experimentSessionController,
'action' : 'getExperimentByName',
'method' : ['GET']
},
# Add experiment
{
'name' : 'addExperiment',
'path' : '%s/experiments' % contextRoot,
'controller' : experimentSessionController,
'action' : 'addExperiment',
'method' : ['POST']
},
# Start experiment
{
'name' : 'startExperiment',
'path' : '%s/experiments/start' % contextRoot,
'controller' : experimentSessionController,
'action' : 'startExperiment',
'method' : ['PUT']
},
# Stop experiment
{
'name' : 'stopExperiment',
'path' : '%s/experiments/stop' % contextRoot,
'controller' : experimentSessionController,
'action' : 'stopExperiment',
'method' : ['PUT']
},
]
return routes
#!/usr/bin/env python
import cherrypy
from dm.common.service.dmSessionController import DmSessionController
from dm.common.exceptions.invalidRequest import InvalidRequest
from dm.common.utility.encoder import Encoder
from dm.ds_web_service.service.impl.experimentSessionControllerImpl import ExperimentSessionControllerImpl
class ExperimentSessionController(DmSessionController):
def __init__(self):
DmSessionController.__init__(self)
self.experimentSessionControllerImpl = ExperimentSessionControllerImpl()
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getExperimentTypes(self, **kwargs):
return self.listToJson(self.experimentSessionControllerImpl.getExperimentTypes())
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getExperiments(self, **kwargs):
return self.listToJson(self.experimentSessionControllerImpl.getExperiments())
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getExperimentByName(self, name, **kwargs):
response = self.experimentSessionControllerImpl.getExperimentByName(name).getFullJsonRep()
self.logger.debug('Returning: %s' % response)
return response
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getExperimentById(self, id, **kwargs):
response = self.experimentSessionControllerImpl.getExperimentByid(id).getFullJsonRep()
self.logger.debug('Returning: %s' % response)
return response
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def addExperiment(self, **kwargs):
name = kwargs.get('name')
if name is None or not len(name):
raise InvalidRequest('Missing experiment name.')
name = Encoder.decode(name)
experimentTypeId = kwargs.get('experimentTypeId')
if experimentTypeId is None:
raise InvalidRequest('Missing experiment type id.')
description = kwargs.get('description')
if description is not None:
description = Encoder.decode(description)
response = self.experimentSessionControllerImpl.addExperiment(name, experimentTypeId, description).getFullJsonRep()
self.logger.debug('Returning: %s' % response)
return response
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def startExperiment(self, **kwargs):
name = kwargs.get('name')
if name is None or not len(name):
raise InvalidRequest('Missing experiment name.')
name = Encoder.decode(name)
response = self.experimentSessionControllerImpl.startExperiment(name).getFullJsonRep()
self.logger.debug('Returning: %s' % response)
return response
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def stopExperiment(self, **kwargs):
name = kwargs.get('name')
if name is None or not len(name):
raise InvalidRequest('Missing experiment name.')
name = Encoder.decode(name)
response = self.experimentSessionControllerImpl.stopExperiment(name).getFullJsonRep()
self.logger.debug('Returning: %s' % response)
return response
#!/usr/bin/env python
#
# Implementation for user info controller.
#
from dm.common.constants import dmRole
from dm.common.objects.authorizationPrincipal import AuthorizationPrincipal
from dm.common.objects.dmObjectManager import DmObjectManager
from dm.common.db.api.userDbApi import UserDbApi
class AuthSessionControllerImpl(DmObjectManager):
""" User info controller implementation class. """
def __init__(self):
DmObjectManager.__init__(self)
self.userDbApi = UserDbApi()
def getAuthorizationPrincipal(self, username):
user = self.userDbApi.getUserWithPasswordByUsername(username)
principal = AuthorizationPrincipal(name=username, token=user.get('password'))
principal.setRole(dmRole.DM_USER_ROLE)
principal.setUserInfo(user)
for userSystemRoleName in user.get('userSystemRoleNameList', []):
if userSystemRoleName == dmRole.DM_ADMIN_ROLE:
principal.setRole(dmRole.DM_ADMIN_ROLE)
return principal
#!/usr/bin/env python
#
# Implementation for experiment session controller.
#
import time
from dm.common.objects.experiment import Experiment
from dm.common.objects.dmObjectManager import DmObjectManager
from dm.common.exceptions.invalidRequest import InvalidRequest
from dm.common.db.api.experimentDbApi import ExperimentDbApi
from dm.ds_web_service.service.impl.storageManager import StorageManager
class ExperimentSessionControllerImpl(DmObjectManager):
""" Experiment session controller implementation class. """
def __init__(self):
DmObjectManager.__init__(self)
self.experimentDbApi = ExperimentDbApi()
def getExperimentTypes(self):
experimentTypeList = self.experimentDbApi.getExperimentTypes()
return experimentTypeList
def getExperiments(self):
experimentList = self.experimentDbApi.getExperiments()
return experimentList
def getExperimentByName(self, name):
experiment = self.experimentDbApi.getExperimentByName(name)
return experiment
def getExperimentById(self, id):
experiment = self.experimentDbApi.getExperimentById(id)
return experiment
def addExperiment(self, name, experimentTypeId, description):
experiment = self.experimentDbApi.addExperiment(name, experimentTypeId, description)
return experiment
def startExperiment(self, name):
experiment = self.experimentDbApi.setExperimentStartDateToNow(name)
StorageManager.getInstance().createExperimentDataDirectory(experiment)
return experiment
def stopExperiment(self, name):
experiment = self.experimentDbApi.setExperimentEndDateToNow(name)
return experiment
#!/usr/bin/env python
#
# Implementation for file system controller.
#
#######################################################################
import threading
from dm.common.objects.dmObject import DmObject
from dm.common.objects.dmObjectManager import DmObjectManager
from dm.common.utility.dmSubprocess import DmSubprocess
#######################################################################
class FileSystemControllerImpl(DmObjectManager):
""" FS controller implementation class. """
def __init__(self):
DmObjectManager.__init__(self)
def getDirectoryList(self, path):
p = DmSubprocess('ls -l %s' % path)
p.run()
return DmObject({'path' : path, 'directoryList' : p.getStdOut()})
def writeFile(self, path, content):
f = open(path, 'w')
f.write('%s\n' % content)
f.close()
return DmObject({'path' : path})
#!/usr/bin/env python
import threading
import time
from dm.common.utility.loggingManager import LoggingManager
from dm.common.utility.configurationManager import ConfigurationManager
from dm.common.utility.singleton import Singleton
class StorageManager(Singleton):
CONFIG_SECTION_NAME = 'StorageManager'
STORAGE_DIRECTORY_KEY = 'storagedirectory'
# Singleton.
__instanceLock = threading.RLock()
__instance = None
def __init__(self):
StorageManager.__instanceLock.acquire()
try:
if StorageManager.__instance:
return
StorageManager.__instance = self
self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__)
self.logger.debug('Initializing')
self.lock = threading.RLock()
self.__configure()
self.logger.debug('Initialization complete')
finally:
StorageManager.__instanceLock.release()
def __configure(self):
cm = ConfigurationManager.getInstance()
configItems = cm.getConfigItems(StorageManager.CONFIG_SECTION_NAME)
self.logger.debug('Got config items: %s' % configItems)
self.storageDirectory = cm.getConfigOption(StorageManager.CONFIG_SECTION_NAME, StorageManager.STORAGE_DIRECTORY_KEY)
def createExperimentDataDirectory(self, experiment):
self.lock.acquire()
try:
experimentTypeName = experiment.get('experimentType').get('rootDataPath')
experimentName = experiment.get('name')
dataDirectory = '%s/%s/%s' % (self.storageDirectory, experimentTypeName, experimentName)
dataDirectory = dataDirectory.replace('//', '/')
self.logger.debug('Creating data directory for experiment %s: %s' % (experimentName, dataDirectory))
experiment['dataDirectory'] = dataDirectory
finally:
self.lock.release()
def start(self):
self.lock.acquire()
try:
self.logger.debug('Started storage manager')
finally:
self.lock.release()
def stop(self):
self.lock.acquire()
try:
self.logger.debug('Stopped storage manager')
finally:
self.lock.release()
####################################################################
# Testing
if __name__ == '__main__':
sm = StorageManager.getInstance()
print sm
#!/usr/bin/env python
#
# Implementation for user info controller.
#
from dm.common.objects.dmObject import DmObject
from dm.common.objects.dmObjectManager import DmObjectManager
from dm.common.db.api.userDbApi import UserDbApi
class UserInfoSessionControllerImpl(DmObjectManager):
""" User info controller implementation class. """
def __init__(self):
DmObjectManager.__init__(self)
self.userDbApi = UserDbApi()
def getUsers(self):
return self.userDbApi.getUsers()
def getUserById(self, id):
return self.userDbApi.getUserById(id)
def getUserByUsername(self, username):
return self.userDbApi.getUserByUsername(username)
#!/usr/bin/env python
import cherrypy
from dm.common.service.dmSessionController import DmSessionController
from dm.ds_web_service.service.impl.userInfoSessionControllerImpl import UserInfoSessionControllerImpl
class UserInfoSessionController(DmSessionController):
def __init__(self):
DmSessionController.__init__(self)
self.userInfoSessionControllerImpl = UserInfoSessionControllerImpl()
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getUsers(self, **kwargs):
return self.listToJson(self.userInfoSessionControllerImpl.getUsers())
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getUserById(self, id, **kwargs):
if not id:
raise InvalidRequest('Invalid id provided.')
response = self.userInfoSessionControllerImpl.getUserById(id).getFullJsonRep()
self.logger.debug('Returning user info for %s: %s' % (id,response))
return response
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getUserByUsername(self, username, **kwargs):
if not len(username):
raise InvalidRequest('Invalid username provided.')
response = self.userInfoSessionControllerImpl.getUserByUsername(username).getFullJsonRep()
self.logger.debug('Returning user info for %s: %s' % (username,response))
return response
#!/usr/bin/env python
#
# User route descriptor.
#
from dm.common.utility.configurationManager import ConfigurationManager
from userInfoSessionController import UserInfoSessionController
class UserRouteDescriptor:
@classmethod
def getRoutes(cls):
contextRoot = ConfigurationManager.getInstance().getContextRoot()
# Static instances shared between different routes
userInfoSessionController = UserInfoSessionController()
# Define routes.
routes = [
# Get user info list
{
'name' : 'getUsers',
'path' : '%s/users' % contextRoot,
'controller' : userInfoSessionController,
'action' : 'getUsers',
'method' : ['GET']
},
# Get user by id
{
'name' : 'getUserById',
'path' : '%s/users/:(id)' % contextRoot,
'controller' : userInfoSessionController,
'action' : 'getUserById',
'method' : ['GET']
},
# Get user by username
{
'name' : 'getUserByUsername',
'path' : '%s/usersByUsername/:(username)' % contextRoot,
'controller' : userInfoSessionController,
'action' : 'getUserByUsername',
'method' : ['GET']
},
]
return routes
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