Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
managementhost
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
DeployIT
managementhost
Commits
4b830561
Commit
4b830561
authored
10 years ago
by
Rafael Ostertag
Browse files
Options
Downloads
Patches
Plain Diff
Uses context manager when working with database.
parent
053db48b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
managementhost/restmodules.py
+27
-29
27 additions, 29 deletions
managementhost/restmodules.py
with
27 additions
and
29 deletions
managementhost/restmodules.py
+
27
−
29
View file @
4b830561
"""
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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment