Added save_public_key and reformatted code

This commit is contained in:
M.Ravi 2017-08-29 18:02:56 +02:00
parent 34580b6514
commit d25bca0860

View file

@ -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,14 +124,17 @@ 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')
logger.debug(
'Created a user for CustomObject: {user} with user id = {u_id}',
user=user, user=user,
u_id=user_id 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(
'Could not connect to host: {host} via protocol {protocol}'.format(
host=settings.OPENNEBULA_DOMAIN, host=settings.OPENNEBULA_DOMAIN,
protocol=settings.OPENNEBULA_PROTOCOL) protocol=settings.OPENNEBULA_PROTOCOL)
) )
@ -141,7 +146,8 @@ 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(
oca.User.METHODS['allocate'], email,
password, 'core') 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}",
@ -150,7 +156,8 @@ 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(
'Could not connect to host: {host} via protocol {protocol}'.format(
host=settings.OPENNEBULA_DOMAIN, host=settings.OPENNEBULA_DOMAIN,
protocol=settings.OPENNEBULA_PROTOCOL) protocol=settings.OPENNEBULA_PROTOCOL)
) )
@ -161,7 +168,8 @@ 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(
'Could not connect to host: {host} via protocol {protocol}'.format(
host=settings.OPENNEBULA_DOMAIN, host=settings.OPENNEBULA_DOMAIN,
protocol=settings.OPENNEBULA_PROTOCOL) protocol=settings.OPENNEBULA_PROTOCOL)
) )
@ -183,7 +191,8 @@ class OpenNebulaManager():
raise ConnectionRefusedError raise ConnectionRefusedError
except ConnectionRefusedError: except ConnectionRefusedError:
logger.info('Could not connect to host: {host} via protocol {protocol}'.format( logger.info(
'Could not connect to host: {host} via protocol {protocol}'.format(
host=settings.OPENNEBULA_DOMAIN, host=settings.OPENNEBULA_DOMAIN,
protocol=settings.OPENNEBULA_PROTOCOL) protocol=settings.OPENNEBULA_PROTOCOL)
) )
@ -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,7 +322,9 @@ 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(
'''Could not connect to host: {host} via protocol \
{protocol}'''.format(
host=settings.OPENNEBULA_DOMAIN, host=settings.OPENNEBULA_DOMAIN,
protocol=settings.OPENNEBULA_PROTOCOL) protocol=settings.OPENNEBULA_PROTOCOL)
) )
@ -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))