1 # Copyright (C) 2010 Oregon State University et al.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 from django
.contrib
.auth
.decorators
import login_required
19 from django
.http
import HttpResponse
, HttpResponseForbidden
20 from django
.shortcuts
import get_object_or_404
21 from django
.utils
import simplejson
as json
22 from django
.views
.generic
.detail
import DetailView
24 from ganeti_web
.middleware
import Http403
25 from ganeti_web
.models
import Job
, Cluster
, VirtualMachine
, Node
26 from ganeti_web
.views
.generic
import NO_PRIVS
, LoginRequiredMixin
28 class JobDetailView(LoginRequiredMixin
, DetailView
):
30 template_name
= "ganeti/job/detail.html"
32 def get_object(self
, queryset
=None):
33 return get_object_or_404(Job
, job_id
=self
.kwargs
["job_id"],
34 cluster__slug
=self
.kwargs
["cluster_slug"])
36 def get_context_data(self
, **kwargs
):
37 job
= kwargs
["object"]
38 user
= self
.request
.user
39 admin
= user
.is_superuser
or user
.has_perm("admin", job
.cluster
)
43 "cluster_admin": admin
,
47 def status(request
, cluster_slug
, job_id
, rest
=False):
49 returns the raw info of a job
51 job
= get_object_or_404(Job
, cluster__slug
=cluster_slug
, job_id
=job_id
)
55 return HttpResponse(json
.dumps(job
.info
), mimetype
='application/json')
59 def clear(request
, cluster_slug
, job_id
):
65 cluster
= get_object_or_404(Cluster
, slug
=cluster_slug
)
66 job
= get_object_or_404(Job
, cluster__slug
=cluster_slug
, job_id
=job_id
)
69 # if not a superuser, check permissions on the object itself
70 cluster_admin
= user
.is_superuser
or user
.has_perm('admin', cluster
)
73 if isinstance(obj
, (Cluster
, Node
)):
74 raise Http403(NO_PRIVS
)
75 elif isinstance(obj
, (VirtualMachine
,)):
76 # object is a virtual machine, check perms on VM and on Cluster
77 if not (obj
.owner_id
== user
.get_profile().pk
78 or user
.has_perm('admin', obj
)
79 or user
.has_perm('admin', obj
.cluster
)):
80 raise Http403(NO_PRIVS
)
82 # If the job points to an object, and the job is the most recent job on
83 # the object, then clear it from the object.
85 if obj
.last_job
== job
:
87 obj
.ignore_cache
= False
90 # "Clear" the job. With extreme prejudice.
93 return HttpResponse('1', mimetype
='application/json')