Ticket #3957 (partial) - ganeti_webmgr uses ganeti python namespace:
[ganeti_webmgr.git] / ganeti_web / views / importing_nodes.py
blob606a71338ec003b2b2ce325f7fc322728d3056c5
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
19 @login_required
20 def importing(request):
21 """
22 View that loads main importing view
23 """
24 user = request.user
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))
32 @login_required
33 def missing_ganeti(request):
34 """
35 View for displaying VirtualMachines missing from the ganeti cluster
36 """
37 user = request.user
38 if user.is_superuser:
39 clusters = Cluster.objects.all()
40 else:
41 clusters = user.get_objects_any_perms(Cluster, ['admin'])
42 if not clusters:
43 return render_403(request, _('You do not have sufficient privileges'))
45 nodes = []
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)
53 if form.is_valid():
54 # update all selected Nodes
55 data = form.cleaned_data
56 node_ids = data['nodes']
57 Node.objects.filter(hostname__in=node_ids).delete()
59 else:
60 form = NodeForm(nodes)
62 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()
68 node_hostnames.sort()
70 node_tuple_list = []
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", {
77 'nodes': nodes,
78 'form':form,
80 context_instance=RequestContext(request),
84 @login_required
85 def missing_db(request):
86 """
87 View for displaying Nodes missing from the database
88 """
89 user = request.user
90 if user.is_superuser:
91 clusters = Cluster.objects.all()
92 else:
93 clusters = user.get_objects_any_perms(Cluster, ['admin'])
94 if not clusters:
95 return render_403(request, _('You do not have sufficient privileges'))
97 nodes = []
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)
106 if form.is_valid():
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)
116 node.refresh()
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)
127 else:
128 form = NodeForm(nodes)
130 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", {
144 'nodes': nodes,
145 'form':form,
147 context_instance=RequestContext(request),