Starting again with actor over REST and fixed a few Xen "typos"

This commit is contained in:
Adolfo Gómez García 2014-09-24 13:57:14 +02:00
parent 9896197c3c
commit 17ec42fca5
3 changed files with 73 additions and 66 deletions

View File

@ -38,11 +38,15 @@ from uds.core.util import Config
from uds.core.util.State import State from uds.core.util.State import State
from uds.core.util import log from uds.core.util import log
from uds.core.managers import cryptoManager from uds.core.managers import cryptoManager
from uds.REST import Handler, AccessDenied, RequestError from uds.REST import Handler
from uds.REST import AccessDenied
from uds.REST import RequestError
from uds.models import UserService from uds.models import UserService
import datetime import datetime
import six
import simplejson as json
import logging import logging
@ -52,7 +56,7 @@ logger = logging.getLogger(__name__)
# Actor key, configurable in Security Section of administration interface # Actor key, configurable in Security Section of administration interface
actorKey = Config.Config.section(Config.SECURITY_SECTION).value('actorKey', actorKey = Config.Config.section(Config.SECURITY_SECTION).value('actorKey',
cryptoManager().uuid(datetime.datetime.now()).replace('-', ''), cryptoManager().uuid(datetime.datetime.now()).replace('-', ''),
type=Config.Config.NUMERIC_FIELD) type=Config.Config.TEXT_FIELD)
actorKey.get() actorKey.get()
@ -64,23 +68,17 @@ class Actor(Handler):
authenticated = False # Actor requests are not authenticated authenticated = False # Actor requests are not authenticated
def test(self): def test(self):
'''
Executes and returns the test
'''
return {'result': _('Correct'), 'date': datetime.datetime.now()} return {'result': _('Correct'), 'date': datetime.datetime.now()}
def getClientIdAndMessage(self): def validateRequestKey(self):
# Now we will process .../clientIds/message
if len(self._args) < 3:
raise RequestError('Invalid arguments provided')
clientIds, message = self._args[1].split(',')[:5], self._args[2]
return clientIds, message
def validateRequest(self):
# Ensures that key is first parameter # Ensures that key is first parameter
# Here, path will be .../actor/KEY/... (probably /rest/actor/KEY/...) # Here, path will be .../actor/ACTION/KEY (probably /rest/actor/KEY/...)
if self._args[0] != actorKey.get(True): if self._params.get('key') != actorKey.get(True):
raise AccessDenied('Invalid actor key') return {'result': _('Invalid key'), 'date': datetime.datetime.now()}
return None
def processRequest(self, clientIds, message, data): def processRequest(self, clientIds, message, data):
logger.debug("Called message for id_ {0}, message \"{1}\" and data \"{2}\"".format(clientIds, message, data)) logger.debug("Called message for id_ {0}, message \"{1}\" and data \"{2}\"".format(clientIds, message, data))
@ -105,35 +103,46 @@ class Actor(Handler):
logger.debug("Returning {0}".format(res)) logger.debug("Returning {0}".format(res))
return res return res
def getUserServiceByIds(self):
'''
This will get the client from the IDs passed from parameters
'''
logger.debug('Getting User services from ids: {}'.format(self._params.get('id')))
try:
clientIds = self._params.get('id').split(',')[:5]
except Exception:
raise RequestError('Invalid request: (no id found)')
services = UserService.objects.filter(unique_id__in=clientIds, state__in=[State.USABLE, State.PREPARING])
if services.count() == 0:
return None
return services[0]
def get(self): def get(self):
''' '''
Processes get requests Processes get requests
''' '''
logger.debug("Actor args for GET: {0}".format(self._args)) logger.debug("Actor args for GET: {0}".format(self._args))
self.validateRequest() # Wil raise an access denied exception if not valid # if path is .../test (/rest/actor/[test|init]?key=.....)
if self._args[0] in ('test', 'init'):
v = self.validateRequestKey()
if v is not None:
return v
if self._args[0] == 'test':
return self.test()
# if path is .../test (/rest/actor/KEY/test) # Returns UID of selected Machine
if self._args[1] == 'test': service = self.getUserServiceByIds()
return self.test() if service is None:
return ""
clientIds, message = self.getClientIdAndMessage() else:
return service.uuid
try:
data = self._args[3]
except Exception:
data = ''
return self.processRequest(clientIds, message, data)
def post(self): def post(self):
''' '''
Processes post requests Processes post requests
''' '''
self.validateRequest() # Wil raise an access denied exception if not valid raise RequestError('Invalid method invoked')
clientIds, message = self.getClientIdAndMessage()
data = self._params[0]
return self.processRequest(clientIds, message, data)

View File

@ -49,7 +49,8 @@ class XenFailure(XenAPI.Failure, XenFault):
exHandleInvalid = 'HANDLE_INVALID' exHandleInvalid = 'HANDLE_INVALID'
exHostIsSlave = 'HOST_IS_SLAVE' exHostIsSlave = 'HOST_IS_SLAVE'
def __init__(self, details=[]): def __init__(self, details=None):
details = [] if details is None else details
super(XenFailure, self).__init__(details) super(XenFailure, self).__init__(details)
def isHandleInvalid(self): def isHandleInvalid(self):
@ -75,7 +76,7 @@ class XenFailure(XenAPI.Failure, XenFault):
err = errList.get(self.details[0], 'Error {0}') err = errList.get(self.details[0], 'Error {0}')
return err.format(*self.details) return err.format(*self.details)
except: except Exception:
return 'Unknown exception: {0}'.format(self.details) return 'Unknown exception: {0}'.format(self.details)
def __unicode__(self): def __unicode__(self):
@ -83,9 +84,8 @@ class XenFailure(XenAPI.Failure, XenFault):
class XenException(XenFault): class XenException(XenFault):
def __init__(self, message): def __init__(self, message):
Exception.__init__(self, message) XenFault.__init__(self, message)
logger.debug('Exception create: {0}'.format(message)) logger.debug('Exception create: {0}'.format(message))
@ -106,37 +106,38 @@ class XenServer(object):
self._loggedIn = False self._loggedIn = False
self._username = username self._username = username
self._password = password self._password = password
self._poolName = '' self._session = None
self._poolName = self._apiVersion = ''
@staticmethod @staticmethod
def toMb(number): def toMb(number):
return int(number) / (1024 * 1024) return int(number) / (1024 * 1024)
def __checkLogin(self): def checkLogin(self):
if self._loggedIn is False: if self._loggedIn is False:
self.login() self.login()
return self._loggedIn return self._loggedIn
def __getXenapiProperty(self, prop): def getXenapiProperty(self, prop):
if self.__checkLogin() is False: if self.checkLogin() is False:
raise "Can't log in" raise Exception("Can't log in")
return getattr(self._session.xenapi, prop) return getattr(self._session.xenapi, prop)
# Properties to fast access XenApi classes # Properties to fast access XenApi classes
Async = property(lambda self: self.__getXenapiProperty('Async')) Async = property(lambda self: self.getXenapiProperty('Async'))
task = property(lambda self: self.__getXenapiProperty('task')) task = property(lambda self: self.getXenapiProperty('task'))
VM = property(lambda self: self.__getXenapiProperty('VM')) VM = property(lambda self: self.getXenapiProperty('VM'))
SR = property(lambda self: self.__getXenapiProperty('SR')) SR = property(lambda self: self.getXenapiProperty('SR'))
pool = property(lambda self: self.__getXenapiProperty('pool')) pool = property(lambda self: self.getXenapiProperty('pool'))
host = property(lambda self: self.__getXenapiProperty('host')) host = property(lambda self: self.getXenapiProperty('host'))
network = property(lambda self: self.__getXenapiProperty('network')) network = property(lambda self: self.getXenapiProperty('network'))
VIF = property(lambda self: self.__getXenapiProperty('VIF')) # Virtual Interface VIF = property(lambda self: self.getXenapiProperty('VIF')) # Virtual Interface
VDI = property(lambda self: self.__getXenapiProperty('VDI')) # Virtual Disk Image VDI = property(lambda self: self.getXenapiProperty('VDI')) # Virtual Disk Image
VBD = property(lambda self: self.__getXenapiProperty('VBD')) # Virtual Block Device VBD = property(lambda self: self.getXenapiProperty('VBD')) # Virtual Block Device
# Properties to access private vars # Properties to access private vars
poolName = property(lambda self: self.__checkLogin() and self._poolName) poolName = property(lambda self: self.checkLogin() and self._poolName)
hasPool = property(lambda self: self.__checkLogin() and self._poolName != '') hasPool = property(lambda self: self.checkLogin() and self._poolName != '')
def getPoolName(self): def getPoolName(self):
pool = self.pool.get_all()[0] pool = self.pool.get_all()[0]
@ -224,7 +225,7 @@ class XenServer(object):
# Only valid SR shared, non iso # Only valid SR shared, non iso
name_label = self.SR.get_name_label(srId) name_label = self.SR.get_name_label(srId)
if self.SR.get_content_type(srId) == 'iso' or \ if self.SR.get_content_type(srId) == 'iso' or \
self.SR.get_shared(srId) == False or \ self.SR.get_shared(srId) is False or \
name_label == '': name_label == '':
continue continue
@ -300,7 +301,7 @@ class XenServer(object):
def startVM(self, vmId, async=True): def startVM(self, vmId, async=True):
vmState = self.getVMPowerState(vmId) vmState = self.getVMPowerState(vmId)
if vmState == XenPowerState.running: if vmState == XenPowerState.running:
return None # Already powered on return None # Already powered on
if async: if async:
return self.Async.VM.start(vmId, False, False) return self.Async.VM.start(vmId, False, False)
return self.VM.start(vmId, False, False) return self.VM.start(vmId, False, False)
@ -308,7 +309,7 @@ class XenServer(object):
def stopVM(self, vmId, async=True): def stopVM(self, vmId, async=True):
vmState = self.getVMPowerState(vmId) vmState = self.getVMPowerState(vmId)
if vmState in (XenPowerState.suspended, XenPowerState.halted): if vmState in (XenPowerState.suspended, XenPowerState.halted):
return None # Already powered off return None # Already powered off
if async: if async:
return self.Async.VM.hard_shutdown(vmId) return self.Async.VM.hard_shutdown(vmId)
return self.VM.hard_shutdown(vmId) return self.VM.hard_shutdown(vmId)
@ -388,7 +389,6 @@ class XenServer(object):
Mac address should be in the range 02:xx:xx:xx:xx (recommended, but not a "have to") Mac address should be in the range 02:xx:xx:xx:xx (recommended, but not a "have to")
''' '''
pass
mac = kwargs.get('mac', None) mac = kwargs.get('mac', None)
memory = kwargs.get('memory', None) memory = kwargs.get('memory', None)
@ -423,7 +423,7 @@ class XenServer(object):
tags = self.VM.get_tags(vmId) tags = self.VM.get_tags(vmId)
try: try:
del tags[tags.index(TAG_TEMPLATE)] del tags[tags.index(TAG_TEMPLATE)]
except: except Exception:
pass pass
tags.append(TAG_MACHINE) tags.append(TAG_MACHINE)
self.VM.set_tags(vmId, tags) self.VM.set_tags(vmId, tags)
@ -432,7 +432,6 @@ class XenServer(object):
return self.Async.VM.provision(vmId) return self.Async.VM.provision(vmId)
return self.VM.provision(vmId) return self.VM.provision(vmId)
def convertToTemplate(self, vmId, shadowMultiplier=4): def convertToTemplate(self, vmId, shadowMultiplier=4):
try: try:
operations = self.VM.get_allowed_operations(vmId) operations = self.VM.get_allowed_operations(vmId)
@ -453,7 +452,7 @@ class XenServer(object):
# Set multiplier # Set multiplier
try: try:
self.VM.set_HVM_shadow_multiplier(vmId, float(shadowMultiplier)) self.VM.set_HVM_shadow_multiplier(vmId, float(shadowMultiplier))
except: except Exception:
# Can't set shadowMultiplier, nothing happens # Can't set shadowMultiplier, nothing happens
pass # TODO: Log this? pass # TODO: Log this?
except XenAPI.Failure as e: except XenAPI.Failure as e:
@ -467,4 +466,3 @@ class XenServer(object):
After cloning template, we must deploy the VM so it's a full usable VM After cloning template, we must deploy the VM so it's a full usable VM
''' '''
return self.cloneVM(templateId, targetName) return self.cloneVM(templateId, targetName)

View File

@ -1,3 +1,3 @@
''' '''
This module is responsible of the communication betwen actors and OS Managers This module is responsible of the communication between actors and OS Managers
''' '''