Refactor code: get_primary_ip -> get_ipv6 and remove unwanted code

This commit is contained in:
PCoder 2018-07-01 11:07:29 +02:00
parent 8993a7bde1
commit c13af95017
2 changed files with 21 additions and 71 deletions

View file

@ -187,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_ip(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:
@ -202,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:

View file

@ -219,46 +219,19 @@ class OpenNebulaManager():
except: except:
raise ConnectionRefusedError raise ConnectionRefusedError
def is_public_ipv4(self, ipv4): def get_ipv6(self, vm_id):
""" """
Checks whether the passed ipv4 is a public IP. Returns the first IPv6 of the given vm.
:param ipv4: :return: An IPv6 address string, if it exists else returns None
:return: Returns true if it is a public IP else returns false
""" """
if ipaddress.ip_address(ipv4).is_private: ipv6_list = self.get_all_ipv6_addresses(vm_id)
return False
else:
return True
def get_primary_ip(self, vm_id):
"""
Returns primary ipv4 if it exists and is a public ip. Otherwise returns
the IPv6 of the machine
:param vm_id:
:return:
"""
ipv4 = self.get_primary_ipv4(vm_id)
if self.is_public_ipv4(ipv4):
return ipv4
else:
return self.get_primary_ipv6(vm_id)
def get_primary_ipv6(self, vm_id):
"""
Returns the primary IPv6 of the given vm.
For now, we return the first available IPv6 (to be changed later)
:return: An IP address string, if it exists else returns None
"""
ipv6_list = self.get_vm_ipv6_addresses(vm_id)
if len(ipv6_list) > 0: if len(ipv6_list) > 0:
return ipv6_list[0] return ipv6_list[0]
else: else:
return None return None
def get_vm_ipv6_addresses(self, vm_id): def get_all_ipv6_addresses(self, vm_id):
""" """
Returns a list of IPv6 addresses of the given vm Returns a list of IPv6 addresses of the given vm
@ -272,33 +245,6 @@ class OpenNebulaManager():
ipv6_list.append(nic.ip6_global) ipv6_list.append(nic.ip6_global)
return ipv6_list return ipv6_list
def get_primary_ipv4(self, vm_id):
"""
Returns the primary IPv4 of the given vm.
To be changed later.
:return: An IP address string, if it exists else returns None
"""
all_ipv4s = self.get_vm_ipv4_addresses(vm_id)
if len(all_ipv4s) > 0:
return all_ipv4s[0]
else:
return None
def get_vm_ipv4_addresses(self, vm_id):
"""
Returns a list of IPv4 addresses of the given vm
:param vm_id: The ID of the vm
:return:
"""
ipv4s = []
vm = self.get_vm(vm_id)
for nic in vm.template.nics:
if hasattr(nic, 'ip'):
ipv4s.append(nic.ip)
return ipv4s
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):
template = self.get_template(template_id) template = self.get_template(template_id)
@ -601,7 +547,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:
@ -614,12 +560,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()
@ -630,7 +578,7 @@ 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:
ip = self.get_primary_ip(order.vm_id) ip = self.get_ipv6(order.vm_id)
hosts.append(ip) hosts.append(ip)
except WrongIdError: except WrongIdError:
logger.debug( logger.debug(