Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gutyán Gábor
/
circlestack
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
9ef83cbc
authored
Mar 23, 2014
by
Dudás Ádám
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
vm: base class for operations
parent
3d50c288
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
93 additions
and
0 deletions
+93
-0
circle/vm/models/operation.py
+79
-0
circle/vm/tasks/local_tasks.py
+14
-0
No files found.
circle/vm/models/operation.py
0 → 100644
View file @
9ef83cbc
from
__future__
import
absolute_import
,
unicode_literals
from
common.models
import
activity_context
from
..tasks.local_tasks
import
async_operation
from
.activity
import
InstanceActivity
class
Operation
:
"""Base class for VM operations.
"""
acl_level
=
'owner'
async_queue
=
'localhost.man'
def
__init__
(
self
,
instance
):
"""Initialize a new operation bound to the specified VM instance.
"""
self
.
instance
=
instance
def
__call__
(
self
,
**
kwargs
):
"""Execute the operation synchronously.
"""
activity
=
self
.
__prelude
(
kwargs
)
return
self
.
_exec_op
(
activity
=
activity
,
**
kwargs
)
def
__unicode__
(
self
):
return
self
.
name
def
__prelude
(
self
,
kwargs
):
"""This method contains the shared prelude of __call__ and async.
"""
user
=
kwargs
.
setdefault
(
'user'
,
None
)
self
.
check_auth
(
user
)
# TODO what's check_auth's specification?
self
.
check_precond
()
return
self
.
create_activity
(
user
=
user
)
def
_exec_op
(
self
,
activity
,
user
=
None
,
**
kwargs
):
"""Execute the operation inside the specified activity's context.
"""
with
activity_context
(
activity
,
on_abort
=
self
.
on_abort
,
on_commit
=
self
.
on_commit
):
return
self
.
_operation
(
activity
,
user
,
**
kwargs
)
def
_operation
(
self
,
activity
,
user
=
None
,
**
kwargs
):
"""This method is the operation's particular implementation.
Deriving classes should implement this method.
"""
pass
def
async
(
self
,
**
kwargs
):
"""Execute the operation asynchronously.
Only a quick, preliminary check is ran before queuing the job.
"""
activity
=
self
.
__prelude
(
kwargs
)
return
async_operation
.
apply_async
(
args
=
(
self
.
id
,
self
.
instance
.
pk
,
activity
.
pk
),
kwargs
=
kwargs
,
queue
=
self
.
async_queue
)
def
check_precond
(
self
):
pass
def
check_auth
(
self
,
user
):
pass
def
create_activity
(
self
,
user
=
None
):
return
InstanceActivity
.
create
(
code_suffix
=
self
.
activity_code_suffix
,
instance
=
self
.
instance
,
user
=
user
)
def
on_abort
(
self
,
activity
,
error
):
"""This method is called when the operation aborts (i.e. raises an
exception).
"""
pass
def
on_commit
(
self
,
activity
):
"""This method is called when the operation executes successfully.
"""
pass
circle/vm/tasks/local_tasks.py
View file @
9ef83cbc
from
manager.mancelery
import
celery
@celery.task
def
async_operation
(
operation_id
,
instance_pk
,
activity_pk
,
**
kwargs
):
from
vm.models
import
Instance
,
InstanceActivity
instance
=
Instance
.
objects
.
get
(
pk
=
instance_pk
)
operation
=
getattr
(
instance
,
operation_id
)
activity
=
InstanceActivity
.
objects
.
get
(
pk
=
activity_pk
)
# save async task UUID to activity
activity
.
task_uuid
=
async_operation
.
request
.
id
activity
.
save
()
return
operation
.
_exec_op
(
activity
=
activity
,
**
kwargs
)
# TODO: Keep synchronised with Instance funcs
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment