2017-05-12 17:13:18 +00:00
|
|
|
|
import logging
|
2017-08-31 07:24:08 +00:00
|
|
|
|
import socket
|
2017-05-12 17:13:18 +00:00
|
|
|
|
|
2017-08-31 07:24:08 +00:00
|
|
|
|
import oca
|
2017-05-09 14:33:56 +00:00
|
|
|
|
from django.conf import settings
|
2017-08-31 07:24:08 +00:00
|
|
|
|
from oca.exceptions import OpenNebulaException
|
|
|
|
|
from oca.pool import WrongNameError, WrongIdError
|
2017-05-09 14:33:56 +00:00
|
|
|
|
|
2017-08-29 16:02:56 +00:00
|
|
|
|
from hosting.models import HostingOrder
|
2017-08-31 07:24:08 +00:00
|
|
|
|
from utils.models import CustomUser
|
2017-08-31 21:03:31 +00:00
|
|
|
|
from utils.tasks import save_ssh_key, save_ssh_key_error_handler
|
2017-08-31 07:24:08 +00:00
|
|
|
|
from .exceptions import KeyExistsError, UserExistsError, UserCredentialError
|
2017-05-31 16:01:54 +00:00
|
|
|
|
|
2017-05-12 17:13:18 +00:00
|
|
|
|
logger = logging.getLogger(__name__)
|
2017-05-10 02:06:12 +00:00
|
|
|
|
|
2017-05-25 09:27:49 +00:00
|
|
|
|
|
2017-05-09 14:33:56 +00:00
|
|
|
|
class OpenNebulaManager():
|
|
|
|
|
"""This class represents an opennebula manager."""
|
|
|
|
|
|
2017-05-13 03:50:56 +00:00
|
|
|
|
def __init__(self, email=None, password=None):
|
2017-08-31 07:24:08 +00:00
|
|
|
|
self.email = email
|
|
|
|
|
self.password = password
|
2017-05-09 14:33:56 +00:00
|
|
|
|
# Get oneadmin client
|
|
|
|
|
self.oneadmin_client = self._get_opennebula_client(
|
|
|
|
|
settings.OPENNEBULA_USERNAME,
|
|
|
|
|
settings.OPENNEBULA_PASSWORD
|
|
|
|
|
)
|
2017-05-25 09:27:49 +00:00
|
|
|
|
|
2017-05-09 14:33:56 +00:00
|
|
|
|
# Get or create oppenebula user using given credentials
|
2017-05-13 03:50:56 +00:00
|
|
|
|
try:
|
|
|
|
|
self.opennebula_user = self._get_or_create_user(
|
|
|
|
|
email,
|
|
|
|
|
password
|
|
|
|
|
)
|
|
|
|
|
# If opennebula user was created/obtained, get his client
|
2017-05-09 14:33:56 +00:00
|
|
|
|
self.client = self._get_opennebula_client(
|
|
|
|
|
email,
|
|
|
|
|
password
|
|
|
|
|
)
|
2017-05-13 03:50:56 +00:00
|
|
|
|
except:
|
|
|
|
|
pass
|
2017-06-29 14:34:40 +00:00
|
|
|
|
|
2017-05-31 16:01:54 +00:00
|
|
|
|
def _get_client(self, user):
|
2017-06-29 14:34:40 +00:00
|
|
|
|
"""Get a opennebula client object for a CustomUser object
|
|
|
|
|
|
2017-05-31 16:01:54 +00:00
|
|
|
|
Args:
|
|
|
|
|
user (CustomUser): dynamicweb CustomUser object
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
oca.Client: Opennebula client object
|
|
|
|
|
|
|
|
|
|
Raise:
|
|
|
|
|
ConnectionError: If the connection to the opennebula server can't be
|
2017-06-29 14:34:40 +00:00
|
|
|
|
established
|
2017-05-31 16:01:54 +00:00
|
|
|
|
"""
|
2018-07-01 09:17:45 +00:00
|
|
|
|
return self._get_opennebula_client(user.email, user.password)
|
2017-05-09 14:33:56 +00:00
|
|
|
|
|
|
|
|
|
def _get_opennebula_client(self, username, password):
|
2018-07-01 09:17:45 +00:00
|
|
|
|
return oca.Client(
|
|
|
|
|
"{0}:{1}".format(username, password),
|
2017-05-09 14:33:56 +00:00
|
|
|
|
"{protocol}://{domain}:{port}{endpoint}".format(
|
|
|
|
|
protocol=settings.OPENNEBULA_PROTOCOL,
|
|
|
|
|
domain=settings.OPENNEBULA_DOMAIN,
|
|
|
|
|
port=settings.OPENNEBULA_PORT,
|
|
|
|
|
endpoint=settings.OPENNEBULA_ENDPOINT
|
2018-07-01 09:17:45 +00:00
|
|
|
|
)
|
|
|
|
|
)
|
2017-05-09 14:33:56 +00:00
|
|
|
|
|
2017-05-31 16:01:54 +00:00
|
|
|
|
def _get_user(self, user):
|
2017-06-29 14:34:40 +00:00
|
|
|
|
"""Get the corresponding opennebula user for a CustomUser object
|
|
|
|
|
|
2017-05-31 16:01:54 +00:00
|
|
|
|
Args:
|
|
|
|
|
user (CustomUser): dynamicweb CustomUser object
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
oca.User: Opennebula user object
|
|
|
|
|
|
|
|
|
|
Raise:
|
|
|
|
|
WrongNameError: If no openebula user with this credentials exists
|
|
|
|
|
ConnectionError: If the connection to the opennebula server can't be
|
2017-06-29 14:34:40 +00:00
|
|
|
|
established
|
2017-05-31 16:01:54 +00:00
|
|
|
|
"""
|
|
|
|
|
user_pool = self._get_user_pool()
|
|
|
|
|
return user_pool.get_by_name(user.email)
|
|
|
|
|
|
|
|
|
|
def create_user(self, user: CustomUser):
|
|
|
|
|
"""Create a new opennebula user or a corresponding CustomUser object
|
|
|
|
|
|
2017-06-29 14:34:40 +00:00
|
|
|
|
|
2017-05-31 16:01:54 +00:00
|
|
|
|
Args:
|
|
|
|
|
user (CustomUser): dynamicweb CustomUser object
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
int: Return the opennebula user id
|
2017-06-29 14:34:40 +00:00
|
|
|
|
|
2017-05-31 16:01:54 +00:00
|
|
|
|
Raises:
|
|
|
|
|
ConnectionError: If the connection to the opennebula server can't be
|
2017-06-29 14:34:40 +00:00
|
|
|
|
established
|
2017-05-31 16:01:54 +00:00
|
|
|
|
UserExistsError: If a user with this credeintals already exits on the
|
|
|
|
|
server
|
|
|
|
|
UserCredentialError: If a user with this email exists but the
|
|
|
|
|
password is worng
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
self._get_user(user)
|
2017-06-29 14:34:40 +00:00
|
|
|
|
try:
|
2017-05-31 16:01:54 +00:00
|
|
|
|
self._get_client(self, user)
|
|
|
|
|
logger.debug('User already exists')
|
|
|
|
|
raise UserExistsError()
|
|
|
|
|
except OpenNebulaException as err:
|
|
|
|
|
logger.error('OpenNebulaException error: {0}'.format(err))
|
2018-07-25 19:41:05 +00:00
|
|
|
|
logger.error('User exists but password is wrong')
|
2017-05-31 16:01:54 +00:00
|
|
|
|
raise UserCredentialError()
|
|
|
|
|
|
|
|
|
|
except WrongNameError:
|
|
|
|
|
user_id = self.oneadmin_client.call(oca.User.METHODS['allocate'],
|
2017-08-29 16:02:56 +00:00
|
|
|
|
user.email, user.password,
|
|
|
|
|
'core')
|
|
|
|
|
logger.debug(
|
|
|
|
|
'Created a user for CustomObject: {user} with user id = {u_id}',
|
|
|
|
|
user=user,
|
|
|
|
|
u_id=user_id
|
|
|
|
|
)
|
2017-06-29 14:34:40 +00:00
|
|
|
|
return user_id
|
2017-05-31 16:01:54 +00:00
|
|
|
|
except ConnectionRefusedError:
|
2017-08-29 16:02:56 +00:00
|
|
|
|
logger.error(
|
|
|
|
|
'Could not connect to host: {host} via protocol {protocol}'.format(
|
|
|
|
|
host=settings.OPENNEBULA_DOMAIN,
|
|
|
|
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
2017-05-31 16:01:54 +00:00
|
|
|
|
)
|
|
|
|
|
raise ConnectionRefusedError
|
|
|
|
|
|
2017-05-09 14:33:56 +00:00
|
|
|
|
def _get_or_create_user(self, email, password):
|
|
|
|
|
try:
|
2017-05-09 22:39:41 +00:00
|
|
|
|
user_pool = self._get_user_pool()
|
2017-05-09 14:33:56 +00:00
|
|
|
|
opennebula_user = user_pool.get_by_name(email)
|
|
|
|
|
return opennebula_user
|
|
|
|
|
except WrongNameError as wrong_name_err:
|
2017-08-29 16:02:56 +00:00
|
|
|
|
opennebula_user = self.oneadmin_client.call(
|
|
|
|
|
oca.User.METHODS['allocate'], email,
|
|
|
|
|
password, 'core')
|
2017-05-13 03:50:56 +00:00
|
|
|
|
logger.debug(
|
2017-09-23 20:33:48 +00:00
|
|
|
|
"User {} does not exist. Created the user. User id = {}".format(
|
2017-09-27 23:27:40 +00:00
|
|
|
|
email,
|
|
|
|
|
opennebula_user
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-05-10 00:49:03 +00:00
|
|
|
|
return opennebula_user
|
2017-05-09 22:39:41 +00:00
|
|
|
|
except ConnectionRefusedError:
|
2018-07-25 19:41:05 +00:00
|
|
|
|
logger.error(
|
2017-08-29 16:02:56 +00:00
|
|
|
|
'Could not connect to host: {host} via protocol {protocol}'.format(
|
|
|
|
|
host=settings.OPENNEBULA_DOMAIN,
|
|
|
|
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
2017-05-25 09:27:49 +00:00
|
|
|
|
)
|
2017-05-11 02:05:58 +00:00
|
|
|
|
raise ConnectionRefusedError
|
2017-05-25 09:27:49 +00:00
|
|
|
|
|
2017-05-09 15:15:12 +00:00
|
|
|
|
def _get_user_pool(self):
|
2017-05-09 22:39:41 +00:00
|
|
|
|
try:
|
|
|
|
|
user_pool = oca.UserPool(self.oneadmin_client)
|
|
|
|
|
user_pool.info()
|
|
|
|
|
except ConnectionRefusedError:
|
2018-07-25 19:41:05 +00:00
|
|
|
|
logger.error(
|
2017-08-29 16:02:56 +00:00
|
|
|
|
'Could not connect to host: {host} via protocol {protocol}'.format(
|
|
|
|
|
host=settings.OPENNEBULA_DOMAIN,
|
|
|
|
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
2017-05-25 09:27:49 +00:00
|
|
|
|
)
|
2017-06-29 14:34:40 +00:00
|
|
|
|
raise
|
2017-05-09 15:15:12 +00:00
|
|
|
|
return user_pool
|
2017-05-09 23:31:27 +00:00
|
|
|
|
|
2019-05-11 07:15:08 +00:00
|
|
|
|
def _get_vm_pool(self, infoextended=True):
|
2019-05-08 22:06:22 +00:00
|
|
|
|
"""
|
|
|
|
|
# filter:
|
|
|
|
|
# -4: Resources belonging to the user’s primary group
|
|
|
|
|
# -3: Resources belonging to the user
|
|
|
|
|
# -2: All resources
|
|
|
|
|
# -1: Resources belonging to the user and any of his groups
|
|
|
|
|
# >= 0: UID User’s Resources
|
|
|
|
|
|
|
|
|
|
# vm states:
|
|
|
|
|
# *-2 Any state, including DONE
|
|
|
|
|
# *-1 Any state, except DONE (Default)
|
|
|
|
|
# *0 INIT
|
|
|
|
|
# *1 PENDING
|
|
|
|
|
# *2 HOLD
|
|
|
|
|
# *3 ACTIVE
|
|
|
|
|
# *4 STOPPED
|
|
|
|
|
# *5 SUSPENDED
|
|
|
|
|
# *6 DONE
|
|
|
|
|
# *7 FAILED
|
|
|
|
|
# *8 POWEROFF
|
|
|
|
|
# *9 UNDEPLOYED
|
|
|
|
|
|
|
|
|
|
:param infoextended: When True calls infoextended api method introduced
|
2019-05-08 22:06:57 +00:00
|
|
|
|
in OpenNebula 5.8 else falls back to info which has limited attributes
|
2019-05-08 22:06:22 +00:00
|
|
|
|
of a VM
|
|
|
|
|
|
|
|
|
|
:return: the oca VirtualMachinePool object
|
|
|
|
|
"""
|
2017-05-11 02:05:58 +00:00
|
|
|
|
try:
|
2017-05-11 10:45:09 +00:00
|
|
|
|
vm_pool = oca.VirtualMachinePool(self.client)
|
2019-04-10 23:05:34 +00:00
|
|
|
|
if infoextended:
|
|
|
|
|
vm_pool.infoextended(
|
2019-05-11 07:15:08 +00:00
|
|
|
|
filter=-1, # User's resources and any of his groups
|
|
|
|
|
vm_state=-1 # Look for VMs in any state, except DONE
|
2019-04-10 23:05:34 +00:00
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
vm_pool.info()
|
2017-05-22 03:01:26 +00:00
|
|
|
|
return vm_pool
|
2019-05-11 07:15:08 +00:00
|
|
|
|
except AttributeError as ae:
|
|
|
|
|
logger.error("AttributeError : %s" % str(ae))
|
2017-05-11 02:05:58 +00:00
|
|
|
|
except ConnectionRefusedError:
|
2018-07-25 19:41:05 +00:00
|
|
|
|
logger.error(
|
2017-08-29 16:02:56 +00:00
|
|
|
|
'Could not connect to host: {host} via protocol {protocol}'.format(
|
|
|
|
|
host=settings.OPENNEBULA_DOMAIN,
|
|
|
|
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
2017-05-25 09:27:49 +00:00
|
|
|
|
)
|
2017-05-11 02:05:58 +00:00
|
|
|
|
raise ConnectionRefusedError
|
2017-05-14 10:22:10 +00:00
|
|
|
|
# For now we'll just handle all other errors as connection errors
|
|
|
|
|
except:
|
|
|
|
|
raise ConnectionRefusedError
|
2017-05-11 02:05:58 +00:00
|
|
|
|
|
2019-05-11 07:15:08 +00:00
|
|
|
|
def get_vms(self):
|
2017-05-13 03:50:56 +00:00
|
|
|
|
try:
|
2019-05-11 07:15:08 +00:00
|
|
|
|
return self._get_vm_pool()
|
2017-05-13 03:50:56 +00:00
|
|
|
|
except ConnectionRefusedError:
|
2017-05-14 10:22:10 +00:00
|
|
|
|
raise ConnectionRefusedError
|
2017-05-25 09:27:49 +00:00
|
|
|
|
|
2017-05-12 10:07:05 +00:00
|
|
|
|
def get_vm(self, vm_id):
|
2017-05-13 04:59:57 +00:00
|
|
|
|
vm_id = int(vm_id)
|
2017-05-13 03:50:56 +00:00
|
|
|
|
try:
|
2019-05-08 22:06:22 +00:00
|
|
|
|
vm_pool = self._get_vm_pool()
|
2017-05-13 04:59:57 +00:00
|
|
|
|
return vm_pool.get_by_id(vm_id)
|
2017-06-15 02:00:47 +00:00
|
|
|
|
except WrongIdError:
|
|
|
|
|
raise WrongIdError
|
2017-05-13 03:50:56 +00:00
|
|
|
|
except:
|
2017-05-14 10:22:10 +00:00
|
|
|
|
raise ConnectionRefusedError
|
2017-05-09 23:31:27 +00:00
|
|
|
|
|
2018-07-01 09:07:29 +00:00
|
|
|
|
def get_ipv6(self, vm_id):
|
2018-06-26 22:01:50 +00:00
|
|
|
|
"""
|
2018-07-01 09:07:29 +00:00
|
|
|
|
Returns the first IPv6 of the given vm.
|
2018-06-26 22:01:50 +00:00
|
|
|
|
|
2018-07-01 09:07:29 +00:00
|
|
|
|
:return: An IPv6 address string, if it exists else returns None
|
2018-06-26 22:01:50 +00:00
|
|
|
|
"""
|
2018-07-01 09:07:29 +00:00
|
|
|
|
ipv6_list = self.get_all_ipv6_addresses(vm_id)
|
2018-07-01 05:57:15 +00:00
|
|
|
|
if len(ipv6_list) > 0:
|
|
|
|
|
return ipv6_list[0]
|
2018-06-26 22:01:50 +00:00
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
2018-07-01 09:07:29 +00:00
|
|
|
|
def get_all_ipv6_addresses(self, vm_id):
|
2018-06-26 22:01:50 +00:00
|
|
|
|
"""
|
|
|
|
|
Returns a list of IPv6 addresses of the given vm
|
|
|
|
|
|
|
|
|
|
:param vm_id: The ID of the vm
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
2018-07-01 05:57:15 +00:00
|
|
|
|
ipv6_list = []
|
2018-06-26 22:01:50 +00:00
|
|
|
|
vm = self.get_vm(vm_id)
|
|
|
|
|
for nic in vm.template.nics:
|
|
|
|
|
if hasattr(nic, 'ip6_global'):
|
2018-07-01 05:57:15 +00:00
|
|
|
|
ipv6_list.append(nic.ip6_global)
|
|
|
|
|
return ipv6_list
|
2018-06-26 22:01:50 +00:00
|
|
|
|
|
2017-06-29 16:23:25 +00:00
|
|
|
|
def create_vm(self, template_id, specs, ssh_key=None, vm_name=None):
|
2017-05-13 04:59:57 +00:00
|
|
|
|
|
2017-05-13 11:47:53 +00:00
|
|
|
|
template = self.get_template(template_id)
|
|
|
|
|
vm_specs_formatter = """<TEMPLATE>
|
|
|
|
|
<MEMORY>{memory}</MEMORY>
|
|
|
|
|
<VCPU>{vcpu}</VCPU>
|
|
|
|
|
<CPU>{cpu}</CPU>
|
2017-05-24 18:45:20 +00:00
|
|
|
|
"""
|
2017-05-14 00:18:16 +00:00
|
|
|
|
try:
|
|
|
|
|
disk = template.template.disks[0]
|
|
|
|
|
image_id = disk.image_id
|
|
|
|
|
vm_specs = vm_specs_formatter.format(
|
2017-05-25 09:27:49 +00:00
|
|
|
|
vcpu=int(specs['cpu']),
|
|
|
|
|
cpu=0.1 * int(specs['cpu']),
|
2018-10-01 06:54:22 +00:00
|
|
|
|
memory=(512 if specs['memory'] == 0.5 else
|
|
|
|
|
1024 * int(specs['memory'])),
|
2017-05-25 09:27:49 +00:00
|
|
|
|
)
|
2017-05-14 00:18:16 +00:00
|
|
|
|
vm_specs += """<DISK>
|
|
|
|
|
<TYPE>fs</TYPE>
|
|
|
|
|
<SIZE>{size}</SIZE>
|
|
|
|
|
<DEV_PREFIX>vd</DEV_PREFIX>
|
|
|
|
|
<IMAGE_ID>{image_id}</IMAGE_ID>
|
|
|
|
|
</DISK>
|
|
|
|
|
""".format(size=1024 * int(specs['disk_size']),
|
|
|
|
|
image_id=image_id)
|
|
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
disk = template.template.disks[0]
|
|
|
|
|
image = disk.image
|
|
|
|
|
image_uname = disk.image_uname
|
|
|
|
|
|
|
|
|
|
vm_specs = vm_specs_formatter.format(
|
2017-05-25 09:27:49 +00:00
|
|
|
|
vcpu=int(specs['cpu']),
|
|
|
|
|
cpu=0.1 * int(specs['cpu']),
|
2018-10-01 06:54:22 +00:00
|
|
|
|
memory=(512 if specs['memory'] == 0.5 else
|
|
|
|
|
1024 * int(specs['memory'])),
|
2017-05-25 09:27:49 +00:00
|
|
|
|
)
|
2017-05-14 00:18:16 +00:00
|
|
|
|
vm_specs += """<DISK>
|
|
|
|
|
<TYPE>fs</TYPE>
|
|
|
|
|
<SIZE>{size}</SIZE>
|
|
|
|
|
<DEV_PREFIX>vd</DEV_PREFIX>
|
|
|
|
|
<IMAGE>{image}</IMAGE>
|
|
|
|
|
<IMAGE_UNAME>{image_uname}</IMAGE_UNAME>
|
|
|
|
|
</DISK>
|
|
|
|
|
""".format(size=1024 * int(specs['disk_size']),
|
|
|
|
|
image=image,
|
|
|
|
|
image_uname=image_uname)
|
2017-06-29 14:34:40 +00:00
|
|
|
|
|
2017-06-21 07:43:10 +00:00
|
|
|
|
vm_specs += "<CONTEXT>"
|
2017-06-01 22:49:17 +00:00
|
|
|
|
if ssh_key:
|
2017-08-29 16:02:56 +00:00
|
|
|
|
vm_specs += "<SSH_PUBLIC_KEY>{ssh}</SSH_PUBLIC_KEY>".format(
|
|
|
|
|
ssh=ssh_key)
|
2017-06-21 07:43:10 +00:00
|
|
|
|
vm_specs += """<NETWORK>YES</NETWORK>
|
2017-06-01 22:49:17 +00:00
|
|
|
|
</CONTEXT>
|
2017-06-21 07:43:10 +00:00
|
|
|
|
</TEMPLATE>
|
|
|
|
|
"""
|
2017-11-29 05:45:09 +00:00
|
|
|
|
try:
|
|
|
|
|
vm_id = self.client.call(
|
|
|
|
|
oca.VmTemplate.METHODS['instantiate'], template.id, '', True,
|
|
|
|
|
vm_specs, False
|
|
|
|
|
)
|
|
|
|
|
except OpenNebulaException as err:
|
|
|
|
|
logger.error("OpenNebulaException: {0}".format(err))
|
|
|
|
|
return None
|
2017-05-13 04:59:57 +00:00
|
|
|
|
|
2017-05-24 22:18:03 +00:00
|
|
|
|
self.oneadmin_client.call(
|
2017-05-25 09:27:49 +00:00
|
|
|
|
oca.VirtualMachine.METHODS['action'],
|
|
|
|
|
'release',
|
|
|
|
|
vm_id
|
|
|
|
|
)
|
2017-06-29 16:23:25 +00:00
|
|
|
|
|
2017-06-21 18:47:53 +00:00
|
|
|
|
if vm_name is not None:
|
|
|
|
|
self.oneadmin_client.call(
|
|
|
|
|
'vm.rename',
|
|
|
|
|
vm_id,
|
|
|
|
|
vm_name
|
|
|
|
|
)
|
2017-05-09 23:31:27 +00:00
|
|
|
|
return vm_id
|
|
|
|
|
|
2017-05-11 02:05:58 +00:00
|
|
|
|
def delete_vm(self, vm_id):
|
2018-08-04 07:52:15 +00:00
|
|
|
|
TERMINATE_ACTION = 'terminate-hard'
|
2017-05-12 17:13:18 +00:00
|
|
|
|
vm_terminated = False
|
|
|
|
|
try:
|
|
|
|
|
self.oneadmin_client.call(
|
|
|
|
|
oca.VirtualMachine.METHODS['action'],
|
|
|
|
|
TERMINATE_ACTION,
|
|
|
|
|
int(vm_id),
|
|
|
|
|
)
|
|
|
|
|
vm_terminated = True
|
|
|
|
|
except socket.timeout as socket_err:
|
2018-07-25 19:41:05 +00:00
|
|
|
|
logger.error("Socket timeout error: {0}".format(socket_err))
|
2017-05-12 17:13:18 +00:00
|
|
|
|
except OpenNebulaException as opennebula_err:
|
2018-07-25 19:41:05 +00:00
|
|
|
|
logger.error(
|
2017-05-25 09:27:49 +00:00
|
|
|
|
"OpenNebulaException error: {0}".format(opennebula_err))
|
2017-05-12 17:13:18 +00:00
|
|
|
|
except OSError as os_err:
|
2018-07-25 19:41:05 +00:00
|
|
|
|
logger.error("OSError : {0}".format(os_err))
|
2017-05-12 17:13:18 +00:00
|
|
|
|
except ValueError as value_err:
|
2018-07-25 19:41:05 +00:00
|
|
|
|
logger.error("ValueError : {0}".format(value_err))
|
2017-05-12 17:13:18 +00:00
|
|
|
|
|
|
|
|
|
return vm_terminated
|
2017-05-11 02:05:58 +00:00
|
|
|
|
|
2019-05-10 23:46:28 +00:00
|
|
|
|
def save_key_in_vm_template(self, vm_id, ssh_key):
|
|
|
|
|
"""
|
|
|
|
|
Update the template of a given VM and set the ssh key of the user
|
|
|
|
|
:param vm_id: the identifier of the VM object
|
|
|
|
|
:param ssh_key: a newline(\n) separated ssh key string that needs to be
|
|
|
|
|
set in the VM template
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
UPDATE_TYPE = 1
|
|
|
|
|
return_value = self.client.call(
|
|
|
|
|
'vm.updateconf',
|
|
|
|
|
vm_id,
|
|
|
|
|
'<CONTEXT><SSH_PUBLIC_KEY>%s</SSH_PUBLIC_KEY></CONTEXT>' % ssh_key,
|
|
|
|
|
UPDATE_TYPE
|
|
|
|
|
)
|
|
|
|
|
if type(return_value) == int:
|
|
|
|
|
logger.debug(
|
|
|
|
|
"Saved the key in VM Template success : %s" % return_value)
|
|
|
|
|
else:
|
|
|
|
|
logger.error(
|
|
|
|
|
"Could not save the key in VM Template. %s" % return_value)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def save_key_in_opennebula_user(self, ssh_key):
|
2019-05-10 07:19:31 +00:00
|
|
|
|
"""
|
|
|
|
|
Save the given ssh key in OpenNebula user
|
|
|
|
|
|
|
|
|
|
# Update type: 0: Replace the whole template.
|
|
|
|
|
1: Merge new template with the existing one.
|
|
|
|
|
:param ssh_key: The ssh key to be saved
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
UPDATE_TYPE = 1
|
|
|
|
|
return_value = self.client.call(
|
2019-05-10 21:51:05 +00:00
|
|
|
|
'user.update',
|
2019-05-10 21:57:52 +00:00
|
|
|
|
self.opennebula_user.id,
|
2019-05-10 22:31:25 +00:00
|
|
|
|
'<CONTEXT><SSH_PUBLIC_KEY>%s</SSH_PUBLIC_KEY></CONTEXT>' % ssh_key,
|
2019-05-10 07:19:31 +00:00
|
|
|
|
UPDATE_TYPE
|
|
|
|
|
)
|
|
|
|
|
if type(return_value) == int:
|
|
|
|
|
logger.debug(
|
|
|
|
|
"Saved the key in opennebula successfully : %s" % return_value)
|
|
|
|
|
else:
|
|
|
|
|
logger.error(
|
|
|
|
|
"Could not save the key in opennebula. %s" % return_value)
|
|
|
|
|
return
|
|
|
|
|
|
2017-05-11 02:05:58 +00:00
|
|
|
|
def _get_template_pool(self):
|
|
|
|
|
try:
|
2017-05-25 09:27:49 +00:00
|
|
|
|
template_pool = oca.VmTemplatePool(self.oneadmin_client)
|
|
|
|
|
template_pool.info()
|
|
|
|
|
return template_pool
|
2017-05-11 02:05:58 +00:00
|
|
|
|
except ConnectionRefusedError:
|
2018-07-25 19:41:05 +00:00
|
|
|
|
logger.error(
|
2017-09-14 13:27:25 +00:00
|
|
|
|
"""Could not connect to host: {host} via protocol
|
|
|
|
|
{protocol}""".format(
|
2017-08-29 16:02:56 +00:00
|
|
|
|
host=settings.OPENNEBULA_DOMAIN,
|
|
|
|
|
protocol=settings.OPENNEBULA_PROTOCOL)
|
2017-05-25 09:27:49 +00:00
|
|
|
|
)
|
2017-05-11 02:05:58 +00:00
|
|
|
|
raise ConnectionRefusedError
|
2017-05-14 10:22:10 +00:00
|
|
|
|
except:
|
|
|
|
|
raise ConnectionRefusedError
|
2017-05-11 02:05:58 +00:00
|
|
|
|
|
2018-05-23 22:27:01 +00:00
|
|
|
|
def get_templates(self, prefix='public-'):
|
2017-05-13 03:50:56 +00:00
|
|
|
|
try:
|
|
|
|
|
public_templates = [
|
2017-05-25 09:27:49 +00:00
|
|
|
|
template
|
|
|
|
|
for template in self._get_template_pool()
|
2018-05-23 22:27:01 +00:00
|
|
|
|
if template.name.startswith(prefix)
|
2017-05-25 09:27:49 +00:00
|
|
|
|
]
|
|
|
|
|
return public_templates
|
2017-05-13 03:50:56 +00:00
|
|
|
|
except ConnectionRefusedError:
|
2017-05-14 10:22:10 +00:00
|
|
|
|
raise ConnectionRefusedError
|
|
|
|
|
except:
|
|
|
|
|
raise ConnectionRefusedError
|
|
|
|
|
|
|
|
|
|
def try_get_templates(self):
|
|
|
|
|
try:
|
|
|
|
|
return self.get_templates()
|
|
|
|
|
except:
|
2017-05-13 03:50:56 +00:00
|
|
|
|
return []
|
|
|
|
|
|
2017-05-12 10:07:05 +00:00
|
|
|
|
def get_template(self, template_id):
|
2017-05-13 04:59:57 +00:00
|
|
|
|
template_id = int(template_id)
|
|
|
|
|
try:
|
|
|
|
|
template_pool = self._get_template_pool()
|
|
|
|
|
return template_pool.get_by_id(template_id)
|
|
|
|
|
except:
|
2017-05-14 10:22:10 +00:00
|
|
|
|
raise ConnectionRefusedError
|
2017-05-11 02:05:58 +00:00
|
|
|
|
|
2017-08-29 16:02:56 +00:00
|
|
|
|
def create_template(self, name, cores, memory, disk_size, core_price,
|
|
|
|
|
memory_price,
|
2017-05-25 09:27:49 +00:00
|
|
|
|
disk_size_price, ssh=''):
|
2017-05-10 00:49:03 +00:00
|
|
|
|
"""Create and add a new template to opennebula.
|
|
|
|
|
:param name: A string representation describing the template.
|
|
|
|
|
Used as label in view.
|
|
|
|
|
:param cores: Amount of virtual cpu cores for the VM.
|
2017-05-12 10:07:05 +00:00
|
|
|
|
:param memory: Amount of RAM for the VM (GB)
|
|
|
|
|
:param disk_size: Amount of disk space for VM (GB)
|
|
|
|
|
:param core_price: Price of virtual cpu for the VM per core.
|
|
|
|
|
:param memory_price: Price of RAM for the VM per GB
|
|
|
|
|
:param disk_size_price: Price of disk space for VM per GB
|
|
|
|
|
:param ssh: User public ssh key
|
2017-05-10 00:49:03 +00:00
|
|
|
|
"""
|
2017-05-09 23:11:49 +00:00
|
|
|
|
template_string_formatter = """<TEMPLATE>
|
|
|
|
|
<NAME>{name}</NAME>
|
|
|
|
|
<MEMORY>{memory}</MEMORY>
|
|
|
|
|
<VCPU>{vcpu}</VCPU>
|
|
|
|
|
<CPU>{cpu}</CPU>
|
|
|
|
|
<DISK>
|
|
|
|
|
<TYPE>fs</TYPE>
|
|
|
|
|
<SIZE>{size}</SIZE>
|
|
|
|
|
<DEV_PREFIX>vd</DEV_PREFIX>
|
|
|
|
|
</DISK>
|
2017-05-12 10:07:05 +00:00
|
|
|
|
<CPU_COST>{cpu_cost}</CPU_COST>
|
|
|
|
|
<MEMORY_COST>{memory_cost}</MEMORY_COST>
|
|
|
|
|
<DISK_COST>{disk_cost}</DISK_COST>
|
|
|
|
|
<SSH_PUBLIC_KEY>{ssh}</SSH_PUBLIC_KEY>
|
2017-05-09 23:11:49 +00:00
|
|
|
|
</TEMPLATE>
|
|
|
|
|
"""
|
|
|
|
|
template_id = oca.VmTemplate.allocate(
|
|
|
|
|
self.oneadmin_client,
|
|
|
|
|
template_string_formatter.format(
|
|
|
|
|
name=name,
|
|
|
|
|
vcpu=cores,
|
2017-05-25 09:27:49 +00:00
|
|
|
|
cpu=0.1 * cores,
|
2017-05-09 23:11:49 +00:00
|
|
|
|
size=1024 * disk_size,
|
2017-05-12 10:07:05 +00:00
|
|
|
|
memory=1024 * memory,
|
|
|
|
|
# * 10 because we set cpu to *0.1
|
2017-05-25 09:27:49 +00:00
|
|
|
|
cpu_cost=10 * core_price,
|
2017-05-12 10:07:05 +00:00
|
|
|
|
memory_cost=memory_price,
|
|
|
|
|
disk_cost=disk_size_price,
|
|
|
|
|
ssh=ssh
|
2017-05-09 23:11:49 +00:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return template_id
|
2017-05-11 02:05:58 +00:00
|
|
|
|
|
|
|
|
|
def delete_template(self, template_id):
|
2018-07-01 09:17:45 +00:00
|
|
|
|
self.oneadmin_client.call(
|
|
|
|
|
oca.VmTemplate.METHODS['delete'], template_id, False
|
|
|
|
|
)
|
2017-05-11 02:05:58 +00:00
|
|
|
|
|
2017-09-25 07:35:18 +00:00
|
|
|
|
def change_user_password(self, passwd_hash):
|
2017-05-13 03:50:56 +00:00
|
|
|
|
self.oneadmin_client.call(
|
|
|
|
|
oca.User.METHODS['passwd'],
|
|
|
|
|
self.opennebula_user.id,
|
2017-09-25 07:35:18 +00:00
|
|
|
|
passwd_hash
|
2017-05-25 09:27:49 +00:00
|
|
|
|
)
|
2017-05-31 16:01:54 +00:00
|
|
|
|
|
2017-06-01 17:08:38 +00:00
|
|
|
|
def add_public_key(self, user, public_key='', merge=False):
|
2017-06-29 14:34:40 +00:00
|
|
|
|
"""
|
2017-05-31 16:01:54 +00:00
|
|
|
|
|
2017-06-29 14:34:40 +00:00
|
|
|
|
Args:
|
|
|
|
|
user (CustomUser): Dynamicweb user
|
2017-05-31 16:01:54 +00:00
|
|
|
|
public_key (string): Public key to add to the user
|
2017-06-01 18:47:11 +00:00
|
|
|
|
merge (bool): Optional if True the new public key replaces the old
|
2017-05-31 16:01:54 +00:00
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
KeyExistsError: If replace is False and the user already has a
|
2017-06-29 14:34:40 +00:00
|
|
|
|
public key
|
2017-05-31 16:01:54 +00:00
|
|
|
|
WrongNameError: If no openebula user with this credentials exists
|
|
|
|
|
ConnectionError: If the connection to the opennebula server can't be
|
2017-06-29 14:34:40 +00:00
|
|
|
|
established
|
2017-05-31 16:01:54 +00:00
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
True if public_key was added
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
# TODO: Check if we can remove this first try because we basically just
|
2017-06-29 14:34:40 +00:00
|
|
|
|
# raise the possible Errors
|
2017-05-31 16:01:54 +00:00
|
|
|
|
try:
|
|
|
|
|
open_user = self._get_user(user)
|
|
|
|
|
try:
|
2017-06-29 14:34:40 +00:00
|
|
|
|
old_key = open_user.template.ssh_public_key
|
2017-06-01 17:08:38 +00:00
|
|
|
|
if not merge:
|
2017-05-31 16:01:54 +00:00
|
|
|
|
raise KeyExistsError()
|
2017-06-01 17:08:38 +00:00
|
|
|
|
public_key += '\n{key}'.format(key=old_key)
|
2017-05-31 16:01:54 +00:00
|
|
|
|
|
|
|
|
|
except AttributeError:
|
|
|
|
|
pass
|
|
|
|
|
self.oneadmin_client.call('user.update', open_user.id,
|
2017-06-29 14:34:40 +00:00
|
|
|
|
'<CONTEXT><SSH_PUBLIC_KEY>{key}</SSH_PUBLIC_KEY></CONTEXT>'
|
|
|
|
|
.format(key=public_key))
|
2017-05-31 16:01:54 +00:00
|
|
|
|
return True
|
|
|
|
|
except WrongNameError:
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
except ConnectionError:
|
|
|
|
|
raise
|
2017-06-01 20:25:10 +00:00
|
|
|
|
|
|
|
|
|
def remove_public_key(self, user, public_key=''):
|
2017-06-29 14:34:40 +00:00
|
|
|
|
"""
|
2017-06-01 20:25:10 +00:00
|
|
|
|
|
2017-06-29 14:34:40 +00:00
|
|
|
|
Args:
|
|
|
|
|
user (CustomUser): Dynamicweb user
|
2017-06-01 20:25:10 +00:00
|
|
|
|
public_key (string): Public key to be removed to the user
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
KeyDoesNotExistsError: If replace is False and the user already has a
|
2017-06-29 14:34:40 +00:00
|
|
|
|
public key
|
2017-06-01 20:25:10 +00:00
|
|
|
|
WrongNameError: If no openebula user with this credentials exists
|
|
|
|
|
ConnectionError: If the connection to the opennebula server can't be
|
2017-06-29 14:34:40 +00:00
|
|
|
|
established
|
2017-06-01 20:25:10 +00:00
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
True if public_key was removed
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
open_user = self._get_user(user)
|
|
|
|
|
try:
|
2017-06-29 14:34:40 +00:00
|
|
|
|
old_key = open_user.template.ssh_public_key
|
2017-06-01 20:25:10 +00:00
|
|
|
|
if public_key not in old_key:
|
2017-06-11 16:44:14 +00:00
|
|
|
|
return False
|
|
|
|
|
# raise KeyDoesNotExistsError()
|
2017-06-01 20:25:10 +00:00
|
|
|
|
if '\n{}'.format(public_key) in old_key:
|
|
|
|
|
public_key = old_key.replace('\n{}'.format(public_key), '')
|
2017-06-29 14:34:40 +00:00
|
|
|
|
else:
|
2017-06-01 20:25:10 +00:00
|
|
|
|
public_key = old_key.replace(public_key, '')
|
|
|
|
|
|
|
|
|
|
except AttributeError:
|
2017-06-11 17:59:57 +00:00
|
|
|
|
return False
|
2017-06-29 14:34:40 +00:00
|
|
|
|
# raise KeyDoesNotExistsError()
|
|
|
|
|
|
2017-06-01 20:25:10 +00:00
|
|
|
|
self.oneadmin_client.call('user.update', open_user.id,
|
2017-06-29 14:34:40 +00:00
|
|
|
|
'<CONTEXT><SSH_PUBLIC_KEY>{key}</SSH_PUBLIC_KEY></CONTEXT>'
|
|
|
|
|
.format(key=public_key))
|
2017-06-01 20:25:10 +00:00
|
|
|
|
return True
|
|
|
|
|
except WrongNameError:
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
except ConnectionError:
|
|
|
|
|
raise
|
2017-08-29 16:02:56 +00:00
|
|
|
|
|
2017-08-31 16:33:07 +00:00
|
|
|
|
def manage_public_key(self, keys, hosts=None, countdown=0):
|
2017-08-29 16:02:56 +00:00
|
|
|
|
"""
|
2017-08-31 16:33:07 +00:00
|
|
|
|
A function that manages the supplied keys in the
|
|
|
|
|
authorized_keys file of the given list of hosts. If hosts
|
|
|
|
|
parameter is not supplied, all hosts of this customer
|
|
|
|
|
will be configured with the supplied keys
|
2017-08-30 07:43:54 +00:00
|
|
|
|
|
2017-08-31 16:33:07 +00:00
|
|
|
|
:param keys: A list of ssh keys that are to be added/removed
|
2017-08-30 07:43:54 +00:00
|
|
|
|
A key should be a dict of the form
|
|
|
|
|
{
|
|
|
|
|
'value': 'sha-.....', # public key as string
|
|
|
|
|
'state': True # whether key is to be added or
|
|
|
|
|
} # removed
|
2018-07-01 09:07:29 +00:00
|
|
|
|
:param hosts: A list of hosts IPv6 addresses
|
2017-08-31 16:33:07 +00:00
|
|
|
|
:param countdown: Parameter to be passed to celery apply_async
|
|
|
|
|
Allows to delay a task by `countdown` number of seconds
|
2017-08-29 16:02:56 +00:00
|
|
|
|
:return:
|
|
|
|
|
"""
|
2017-08-31 16:33:07 +00:00
|
|
|
|
if hosts is None:
|
|
|
|
|
hosts = self.get_all_hosts()
|
|
|
|
|
|
|
|
|
|
if len(hosts) > 0 and len(keys) > 0:
|
2017-08-31 21:03:31 +00:00
|
|
|
|
save_ssh_key.apply_async((hosts, keys), countdown=countdown,
|
|
|
|
|
link_error=save_ssh_key_error_handler.s())
|
2017-08-31 16:33:07 +00:00
|
|
|
|
else:
|
2017-09-13 10:24:08 +00:00
|
|
|
|
logger.debug(
|
2018-07-01 09:07:29 +00:00
|
|
|
|
"Keys and/or hosts are empty, so not managing any keys"
|
|
|
|
|
)
|
2017-08-31 16:33:07 +00:00
|
|
|
|
|
|
|
|
|
def get_all_hosts(self):
|
|
|
|
|
"""
|
|
|
|
|
A utility function to obtain all hosts of this owner
|
2018-07-01 09:07:29 +00:00
|
|
|
|
:return: A list of IPv6 addresses of all the hosts of this customer or
|
|
|
|
|
an empty list if none exist
|
2017-08-31 16:33:07 +00:00
|
|
|
|
"""
|
2017-08-29 16:02:56 +00:00
|
|
|
|
owner = CustomUser.objects.filter(
|
2017-08-31 07:24:08 +00:00
|
|
|
|
email=self.email).first()
|
2017-08-29 16:02:56 +00:00
|
|
|
|
all_orders = HostingOrder.objects.filter(customer__user=owner)
|
2017-08-31 16:33:07 +00:00
|
|
|
|
hosts = []
|
2017-08-29 16:02:56 +00:00
|
|
|
|
if len(all_orders) > 0:
|
|
|
|
|
logger.debug("The user {} has 1 or more VMs. We need to configure "
|
2017-08-31 07:24:08 +00:00
|
|
|
|
"the ssh keys.".format(self.email))
|
2017-08-29 16:02:56 +00:00
|
|
|
|
for order in all_orders:
|
|
|
|
|
try:
|
2018-07-01 09:07:29 +00:00
|
|
|
|
ip = self.get_ipv6(order.vm_id)
|
2018-06-26 23:06:10 +00:00
|
|
|
|
hosts.append(ip)
|
2017-08-29 16:02:56 +00:00
|
|
|
|
except WrongIdError:
|
|
|
|
|
logger.debug(
|
|
|
|
|
"VM with ID {} does not exist".format(order.vm_id))
|
|
|
|
|
else:
|
|
|
|
|
logger.debug("The user {} has no VMs. We don't need to configure "
|
2017-08-31 07:24:08 +00:00
|
|
|
|
"the ssh keys.".format(self.email))
|
2017-08-31 16:33:07 +00:00
|
|
|
|
return hosts
|