Move default template for tables out of template into table base class
[ganeti_webmgr.git] / ganeti_web / views / importing_nodes.py
blob94c39d641339b0938d5d8023be852875f39e4b98
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.
19 from django.contrib.auth.decorators import login_required
20 from django.shortcuts import render_to_response
21 from django.template.context import RequestContext
23 from ganeti_web.forms.importing import NodeForm
24 from ganeti_web.middleware import Http403
25 from ganeti_web.models import Cluster, Node, VirtualMachine
26 from ganeti_web.views.generic import NO_PRIVS
29 @login_required
30 def importing(request):
31 """
32 View that loads main importing view
33 """
34 user = request.user
35 if not user.is_superuser or user.get_objects_any_perms(Cluster, ['admin']):
36 raise Http403(NO_PRIVS)
38 return render_to_response('ganeti/importing/nodes/main.html',
39 context_instance=RequestContext(request))
42 @login_required
43 def missing_ganeti(request):
44 """
45 View for displaying VirtualMachines missing from the ganeti cluster
46 """
47 user = request.user
48 if user.is_superuser:
49 clusters = Cluster.objects.all()
50 else:
51 clusters = user.get_objects_any_perms(Cluster, ['admin'])
52 if not clusters:
53 raise Http403(NO_PRIVS)
55 nodes = []
56 for cluster in clusters:
57 for node in cluster.nodes_missing_in_ganeti:
58 nodes.append((node, node))
60 if request.method == 'POST':
61 # process updates if this was a form submission
62 form = NodeForm(nodes, request.POST)
63 if form.is_valid():
64 # update all selected Nodes
65 data = form.cleaned_data
66 node_ids = data['nodes']
67 Node.objects.filter(hostname__in=node_ids).delete()
69 else:
70 form = NodeForm(nodes)
72 nodes = {}
73 for cluster in clusters:
74 for node in cluster.nodes_missing_in_ganeti:
75 nodes[node] = (cluster.hostname, node)
77 node_hostnames = nodes.keys()
78 node_hostnames.sort()
80 node_tuple_list = []
81 for i in node_hostnames:
82 node_tuple_list.append((i, nodes[i][0], nodes[i][1]))
84 nodes = node_tuple_list
86 return render_to_response("ganeti/importing/nodes/missing.html",
87 {'nodes': nodes,
88 'form': form, },
89 context_instance=RequestContext(request),
93 @login_required
94 def missing_db(request):
95 """
96 View for displaying Nodes missing from the database
97 """
98 user = request.user
99 if user.is_superuser:
100 clusters = Cluster.objects.all()
101 else:
102 clusters = user.get_objects_any_perms(Cluster, ['admin'])
103 if not clusters:
104 raise Http403(NO_PRIVS)
106 nodes = []
107 for cluster in clusters:
108 for hostname in cluster.nodes_missing_in_db:
109 nodes.append(('%s:%s' % (cluster.id, hostname), hostname))
111 if request.method == 'POST':
112 # process updates if this was a form submission
113 form = NodeForm(nodes, request.POST)
115 if form.is_valid():
116 # update all selected Nodes
117 data = form.cleaned_data
118 node_ids = data['nodes']
120 # create missing Nodes
121 for node in node_ids:
122 cluster_id, host = node.split(':')
123 cluster = Cluster.objects.get(id=cluster_id)
124 node = Node.objects.create(hostname=host, cluster=cluster)
125 node.refresh()
127 # refresh all vms on this node
128 VirtualMachine.objects \
129 .filter(cluster=cluster,
130 hostname__in=node.info['pinst_list']) \
131 .update(primary_node=node)
133 VirtualMachine.objects \
134 .filter(cluster=cluster,
135 hostname__in=node.info['sinst_list']) \
136 .update(secondary_node=node)
138 else:
139 form = NodeForm(nodes)
141 nodes = {}
142 for cluster in clusters:
143 for hostname in cluster.nodes_missing_in_db:
144 nodes[hostname] = ('%s:%s' % (cluster.id, hostname),
145 cluster.hostname, hostname)
146 node_hostnames = nodes.keys()
147 node_hostnames.sort()
149 nodes_tuple_list = []
150 for i in node_hostnames:
151 nodes_tuple_list.append(nodes[i])
153 nodes = nodes_tuple_list
155 return render_to_response("ganeti/importing/nodes/import.html",
156 {'nodes': nodes,
157 'form': form, },
158 context_instance=RequestContext(request), )