Don't set the key in the live template

This commit is contained in:
PCoder 2019-05-11 09:15:08 +02:00
parent b189371a7b
commit 3133bde0e9
2 changed files with 8 additions and 27 deletions

View file

@ -464,10 +464,6 @@ class SSHKeyDeleteView(LoginRequiredMixin, DeleteView):
keys = UserHostingKey.objects.filter(user=self.request.user) keys = UserHostingKey.objects.filter(user=self.request.user)
keys_to_save = [k.public_key for k in keys if k != public_key] 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)) 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) return super(SSHKeyDeleteView, self).delete(request, *args, **kwargs)
@ -517,11 +513,7 @@ class SSHKeyChoiceView(LoginRequiredMixin, View):
password=owner.password password=owner.password
) )
keys = get_all_public_keys(request.user) keys = get_all_public_keys(request.user)
vm_ids = manager.get_all_active_vmids()
manager.save_key_in_opennebula_user('\n'.join(keys)) 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') return redirect(reverse_lazy('hosting:ssh_keys'), foo='bar')
@ -571,13 +563,8 @@ class SSHKeyCreateView(LoginRequiredMixin, FormView):
email=owner.email, email=owner.email,
password=owner.password password=owner.password
) )
keys = UserHostingKey.objects.filter(user=self.request.user) keys_to_save = get_all_public_keys(self.request.user)
keys_to_save = [k.public_key for k in keys]
manager.save_key_in_opennebula_user('\n'.join(keys_to_save)) 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) return HttpResponseRedirect(self.success_url)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):

View file

@ -168,7 +168,7 @@ class OpenNebulaManager():
raise raise
return user_pool return user_pool
def _get_vm_pool(self, infoextended=True, vm_state=-1): def _get_vm_pool(self, infoextended=True):
""" """
# filter: # filter:
# -4: Resources belonging to the users primary group # -4: Resources belonging to the users primary group
@ -201,15 +201,14 @@ class OpenNebulaManager():
vm_pool = oca.VirtualMachinePool(self.client) vm_pool = oca.VirtualMachinePool(self.client)
if infoextended: if infoextended:
vm_pool.infoextended( vm_pool.infoextended(
filter=-1, # User's resources and any of his groups filter=-1, # User's resources and any of his groups
vm_state=vm_state vm_state=-1 # Look for VMs in any state, except DONE
) )
else: else:
vm_pool.info() vm_pool.info()
return vm_pool return vm_pool
except AttributeError: except AttributeError as ae:
logger.error( logger.error("AttributeError : %s" % str(ae))
'Could not connect via client, using oneadmin instead')
except ConnectionRefusedError: except ConnectionRefusedError:
logger.error( logger.error(
'Could not connect to host: {host} via protocol {protocol}'.format( 'Could not connect to host: {host} via protocol {protocol}'.format(
@ -221,9 +220,9 @@ class OpenNebulaManager():
except: except:
raise ConnectionRefusedError raise ConnectionRefusedError
def get_vms(self, vm_state=-1): def get_vms(self):
try: try:
return self._get_vm_pool(vm_state=vm_state) return self._get_vm_pool()
except ConnectionRefusedError: except ConnectionRefusedError:
raise ConnectionRefusedError raise ConnectionRefusedError
@ -630,11 +629,6 @@ class OpenNebulaManager():
"Keys and/or hosts are empty, so not managing any keys" "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): def get_all_hosts(self):
""" """
A utility function to obtain all hosts of this owner A utility function to obtain all hosts of this owner