Move default template for tables out of template into table base class
[ganeti_webmgr.git] / ganeti_web / views / importing.py
blob725b0fd3480cef7aecc28ee7ba22e065e9f6a5ba
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,
16 # USA.
17 from collections import defaultdict
19 from django.contrib.auth.decorators import login_required
20 from django.shortcuts import render_to_response
21 from django.template import RequestContext
23 from ganeti_web.forms.importing \
24 import ImportForm, OrphanForm, VirtualMachineForm
25 from ganeti_web.middleware import Http403
26 from ganeti_web.models import VirtualMachine, Cluster
27 from ganeti_web.views.generic import NO_PRIVS
30 @login_required
31 def orphans(request):
32 """
33 displays list of orphaned VirtualMachines, i.e. VirtualMachines without
34 an owner.
35 """
36 user = request.user
37 if user.is_superuser:
38 clusters = Cluster.objects.all()
39 else:
40 clusters = user.get_objects_any_perms(Cluster, ['admin'])
41 if not clusters:
42 raise Http403(NO_PRIVS)
44 vms_with_cluster = VirtualMachine.objects.filter(owner=None,
45 cluster__in=clusters) \
46 .order_by('hostname').values_list('id', 'hostname', 'cluster')
48 if request.method == 'POST':
49 # strip cluster from vms
50 vms = [(i[0], i[1]) for i in vms_with_cluster]
52 # process updates if this was a form submission
53 form = OrphanForm(vms, request.POST)
54 if form.is_valid():
55 # update all selected VirtualMachines
56 data = form.cleaned_data
57 owner = data['owner']
58 vm_ids = data['virtual_machines']
60 # update the owner and save the vm. This isn't the most efficient
61 # way of updating the VMs but we would otherwise need to group them
62 # by cluster
63 orphaned = defaultdict(lambda: 0)
64 for id in vm_ids:
65 vm = VirtualMachine.objects.get(id=id)
66 vm.owner = owner
67 vm.save()
68 orphaned[vm.cluster_id] -= 1
70 # remove updated vms from the list
71 vms_with_cluster = [i for i in vms_with_cluster
72 if unicode(i[0]) not in vm_ids]
74 else:
75 # strip cluster from vms
76 form = ImportForm([(i[0], i[1]) for i in vms_with_cluster])
78 clusterdict = {}
79 for i in clusters:
80 clusterdict[i.id] = i.hostname
81 vms = [(i[0], clusterdict[i[2]],
82 i[1]) for i in vms_with_cluster]
84 return render_to_response("ganeti/importing/orphans.html",
85 {'vms': vms,
86 'form': form, },
87 context_instance=RequestContext(request), )
90 @login_required
91 def missing_ganeti(request):
92 """
93 View for displaying VirtualMachines missing from the ganeti cluster
94 """
95 user = request.user
96 if user.is_superuser:
97 clusters = Cluster.objects.all()
98 else:
99 clusters = user.get_objects_any_perms(Cluster, ['admin'])
100 if not clusters:
101 raise Http403(NO_PRIVS)
103 vms = []
104 for cluster in clusters:
105 for vm in cluster.missing_in_ganeti:
106 vms.append((vm, vm))
108 if request.method == 'POST':
109 # process updates if this was a form submission
110 form = VirtualMachineForm(vms, request.POST)
111 if form.is_valid():
112 # update all selected VirtualMachines
113 data = form.cleaned_data
114 vm_ids = data['virtual_machines']
115 q = VirtualMachine.objects.filter(hostname__in=vm_ids)
117 missing = defaultdict(lambda: 0)
118 for i in q:
119 missing[i.cluster_id] -= 1
121 q.delete()
123 # remove updated vms from the list
124 vms = filter(lambda x: unicode(x[0]) not in vm_ids, vms)
126 else:
127 form = VirtualMachineForm(vms)
129 vms = {}
130 for cluster in clusters:
131 for vm in cluster.missing_in_ganeti:
132 vms[vm] = (cluster.hostname, vm)
134 vmhostnames = vms.keys()
135 vmhostnames.sort()
137 vms_tuplelist = []
138 for i in vmhostnames:
139 vms_tuplelist.append((i, vms[i][0], vms[i][1]))
141 vms = vms_tuplelist
143 return render_to_response("ganeti/importing/missing.html",
144 {'vms': vms,
145 'form': form, },
146 context_instance=RequestContext(request), )
149 @login_required
150 def missing_db(request):
152 View for displaying VirtualMachines missing from the database
154 user = request.user
155 if user.is_superuser:
156 clusters = Cluster.objects.all()
157 else:
158 clusters = user.get_objects_any_perms(Cluster, ['admin'])
159 if not clusters:
160 raise Http403(NO_PRIVS)
162 vms = []
163 for cluster in clusters:
164 for hostname in cluster.missing_in_db:
165 vms.append(('%s:%s' % (cluster.id, hostname), hostname))
167 if request.method == 'POST':
168 # process updates if this was a form submission
169 form = ImportForm(vms, request.POST)
170 if form.is_valid():
171 # update all selected VirtualMachines
172 data = form.cleaned_data
173 owner = data['owner']
174 vm_ids = data['virtual_machines']
176 import_ready = defaultdict(lambda: 0)
177 orphaned = defaultdict(lambda: 0)
179 # create missing VMs
180 for vm in vm_ids:
181 cluster_id, host = vm.split(':')
182 cluster = Cluster.objects.get(id=cluster_id)
183 VirtualMachine(hostname=host, cluster=cluster,
184 owner=owner).save()
185 import_ready[cluster.pk] -= 1
186 if owner is None:
187 orphaned[cluster.pk] += 1
189 # remove created vms from the list
190 vms = filter(lambda x: unicode(x[0])
191 not in vm_ids, vms)
193 else:
194 form = ImportForm(vms)
196 vms = {}
197 for cluster in clusters:
198 for hostname in cluster.missing_in_db:
199 vms[hostname] = ('%s:%s' % (cluster.id, hostname),
200 cluster.hostname, hostname)
201 vmhostnames = vms.keys()
202 vmhostnames.sort()
204 vms_tuplelist = []
205 for i in vmhostnames:
206 vms_tuplelist.append(vms[i])
208 vms = vms_tuplelist
210 return render_to_response("ganeti/importing/missing_db.html",
211 {'vms': vms,
212 'form': form,
214 context_instance=RequestContext(request), )