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,
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
.context
import RequestContext
24 from ganeti_web
.forms
.importing
import NodeForm
25 from ganeti_web
.models
import Cluster
, Node
, VirtualMachine
26 from ganeti_web
.views
.generic
import NO_PRIVS
30 def importing(request
):
32 View that loads main importing view
35 if not user
.is_superuser
or user
.get_objects_any_perms(Cluster
, ['admin']):
36 raise PermissionDenied(NO_PRIVS
)
38 return render_to_response('ganeti/importing/nodes/main.html',
39 context_instance
=RequestContext(request
))
43 def missing_ganeti(request
):
45 View for displaying VirtualMachines missing from the ganeti cluster
49 clusters
= Cluster
.objects
.all()
51 clusters
= user
.get_objects_any_perms(Cluster
, ['admin'])
53 raise PermissionDenied(NO_PRIVS
)
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
)
64 # update all selected Nodes
65 data
= form
.cleaned_data
66 node_ids
= data
['nodes']
67 Node
.objects
.filter(hostname__in
=node_ids
).delete()
70 form
= NodeForm(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()
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",
89 context_instance
=RequestContext(request
),
94 def missing_db(request
):
96 View for displaying Nodes missing from the database
100 clusters
= Cluster
.objects
.all()
102 clusters
= user
.get_objects_any_perms(Cluster
, ['admin'])
104 raise PermissionDenied(NO_PRIVS
)
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
)
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
)
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
)
139 form
= NodeForm(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",
158 context_instance
=RequestContext(request
), )