From 08608c726fb20b84b76aa93a0f7793676da2884f Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 25 Jun 2019 02:11:57 +0200 Subject: [PATCH] Code cleanup: remove updating ssh keys on live VMs --- datacenterlight/tasks.py | 87 +------------------ .../hosting/js/virtual_machine_detail.js | 52 ----------- .../hosting/virtual_machine_detail.html | 35 -------- hosting/urls.py | 4 +- hosting/views.py | 55 ------------ 5 files changed, 4 insertions(+), 229 deletions(-) diff --git a/datacenterlight/tasks.py b/datacenterlight/tasks.py index 641bcf6b..8b4626e8 100644 --- a/datacenterlight/tasks.py +++ b/datacenterlight/tasks.py @@ -1,5 +1,4 @@ from datetime import datetime -from time import sleep from celery import current_task from celery.exceptions import MaxRetriesExceededError @@ -178,7 +177,7 @@ def create_vm_task(self, vm_template_id, user, specs, template, order_id): kwargs={'pk': order_id}), 'page_header': _( 'Your New VM %(vm_name)s at Data Center Light') % { - 'vm_name': vm.get('name')}, + 'vm_name': vm.get('name')}, 'vm_name': vm.get('name') } email_data = { @@ -193,7 +192,8 @@ def create_vm_task(self, vm_template_id, user, specs, template, order_id): email.send() logger.debug("New VM ID is {vm_id}".format(vm_id=vm_id)) - get_or_create_vm_detail(custom_user, manager, vm_id) + if vm_id > 0: + get_or_create_vm_detail(custom_user, manager, vm_id) except Exception as e: logger.error(str(e)) try: @@ -215,84 +215,3 @@ def create_vm_task(self, vm_template_id, user, specs, template, order_id): return return vm_id - - -@app.task(bind=True, max_retries=settings.CELERY_MAX_RETRIES) -def save_ssh_key_in_vm_template_task(self, user, vm_id, ssh_key_str): - logger.debug("Inside save_ssh_key_in_vm_template_task %s" % vm_id) - - on_user = user.get('email') - on_pass = user.get('pass') - - if on_user is None or on_pass is None: - logger.error( - "Either email or password not supplied. Can't save ssh key" - ) - return - - manager = OpenNebulaManager(email=on_user, password=on_pass) - - # poweroff the vm - vm = manager.power_off_vm(vm_id) - - powered_off = False - for t in range(15): - vm = manager.get_vm(vm_id) - if vm.str_state == 'POWEROFF': - logger.debug( - "VM %s has been powered off. Now adding ssh keys" % vm.id - ) - powered_off = True - break - else: - logger.debug( - "VM {} has state {}. Waiting 2 more seconds to see if it " - "powers off".format(vm.id, vm.str_state) - ) - sleep(2) - - if powered_off: - logger.debug( - "VM %s was powered off by api call" % vm.id - ) - if manager.save_key_in_vm_template(vm_id=vm_id, ssh_key=ssh_key_str) > 0: - logger.debug( - "Added ssh_keys of user %s to VM %s successfully" % - (on_user, vm_id) - ) - manager.resume(vm_id) - lang = 'en-us' - if user.get('language') is not None: - logger.debug( - "Language is set to {}".format(user.get('language'))) - lang = user.get('language') - translation.activate(lang) - # Send notification to the user as soon as VM has been booked - context = { - 'page_header': str(_("Adding of SSH key completed")), - 'base_url': "{0}://{1}".format(user.get('request_scheme'), - user.get('request_host')), - 'vm_detail_url': reverse('hosting:virtual_machines', - kwargs={'pk': vm_id}), - 'vm_name': vm.name - } - email_data = { - 'subject': context.get('page_header'), - 'to': user.get('email'), - 'context': context, - 'template_name': 'ssh_key_added_to_vm', - 'template_path': 'hosting/emails/', - 'from_address': settings.DCL_SUPPORT_FROM_ADDRESS, - } - email = BaseEmail(**email_data) - email.send() - else: - logger.error( - "There was an error updating ssh keys of the VM %s" % vm_id - ) - else: - logger.error( - "VM {} did not poweroff within 30 seconds after the poweroff api " - "call. Please, ask the admin to poweroff and add the key " - "manually.".format(vm_id) - ) diff --git a/hosting/static/hosting/js/virtual_machine_detail.js b/hosting/static/hosting/js/virtual_machine_detail.js index e90e73f6..28592883 100644 --- a/hosting/static/hosting/js/virtual_machine_detail.js +++ b/hosting/static/hosting/js/virtual_machine_detail.js @@ -81,58 +81,6 @@ $(document).ready(function() { }) }); - $('#modal-add-ssh-key-button').click(function(e) { - var url = $('#add_ssh_key_to_vm_form').attr('action'); - console.log("Url to POST " + url); - - // Declare a checkbox array - var chkArray = []; - var encoded_data =""; - - // Look for all checkboxes that have a specific class and was checked - $(".chk-ssh-key:checked").each(function() { - chkArray.push($(this).val()); - }); - encoded_data = encodeURIComponent(chkArray.join(",")); - console.log("Encoded data = " + encoded_data); - - fa_icon = $('#ssh-key-modal-icon'); - modal_btn = $('#modal-add-ssh-key-button'); - modal_btn.prop("disabled", true); - modal_btn.html(''); - $.post(url, {selected_key: encoded_data}) - .done(function(data) { - console.log("Request Done"); - modal_btn.prop("disabled", false); - modal_btn.html("OK"); - if (data.status === true) { - fa_icon.html(''); - } else { - fa_icon.html(''); - modal_btn.attr('class', '').addClass('btn btn-danger btn-ok btn-wide'); - } - console.log("title = " + data.msg_title); - console.log("desc = " + data.msg_body); - $('#ssh-key-modal-title').text(data.msg_title); - $('#ssh-key-modal-description').html(data.msg_body); - console.log("Request Done end"); - }) - .fail(function(data) { - console.log("Request Failed"); - console.log("title " + data.msg_title); - console.log("body " + data.msg_body); - modal_btn.attr('class', '').addClass('btn btn-danger btn-ok btn-wide'); - $('#ssh-key-modal-title').text(data.msg_title); - $('#ssh-key-modal-description').html(data.msg_body); - }) - .always(function () { - console.log("changing href to location: " + location); - $('#modal-add-ssh-key-button').unbind('click').click(function () { - location.reload(); - }); - }) - }); - var hash = window.location.hash; hash && $('ul.nav a[href="' + hash + '"]').tab('show'); diff --git a/hosting/templates/hosting/virtual_machine_detail.html b/hosting/templates/hosting/virtual_machine_detail.html index edbc71f9..bf869f91 100644 --- a/hosting/templates/hosting/virtual_machine_detail.html +++ b/hosting/templates/hosting/virtual_machine_detail.html @@ -113,41 +113,6 @@ {% trans "BACK TO LIST" %} - - -