Added save_public_key and reformatted code
This commit is contained in:
parent
34580b6514
commit
d25bca0860
1 changed files with 66 additions and 24 deletions
|
@ -9,6 +9,8 @@ from django.conf import settings
|
||||||
|
|
||||||
from utils.models import CustomUser
|
from utils.models import CustomUser
|
||||||
from .exceptions import KeyExistsError, UserExistsError, UserCredentialError
|
from .exceptions import KeyExistsError, UserExistsError, UserCredentialError
|
||||||
|
from hosting.models import HostingOrder
|
||||||
|
from utils.tasks import save_ssh_key
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -122,16 +124,19 @@ class OpenNebulaManager():
|
||||||
|
|
||||||
except WrongNameError:
|
except WrongNameError:
|
||||||
user_id = self.oneadmin_client.call(oca.User.METHODS['allocate'],
|
user_id = self.oneadmin_client.call(oca.User.METHODS['allocate'],
|
||||||
user.email, user.password, 'core')
|
user.email, user.password,
|
||||||
logger.debug('Created a user for CustomObject: {user} with user id = {u_id}',
|
'core')
|
||||||
user=user,
|
logger.debug(
|
||||||
u_id=user_id
|
'Created a user for CustomObject: {user} with user id = {u_id}',
|
||||||
)
|
user=user,
|
||||||
|
u_id=user_id
|
||||||
|
)
|
||||||
return user_id
|
return user_id
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError:
|
||||||
logger.error('Could not connect to host: {host} via protocol {protocol}'.format(
|
logger.error(
|
||||||
host=settings.OPENNEBULA_DOMAIN,
|
'Could not connect to host: {host} via protocol {protocol}'.format(
|
||||||
protocol=settings.OPENNEBULA_PROTOCOL)
|
host=settings.OPENNEBULA_DOMAIN,
|
||||||
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
||||||
)
|
)
|
||||||
raise ConnectionRefusedError
|
raise ConnectionRefusedError
|
||||||
|
|
||||||
|
@ -141,8 +146,9 @@ class OpenNebulaManager():
|
||||||
opennebula_user = user_pool.get_by_name(email)
|
opennebula_user = user_pool.get_by_name(email)
|
||||||
return opennebula_user
|
return opennebula_user
|
||||||
except WrongNameError as wrong_name_err:
|
except WrongNameError as wrong_name_err:
|
||||||
opennebula_user = self.oneadmin_client.call(oca.User.METHODS['allocate'], email,
|
opennebula_user = self.oneadmin_client.call(
|
||||||
password, 'core')
|
oca.User.METHODS['allocate'], email,
|
||||||
|
password, 'core')
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"User {0} does not exist. Created the user. User id = {1}",
|
"User {0} does not exist. Created the user. User id = {1}",
|
||||||
email,
|
email,
|
||||||
|
@ -150,9 +156,10 @@ class OpenNebulaManager():
|
||||||
)
|
)
|
||||||
return opennebula_user
|
return opennebula_user
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError:
|
||||||
logger.info('Could not connect to host: {host} via protocol {protocol}'.format(
|
logger.info(
|
||||||
host=settings.OPENNEBULA_DOMAIN,
|
'Could not connect to host: {host} via protocol {protocol}'.format(
|
||||||
protocol=settings.OPENNEBULA_PROTOCOL)
|
host=settings.OPENNEBULA_DOMAIN,
|
||||||
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
||||||
)
|
)
|
||||||
raise ConnectionRefusedError
|
raise ConnectionRefusedError
|
||||||
|
|
||||||
|
@ -161,9 +168,10 @@ class OpenNebulaManager():
|
||||||
user_pool = oca.UserPool(self.oneadmin_client)
|
user_pool = oca.UserPool(self.oneadmin_client)
|
||||||
user_pool.info()
|
user_pool.info()
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError:
|
||||||
logger.info('Could not connect to host: {host} via protocol {protocol}'.format(
|
logger.info(
|
||||||
host=settings.OPENNEBULA_DOMAIN,
|
'Could not connect to host: {host} via protocol {protocol}'.format(
|
||||||
protocol=settings.OPENNEBULA_PROTOCOL)
|
host=settings.OPENNEBULA_DOMAIN,
|
||||||
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
||||||
)
|
)
|
||||||
raise
|
raise
|
||||||
return user_pool
|
return user_pool
|
||||||
|
@ -183,9 +191,10 @@ class OpenNebulaManager():
|
||||||
raise ConnectionRefusedError
|
raise ConnectionRefusedError
|
||||||
|
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError:
|
||||||
logger.info('Could not connect to host: {host} via protocol {protocol}'.format(
|
logger.info(
|
||||||
host=settings.OPENNEBULA_DOMAIN,
|
'Could not connect to host: {host} via protocol {protocol}'.format(
|
||||||
protocol=settings.OPENNEBULA_PROTOCOL)
|
host=settings.OPENNEBULA_DOMAIN,
|
||||||
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
||||||
)
|
)
|
||||||
raise ConnectionRefusedError
|
raise ConnectionRefusedError
|
||||||
# For now we'll just handle all other errors as connection errors
|
# For now we'll just handle all other errors as connection errors
|
||||||
|
@ -258,7 +267,8 @@ class OpenNebulaManager():
|
||||||
|
|
||||||
vm_specs += "<CONTEXT>"
|
vm_specs += "<CONTEXT>"
|
||||||
if ssh_key:
|
if ssh_key:
|
||||||
vm_specs += "<SSH_PUBLIC_KEY>{ssh}</SSH_PUBLIC_KEY>".format(ssh=ssh_key)
|
vm_specs += "<SSH_PUBLIC_KEY>{ssh}</SSH_PUBLIC_KEY>".format(
|
||||||
|
ssh=ssh_key)
|
||||||
vm_specs += """<NETWORK>YES</NETWORK>
|
vm_specs += """<NETWORK>YES</NETWORK>
|
||||||
</CONTEXT>
|
</CONTEXT>
|
||||||
</TEMPLATE>
|
</TEMPLATE>
|
||||||
|
@ -312,9 +322,11 @@ class OpenNebulaManager():
|
||||||
template_pool.info()
|
template_pool.info()
|
||||||
return template_pool
|
return template_pool
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError:
|
||||||
logger.info('Could not connect to host: {host} via protocol {protocol}'.format(
|
logger.info(
|
||||||
host=settings.OPENNEBULA_DOMAIN,
|
'''Could not connect to host: {host} via protocol \
|
||||||
protocol=settings.OPENNEBULA_PROTOCOL)
|
{protocol}'''.format(
|
||||||
|
host=settings.OPENNEBULA_DOMAIN,
|
||||||
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
||||||
)
|
)
|
||||||
raise ConnectionRefusedError
|
raise ConnectionRefusedError
|
||||||
except:
|
except:
|
||||||
|
@ -347,7 +359,8 @@ class OpenNebulaManager():
|
||||||
except:
|
except:
|
||||||
raise ConnectionRefusedError
|
raise ConnectionRefusedError
|
||||||
|
|
||||||
def create_template(self, name, cores, memory, disk_size, core_price, memory_price,
|
def create_template(self, name, cores, memory, disk_size, core_price,
|
||||||
|
memory_price,
|
||||||
disk_size_price, ssh=''):
|
disk_size_price, ssh=''):
|
||||||
"""Create and add a new template to opennebula.
|
"""Create and add a new template to opennebula.
|
||||||
:param name: A string representation describing the template.
|
:param name: A string representation describing the template.
|
||||||
|
@ -490,3 +503,32 @@ class OpenNebulaManager():
|
||||||
|
|
||||||
except ConnectionError:
|
except ConnectionError:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
def save_public_key(self, keys):
|
||||||
|
"""
|
||||||
|
A function that saves the supplied keys to the authorized_keys file
|
||||||
|
|
||||||
|
:param keys: List of ssh keys that are to be added
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
owner = CustomUser.objects.filter(
|
||||||
|
email=self.opennebula_user.name).first()
|
||||||
|
all_orders = HostingOrder.objects.filter(customer__user=owner)
|
||||||
|
if len(all_orders) > 0:
|
||||||
|
logger.debug("The user {} has 1 or more VMs. We need to configure "
|
||||||
|
"the ssh keys.".format(self.opennebula_user.name))
|
||||||
|
hosts = []
|
||||||
|
for order in all_orders:
|
||||||
|
try:
|
||||||
|
vm = self.get_vm(order.vm_id)
|
||||||
|
for nic in vm.template.nics:
|
||||||
|
if hasattr(nic, 'ip'):
|
||||||
|
hosts.append(nic.ip)
|
||||||
|
except WrongIdError:
|
||||||
|
logger.debug(
|
||||||
|
"VM with ID {} does not exist".format(order.vm_id))
|
||||||
|
if len(keys) > 0:
|
||||||
|
save_ssh_key.delay(hosts, keys)
|
||||||
|
else:
|
||||||
|
logger.debug("The user {} has no VMs. We don't need to configure "
|
||||||
|
"the ssh keys.".format(self.opennebula_user.name))
|
||||||
|
|
Loading…
Reference in a new issue