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

automatic remove instances with the same name

sometimes (fe. after manual migrations) some instances appear
multiple times in the database. this patch removes them from
the database while refreshing instance uuid. only under superuser.
records log message.
This commit is contained in:
Ing. Jan KRCMAR 2017-04-20 14:46:42 +02:00
parent 6858448b60
commit b7457a4362

View file

@ -57,6 +57,25 @@ def instances(request):
info['first_user'] = None info['first_user'] = None
return info return info
def refresh_instance_database(comp, vm, info):
instances_count = Instance.objects.filter(name=vm).count()
if instances_count > 1:
for i in Instance.objects.filter(name=vm):
user_instances_count = UserInstance.objects.filter(instance=i).count()
if user_instances_count == 0:
addlogmsg(request.user.username, i.name, _("Deleting due to multiple records."))
i.delete()
try:
check_uuid = Instance.objects.get(compute_id=comp.id, name=vm)
if check_uuid.uuid != info['uuid']:
check_uuid.save()
all_host_vms[comp.id, comp.name][vm]['is_template'] = check_uuid.is_template
all_host_vms[comp.id, comp.name][vm]['userinstances'] = get_userinstances_info(check_uuid)
except Instance.DoesNotExist:
check_uuid = Instance(compute_id=comp.id, name=vm, uuid=info['uuid'])
check_uuid.save()
if not request.user.is_superuser: if not request.user.is_superuser:
user_instances = UserInstance.objects.filter(user_id=request.user.id) user_instances = UserInstance.objects.filter(user_id=request.user.id)
for usr_inst in user_instances: for usr_inst in user_instances:
@ -76,15 +95,8 @@ def instances(request):
if conn.get_host_instances(): if conn.get_host_instances():
all_host_vms[comp.id, comp.name] = conn.get_host_instances() all_host_vms[comp.id, comp.name] = conn.get_host_instances()
for vm, info in conn.get_host_instances().items(): for vm, info in conn.get_host_instances().items():
try: refresh_instance_database(comp, vm, info)
check_uuid = Instance.objects.get(compute_id=comp.id, name=vm)
if check_uuid.uuid != info['uuid']:
check_uuid.save()
all_host_vms[comp.id, comp.name][vm]['is_template'] = check_uuid.is_template
all_host_vms[comp.id, comp.name][vm]['userinstances'] = get_userinstances_info(check_uuid)
except Instance.DoesNotExist:
check_uuid = Instance(compute_id=comp.id, name=vm, uuid=info['uuid'])
check_uuid.save()
conn.close() conn.close()
except libvirtError as lib_err: except libvirtError as lib_err:
error_messages.append(lib_err) error_messages.append(lib_err)