Commit 2b579871 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_slap_tool: General code clean up

parent 1b1da7c8
...@@ -42,8 +42,7 @@ from Products.ERP5Type.Cache import CachingMethod ...@@ -42,8 +42,7 @@ from Products.ERP5Type.Cache import CachingMethod
from lxml import etree from lxml import etree
try: try:
from slapos.slap.slap import ( from slapos.slap.slap import (
Computer as ComputeNode, Computer as ComputeNode)
SoftwareInstance)
from slapos.util import dict2xml, calculate_dict_hash, loads, dumps from slapos.util import dict2xml, calculate_dict_hash, loads, dumps
except ImportError: except ImportError:
# Do no prevent instance from starting # Do no prevent instance from starting
...@@ -51,9 +50,6 @@ except ImportError: ...@@ -51,9 +50,6 @@ except ImportError:
class ComputeNode: class ComputeNode:
def __init__(self): def __init__(self):
raise ImportError raise ImportError
class SoftwareInstance:
def __init__(self):
raise ImportError
def dict2xml(dictionary): def dict2xml(dictionary):
raise ImportError raise ImportError
def calculate_dict_hash(dictionary): def calculate_dict_hash(dictionary):
...@@ -662,7 +658,7 @@ class SlapTool(BaseTool): ...@@ -662,7 +658,7 @@ class SlapTool(BaseTool):
compute_partition_document = self._getComputePartitionDocument( compute_partition_document = self._getComputePartitionDocument(
computer_reference, computer_partition_reference) computer_reference, computer_partition_reference)
result = compute_partition_document._registerComputePartition() slap_compute_partition = compute_partition_document._registerComputePartition()
# Keep in cache server for 7 days # Keep in cache server for 7 days
self.REQUEST.response.setStatus(200) self.REQUEST.response.setStatus(200)
...@@ -672,7 +668,7 @@ class SlapTool(BaseTool): ...@@ -672,7 +668,7 @@ class SlapTool(BaseTool):
'REMOTE_USER') 'REMOTE_USER')
self.REQUEST.response.setHeader('Last-Modified', rfc1123_date(DateTime())) self.REQUEST.response.setHeader('Last-Modified', rfc1123_date(DateTime()))
self.REQUEST.response.setHeader('Content-Type', 'text/xml; charset=utf-8') self.REQUEST.response.setHeader('Content-Type', 'text/xml; charset=utf-8')
self.REQUEST.response.setBody(result) self.REQUEST.response.setBody(dumps(slap_compute_partition))
return self.REQUEST.response return self.REQUEST.response
...@@ -898,15 +894,6 @@ class SlapTool(BaseTool): ...@@ -898,15 +894,6 @@ class SlapTool(BaseTool):
sla_xml = etree.tostring(instance, pretty_print=True, sla_xml = etree.tostring(instance, pretty_print=True,
xml_declaration=True, encoding='utf-8') xml_declaration=True, encoding='utf-8')
portal = self.getPortalObject()
if compute_node_id and compute_partition_id:
# requested by Software Instance, there is already top part of tree
software_instance_document = self.\
_getSoftwareInstanceForComputePartition(compute_node_id,
compute_partition_id)
instance_tree = software_instance_document.getSpecialiseValue()
if instance_tree is not None and instance_tree.getSlapState() == "stop_requested":
state = 'stopped'
kw = dict(software_release=software_release, kw = dict(software_release=software_release,
software_type=software_type, software_type=software_type,
software_title=partition_reference, software_title=partition_reference,
...@@ -914,51 +901,43 @@ class SlapTool(BaseTool): ...@@ -914,51 +901,43 @@ class SlapTool(BaseTool):
shared=shared, shared=shared,
sla_xml=sla_xml, sla_xml=sla_xml,
state=state) state=state)
key = '_'.join([software_instance_document.getSpecialise(), partition_reference])
value = dict( portal = self.getPortalObject()
hash='_'.join([software_instance_document.getRelativeUrl(), str(kw)]), if compute_node_id and compute_partition_id:
) requester = self.\
last_data = software_instance_document.getLastData(key) _getSoftwareInstanceForComputePartition(compute_node_id,
requested_software_instance = None compute_partition_id)
if last_data is not None and isinstance(last_data, dict): instance_tree = requester.getSpecialiseValue()
requested_software_instance = portal.restrictedTraverse( if instance_tree is not None and instance_tree.getSlapState() == "stop_requested":
last_data.get('request_instance'), None) kw['state'] = 'stopped'
if last_data is None or not isinstance(last_data, type(value)) or \ key = '_'.join([instance_tree.getRelativeUrl(), partition_reference])
last_data.get('hash') != value['hash'] or \
requested_software_instance is None:
software_instance_document.requestInstance(**kw)
requested_software_instance = self.REQUEST.get('request_instance')
if requested_software_instance is not None:
value['request_instance'] = requested_software_instance\
.getRelativeUrl()
software_instance_document.setLastData(value, key=key)
else: else:
# requested as root, so done by human # requested as root, so done by human
person = portal.portal_membership.getAuthenticatedMember().getUserValue() requester = portal.portal_membership.getAuthenticatedMember().getUserValue()
kw = dict(software_release=software_release, key = '_'.join([requester.getRelativeUrl(), partition_reference])
software_type=software_type,
software_title=partition_reference, last_data = requester.getLastData(key)
shared=shared, requested_software_instance = None
instance_xml=instance_xml,
sla_xml=sla_xml,
state=state)
key = '_'.join([person.getRelativeUrl(), partition_reference])
value = dict( value = dict(
hash=str(kw) hash='_'.join([requester.getRelativeUrl(), str(kw)]),
) )
last_data = person.getLastData(key)
if last_data is not None and isinstance(last_data, dict): if last_data is not None and isinstance(last_data, type(value)):
requested_software_instance = portal.restrictedTraverse( requested_software_instance = self.restrictedTraverse(
last_data.get('request_instance'), None) last_data.get('request_instance'), None)
if last_data is None or not isinstance(last_data, type(value)) or \ if last_data is None or not isinstance(last_data, type(value)) or \
last_data.get('hash') != value['hash'] or \ last_data.get('hash') != value['hash'] or \
requested_software_instance is None: requested_software_instance is None:
person.requestSoftwareInstance(**kw) if compute_node_id and compute_partition_id:
requester.requestInstance(**kw)
else:
requester.requestSoftwareInstance(**kw)
requested_software_instance = self.REQUEST.get('request_instance') requested_software_instance = self.REQUEST.get('request_instance')
if requested_software_instance is not None: if requested_software_instance is not None:
value['request_instance'] = requested_software_instance\ value['request_instance'] = requested_software_instance\
.getRelativeUrl() .getRelativeUrl()
requested_software_instance.setLastData(value, key=key) requester.setLastData(value, key=key)
if requested_software_instance is None: if requested_software_instance is None:
raise SoftwareInstanceNotReady raise SoftwareInstanceNotReady
...@@ -966,24 +945,7 @@ class SlapTool(BaseTool): ...@@ -966,24 +945,7 @@ class SlapTool(BaseTool):
if not requested_software_instance.getAggregate(portal_type="Compute Partition"): if not requested_software_instance.getAggregate(portal_type="Compute Partition"):
raise SoftwareInstanceNotReady raise SoftwareInstanceNotReady
else: else:
parameter_dict = requested_software_instance._asParameterDict() return dumps(requested_software_instance._asSoftwareInstance())
# software instance has to define an xml parameter
xml = requested_software_instance._instanceXmlToDict(
parameter_dict.pop('xml'))
connection_xml = requested_software_instance._instanceXmlToDict(
parameter_dict.pop('connection_xml'))
filter_xml = requested_software_instance._instanceXmlToDict(
parameter_dict.pop('filter_xml'))
instance_guid = parameter_dict.pop('instance_guid')
software_instance = SoftwareInstance(**parameter_dict)
software_instance._parameter_dict = xml
software_instance._connection_dict = connection_xml
software_instance._filter_dict = filter_xml
software_instance._requested_state = state
software_instance._instance_guid = instance_guid
return dumps(software_instance)
@UnrestrictedMethod @UnrestrictedMethod
def _updateComputePartitionRelatedInstanceList(self, compute_node_id, def _updateComputePartitionRelatedInstanceList(self, compute_node_id,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment