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,
17 from collections
import defaultdict
19 from django
.contrib
.auth
.decorators
import login_required
20 from django
.core
.exceptions
import PermissionDenied
21 from django
.shortcuts
import render_to_response
22 from django
.template
import RequestContext
24 from ganeti_web
.forms
.importing \
25 import ImportForm
, OrphanForm
, VirtualMachineForm
26 from ganeti_web
.models
import VirtualMachine
, Cluster
27 from ganeti_web
.views
.generic
import NO_PRIVS
33 displays list of orphaned VirtualMachines, i.e. VirtualMachines without
38 clusters
= Cluster
.objects
.all()
40 clusters
= user
.get_objects_any_perms(Cluster
, ['admin'])
42 raise PermissionDenied(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
)
55 # update all selected VirtualMachines
56 data
= form
.cleaned_data
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
63 orphaned
= defaultdict(lambda: 0)
65 vm
= VirtualMachine
.objects
.get(id=id)
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
]
75 # strip cluster from vms
76 form
= ImportForm([(i
[0], i
[1]) for i
in vms_with_cluster
])
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",
87 context_instance
=RequestContext(request
), )
91 def missing_ganeti(request
):
93 View for displaying VirtualMachines missing from the ganeti cluster
97 clusters
= Cluster
.objects
.all()
99 clusters
= user
.get_objects_any_perms(Cluster
, ['admin'])
101 raise PermissionDenied(NO_PRIVS
)
104 for cluster
in clusters
:
105 for vm
in cluster
.missing_in_ganeti
:
108 if request
.method
== 'POST':
109 # process updates if this was a form submission
110 form
= VirtualMachineForm(vms
, request
.POST
)
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)
119 missing
[i
.cluster_id
] -= 1
123 # remove updated vms from the list
124 vms
= filter(lambda x
: unicode(x
[0]) not in vm_ids
, vms
)
127 form
= VirtualMachineForm(vms
)
130 for cluster
in clusters
:
131 for vm
in cluster
.missing_in_ganeti
:
132 vms
[vm
] = (cluster
.hostname
, vm
)
134 vmhostnames
= vms
.keys()
138 for i
in vmhostnames
:
139 vms_tuplelist
.append((i
, vms
[i
][0], vms
[i
][1]))
143 return render_to_response("ganeti/importing/missing.html",
146 context_instance
=RequestContext(request
), )
150 def missing_db(request
):
152 View for displaying VirtualMachines missing from the database
155 if user
.is_superuser
:
156 clusters
= Cluster
.objects
.all()
158 clusters
= user
.get_objects_any_perms(Cluster
, ['admin'])
160 raise PermissionDenied(NO_PRIVS
)
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
)
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)
181 cluster_id
, host
= vm
.split(':')
182 cluster
= Cluster
.objects
.get(id=cluster_id
)
183 VirtualMachine(hostname
=host
, cluster
=cluster
,
185 import_ready
[cluster
.pk
] -= 1
187 orphaned
[cluster
.pk
] += 1
189 # remove created vms from the list
190 vms
= filter(lambda x
: unicode(x
[0])
194 form
= ImportForm(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()
205 for i
in vmhostnames
:
206 vms_tuplelist
.append(vms
[i
])
210 return render_to_response("ganeti/importing/missing_db.html",
214 context_instance
=RequestContext(request
), )