1 from django
import forms
2 from django
.contrib
.auth
.decorators
import login_required
3 from django
.shortcuts
import render_to_response
4 from django
.template
.context
import RequestContext
5 from django
.utils
.translation
import ugettext
as _
7 from ganeti_web
.models
import Cluster
, Node
, VirtualMachine
8 from ganeti_web
.views
import render_403
11 class NodeForm(forms
.Form
):
12 nodes
= forms
.MultipleChoiceField()
14 def __init__(self
, choices
, *args
, **kwargs
):
15 super(NodeForm
, self
).__init
__(*args
, **kwargs
)
16 self
.fields
['nodes'].choices
= choices
20 def importing(request
):
22 View that loads main importing view
25 if not user
.is_superuser
or user
.get_objects_any_perms(Cluster
, ['admin']):
26 return render_403(request
, _('You do not have sufficient privileges'))
28 return render_to_response('importing/nodes/main.html',
29 context_instance
=RequestContext(request
))
33 def missing_ganeti(request
):
35 View for displaying VirtualMachines missing from the ganeti cluster
39 clusters
= Cluster
.objects
.all()
41 clusters
= user
.get_objects_any_perms(Cluster
, ['admin'])
43 return render_403(request
, _('You do not have sufficient privileges'))
46 for cluster
in clusters
:
47 for node
in cluster
.nodes_missing_in_ganeti
:
48 nodes
.append((node
, node
))
50 if request
.method
== 'POST':
51 # process updates if this was a form submission
52 form
= NodeForm(nodes
, request
.POST
)
54 # update all selected Nodes
55 data
= form
.cleaned_data
56 node_ids
= data
['nodes']
57 Node
.objects
.filter(hostname__in
=node_ids
).delete()
60 form
= NodeForm(nodes
)
63 for cluster
in clusters
:
64 for node
in cluster
.nodes_missing_in_ganeti
:
65 nodes
[node
] = (cluster
.hostname
, node
)
67 node_hostnames
= nodes
.keys()
71 for i
in node_hostnames
:
72 node_tuple_list
.append((i
, nodes
[i
][0], nodes
[i
][1]))
74 nodes
= node_tuple_list
76 return render_to_response("importing/nodes/missing.html", {
80 context_instance
=RequestContext(request
),
85 def missing_db(request
):
87 View for displaying Nodes missing from the database
91 clusters
= Cluster
.objects
.all()
93 clusters
= user
.get_objects_any_perms(Cluster
, ['admin'])
95 return render_403(request
, _('You do not have sufficient privileges'))
98 for cluster
in clusters
:
99 for hostname
in cluster
.nodes_missing_in_db
:
100 nodes
.append(('%s:%s' % (cluster
.id, hostname
), hostname
))
102 if request
.method
== 'POST':
103 # process updates if this was a form submission
104 form
= NodeForm(nodes
, request
.POST
)
107 # update all selected Nodes
108 data
= form
.cleaned_data
109 node_ids
= data
['nodes']
111 # create missing Nodes
112 for node
in node_ids
:
113 cluster_id
, host
= node
.split(':')
114 cluster
= Cluster
.objects
.get(id=cluster_id
)
115 node
= Node
.objects
.create(hostname
=host
, cluster
=cluster
)
118 # refresh all vms on this node
119 VirtualMachine
.objects\
120 .filter(cluster
=cluster
, hostname__in
=node
.info
['pinst_list']) \
121 .update(primary_node
=node
)
123 VirtualMachine
.objects\
124 .filter(cluster
=cluster
, hostname__in
=node
.info
['sinst_list']) \
125 .update(secondary_node
=node
)
128 form
= NodeForm(nodes
)
131 for cluster
in clusters
:
132 for hostname
in cluster
.nodes_missing_in_db
:
133 nodes
[hostname
] = ('%s:%s' % (cluster
.id, hostname
), cluster
.hostname
, hostname
)
134 node_hostnames
= nodes
.keys()
135 node_hostnames
.sort()
137 nodes_tuple_list
= []
138 for i
in node_hostnames
:
139 nodes_tuple_list
.append(nodes
[i
])
141 nodes
= nodes_tuple_list
143 return render_to_response("importing/nodes/import.html", {
147 context_instance
=RequestContext(request
),