Skip to content
Snippets Groups Projects
Commit 4b830561 authored by Rafael Ostertag's avatar Rafael Ostertag
Browse files

Uses context manager when working with database.

parent 053db48b
No related branches found
No related tags found
No related merge requests found
""" Base class for Rest Modules"""
import deployit.modules
import deployit.rabbit
import deployit.config
import managementhost.database
import web
......@@ -10,7 +9,7 @@ import string
import json
class RestModule(deployit.Module):
"""Base class for Management Host
"""Base class for Management Host
The resourcename is used as endpoint for REST and Rabbit.
......@@ -19,26 +18,20 @@ class RestModule(deployit.Module):
It is required to have deployit.config initialized before
instantiating REST modules.
Because this class uses managementhost.database, the function
deployit.config.read() has to be called prior instantiation.
"""
def __init__(self, name, resourcename, version):
super(RestModule, self).__init__(name, resourcename, version)
deployit.rabbit.connect_rabbit(
deployit.config.rabbit_config('host'),
int(deployit.config.rabbit_config('port')),
deployit.config.rabbit_config('vhost'),
deployit.config.rabbit_config('user'),
deployit.config.rabbit_config('password'))
self.rabbit_channel = deployit.rabbit.new_channel()
self.rabbit_channel.create_exchange(resourcename)
def get_endpoints(self):
"""
"""
Return a dictionary of supported HTTP Requests.
This will also be used to implement the OPTIONS method (self.options())
"""
"""
raise NotImplementedError
def get_method(self, *args, **kwargs):
......@@ -64,31 +57,36 @@ class RestModule(deployit.Module):
# multithreaded and the database connection is only valid
# in the thread it was created. This might be a
# performance hog.
database = managementhost.database.Database(
deployit.config.database_config('path'))
database.add_log(msg['header']['request_id'],
msg['header']['date'],
web.ctx.get('ip'),
self.name,
self.version,
deployit.modules.modulelist[self.name]['path'],
method,
json.dumps(msg))
with managementhost.database.Database() as database:
database.add_log(msg['header']['request_id'],
msg['header']['date'],
web.ctx.get('ip'),
self.name,
self.version,
deployit.modules.modulelist[self.name]['path'],
method,
json.dumps(msg))
def get_log_from_database(self, uuid):
# We have to open the database here because we might run
# multithreaded and the database connection is only valid
# in the thread it was created. This might be a
# performance hog.
database = managementhost.database.Database(
deployit.config.database_config('path'))
return database.get_log(uuid)
with managementhost.database.Database() as database:
return database.get_log(uuid)
def get_resource_status_from_database(self, uuid):
# We have to open the database here because we might run
# multithreaded and the database connection is only valid
# in the thread it was created. This might be a
# performance hog.
with managementhost.database.Database() as database:
return database.get_resource_status(uuid)
def get_module_logs(self):
# We have to open the database here because we might run
# multithreaded and the database connection is only valid
# in the thread it was created. This might be a
# performance hog.
database = managementhost.database.Database(
deployit.config.database_config('path'))
return database.get_module_logs(self.name, self.version)
with managementhost.database.Database() as database:
return database.get_module_logs(self.name, self.version)
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