Refactor get_all_vmids -> get_all_active_vmids

We now get this info from opennebula
This commit is contained in:
PCoder 2019-05-11 02:23:51 +02:00
parent 0b85784fd3
commit 7f6d4c1c53
2 changed files with 11 additions and 26 deletions

View file

@ -464,7 +464,7 @@ class SSHKeyDeleteView(LoginRequiredMixin, DeleteView):
keys = UserHostingKey.objects.filter(user=self.request.user)
keys_to_save = [k.public_key for k in keys if k != public_key]
manager.save_key_in_opennebula_user('\n'.join(keys_to_save))
vm_ids = manager.get_all_vmids()
vm_ids = manager.get_vms()
if len(vm_ids) > 0 and len(keys_to_save) > 0:
for vm_id in vm_ids:
manager.save_key_in_vm_template(vm_id, '\n'.join(keys_to_save))
@ -517,7 +517,7 @@ class SSHKeyChoiceView(LoginRequiredMixin, View):
password=owner.password
)
keys = get_all_public_keys(request.user)
vm_ids = manager.get_all_vmids()
vm_ids = manager.get_all_active_vmids()
manager.save_key_in_opennebula_user('\n'.join(keys))
if len(vm_ids) > 0 and len(keys) > 0:
for vm_id in vm_ids:
@ -574,7 +574,7 @@ class SSHKeyCreateView(LoginRequiredMixin, FormView):
keys = UserHostingKey.objects.filter(user=self.request.user)
keys_to_save = [k.public_key for k in keys]
manager.save_key_in_opennebula_user('\n'.join(keys_to_save))
vm_ids = manager.get_all_vmids()
vm_ids = manager.get_all_active_vmids()
if len(vm_ids) > 0 and len(keys) > 0:
for vm_id in vm_ids:
manager.save_key_in_vm_template(vm_id, '\n'.join(keys_to_save))

View file

@ -168,7 +168,7 @@ class OpenNebulaManager():
raise
return user_pool
def _get_vm_pool(self, infoextended=True):
def _get_vm_pool(self, infoextended=True, vm_state=-1):
"""
# filter:
# -4: Resources belonging to the users primary group
@ -202,7 +202,7 @@ class OpenNebulaManager():
if infoextended:
vm_pool.infoextended(
filter=-1, # User's resources and any of his groups
vm_state=-1 # Look for VMs in any state, except DONE
vm_state=vm_state
)
else:
vm_pool.info()
@ -210,13 +210,6 @@ class OpenNebulaManager():
except AttributeError:
logger.error(
'Could not connect via client, using oneadmin instead')
try:
vm_pool = oca.VirtualMachinePool(self.oneadmin_client)
vm_pool.info(filter=-2)
return vm_pool
except:
raise ConnectionRefusedError
except ConnectionRefusedError:
logger.error(
'Could not connect to host: {host} via protocol {protocol}'.format(
@ -228,9 +221,9 @@ class OpenNebulaManager():
except:
raise ConnectionRefusedError
def get_vms(self):
def get_vms(self, vm_state=-1):
try:
return self._get_vm_pool()
return self._get_vm_pool(vm_state=vm_state)
except ConnectionRefusedError:
raise ConnectionRefusedError
@ -637,17 +630,9 @@ class OpenNebulaManager():
"Keys and/or hosts are empty, so not managing any keys"
)
def get_all_vmids(self):
owner = CustomUser.objects.filter(email=self.email).first()
all_orders = HostingOrder.objects.filter(customer__user=owner)
vm_ids = []
if len(all_orders) > 0:
logger.debug("The user {} has 1 or more VMs. We need to configure "
"the ssh keys.".format(self.email))
vm_ids = [order.vm_id for order in all_orders]
else:
logger.debug("The user {} has no VMs. We don't need to configure "
"the ssh keys.".format(self.email))
def get_all_active_vmids(self):
vm_pool = self.get_vms(vm_state=3) # get active vms of the user
vm_ids = [vm.id for vm in vm_pool]
return vm_ids
def get_all_hosts(self):