Merge pull request #640 from pcoder/task/4890/ssh_key_manage_for_ipv6
Task/4890/ssh key manage for ipv6
This commit is contained in:
commit
ba286eb053
2 changed files with 40 additions and 48 deletions
|
@ -1,8 +1,8 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from celery import current_task
|
||||||
from celery.exceptions import MaxRetriesExceededError
|
from celery.exceptions import MaxRetriesExceededError
|
||||||
from celery.utils.log import get_task_logger
|
from celery.utils.log import get_task_logger
|
||||||
from celery import current_task
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.mail import EmailMessage
|
from django.core.mail import EmailMessage
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
|
@ -14,11 +14,10 @@ from hosting.models import HostingOrder, HostingBill
|
||||||
from membership.models import StripeCustomer, CustomUser
|
from membership.models import StripeCustomer, CustomUser
|
||||||
from opennebula_api.models import OpenNebulaManager
|
from opennebula_api.models import OpenNebulaManager
|
||||||
from opennebula_api.serializers import VirtualMachineSerializer
|
from opennebula_api.serializers import VirtualMachineSerializer
|
||||||
from utils.hosting_utils import get_all_public_keys, get_or_create_vm_detail
|
|
||||||
from utils.forms import UserBillingAddressForm
|
from utils.forms import UserBillingAddressForm
|
||||||
|
from utils.hosting_utils import get_all_public_keys, get_or_create_vm_detail
|
||||||
from utils.mailer import BaseEmail
|
from utils.mailer import BaseEmail
|
||||||
from utils.models import BillingAddress
|
from utils.models import BillingAddress
|
||||||
|
|
||||||
from .models import VMPricing
|
from .models import VMPricing
|
||||||
|
|
||||||
logger = get_task_logger(__name__)
|
logger = get_task_logger(__name__)
|
||||||
|
@ -188,11 +187,11 @@ def create_vm_task(self, vm_template_id, user, specs, template,
|
||||||
email = BaseEmail(**email_data)
|
email = BaseEmail(**email_data)
|
||||||
email.send()
|
email.send()
|
||||||
|
|
||||||
# try to see if we have the IP and that if the ssh keys can
|
# try to see if we have the IPv6 of the new vm and that if the ssh
|
||||||
# be configured
|
# keys can be configured
|
||||||
new_host = manager.get_primary_ipv4(vm_id)
|
vm_ipv6 = manager.get_ipv6(vm_id)
|
||||||
logger.debug("New VM ID is {vm_id}".format(vm_id=vm_id))
|
logger.debug("New VM ID is {vm_id}".format(vm_id=vm_id))
|
||||||
if new_host is not None:
|
if vm_ipv6 is not None:
|
||||||
custom_user = CustomUser.objects.get(email=user.get('email'))
|
custom_user = CustomUser.objects.get(email=user.get('email'))
|
||||||
get_or_create_vm_detail(custom_user, manager, vm_id)
|
get_or_create_vm_detail(custom_user, manager, vm_id)
|
||||||
if custom_user is not None:
|
if custom_user is not None:
|
||||||
|
@ -203,13 +202,15 @@ def create_vm_task(self, vm_template_id, user, specs, template,
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"Calling configure on {host} for "
|
"Calling configure on {host} for "
|
||||||
"{num_keys} keys".format(
|
"{num_keys} keys".format(
|
||||||
host=new_host, num_keys=len(keys)))
|
host=vm_ipv6, num_keys=len(keys)
|
||||||
|
)
|
||||||
|
)
|
||||||
# Let's delay the task by 75 seconds to be sure
|
# Let's delay the task by 75 seconds to be sure
|
||||||
# that we run the cdist configure after the host
|
# that we run the cdist configure after the host
|
||||||
# is up
|
# is up
|
||||||
manager.manage_public_key(keys,
|
manager.manage_public_key(
|
||||||
hosts=[new_host],
|
keys, hosts=[vm_ipv6], countdown=75
|
||||||
countdown=75)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(str(e))
|
logger.error(str(e))
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -53,27 +53,18 @@ class OpenNebulaManager():
|
||||||
ConnectionError: If the connection to the opennebula server can't be
|
ConnectionError: If the connection to the opennebula server can't be
|
||||||
established
|
established
|
||||||
"""
|
"""
|
||||||
return oca.Client("{0}:{1}".format(
|
return self._get_opennebula_client(user.email, user.password)
|
||||||
user.email,
|
|
||||||
user.password),
|
|
||||||
"{protocol}://{domain}:{port}{endpoint}".format(
|
|
||||||
protocol=settings.OPENNEBULA_PROTOCOL,
|
|
||||||
domain=settings.OPENNEBULA_DOMAIN,
|
|
||||||
port=settings.OPENNEBULA_PORT,
|
|
||||||
endpoint=settings.OPENNEBULA_ENDPOINT
|
|
||||||
))
|
|
||||||
|
|
||||||
def _get_opennebula_client(self, username, password):
|
def _get_opennebula_client(self, username, password):
|
||||||
return oca.Client("{0}:{1}".format(
|
return oca.Client(
|
||||||
username,
|
"{0}:{1}".format(username, password),
|
||||||
|
|
||||||
password),
|
|
||||||
"{protocol}://{domain}:{port}{endpoint}".format(
|
"{protocol}://{domain}:{port}{endpoint}".format(
|
||||||
protocol=settings.OPENNEBULA_PROTOCOL,
|
protocol=settings.OPENNEBULA_PROTOCOL,
|
||||||
domain=settings.OPENNEBULA_DOMAIN,
|
domain=settings.OPENNEBULA_DOMAIN,
|
||||||
port=settings.OPENNEBULA_PORT,
|
port=settings.OPENNEBULA_PORT,
|
||||||
endpoint=settings.OPENNEBULA_ENDPOINT
|
endpoint=settings.OPENNEBULA_ENDPOINT
|
||||||
))
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def _get_user(self, user):
|
def _get_user(self, user):
|
||||||
"""Get the corresponding opennebula user for a CustomUser object
|
"""Get the corresponding opennebula user for a CustomUser object
|
||||||
|
@ -218,32 +209,31 @@ class OpenNebulaManager():
|
||||||
except:
|
except:
|
||||||
raise ConnectionRefusedError
|
raise ConnectionRefusedError
|
||||||
|
|
||||||
def get_primary_ipv4(self, vm_id):
|
def get_ipv6(self, vm_id):
|
||||||
"""
|
"""
|
||||||
Returns the primary IPv4 of the given vm.
|
Returns the first IPv6 of the given vm.
|
||||||
To be changed later.
|
|
||||||
|
|
||||||
:return: An IP address string, if it exists else returns None
|
:return: An IPv6 address string, if it exists else returns None
|
||||||
"""
|
"""
|
||||||
all_ipv4s = self.get_vm_ipv4_addresses(vm_id)
|
ipv6_list = self.get_all_ipv6_addresses(vm_id)
|
||||||
if len(all_ipv4s) > 0:
|
if len(ipv6_list) > 0:
|
||||||
return all_ipv4s[0]
|
return ipv6_list[0]
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_vm_ipv4_addresses(self, vm_id):
|
def get_all_ipv6_addresses(self, vm_id):
|
||||||
"""
|
"""
|
||||||
Returns a list of IPv4 addresses of the given vm
|
Returns a list of IPv6 addresses of the given vm
|
||||||
|
|
||||||
:param vm_id: The ID of the vm
|
:param vm_id: The ID of the vm
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
ipv4s = []
|
ipv6_list = []
|
||||||
vm = self.get_vm(vm_id)
|
vm = self.get_vm(vm_id)
|
||||||
for nic in vm.template.nics:
|
for nic in vm.template.nics:
|
||||||
if hasattr(nic, 'ip'):
|
if hasattr(nic, 'ip6_global'):
|
||||||
ipv4s.append(nic.ip)
|
ipv6_list.append(nic.ip6_global)
|
||||||
return ipv4s
|
return ipv6_list
|
||||||
|
|
||||||
def create_vm(self, template_id, specs, ssh_key=None, vm_name=None):
|
def create_vm(self, template_id, specs, ssh_key=None, vm_name=None):
|
||||||
|
|
||||||
|
@ -438,8 +428,9 @@ class OpenNebulaManager():
|
||||||
return template_id
|
return template_id
|
||||||
|
|
||||||
def delete_template(self, template_id):
|
def delete_template(self, template_id):
|
||||||
self.oneadmin_client.call(oca.VmTemplate.METHODS[
|
self.oneadmin_client.call(
|
||||||
'delete'], template_id, False)
|
oca.VmTemplate.METHODS['delete'], template_id, False
|
||||||
|
)
|
||||||
|
|
||||||
def change_user_password(self, passwd_hash):
|
def change_user_password(self, passwd_hash):
|
||||||
self.oneadmin_client.call(
|
self.oneadmin_client.call(
|
||||||
|
@ -547,7 +538,7 @@ class OpenNebulaManager():
|
||||||
'value': 'sha-.....', # public key as string
|
'value': 'sha-.....', # public key as string
|
||||||
'state': True # whether key is to be added or
|
'state': True # whether key is to be added or
|
||||||
} # removed
|
} # removed
|
||||||
:param hosts: A list of hosts IP addresses
|
:param hosts: A list of hosts IPv6 addresses
|
||||||
:param countdown: Parameter to be passed to celery apply_async
|
:param countdown: Parameter to be passed to celery apply_async
|
||||||
Allows to delay a task by `countdown` number of seconds
|
Allows to delay a task by `countdown` number of seconds
|
||||||
:return:
|
:return:
|
||||||
|
@ -560,12 +551,14 @@ class OpenNebulaManager():
|
||||||
link_error=save_ssh_key_error_handler.s())
|
link_error=save_ssh_key_error_handler.s())
|
||||||
else:
|
else:
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"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_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
|
||||||
:return: A list of hosts IP addresses, empty if none exist
|
:return: A list of IPv6 addresses of all the hosts of this customer or
|
||||||
|
an empty list if none exist
|
||||||
"""
|
"""
|
||||||
owner = CustomUser.objects.filter(
|
owner = CustomUser.objects.filter(
|
||||||
email=self.email).first()
|
email=self.email).first()
|
||||||
|
@ -576,10 +569,8 @@ class OpenNebulaManager():
|
||||||
"the ssh keys.".format(self.email))
|
"the ssh keys.".format(self.email))
|
||||||
for order in all_orders:
|
for order in all_orders:
|
||||||
try:
|
try:
|
||||||
vm = self.get_vm(order.vm_id)
|
ip = self.get_ipv6(order.vm_id)
|
||||||
for nic in vm.template.nics:
|
hosts.append(ip)
|
||||||
if hasattr(nic, 'ip'):
|
|
||||||
hosts.append(nic.ip)
|
|
||||||
except WrongIdError:
|
except WrongIdError:
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"VM with ID {} does not exist".format(order.vm_id))
|
"VM with ID {} does not exist".format(order.vm_id))
|
||||||
|
|
Loading…
Reference in a new issue