Save user's key in opennebula

This commit is contained in:
PCoder 2019-05-10 09:19:31 +02:00
parent cd47af23f1
commit 1d70563ea2
3 changed files with 35 additions and 52 deletions

View file

@ -197,54 +197,7 @@ def create_vm_task(self, vm_template_id, user, specs, template, order_id):
get_or_create_vm_detail(custom_user, manager, vm_id)
if custom_user is not None:
public_keys = get_all_public_keys(custom_user)
keys = [{'value': key, 'state': True} for key in
public_keys]
if len(keys) > 0:
logger.debug(
"Calling configure on {host} for "
"{num_keys} keys".format(
host=vm_ipv6, num_keys=len(keys)
)
)
# Let's wait until the IP responds to ping before we
# run the cdist configure on the host
did_manage_public_key = False
for i in range(0, 15):
if ping_ok(vm_ipv6):
logger.debug(
"{} is pingable. Doing a "
"manage_public_key".format(vm_ipv6)
)
sleep(10)
manager.manage_public_key(
keys, hosts=[vm_ipv6]
)
did_manage_public_key = True
break
else:
logger.debug(
"Can't ping {}. Wait 5 secs".format(
vm_ipv6
)
)
sleep(5)
if not did_manage_public_key:
emsg = ("Waited for over 75 seconds for {} to be "
"pingable. But the VM was not reachable. "
"So, gave up manage_public_key. Please do "
"this manually".format(vm_ipv6))
logger.error(emsg)
email_data = {
'subject': '{} CELERY TASK INCOMPLETE: {} not '
'pingable for 75 seconds'.format(
settings.DCL_TEXT, vm_ipv6
),
'from_email': current_task.request.hostname,
'to': settings.DCL_ERROR_EMAILS_TO_LIST,
'body': emsg
}
email = EmailMessage(**email_data)
email.send()
manager.save_key_in_opennebula('\n'.join(public_keys))
else:
logger.debug("VM's ipv6 is None. Hence not created VMDetail")
except Exception as e:

View file

@ -36,6 +36,7 @@ from datacenterlight.cms_models import DCLCalculatorPluginModel
from datacenterlight.models import VMTemplate, VMPricing
from datacenterlight.utils import create_vm, get_cms_integration
from hosting.models import UserCardDetail
from utils.hosting_utils import get_all_public_keys
from membership.models import CustomUser, StripeCustomer
from opennebula_api.models import OpenNebulaManager
from opennebula_api.serializers import (
@ -460,7 +461,9 @@ class SSHKeyDeleteView(LoginRequiredMixin, DeleteView):
pk = self.kwargs.get('pk')
# Get user ssh key
public_key = UserHostingKey.objects.get(pk=pk).public_key
manager.manage_public_key([{'value': public_key, 'state': False}])
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('\n'.join(keys_to_save))
return super(SSHKeyDeleteView, self).delete(request, *args, **kwargs)
@ -509,8 +512,8 @@ class SSHKeyChoiceView(LoginRequiredMixin, View):
email=owner.email,
password=owner.password
)
public_key_str = public_key.decode()
manager.manage_public_key([{'value': public_key_str, 'state': True}])
keys = get_all_public_keys(request.user)
manager.save_key_in_opennebula('\n'.join(keys))
return redirect(reverse_lazy('hosting:ssh_keys'), foo='bar')
@ -563,7 +566,9 @@ class SSHKeyCreateView(LoginRequiredMixin, FormView):
public_key = form.cleaned_data['public_key']
if type(public_key) is bytes:
public_key = public_key.decode()
manager.manage_public_key([{'value': public_key, 'state': True}])
keys = UserHostingKey.objects.filter(user=self.request.user)
keys_to_save = [k.public_key for k in keys]
manager.save_key_in_opennebula('\n'.join(keys_to_save))
return HttpResponseRedirect(self.success_url)
def post(self, request, *args, **kwargs):

View file

@ -371,6 +371,31 @@ class OpenNebulaManager():
return vm_terminated
def save_key_in_opennebula(self, ssh_key):
"""
Save the given ssh key in OpenNebula user
# Update type: 0: Replace the whole template.
1: Merge new template with the existing one.
:param ssh_key: The ssh key to be saved
:return:
"""
UPDATE_TYPE = 1
return_value = self.client.call(
'one.user.update',
self.email,
self.password,
'<SSH_PUBLIC_KEY>%s</SSH_PUBLIC_KEY>' % ssh_key,
UPDATE_TYPE
)
if type(return_value) == int:
logger.debug(
"Saved the key in opennebula successfully : %s" % return_value)
else:
logger.error(
"Could not save the key in opennebula. %s" % return_value)
return
def _get_template_pool(self):
try:
template_pool = oca.VmTemplatePool(self.oneadmin_client)