From 3133bde0e9879909aa68fcd4fe3fd4b18f15f150 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 11 May 2019 09:15:08 +0200 Subject: [PATCH] Don't set the key in the live template --- hosting/views.py | 15 +-------------- opennebula_api/models.py | 20 +++++++------------- 2 files changed, 8 insertions(+), 27 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index 5a3e06b2..dc09644e 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -464,10 +464,6 @@ 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_active_vmids() - 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)) return super(SSHKeyDeleteView, self).delete(request, *args, **kwargs) @@ -517,11 +513,7 @@ class SSHKeyChoiceView(LoginRequiredMixin, View): password=owner.password ) keys = get_all_public_keys(request.user) - 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: - manager.save_key_in_vm_template(vm_id, '\n'.join(keys)) return redirect(reverse_lazy('hosting:ssh_keys'), foo='bar') @@ -571,13 +563,8 @@ class SSHKeyCreateView(LoginRequiredMixin, FormView): email=owner.email, password=owner.password ) - keys = UserHostingKey.objects.filter(user=self.request.user) - keys_to_save = [k.public_key for k in keys] + keys_to_save = get_all_public_keys(self.request.user) manager.save_key_in_opennebula_user('\n'.join(keys_to_save)) - 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)) return HttpResponseRedirect(self.success_url) def post(self, request, *args, **kwargs): diff --git a/opennebula_api/models.py b/opennebula_api/models.py index 819bd803..01bd065a 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -168,7 +168,7 @@ class OpenNebulaManager(): raise return user_pool - def _get_vm_pool(self, infoextended=True, vm_state=-1): + def _get_vm_pool(self, infoextended=True): """ # filter: # -4: Resources belonging to the user’s primary group @@ -201,15 +201,14 @@ class OpenNebulaManager(): vm_pool = oca.VirtualMachinePool(self.client) if infoextended: vm_pool.infoextended( - filter=-1, # User's resources and any of his groups - vm_state=vm_state + filter=-1, # User's resources and any of his groups + vm_state=-1 # Look for VMs in any state, except DONE ) else: vm_pool.info() return vm_pool - except AttributeError: - logger.error( - 'Could not connect via client, using oneadmin instead') + except AttributeError as ae: + logger.error("AttributeError : %s" % str(ae)) except ConnectionRefusedError: logger.error( 'Could not connect to host: {host} via protocol {protocol}'.format( @@ -221,9 +220,9 @@ class OpenNebulaManager(): except: raise ConnectionRefusedError - def get_vms(self, vm_state=-1): + def get_vms(self): try: - return self._get_vm_pool(vm_state=vm_state) + return self._get_vm_pool() except ConnectionRefusedError: raise ConnectionRefusedError @@ -630,11 +629,6 @@ class OpenNebulaManager(): "Keys and/or hosts are empty, so not managing any keys" ) - 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): """ A utility function to obtain all hosts of this owner