Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
94
Merge Requests
10
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
dedcfc62
authored
Apr 22, 2014
by
Kálmán Viktor
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into issue-vm-detail-fixes
Conflicts: circle/dashboard/tests/test_views.py
parents
1a890736
5d753e30
Show whitespace changes
Inline
Side-by-side
Showing
49 changed files
with
866 additions
and
327 deletions
+866
-327
.gitignore
+10
-0
circle/circle/settings/base.py
+0
-5
circle/common/operations.py
+36
-4
circle/dashboard/forms.py
+2
-0
circle/dashboard/models.py
+2
-0
circle/dashboard/static/dashboard/vm-common.js
+9
-10
circle/dashboard/static/dashboard/vm-details.js
+1
-0
circle/dashboard/tables.py
+2
-0
circle/dashboard/templates/dashboard/_base.html
+20
-0
circle/dashboard/templates/dashboard/_modal.html
+12
-0
circle/dashboard/templates/dashboard/_vm-migrate.html
+14
-6
circle/dashboard/templates/dashboard/index-nodes.html
+1
-1
circle/dashboard/templates/dashboard/operate.html
+19
-0
circle/dashboard/templates/dashboard/vm-detail.html
+2
-88
circle/dashboard/templates/dashboard/vm-detail/_operations.html
+9
-0
circle/dashboard/tests/test_mockedviews.py
+81
-4
circle/dashboard/tests/test_views.py
+15
-6
circle/dashboard/urls.py
+3
-2
circle/dashboard/views.py
+126
-103
circle/dashboard/vm/__init__.py
+0
-0
circle/dashboard/vm/urls.py
+8
-0
circle/firewall/admin.py
+5
-5
circle/firewall/fw.py
+3
-2
circle/firewall/iptables.py
+1
-1
circle/firewall/migrations/0045_auto__del_field_host_pub_ipv4__add_field_host_external_ipv4__del_field.py
+2
-2
circle/firewall/migrations/0051_rename_blacklist_to_blacklistitem.py
+215
-0
circle/firewall/models.py
+8
-4
circle/firewall/views.py
+2
-2
circle/network/forms.py
+3
-3
circle/network/tables.py
+1
-1
circle/network/templates/network/blacklist-create.html
+1
-1
circle/network/templates/network/blacklist-edit.html
+1
-1
circle/network/templates/network/blacklist-list.html
+2
-2
circle/network/templates/network/menu.html
+2
-2
circle/network/urls.py
+4
-4
circle/network/views.py
+15
-14
circle/storage/admin.py
+2
-1
circle/storage/models.py
+71
-9
circle/storage/tasks/local_tasks.py
+15
-10
circle/storage/tasks/periodic_tasks.py
+0
-1
circle/storage/tests/test_models.py
+2
-2
circle/vm/models/instance.py
+7
-3
circle/vm/models/network.py
+2
-2
circle/vm/operations.py
+52
-25
circle/vm/tests/test_operations.py
+8
-0
docs/install.rst
+2
-1
miscellaneous/nginx-uwsgi.conf
+52
-0
miscellaneous/portal-uwsgi.conf
+15
-0
requirements/production.txt
+1
-0
No files found.
.gitignore
View file @
dedcfc62
...
...
@@ -25,3 +25,13 @@ coverage.xml
# Gettext object file:
*.mo
# saml
circle/attribute-maps
circle/remote_metadata.xml
circle/*.key
circle/*.pem
# collected static files:
circle/static
circle/static_collected
circle/circle/settings/base.py
View file @
dedcfc62
...
...
@@ -125,11 +125,6 @@ STATIC_ROOT = normpath(join(SITE_ROOT, 'static_collected'))
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL
=
get_env_variable
(
'DJANGO_STATIC_URL'
,
default
=
'/static/'
)
# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS
=
(
normpath
(
join
(
SITE_ROOT
,
'static'
)),
)
# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS
=
(
'django.contrib.staticfiles.finders.FileSystemFinder'
,
...
...
circle/common/operations.py
View file @
dedcfc62
...
...
@@ -13,6 +13,7 @@ class Operation(object):
"""
async_queue
=
'localhost.man'
required_perms
=
()
do_not_call_in_templates
=
True
def
__call__
(
self
,
**
kwargs
):
return
self
.
call
(
**
kwargs
)
...
...
@@ -28,11 +29,11 @@ class Operation(object):
def
__prelude
(
self
,
kwargs
):
"""This method contains the shared prelude of call and async.
"""
skip_
checks
=
kwargs
.
setdefault
(
'system'
,
False
)
skip_
auth_check
=
kwargs
.
setdefault
(
'system'
,
False
)
user
=
kwargs
.
setdefault
(
'user'
,
None
)
parent_activity
=
kwargs
.
pop
(
'parent_activity'
,
None
)
if
not
skip_
checks
:
if
not
skip_
auth_check
:
self
.
check_auth
(
user
)
self
.
check_precond
()
return
self
.
create_activity
(
parent
=
parent_activity
,
user
=
user
)
...
...
@@ -42,7 +43,7 @@ class Operation(object):
"""
with
activity_context
(
activity
,
on_abort
=
self
.
on_abort
,
on_commit
=
self
.
on_commit
):
return
self
.
_operation
(
activity
,
user
,
**
kwargs
)
return
self
.
_operation
(
activity
=
activity
,
user
=
user
,
**
kwargs
)
def
_operation
(
self
,
activity
,
user
,
system
,
**
kwargs
):
"""This method is the operation's particular implementation.
...
...
@@ -127,15 +128,46 @@ class OperatedMixin(object):
raise
AttributeError
(
"
%
r object has no attribute
%
r"
%
(
self
.
__class__
.
__name__
,
name
))
def
get_available_operations
(
self
,
user
):
"""Yield Operations that match permissions of user and preconditions.
"""
for
name
in
getattr
(
self
,
operation_registry_name
,
{}):
try
:
op
=
getattr
(
self
,
name
)
op
.
check_auth
(
user
)
op
.
check_precond
()
except
:
pass
# unavailable
else
:
yield
op
def
register_operation
(
target_cls
,
op_cls
,
op_id
=
None
):
def
register_operation
(
op_cls
,
op_id
=
None
,
target_cls
=
None
):
"""Register the specified operation with the target class.
You can optionally specify an ID to be used for the registration;
otherwise, the operation class' 'id' attribute will be used.
"""
if
op_id
is
None
:
try
:
op_id
=
op_cls
.
id
except
AttributeError
:
raise
NotImplementedError
(
"Operations should specify an 'id' "
"attribute designating the name the "
"operation can be called by on its "
"host. Alternatively, provide the name "
"in the 'op_id' parameter to this call."
)
if
target_cls
is
None
:
try
:
target_cls
=
op_cls
.
host_cls
except
AttributeError
:
raise
NotImplementedError
(
"Operations should specify a 'host_cls' "
"attribute designating the host class "
"the operation should be registered to. "
"Alternatively, provide the host class "
"in the 'target_cls' parameter to this "
"call."
)
if
not
issubclass
(
target_cls
,
OperatedMixin
):
raise
TypeError
(
"
%
r is not a subclass of
%
r"
%
...
...
circle/dashboard/forms.py
View file @
dedcfc62
from
__future__
import
absolute_import
from
datetime
import
timedelta
from
django.contrib.auth.models
import
User
...
...
circle/dashboard/models.py
View file @
dedcfc62
from
__future__
import
absolute_import
from
itertools
import
chain
from
logging
import
getLogger
...
...
circle/dashboard/static/dashboard/vm-common.js
View file @
dedcfc62
...
...
@@ -2,22 +2,21 @@
$
(
function
()
{
/* vm migrate */
$
(
'.vm-migrate'
).
click
(
function
(
e
)
{
var
icon
=
$
(
this
).
children
(
"i"
);
var
vm
=
$
(
this
).
data
(
"vm-pk"
);
icon
.
removeClass
(
"icon-truck"
).
addClass
(
"icon-spinner icon-spin"
);
/* vm operations */
$
(
'#ops'
).
on
(
'click'
,
'.operation.btn'
,
function
(
e
)
{
var
icon
=
$
(
this
).
children
(
"i"
).
addClass
(
'icon-spinner icon-spin'
);
$
.
ajax
({
type
:
'GET'
,
url
:
'/dashboard/vm/'
+
vm
+
'/migrate/'
,
url
:
$
(
this
).
attr
(
'href'
),
success
:
function
(
data
)
{
icon
.
addClass
(
"icon-truck"
).
removeClass
(
"icon-spinner icon-spin"
);
icon
.
removeClass
(
"icon-spinner icon-spin"
);
$
(
'body'
).
append
(
data
);
$
(
'#c
reate
-modal'
).
modal
(
'show'
);
$
(
'#c
reate
-modal'
).
on
(
'hidden.bs.modal'
,
function
()
{
$
(
'#c
reate
-modal'
).
remove
();
$
(
'#c
onfirmation
-modal'
).
modal
(
'show'
);
$
(
'#c
onfirmation
-modal'
).
on
(
'hidden.bs.modal'
,
function
()
{
$
(
'#c
onfirmation
-modal'
).
remove
();
});
$
(
'#vm-migrate-node-list li'
).
click
(
function
(
e
)
{
var
li
=
$
(
this
).
closest
(
'li'
);
if
(
li
.
find
(
'input'
).
attr
(
'disabled'
))
...
...
circle/dashboard/static/dashboard/vm-details.js
View file @
dedcfc62
...
...
@@ -277,6 +277,7 @@ function checkNewActivity(only_status, runs) {
success
:
function
(
data
)
{
if
(
!
only_status
)
{
$
(
"#activity-timeline"
).
html
(
data
[
'activities'
]);
$
(
"#ops"
).
html
(
data
[
'ops'
]);
$
(
"[title]"
).
tooltip
();
}
...
...
circle/dashboard/tables.py
View file @
dedcfc62
from
__future__
import
absolute_import
from
django.contrib.auth.models
import
Group
,
User
from
django_tables2
import
Table
,
A
from
django_tables2.columns
import
(
TemplateColumn
,
Column
,
BooleanColumn
,
...
...
circle/dashboard/templates/dashboard/_base.html
0 → 100644
View file @
dedcfc62
{% extends "dashboard/base.html" %}
{% load i18n %}
{% block content %}
<div
class=
"body-content"
>
<div
class=
"panel panel-default"
style=
"margin-top: 60px;"
>
<div
class=
"panel-heading"
>
<h3
class=
"no-margin"
>
{% if title %}
{{ title }}
{% else %}
{% trans "Confirmation" %}
{% endif %}
</h3>
</div>
<div
class=
"panel-body"
>
{{ body|safe|default:"(body missing from context.)" }}
</div>
</div>
{% endblock %}
circle/dashboard/templates/dashboard/_modal.html
0 → 100644
View file @
dedcfc62
{% load i18n %}
<div
class=
"modal fade"
id=
"confirmation-modal"
tabindex=
"-1"
role=
"dialog"
>
<div
class=
"modal-dialog"
>
<div
class=
"modal-content"
>
<div
class=
"modal-body"
>
{{ body|safe|default:"(body missing from context.)" }}
<div
class=
"clearfix"
></div>
</div>
<div
class=
"clearfix"
></div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
circle/dashboard/templates/dashboard/_vm-migrate.html
View file @
dedcfc62
{% extends "dashboard/operate.html" %}
{% load i18n %}
{% load sizefieldtags %}
<form
method=
"POST"
action=
"{% url "
dashboard
.
views
.
vm-migrate
"
pk=
vm.pk
%}"
>
{% csrf_token %}
<ul
id=
"vm-migrate-node-list"
>
{% with current=vm.node.pk selected=vm.select_node.pk %}
{% block question %}
<p>
{% blocktrans with obj=object op=op.name %}
Choose a compute node to migrate {{obj}} to.
{% endblocktrans %}
</p>
<p
class=
"text-info"
>
{{op.name}}: {{op.description}}
</p>
{% endblock %}
{% block formfields %}
<ul
id=
"vm-migrate-node-list"
class=
"list-unstyled"
>
{% with current=object.node.pk selected=object.select_node.pk %}
{% for n in nodes %}
<li
class=
"panel panel-default"
><div
class=
"panel-body"
>
<label
for=
"migrate-to-{{n.pk}}"
>
...
...
@@ -22,5 +31,4 @@
{% endfor %}
{% endwith %}
</ul>
<button
type=
"submit"
class=
"btn btn-primary btn-sm"
><i
class=
"icon-truck"
></i>
Migrate
</button>
</form>
{% endblock %}
circle/dashboard/templates/dashboard/index-nodes.html
View file @
dedcfc62
...
...
@@ -16,7 +16,7 @@
<div
class=
"list-group"
id=
"node-list-view"
>
{% for i in nodes %}
<a
href=
"{% url "
dashboard
.
views
.
node-detail
"
pk=
i.pk
%}"
class=
"list-group-item"
>
<i
class=
"icon-{% if i.enabled == True %}play-sign{% else %}pause{% endif %}"
></i>
{{ i.name }}
<div
class=
"pull-right"
><i
class=
"icon-star text-primary"
title=
"Mark as favorite."
></i></div>
<i
class=
"icon-{% if i.enabled == True %}play-sign{% else %}pause{% endif %}"
></i>
{{ i.name }}
</a>
{% endfor %}
<div
href=
"#"
class=
"list-group-item list-group-footer"
>
...
...
circle/dashboard/templates/dashboard/operate.html
0 → 100644
View file @
dedcfc62
{% load i18n %}
{% block question %}
<p>
{% blocktrans with obj=object op=op.name %}
Do you want to do the following operation on {{obj}}:
<strong>
{{op}}
</strong>
?
{% endblocktrans %}
</p>
<p
class=
"text-info"
>
{{op.name}}: {{op.description}}
</p>
{% endblock %}
<form
method=
"POST"
action=
"{{url}}"
>
{% csrf_token %}
{% block formfields %}{% endblock %}
<div
class=
"pull-right"
>
<a
class=
"btn btn-default"
href=
"{{object.get_absolute_url}}"
data-dismiss=
"modal"
>
{% trans "Cancel" %}
</a>
<button
class=
"btn btn-danger"
type=
"submit"
>
{% if op.icon %}
<i
class=
"icon-{{op.icon}}"
></i>
{% endif %}{{ op|capfirst }}
</button>
</div>
</form>
circle/dashboard/templates/dashboard/vm-detail.html
View file @
dedcfc62
...
...
@@ -6,54 +6,8 @@
{% block content %}
<div
class=
"body-content"
>
<div
class=
"page-header"
>
<div
class=
"pull-right"
style=
"padding-top: 15px;"
>
<form
style=
"display: inline;"
method=
"POST"
action=
"{% url "
dashboard
.
views
.
detail
"
pk=
instance.pk
%}"
>
{% csrf_token %}
<input
type=
"hidden"
name=
"sleep"
/>
<button
title=
"{% trans "
Sleep
"
%}"
class=
"btn btn-default btn-xs"
type=
"submit"
><i
class=
"icon-moon"
></i></button>
</form>
<form
style=
"display: inline;"
method=
"POST"
action=
"{% url "
dashboard
.
views
.
detail
"
pk=
instance.pk
%}"
>
{% csrf_token %}
<input
type=
"hidden"
name=
"deploy"
/>
<button
title=
"{% trans "
Deploy
"
%}"
class=
"btn btn-default btn-xs"
type=
"submit"
><i
class=
"icon-play"
></i></button>
</form>
<form
style=
"display: inline;"
method=
"POST"
action=
"{% url "
dashboard
.
views
.
detail
"
pk=
instance.pk
%}"
>
{% csrf_token %}
<input
type=
"hidden"
name=
"wake_up"
/>
<button
title=
"{% trans "
Wake
up
"
%}"
class=
"btn btn-default btn-xs"
type=
"submit"
><i
class=
"icon-sun"
></i></button>
</form>
<form
style=
"display: inline;"
method=
"POST"
action=
"{% url "
dashboard
.
views
.
detail
"
pk=
instance.pk
%}"
>
{% csrf_token %}
<input
type=
"hidden"
name=
"shut_down"
/>
<button
title=
"{% trans "
Shut
down
"
%}"
class=
"btn btn-default btn-xs"
type=
"submit"
><i
class=
"icon-off"
></i></button>
</form>
<form
style=
"display: inline;"
method=
"POST"
action=
"{% url "
dashboard
.
views
.
detail
"
pk=
instance.pk
%}"
>
{% csrf_token %}
<input
type=
"hidden"
name=
"reboot"
/>
<button
title=
"{% trans "
Reboot
(
ctrl
+
alt
+
del
)"
%}"
class=
"btn btn-default btn-xs"
type=
"submit"
><i
class=
"icon-refresh"
></i></button>
</form>
<form
style=
"display: inline;"
method=
"POST"
action=
"{% url "
dashboard
.
views
.
detail
"
pk=
instance.pk
%}"
>
{% csrf_token %}
<input
type=
"hidden"
name=
"reset"
/>
<button
title=
"{% trans "
Reset
(
power
cycle
)"
%}"
class=
"btn btn-default btn-xs"
type=
"submit"
><i
class=
"icon-bolt"
></i></button>
</form>
<form
style=
"display: inline;"
method=
"POST"
action=
"{% url "
dashboard
.
views
.
detail
"
pk=
instance.pk
%}"
>
{% csrf_token %}
<input
type=
"hidden"
name=
"shut_off"
/>
<button
title=
"{% trans "
Shut
off
"
%}"
class=
"btn btn-default btn-xs"
type=
"submit"
>
<i
class=
"icon-ban-circle"
></i>
</button>
</form>
<a
title=
"Migrate"
data-vm-pk=
"{{ instance.pk }}"
href=
"{% url "
dashboard
.
views
.
vm-migrate
"
pk=
instance.pk
%}"
class=
"btn btn-default btn-xs vm-migrate"
>
<i
class=
"icon-truck"
></i>
</a>
<form
style=
"display: inline;"
method=
"POST"
action=
"{% url "
dashboard
.
views
.
detail
"
pk=
instance.pk
%}"
>
{% csrf_token %}
<input
type=
"hidden"
name=
"save_as"
/>
<button
title=
"{% trans "
Save
as
template
"
%}"
class=
"btn btn-default btn-xs"
type=
"submit"
><i
class=
"icon-save"
></i></button>
</form>
<a
title=
"{% trans "
Destroy
"
%}"
href=
"{% url "
dashboard
.
views
.
delete-vm
"
pk=
instance.pk
%}"
class=
"btn btn-default btn-xs vm-delete"
data-vm-pk=
"{{ instance.pk }}"
><i
class=
"icon-remove"
></i></a>
<a
title=
"{% trans "
Help
"
%}"
href=
"#"
class=
"btn btn-default btn-xs vm-details-help-button"
><i
class=
"icon-question"
></i></a>
<div
class=
"pull-right"
style=
"padding-top: 15px;"
id=
"ops"
>
{% include "dashboard/vm-detail/_operations.html" %}
</div>
<h1>
<div
id=
"vm-details-rename"
class=
"vm-details-home-rename-form-div"
>
...
...
@@ -72,46 +26,6 @@
</div>
<small>
{{ instance.primary_host.get_fqdn }}
</small>
</h1>
<div
class=
"vm-details-help js-hidden"
>
<ul
style=
"list-style: none;"
>
<li>
<strong>
{% trans "Sleep" %}:
</strong>
{% trans "Suspend virtual machine with memory dump." %}
</li>
<li>
<strong>
{% trans "Wake up" %}:
</strong>
{% trans "Wake up suspended machine." %}
</li>
<li>
<strong>
{% trans "Shutdown" %}:
</strong>
{% trans "Shutdown virtual machine with ACPI signal." %}
</li>
<li>
<strong>
{% trans "Reboot (ctrl + alt + del)" %}:
</strong>
{% trans "Reboot virtual machine with Ctrl+Alt+Del signal." %}
</li>
<li>
<strong>
{% trans "Reset (power cycle)" %}:
</strong>
{% trans "Reset virtual machine (reset button)" %}
</li>
<li>
<strong>
{% trans "Shut off" %}:
</strong>
{% trans "Shut off VM. (plug-out)" %}
</li>
<li>
<strong>
{% trans "Migrate" %}:
</strong>
{% trans "Live migrate running vm to another node." %}
</li>
<li>
<strong>
{% trans "Save as template" %}:
</strong>
{% trans "Shut down the virtual machine, and save it as a new template." %}
</li>
<li>
<strong>
{% trans "Destroy" %}:
</strong>
{% trans "Remove virtual machine and its networks." %}
</li>
</ul>
</div>
<div
style=
"clear: both;"
></div>
</div>
<div
class=
"row"
>
...
...
circle/dashboard/templates/dashboard/vm-detail/_operations.html
0 → 100644
View file @
dedcfc62
{% load i18n %}
{% for op in ops %}
<a
href=
"{{op.get_url}}"
class=
"operation operation-{{op.op}} btn btn-default btn-xs"
title=
"{{op.name}}: {{op.description}}"
>
<i
class=
"icon-{{op.icon}}"
></i>
<span
class=
"sr-only"
>
{{op.name}}
</span>
</a>
{% endfor %}
circle/dashboard/tests/test_mockedviews.py
View file @
dedcfc62
...
...
@@ -3,10 +3,11 @@ from factory import Factory, Sequence
from
mock
import
patch
,
MagicMock
from
django.contrib.auth.models
import
User
#
from django.core.exceptions import PermissionDenied
from
django.core.exceptions
import
PermissionDenied
from
django.http
import
HttpRequest
,
Http404
from
dashboard.views
import
InstanceActivityDetail
,
InstanceActivity
from
..views
import
InstanceActivityDetail
,
InstanceActivity
from
..views
import
vm_ops
,
Instance
class
ViewUserTestCase
(
unittest
.
TestCase
):
...
...
@@ -36,6 +37,82 @@ class ViewUserTestCase(unittest.TestCase):
self
.
assertEquals
(
view
(
request
,
pk
=
1234
)
.
render
()
.
status_code
,
200
)
class
VmOperationViewTestCase
(
unittest
.
TestCase
):
def
test_available
(
self
):
request
=
FakeRequestFactory
(
superuser
=
True
)
view
=
vm_ops
[
'destroy'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
:
inst
=
MagicMock
(
spec
=
Instance
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
destroy
=
Instance
.
_ops
[
'destroy'
](
inst
)
go
.
return_value
=
inst
self
.
assertEquals
(
view
.
as_view
()(
request
,
pk
=
1234
)
.
render
()
.
status_code
,
200
)
def
test_unpermitted
(
self
):
request
=
FakeRequestFactory
()
view
=
vm_ops
[
'destroy'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
:
inst
=
MagicMock
(
spec
=
Instance
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
destroy
=
Instance
.
_ops
[
'destroy'
](
inst
)
inst
.
has_level
.
return_value
=
False
go
.
return_value
=
inst
with
self
.
assertRaises
(
PermissionDenied
):
view
.
as_view
()(
request
,
pk
=
1234
)
.
render
()
def
test_migrate
(
self
):
request
=
FakeRequestFactory
(
POST
=
{
'node'
:
1
})
view
=
vm_ops
[
'migrate'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
,
\
patch
(
'dashboard.views.messages'
)
as
msg
,
\
patch
(
'dashboard.views.get_object_or_404'
)
as
go4
:
inst
=
MagicMock
(
spec
=
Instance
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
migrate
=
Instance
.
_ops
[
'migrate'
](
inst
)
inst
.
migrate
.
async
=
MagicMock
()
inst
.
has_level
.
return_value
=
True
go
.
return_value
=
inst
go4
.
return_value
=
MagicMock
()
assert
view
.
as_view
()(
request
,
pk
=
1234
)[
'location'
]
assert
not
msg
.
error
.
called
def
test_migrate_failed
(
self
):
request
=
FakeRequestFactory
(
POST
=
{
'node'
:
1
})
view
=
vm_ops
[
'migrate'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
,
\
patch
(
'dashboard.views.messages'
)
as
msg
,
\
patch
(
'dashboard.views.get_object_or_404'
)
as
go4
:
inst
=
MagicMock
(
spec
=
Instance
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
migrate
=
Instance
.
_ops
[
'migrate'
](
inst
)
inst
.
migrate
.
async
=
MagicMock
()
inst
.
migrate
.
async
.
side_effect
=
Exception
inst
.
has_level
.
return_value
=
True
go
.
return_value
=
inst
go4
.
return_value
=
MagicMock
()
assert
view
.
as_view
()(
request
,
pk
=
1234
)[
'location'
]
assert
msg
.
error
.
called
def
test_migrate_template
(
self
):
request
=
FakeRequestFactory
()
view
=
vm_ops
[
'migrate'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
:
inst
=
MagicMock
(
spec
=
Instance
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
migrate
=
Instance
.
_ops
[
'migrate'
](
inst
)
inst
.
has_level
.
return_value
=
True
go
.
return_value
=
inst
self
.
assertEquals
(
view
.
as_view
()(
request
,
pk
=
1234
)
.
render
()
.
status_code
,
200
)
def
FakeRequestFactory
(
*
args
,
**
kwargs
):
''' FakeRequestFactory, FakeMessages and FakeRequestContext are good for
mocking out django views; they are MUCH faster than the Django test client.
...
...
@@ -48,12 +125,12 @@ def FakeRequestFactory(*args, **kwargs):
request
=
HttpRequest
()
request
.
user
=
user
request
.
session
=
kwargs
.
get
(
'session'
,
{})
if
kwargs
.
get
(
'POST'
):
if
kwargs
.
get
(
'POST'
)
is
not
None
:
request
.
method
=
'POST'
request
.
POST
=
kwargs
.
get
(
'POST'
)
else
:
request
.
method
=
'GET'
request
.
POS
T
=
kwargs
.
get
(
'GET'
,
{})
request
.
GE
T
=
kwargs
.
get
(
'GET'
,
{})
return
request
...
...
circle/dashboard/tests/test_views.py
View file @
dedcfc62
import
json
from
unittest
import
skip
from
django.test
import
TestCase
from
django.test.client
import
Client
from
django.contrib.auth.models
import
User
,
Group
...
...
@@ -279,6 +280,7 @@ class VmDetailTest(LoginMixin, TestCase):
self
.
assertEqual
(
response
.
status_code
,
403
)
self
.
assertEqual
(
disks
,
inst
.
disks
.
count
())
@skip
(
"until fix merged"
)
def
test_permitted_vm_disk_add
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user1"
)
...
...
@@ -495,9 +497,11 @@ class VmDetailTest(LoginMixin, TestCase):
mock_method
.
side_effect
=
inst
.
wake_up
inst
.
manual_state_change
(
'RUNNING'
)
inst
.
set_level
(
self
.
u2
,
'owner'
)
self
.
assertRaises
(
inst
.
WrongStateError
,
c
.
post
,
"/dashboard/vm/1/"
,
{
'wake_up'
:
True
})
self
.
assertEqual
(
inst
.
status
,
'RUNNING'
)
with
patch
(
'dashboard.views.messages'
)
as
msg
:
c
.
post
(
"/dashboard/vm/1/op/wake_up/"
)
assert
msg
.
error
.
called
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
self
.
assertEqual
(
inst
.
status
,
'RUNNING'
)
# mocked anyway
assert
mock_method
.
called
def
test_permitted_wake_up
(
self
):
...
...
@@ -511,7 +515,9 @@ class VmDetailTest(LoginMixin, TestCase):
inst
.
get_remote_queue_name
=
Mock
(
return_value
=
'test'
)
inst
.
manual_state_change
(
'SUSPENDED'
)
inst
.
set_level
(
self
.
u2
,
'owner'
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'wake_up'
:
True
})
with
patch
(
'dashboard.views.messages'
)
as
msg
:
response
=
c
.
post
(
"/dashboard/vm/1/op/wake_up/"
)
assert
not
msg
.
error
.
called
self
.
assertEqual
(
response
.
status_code
,
302
)
self
.
assertEqual
(
inst
.
status
,
'RUNNING'
)
assert
new_wake_up
.
called
...
...
@@ -523,8 +529,11 @@ class VmDetailTest(LoginMixin, TestCase):
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
manual_state_change
(
'SUSPENDED'
)
inst
.
set_level
(
self
.
u2
,
'user'
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'wake_up'
:
True
})
self
.
assertEqual
(
response
.
status_code
,
403
)
with
patch
(
'dashboard.views.messages'
)
as
msg
:
response
=
c
.
post
(
"/dashboard/vm/1/op/wake_up/"
)
assert
msg
.
error
.
called
self
.
assertEqual
(
response
.
status_code
,
302
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
self
.
assertEqual
(
inst
.
status
,
'SUSPENDED'
)
def
test_non_existing_template_get
(
self
):
...
...
circle/dashboard/urls.py
View file @
dedcfc62
from
django.conf.urls
import
patterns
,
url
from
__future__
import
absolute_import
from
django.conf.urls
import
patterns
,
url
,
include
from
vm.models
import
Instance
from
.views
import
(
...
...
@@ -34,7 +35,7 @@ urlpatterns = patterns(
name
=
"dashboard.views.template-list"
),
url
(
r"^template/delete/(?P<pk>\d+)/$"
,
TemplateDelete
.
as_view
(),
name
=
"dashboard.views.template-delete"
),
url
(
r'^vm/(?P<pk>\d+)/op/'
,
include
(
'dashboard.vm.urls'
)),
url
(
r'^vm/(?P<pk>\d+)/remove_port/(?P<rule>\d+)/$'
,
PortDelete
.
as_view
(),
name
=
'dashboard.views.remove-port'
),
url
(
r'^vm/(?P<pk>\d+)/$'
,
VmDetailView
.
as_view
(),
...
...
circle/dashboard/views.py
View file @
dedcfc62
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
,
absolute_import
from
os
import
getenv
import
json
import
logging
import
re
from
datetime
import
datetime
import
requests
from
django.conf
import
settings
...
...
@@ -47,7 +46,7 @@ from vm.models import (
)
from
storage.models
import
Disk
from
firewall.models
import
Vlan
,
Host
,
Rule
from
dashboard
.models
import
Favourite
,
Profile
from
.models
import
Favourite
,
Profile
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -203,7 +202,8 @@ class VmDetailView(CheckedDetailView):
context
.
update
({
'graphite_enabled'
:
VmGraphView
.
get_graphite_url
()
is
not
None
,
'vnc_url'
:
reverse_lazy
(
"dashboard.views.detail-vnc"
,
kwargs
=
{
'pk'
:
self
.
object
.
pk
})
kwargs
=
{
'pk'
:
self
.
object
.
pk
}),
'ops'
:
get_operations
(
instance
,
self
.
request
.
user
),
})
# activity data
...
...
@@ -243,14 +243,6 @@ class VmDetailView(CheckedDetailView):
'to_remove'
:
self
.
__remove_tag
,
'port'
:
self
.
__add_port
,
'new_network_vlan'
:
self
.
__new_network
,
'save_as'
:
self
.
__save_as
,
'shut_down'
:
self
.
__shut_down
,
'sleep'
:
self
.
__sleep
,
'wake_up'
:
self
.
__wake_up
,
'deploy'
:
self
.
__deploy
,
'reset'
:
self
.
__reset
,
'reboot'
:
self
.
__reboot
,
'shut_off'
:
self
.
__shut_off
,
}
for
k
,
v
in
options
.
iteritems
():
if
request
.
POST
.
get
(
k
)
is
not
None
:
...
...
@@ -437,75 +429,132 @@ class VmDetailView(CheckedDetailView):
return
redirect
(
"
%
s#network"
%
reverse_lazy
(
"dashboard.views.detail"
,
kwargs
=
{
'pk'
:
self
.
object
.
pk
}))
def
__save_as
(
self
,
request
):
self
.
object
=
self
.
get_object
()
if
not
self
.
object
.
has_level
(
request
.
user
,
'owner'
):
raise
PermissionDenied
()
date
=
datetime
.
now
()
.
strftime
(
"
%
Y-
%
m-
%
d
%
H:
%
M"
)
new_name
=
"Saved from
%
s (#
%
d) at
%
s"
%
(
self
.
object
.
name
,
self
.
object
.
pk
,
date
)
self
.
object
.
save_as_template
.
async
(
name
=
new_name
,
user
=
request
.
user
)
messages
.
success
(
request
,
_
(
"Saving instance as template!"
))
return
redirect
(
"
%
s#activity"
%
self
.
object
.
get_absolute_url
())
class
OperationView
(
DetailView
):
def
__shut_down
(
self
,
request
):
self
.
object
=
self
.
get_object
()
if
not
self
.
object
.
has_level
(
request
.
user
,
'owner'
):
raise
PermissionDenied
()
template_name
=
'dashboard/operate.html'
self
.
object
.
shutdown
.
async
(
user
=
request
.
user
)
return
redirect
(
"
%
s#activity"
%
self
.
object
.
get_absolute_url
())
@property
def
name
(
self
):
return
self
.
get_op
()
.
name
def
__sleep
(
self
,
request
):
self
.
object
=
self
.
get_object
()
if
not
self
.
object
.
has_level
(
request
.
user
,
'owner'
):
raise
PermissionDenied
()
@property
def
description
(
self
):
return
self
.
get_op
()
.
description
self
.
object
.
sleep
.
async
(
user
=
request
.
user
)
return
redirect
(
"
%
s#activity"
%
self
.
object
.
get_absolute_url
())
@classmethod
def
get_urlname
(
cls
):
return
'dashboard.vm.op.
%
s'
%
cls
.
op
def
__wake_up
(
self
,
request
):
self
.
object
=
self
.
get_object
()
if
not
self
.
object
.
has_level
(
request
.
user
,
'owner'
):
raise
PermissionDenied
()
def
get_url
(
self
):
return
reverse
(
self
.
get_urlname
(),
args
=
(
self
.
get_object
()
.
pk
,
))
self
.
object
.
wake_up
.
async
(
user
=
request
.
user
)
return
redirect
(
"
%
s#activity"
%
self
.
object
.
get_absolute_url
())
def
get_wrapper_template_name
(
self
):
if
self
.
request
.
is_ajax
():
return
'dashboard/_modal.html'
else
:
return
'dashboard/_base.html'
def
__deploy
(
self
,
request
):
self
.
object
=
self
.
get_object
()
if
not
self
.
object
.
has_level
(
request
.
user
,
'owner'
):
raise
PermissionDenied
()
@classmethod
def
get_op_by_object
(
cls
,
obj
):
return
getattr
(
obj
,
cls
.
op
)
self
.
object
.
deploy
.
async
(
user
=
request
.
user
)
return
redirect
(
"
%
s#activity"
%
self
.
object
.
get_absolute_url
())
def
get_op
(
self
):
if
not
hasattr
(
self
,
'_opobj'
):
setattr
(
self
,
'_opobj'
,
getattr
(
self
.
get_object
(),
self
.
op
))
return
self
.
_opobj
def
__reset
(
self
,
request
):
self
.
object
=
self
.
get_object
()
if
not
self
.
object
.
has_level
(
request
.
user
,
'owner'
):
raise
PermissionDenied
()
def
get_context_data
(
self
,
**
kwargs
):
ctx
=
super
(
OperationView
,
self
)
.
get_context_data
(
**
kwargs
)
ctx
[
'op'
]
=
self
.
get_op
()
ctx
[
'url'
]
=
self
.
request
.
path
return
ctx
self
.
object
.
reset
.
async
(
user
=
request
.
user
)
return
redirect
(
"
%
s#activity"
%
self
.
object
.
get_absolute_url
())
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
self
.
get_op
()
.
check_auth
(
request
.
user
)
response
=
super
(
OperationView
,
self
)
.
get
(
request
,
*
args
,
**
kwargs
)
response
.
render
()
response
.
content
=
render_to_string
(
self
.
get_wrapper_template_name
(),
{
'body'
:
response
.
content
})
return
response
def
__reboot
(
self
,
request
):
def
post
(
self
,
request
,
extra
=
None
,
*
args
,
**
kwargs
):
self
.
object
=
self
.
get_object
()
if
not
self
.
object
.
has_level
(
request
.
user
,
'owner'
):
raise
PermissionDenied
()
self
.
object
.
reboot
.
async
(
user
=
request
.
user
)
if
extra
is
None
:
extra
=
{}
try
:
self
.
get_op
()
.
async
(
user
=
request
.
user
,
**
extra
)
except
Exception
as
e
:
messages
.
error
(
request
,
_
(
'Could not start operation.'
))
logger
.
error
(
e
)
return
redirect
(
"
%
s#activity"
%
self
.
object
.
get_absolute_url
())
def
__shut_off
(
self
,
request
):
self
.
object
=
self
.
get_object
()
if
not
self
.
object
.
has_level
(
request
.
user
,
'owner'
):
raise
PermissionDenied
(
)
@classmethod