1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-24 23:25:24 +00:00

Refactoring

This commit is contained in:
Retspen 2015-02-27 11:51:33 +02:00
parent dd5f98cbe8
commit fa3df5bff3
6 changed files with 73 additions and 72 deletions

View file

@ -28,8 +28,9 @@ def instances(request):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index')) return HttpResponseRedirect(reverse('index'))
computes = Compute.objects.filter() error_messages = []
all_host_vms = {} all_host_vms = {}
computes = Compute.objects.filter()
for compute in computes: for compute in computes:
if connection_manager.host_is_up(compute.type, compute.hostname): if connection_manager.host_is_up(compute.type, compute.hostname):
@ -37,8 +38,8 @@ def instances(request):
conn = wvmHostDetails(compute, compute.login, compute.password, compute.type) conn = wvmHostDetails(compute, compute.login, compute.password, compute.type)
all_host_vms[compute.id, compute.name] = conn.get_host_instances() all_host_vms[compute.id, compute.name] = conn.get_host_instances()
conn.close() conn.close()
except libvirtError: except libvirtError as lib_err:
pass error_messages.append(lib_err)
return render(request, 'instances.html', locals()) return render(request, 'instances.html', locals())

View file

@ -16,8 +16,8 @@ def interfaces(request, compute_id):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index')) return HttpResponseRedirect(reverse('index'))
errors = []
ifaces_all = [] ifaces_all = []
error_messages = []
compute = Compute.objects.get(id=compute_id) compute = Compute.objects.get(id=compute_id)
try: try:
@ -45,8 +45,8 @@ def interfaces(request, compute_id):
data['stp'], data['delay']) data['stp'], data['delay'])
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
conn.close() conn.close()
except libvirtError as err: except libvirtError as lib_err:
errors.append(err) error_messages.append(lib_err)
return render(request, 'interfaces.html', locals()) return render(request, 'interfaces.html', locals())
@ -60,8 +60,8 @@ def interface(request, compute_id, iface):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index')) return HttpResponseRedirect(reverse('index'))
errors = []
ifaces_all = [] ifaces_all = []
error_messages = []
compute = Compute.objects.get(id=compute_id) compute = Compute.objects.get(id=compute_id)
try: try:
@ -89,9 +89,9 @@ def interface(request, compute_id, iface):
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
if 'delete' in request.POST: if 'delete' in request.POST:
conn.delete_iface() conn.delete_iface()
return HttpResponseRedirect(reverse('interfaces', args=[host_id])) return HttpResponseRedirect(reverse('interfaces', args=[compute_id]))
conn.close() conn.close()
except libvirtError as err: except libvirtError as lib_err:
errors.append(err) error_messages.append(lib_err)
return render(request, 'interface.html', locals()) return render(request, 'interface.html', locals())

View file

@ -18,7 +18,7 @@ def networks(request, compute_id):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index')) return HttpResponseRedirect(reverse('index'))
errors = [] error_messages = []
compute = Compute.objects.get(id=compute_id) compute = Compute.objects.get(id=compute_id)
try: try:
@ -35,23 +35,23 @@ def networks(request, compute_id):
data = form.cleaned_data data = form.cleaned_data
if data['name'] in networks: if data['name'] in networks:
msg = _("Pool name already in use") msg = _("Pool name already in use")
errors.append(msg) error_messages.append(msg)
if data['forward'] == 'bridge' and data['bridge_name'] == '': if data['forward'] == 'bridge' and data['bridge_name'] == '':
errors.append('Please enter bridge name') error_messages.append('Please enter bridge name')
try: try:
gateway, netmask, dhcp = network_size(data['subnet'], data['dhcp']) gateway, netmask, dhcp = network_size(data['subnet'], data['dhcp'])
except: except:
msg = _("Input subnet pool error") error_msg = _("Input subnet pool error")
errors.append(msg) error_messages.append(error_msg)
if not errors: if not error_messages:
conn.create_network(data['name'], data['forward'], gateway, netmask, conn.create_network(data['name'], data['forward'], gateway, netmask,
dhcp, data['bridge_name'], data['openvswitch'], data['fixed']) dhcp, data['bridge_name'], data['openvswitch'], data['fixed'])
return HttpResponseRedirect(reverse('network', args=[host_id, data['name']])) return HttpResponseRedirect(reverse('network', args=[compute_id, data['name']]))
conn.close() conn.close()
except libvirtError as err: except libvirtError as lib_err:
errors.append(err) error_messages.append(lib_err)
return render(request, 'network.html', locals()) return render(request, 'networks.html', locals())
def network(request, compute_id, pool): def network(request, compute_id, pool):
@ -63,7 +63,7 @@ def network(request, compute_id, pool):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index')) return HttpResponseRedirect(reverse('index'))
errors = [] error_messages = []
compute = Compute.objects.get(id=compute_id) compute = Compute.objects.get(id=compute_id)
try: try:
@ -81,40 +81,40 @@ def network(request, compute_id, pool):
ipv4_dhcp_range_end = conn.get_ipv4_dhcp_range_end() ipv4_dhcp_range_end = conn.get_ipv4_dhcp_range_end()
ipv4_network = conn.get_ipv4_network() ipv4_network = conn.get_ipv4_network()
fixed_address = conn.get_mac_ipaddr() fixed_address = conn.get_mac_ipaddr()
except libvirtError as err: except libvirtError as lib_err:
errors.append(err) error_messages.append(lib_err)
if request.method == 'POST': if request.method == 'POST':
if 'start' in request.POST: if 'start' in request.POST:
try: try:
conn.start() conn.start()
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg: except libvirtError as lib_err:
errors.append(error_msg.message) error_messages.append(lib_err.message)
if 'stop' in request.POST: if 'stop' in request.POST:
try: try:
conn.stop() conn.stop()
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg: except libvirtError as lib_err:
errors.append(error_msg.message) error_messages.append(lib_err.message)
if 'delete' in request.POST: if 'delete' in request.POST:
try: try:
conn.delete() conn.delete()
return HttpResponseRedirect(reverse('networks', args=[host_id])) return HttpResponseRedirect(reverse('networks', args=[compute_id]))
except libvirtError as error_msg: except libvirtError as lib_err:
errors.append(error_msg.message) error_messages.append(lib_err.message)
if 'set_autostart' in request.POST: if 'set_autostart' in request.POST:
try: try:
conn.set_autostart(1) conn.set_autostart(1)
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg: except libvirtError as lib_err:
errors.append(error_msg.message) error_messages.append(lib_err.message)
if 'unset_autostart' in request.POST: if 'unset_autostart' in request.POST:
try: try:
conn.set_autostart(0) conn.set_autostart(0)
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg: except libvirtError as lib_err:
errors.append(error_msg.message) error_messages.append(lib_err.message)
conn.close() conn.close()

View file

@ -16,8 +16,8 @@ def secrets(request, compute_id):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index')) return HttpResponseRedirect(reverse('index'))
errors = []
secrets_all = [] secrets_all = []
error_messages = []
compute = Compute.objects.get(id=compute_id) compute = Compute.objects.get(id=compute_id)
try: try:
@ -30,8 +30,8 @@ def secrets(request, compute_id):
secrt = conn.get_secret(uuid) secrt = conn.get_secret(uuid)
try: try:
secret_value = conn.get_secret_value(uuid) secret_value = conn.get_secret_value(uuid)
except: except libvirtError as lib_err:
secret_value = '' secret_value = None
secrets_all.append({'usage': secrt.usageID(), secrets_all.append({'usage': secrt.usageID(),
'uuid': secrt.UUIDString(), 'uuid': secrt.UUIDString(),
'usageType': secrt.usageType(), 'usageType': secrt.usageType(),
@ -54,6 +54,6 @@ def secrets(request, compute_id):
conn.set_secret_value(uuid, value) conn.set_secret_value(uuid, value)
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
except libvirtError as err: except libvirtError as err:
errors.append(err) error_messages.append(err)
return render(request, 'secrets.html', locals()) return render(request, 'secrets.html', locals())

View file

@ -17,7 +17,7 @@ def storages(request, compute_id):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index')) return HttpResponseRedirect(reverse('index'))
errors = [] error_messages = []
compute = Compute.objects.get(id=compute_id) compute = Compute.objects.get(id=compute_id)
try: try:
@ -35,15 +35,15 @@ def storages(request, compute_id):
data = form.cleaned_data data = form.cleaned_data
if data['name'] in storages: if data['name'] in storages:
msg = _("Pool name already use") msg = _("Pool name already use")
errors.append(msg) error_messages.append(msg)
if data['stg_type'] == 'rbd': if data['stg_type'] == 'rbd':
if not data['secret']: if not data['secret']:
msg = _("You need create secret for pool") msg = _("You need create secret for pool")
errors.append(msg) error_messages.append(msg)
if not data['ceph_pool'] and not data['ceph_host'] and not data['ceph_user']: if not data['ceph_pool'] and not data['ceph_host'] and not data['ceph_user']:
msg = _("You need input all fields for creating ceph pool") msg = _("You need input all fields for creating ceph pool")
errors.append(msg) error_messages.append(msg)
if not errors: if not error_messages:
if data['stg_type'] == 'rbd': if data['stg_type'] == 'rbd':
conn.create_storage_ceph(data['stg_type'], data['name'], conn.create_storage_ceph(data['stg_type'], data['name'],
data['ceph_pool'], data['ceph_host'], data['ceph_pool'], data['ceph_host'],
@ -54,10 +54,10 @@ def storages(request, compute_id):
data['source_format'], data['target']) data['source_format'], data['target'])
else: else:
conn.create_storage(data['stg_type'], data['name'], data['source'], data['target']) conn.create_storage(data['stg_type'], data['name'], data['source'], data['target'])
return HttpResponseRedirect(reverse('storage', args=[host_id, data['name']])) return HttpResponseRedirect(reverse('storage', args=[compute_id, data['name']]))
conn.close() conn.close()
except libvirtError as err: except libvirtError as lib_err:
errors.append(err) error_messages.append(lib_err)
return render(request, 'storages.html', locals()) return render(request, 'storages.html', locals())
@ -78,7 +78,7 @@ def storage(request, compute_id, pool):
destination.write(chunk) destination.write(chunk)
destination.close() destination.close()
errors = [] error_messages = []
compute = Compute.objects.get(id=compute_id) compute = Compute.objects.get(id=compute_id)
meta_prealloc = False meta_prealloc = False
@ -107,40 +107,40 @@ def storage(request, compute_id, pool):
volumes = conn.update_volumes() volumes = conn.update_volumes()
else: else:
volumes = None volumes = None
except libvirtError as err: except libvirtError as lib_err:
errors.append(err) error_messages.append(lib_err)
if request.method == 'POST': if request.method == 'POST':
if 'start' in request.POST: if 'start' in request.POST:
try: try:
conn.start() conn.start()
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg: except libvirtError as lib_err:
errors.append(error_msg.message) error_messages.append(lib_err.message)
if 'stop' in request.POST: if 'stop' in request.POST:
try: try:
conn.stop() conn.stop()
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg: except libvirtError as lib_err:
errors.append(error_msg.message) error_messages.append(lib_err.message)
if 'delete' in request.POST: if 'delete' in request.POST:
try: try:
conn.delete() conn.delete()
return HttpResponseRedirect(reverse('storages', args=[host_id])) return HttpResponseRedirect(reverse('storages', args=[host_id]))
except libvirtError as error_msg: except libvirtError as lib_err:
errors.append(error_msg.message) error_messages.append(lib_err.message)
if 'set_autostart' in request.POST: if 'set_autostart' in request.POST:
try: try:
conn.set_autostart(1) conn.set_autostart(1)
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg: except libvirtError as lib_err:
errors.append(error_msg.message) error_messages.append(lib_err.message)
if 'unset_autostart' in request.POST: if 'unset_autostart' in request.POST:
try: try:
conn.set_autostart(0) conn.set_autostart(0)
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg: except libvirtError as lib_err:
errors.append(error_msg.message) error_messages.append(lib_err.message)
if 'add_volume' in request.POST: if 'add_volume' in request.POST:
form = AddImage(request.POST) form = AddImage(request.POST)
if form.is_valid(): if form.is_valid():
@ -150,20 +150,20 @@ def storage(request, compute_id, pool):
try: try:
conn.create_volume(data['name'], data['size'], data['format'], meta_prealloc) conn.create_volume(data['name'], data['size'], data['format'], meta_prealloc)
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
except libvirtError as err: except libvirtError as lib_err:
errors.append(err) error_messages.append(lib_err)
if 'del_volume' in request.POST: if 'del_volume' in request.POST:
volname = request.POST.get('volname', '') volname = request.POST.get('volname', '')
try: try:
vol = conn.get_volume(volname) vol = conn.get_volume(volname)
vol.delete(0) vol.delete(0)
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg: except libvirtError as lib_err:
errors.append(error_msg.message) error_messages.append(lib_err.message)
if 'iso_upload' in request.POST: if 'iso_upload' in request.POST:
if str(request.FILES['file']) in conn.update_volumes(): if str(request.FILES['file']) in conn.update_volumes():
msg = _("ISO image already exist") error_msg = _("ISO image already exist")
errors.append(msg) error_messages.append(error_msg)
else: else:
handle_uploaded_file(path, request.FILES['file']) handle_uploaded_file(path, request.FILES['file'])
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
@ -175,8 +175,8 @@ def storage(request, compute_id, pool):
meta_prealloc = 0 meta_prealloc = 0
if img_name in conn.update_volumes(): if img_name in conn.update_volumes():
msg = _("Name of volume name already use") msg = _("Name of volume name already use")
errors.append(msg) error_messages.append(msg)
if not errors: if not error_messages:
if data['convert']: if data['convert']:
format = data['format'] format = data['format']
if data['meta_prealloc'] and data['format'] == 'qcow2': if data['meta_prealloc'] and data['format'] == 'qcow2':
@ -186,8 +186,8 @@ def storage(request, compute_id, pool):
try: try:
conn.clone_volume(data['image'], data['name'], format, meta_prealloc) conn.clone_volume(data['image'], data['name'], format, meta_prealloc)
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
except libvirtError as err: except libvirtError as lib_err:
errors.append(err) error_messages.append(lib_err)
conn.close() conn.close()
return render(request, 'storage.html', locals()) return render(request, 'storage.html', locals())

View file

@ -1,8 +1,8 @@
{% if errors %} {% if error_messages %}
{% for error in errors %} {% for error in error_messages %}
<div class="alert alert-danger alert-dismissible" role="danger"> <div class="alert alert-danger alert-dismissible" role="danger">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Error:</strong> Better check yourself, you're not looking too good. <strong>Error:</strong> {{ error }}
</div> </div>
{% endfor %} {% endfor %}
{% endif %} {% endif %}