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
29 class JobDetailView(LoginRequiredMixin
, DetailView
):
31 template_name
= "ganeti/job/detail.html"
33 def get_object(self
, queryset
=None):
34 return get_object_or_404(Job
, job_id
=self
.kwargs
["job_id"],
35 cluster__slug
=self
.kwargs
["cluster_slug"])
37 def get_context_data(self
, **kwargs
):
38 job
= kwargs
["object"]
39 user
= self
.request
.user
40 admin
= user
.is_superuser
or user
.has_perm("admin", job
.cluster
)
44 "cluster_admin": admin
,
49 def status(request
, cluster_slug
, job_id
, rest
=False):
51 returns the raw info of a job
53 job
= get_object_or_404(Job
, cluster__slug
=cluster_slug
, job_id
=job_id
)
57 return HttpResponse(json
.dumps(job
.info
), mimetype
='application/json')
61 def clear(request
, cluster_slug
, job_id
):
67 cluster
= get_object_or_404(Cluster
, slug
=cluster_slug
)
68 job
= get_object_or_404(Job
, cluster__slug
=cluster_slug
, job_id
=job_id
)
71 # if not a superuser, check permissions on the object itself
72 cluster_admin
= user
.is_superuser
or user
.has_perm('admin', cluster
)
75 if isinstance(obj
, (Cluster
, Node
)):
76 raise Http403(NO_PRIVS
)
77 elif isinstance(obj
, (VirtualMachine
,)):
78 # object is a virtual machine, check perms on VM and on Cluster
79 if not (obj
.owner_id
== user
.get_profile().pk
80 or user
.has_perm('admin', obj
)
81 or user
.has_perm('admin', obj
.cluster
)):
82 raise Http403(NO_PRIVS
)
84 # If the job points to an object, and the job is the most recent job on
85 # the object, then clear it from the object.
87 if obj
.last_job
== job
:
89 obj
.ignore_cache
= False
92 # "Clear" the job. With extreme prejudice.
95 return HttpResponse('1', mimetype
='application/json')