Commit 96352c69 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_cloud: Introduce SlapOSCacheMixin

   This API replaces the usage of SlapTool code for handling data stored in memcached. It introduces an New API for it at the context of the objects to handle volatile data used on SlapOS context.
parent 1e74ed08
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2005-2010 Nexedi SA and Contributors. All Rights Reserved.
# Romain Courteaud <romain@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions
from DateTime import DateTime
from App.Common import rfc1123_date
from Products.ERP5Type.Cache import DEFAULT_CACHE_SCOPE
import json
class SlapOSCacheMixin:
# Declarative security
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation)
def _getSlapOSMemcacheDict(self):
""" Get the Memcache dict for SlapOS context
"""
return self.getPortalObject().portal_memcached.getMemcachedDict(
# Key prefix is required for backward compatibility
key_prefix='slap_tool',
plugin_path='portal_memcached/default_memcached_plugin')
def _getCachedAccessInfo(self):
memcached_dict = self._getSlapOSMemcacheDict()
if not self.getReference():
return None
try:
data = memcached_dict[self.getReference()]
except KeyError:
return None
return data
def getTextAccessStatus(self):
return self.getAccessStatus()['text']
def getAccessStatus(self):
data_dict = self._getCachedAccessInfo()
last_modified = rfc1123_date(DateTime())
if data_dict is None:
data_dict = {
"user": "SlapOS Master",
'created_at': '%s' % last_modified,
'since': '%s' % last_modified,
'state': "",
"text": "#error no data found for %s" % self.getReference()
}
# Prepare for xml marshalling
data_dict["user"] = data_dict["user"].decode("UTF-8")
data_dict["text"] = data_dict["text"].decode("UTF-8")
return data_dict
return json.loads(data_dict)
def setAccessStatus(self, text, state=""):
user_reference = self.getPortalObject().portal_membership.getAuthenticatedMember()\
.getUserName()
memcached_dict = self._getSlapOSMemcacheDict()
previous = self._getCachedAccessInfo()
created_at = rfc1123_date(DateTime())
since = created_at
status_changed = True
if previous is not None:
previous_json = json.loads(previous)
if text.split(" ")[0] == previous_json.get("text", "").split(" ")[0]:
since = previous_json.get("since",
previous_json.get("created_at", rfc1123_date(DateTime())))
status_changed = False
if state == "":
state = previous_json.get("state", "")
value = json.dumps({
'user': '%s' % user_reference,
'created_at': '%s' % created_at,
'text': '%s' % text,
'since': '%s' % since,
'state': state
})
memcached_dict[self.getReference()] = value
return status_changed
#####################
# SlapOS Last Data
#####################
def _getLastDataPlugin(self):
return self.getPortalObject().portal_caches\
.getRamCacheRoot().get('last_stored_data_cache_factory')\
.getCachePluginList()[0]
def setLastData(self, value, key=None):
# Key is used as suffix of reference, so
# each instance cannot modify others informations.
cache_key = self.getReference()
if key is not None:
cache_key += key
self._getLastDataPlugin().set(cache_key, DEFAULT_CACHE_SCOPE, value,
cache_duration=self.getPortalObject().portal_caches\
.getRamCacheRoot().get('last_stored_data_cache_factory').cache_duration)
def getLastData(self, key=None):
# Key is used as suffix of reference, so
# each instance cannot modify others informations.
cache_key = self.getReference()
if key is not None:
cache_key = key
try:
entry = self._getLastDataPlugin().get(cache_key, DEFAULT_CACHE_SCOPE)
except KeyError:
entry = None
else:
entry = entry.getValue()
return entry
\ No newline at end of file
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Mixin Component" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_recorded_property_dict</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>default_reference</string> </key>
<value> <string>SlapOSCacheMixin</string> </value>
</item>
<item>
<key> <string>default_source_reference</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>mixin.erp5.SlapOSCacheMixin</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Mixin Component</string> </value>
</item>
<item>
<key> <string>sid</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>text_content_error_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>version</string> </key>
<value> <string>erp5</string> </value>
</item>
<item>
<key> <string>workflow_history</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary/>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary>
<item>
<key> <string>component_validation_workflow</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAQ=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="4" aka="AAAAAAAAAAQ=">
<pickle>
<global name="WorkflowHistoryList" module="Products.ERP5Type.Workflow"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_log</string> </key>
<value>
<list>
<dictionary>
<item>
<key> <string>action</string> </key>
<value> <string>validate</string> </value>
</item>
<item>
<key> <string>validation_state</string> </key>
<value> <string>validated</string> </value>
</item>
</dictionary>
</list>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Base Type" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>content_icon</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>factory</string> </key>
<value> <string>addXMLObject</string> </value>
</item>
<item>
<key> <string>group_list</string> </key>
<value>
<tuple>
<string>item</string>
</tuple>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Compute Node</string> </value>
</item>
<item>
<key> <string>init_script</string> </key>
<value> <string>ComputeNode_init</string> </value>
</item>
<item>
<key> <string>permission</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Base Type</string> </value>
</item>
<item>
<key> <string>type_class</string> </key>
<value> <string>Computer</string> </value>
</item>
<item>
<key> <string>type_interface</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>type_mixin</string> </key>
<value>
<tuple>
<string>SlapOSCacheMixin</string>
</tuple>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Base Type" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_property_domain_dict</string> </key>
<value>
<dictionary>
<item>
<key> <string>short_title</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>title</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>acquire_local_roles</string> </key>
<value> <int>1</int> </value>
</item>
<item>
<key> <string>content_icon</string> </key>
<value> <string>document_icon.gif</string> </value>
</item>
<item>
<key> <string>content_meta_type</string> </key>
<value> <string>ERP5 Computer</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>factory</string> </key>
<value> <string>addComputerPartition</string> </value>
</item>
<item>
<key> <string>filter_content_types</string> </key>
<value> <int>1</int> </value>
</item>
<item>
<key> <string>group_list</string> </key>
<value>
<tuple>
<string>item</string>
</tuple>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Compute Partition</string> </value>
</item>
<item>
<key> <string>init_script</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>permission</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Compute Partition</string> </value>
</item>
<item>
<key> <string>type_class</string> </key>
<value> <string>ComputerPartition</string> </value>
</item>
<item>
<key> <string>type_interface</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>type_mixin</string> </key>
<value>
<tuple>
<string>SlapOSCacheMixin</string>
</tuple>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<tuple>
<global name="TranslationInformation" module="Products.ERP5Type.TranslationProviderBase"/>
<tuple/>
</tuple>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>domain_name</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>property_name</string> </key>
<value> <string>short_title</string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<tuple>
<global name="TranslationInformation" module="Products.ERP5Type.TranslationProviderBase"/>
<tuple/>
</tuple>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>domain_name</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>property_name</string> </key>
<value> <string>title</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Base Type" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_property_domain_dict</string> </key>
<value>
<dictionary>
<item>
<key> <string>first_name</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>last_name</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
<item>
<key> <string>middle_name</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAQ=</string> </persistent>
</value>
</item>
<item>
<key> <string>short_title</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAU=</string> </persistent>
</value>
</item>
<item>
<key> <string>title</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAY=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>acquire_local_roles</string> </key>
<value> <int>0</int> </value>
</item>
<item>
<key> <string>content_icon</string> </key>
<value> <string>person_icon.gif</string> </value>
</item>
<item>
<key> <string>content_meta_type</string> </key>
<value> <string>ERP5 Person</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value> <string>Persons capture the contact information (ex. name, phone number) and the relation between entities (ex. who is assigned to which function and which organisation). Persons are supported by a standard validation workflow.</string> </value>
</item>
<item>
<key> <string>factory</string> </key>
<value> <string>addPerson</string> </value>
</item>
<item>
<key> <string>filter_content_types</string> </key>
<value> <int>1</int> </value>
</item>
<item>
<key> <string>group_list</string> </key>
<value>
<tuple>
<string>node</string>
</tuple>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Person</string> </value>
</item>
<item>
<key> <string>init_script</string> </key>
<value> <string>Person_init</string> </value>
</item>
<item>
<key> <string>permission</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>type_class</string> </key>
<value> <string>Person</string> </value>
</item>
<item>
<key> <string>type_interface</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>type_mixin</string> </key>
<value>
<tuple>
<string>SlapOSCacheMixin</string>
</tuple>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="TranslationInformation" module="Products.ERP5Type.TranslationProviderBase"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>domain_name</string> </key>
<value> <string>content_translation</string> </value>
</item>
<item>
<key> <string>property_name</string> </key>
<value> <string>first_name</string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="TranslationInformation" module="Products.ERP5Type.TranslationProviderBase"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>domain_name</string> </key>
<value> <string>content_translation</string> </value>
</item>
<item>
<key> <string>property_name</string> </key>
<value> <string>last_name</string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="4" aka="AAAAAAAAAAQ=">
<pickle>
<global name="TranslationInformation" module="Products.ERP5Type.TranslationProviderBase"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>domain_name</string> </key>
<value> <string>content_translation</string> </value>
</item>
<item>
<key> <string>property_name</string> </key>
<value> <string>middle_name</string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="5" aka="AAAAAAAAAAU=">
<pickle>
<global name="TranslationInformation" module="Products.ERP5Type.TranslationProviderBase"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>domain_name</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>property_name</string> </key>
<value> <string>short_title</string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="6" aka="AAAAAAAAAAY=">
<pickle>
<global name="TranslationInformation" module="Products.ERP5Type.TranslationProviderBase"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>domain_name</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>property_name</string> </key>
<value> <string>title</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -107,7 +107,9 @@
<item>
<key> <string>type_mixin</string> </key>
<value>
<tuple/>
<tuple>
<string>SlapOSCacheMixin</string>
</tuple>
</value>
</item>
</dictionary>
......
......@@ -74,7 +74,9 @@
<item>
<key> <string>type_mixin</string> </key>
<value>
<tuple/>
<tuple>
<string>SlapOSCacheMixin</string>
</tuple>
</value>
</item>
</dictionary>
......
......@@ -105,7 +105,9 @@
<item>
<key> <string>type_mixin</string> </key>
<value>
<tuple/>
<tuple>
<string>SlapOSCacheMixin</string>
</tuple>
</value>
</item>
</dictionary>
......
mixin.erp5.SlapOSCacheMixin
\ No newline at end of file
Compute Node
Compute Partition
Hosting Subscription
Hosting Subscription Module
Instance Tree
Instance Tree Module
Person
Slave Instance
Software Installation
Software Installation Module
......
......@@ -79,7 +79,6 @@ except ImportError:
from zLOG import LOG, INFO
import StringIO
import pkg_resources
import json
from DateTime import DateTime
from App.Common import rfc1123_date
class SoftwareInstanceNotReady(Exception):
......@@ -119,6 +118,25 @@ def convertToREST(function):
wrapper.__doc__ = function.__doc__
return wrapper
def convertToStatus(function):
def wrapper(self, *args, **kwd):
data_dict = function(self, *args, **kwd)
last_modified = rfc1123_date(DateTime())
# Keep in cache server for 7 days
self.REQUEST.response.setStatus(200)
self.REQUEST.response.setHeader('Cache-Control',
'public, max-age=60, stale-if-error=604800')
self.REQUEST.response.setHeader('Vary',
'REMOTE_USER')
self.REQUEST.response.setHeader('Last-Modified', last_modified)
self.REQUEST.response.setHeader('Content-Type', 'text/xml; charset=utf-8')
self.REQUEST.response.setBody(dumps(data_dict))
return self.REQUEST.response
return wrapper
def _assertACI(document):
sm = getSecurityManager()
if sm.checkPermission(access_contents_information,
......@@ -216,25 +234,6 @@ class SlapTool(BaseTool):
# called on site
pass
def _storeLastData(self, key, value):
cache_plugin = self.getPortalObject().portal_caches\
.getRamCacheRoot().get('last_stored_data_cache_factory')\
.getCachePluginList()[0]
cache_plugin.set(key, DEFAULT_CACHE_SCOPE, value,
cache_duration=self.getPortalObject().portal_caches\
.getRamCacheRoot().get('last_stored_data_cache_factory').cache_duration)
def _getLastData(self, key):
cache_plugin = self.getPortalObject().portal_caches\
.getRamCacheRoot().get('last_stored_data_cache_factory')\
.getCachePluginList()[0]
try:
entry = cache_plugin.get(key, DEFAULT_CACHE_SCOPE)
except KeyError:
entry = None
else:
entry = entry.getValue()
return entry
def _activateFillComputeNodeInformationCache(self, compute_node_id, user):
tag = 'compute_node_information_cache_fill_%s_%s' % (compute_node_id, user)
......@@ -380,7 +379,8 @@ class SlapTool(BaseTool):
"""
user = self.getPortalObject().portal_membership.getAuthenticatedMember().getUserName()
if str(user) == computer_id:
self._logAccess(user, user, '#access %s' % computer_id)
compute_node = self.getPortalObject().portal_membership.getAuthenticatedMember().getUserValue()
compute_node.setAccessStatus('#access %s' % computer_id)
refresh_etag = self._calculateRefreshEtag()
body, etag = self._getComputeNodeInformation(computer_id, user, refresh_etag)
......@@ -448,6 +448,7 @@ class SlapTool(BaseTool):
security.declareProtected(Permissions.AccessContentsInformation,
'getComputerPartitionStatus')
@convertToStatus
def getComputerPartitionStatus(self, computer_id, computer_partition_id):
"""
Get the connection status of the partition
......@@ -459,10 +460,11 @@ class SlapTool(BaseTool):
except NotFound:
return self._getAccessStatus(None)
else:
return self._getAccessStatus(instance.getReference())
return instance.getAccessStatus()
security.declareProtected(Permissions.AccessContentsInformation,
'getComputerStatus')
@convertToStatus
def getComputerStatus(self, computer_id):
"""
Get the connection status of the partition
......@@ -472,10 +474,11 @@ class SlapTool(BaseTool):
validation_state="validated")[0].getObject()
# Be sure to prevent accessing information to disallowed users
compute_node = _assertACI(compute_node)
return self._getAccessStatus(computer_id)
return compute_node.getAccessStatus()
security.declareProtected(Permissions.AccessContentsInformation,
'getSoftwareInstallationStatus')
@convertToStatus
def getSoftwareInstallationStatus(self, url, computer_id):
"""
Get the connection status of the software installation
......@@ -492,7 +495,7 @@ class SlapTool(BaseTool):
except NotFound:
return self._getAccessStatus(None)
else:
return self._getAccessStatus(software_installation.getReference())
return software_installation.getAccessStatus()
security.declareProtected(Permissions.AccessContentsInformation,
'getSoftwareReleaseListFromSoftwareProduct')
......@@ -771,11 +774,9 @@ class SlapTool(BaseTool):
"""
Fire up bung on Compute Node
"""
user = self.getPortalObject().portal_membership.getAuthenticatedMember()\
.getUserName()
self._logAccess(user, compute_node_id, '#error bang')
return self._getComputeNodeDocument(compute_node_id).reportComputeNodeBang(
comment=message)
compute_node = self._getComputeNodeDocument(compute_node_id)
compute_node.setAccessStatus('#error bang')
return compute_node.reportComputeNodeBang(comment=message)
security.declareProtected(Permissions.AccessContentsInformation,
'computerBang')
......@@ -925,34 +926,6 @@ class SlapTool(BaseTool):
# Internal methods
####################################################
def _logAccess(self, user_reference, context_reference, text, state=""):
memcached_dict = self.Base_getSlapToolMemcachedDict()
previous = self._getCachedAccessInfo(context_reference)
created_at = rfc1123_date(DateTime())
since = created_at
status_changed = True
if previous is not None:
previous_json = json.loads(previous)
if text.split(" ")[0] == previous_json.get("text", "").split(" ")[0]:
since = previous_json.get("since",
previous_json.get("created_at", rfc1123_date(DateTime())))
status_changed = False
if state == "":
state = previous_json.get("state", "")
value = json.dumps({
'user': '%s' % user_reference,
'created_at': '%s' % created_at,
'text': '%s' % text,
'since': '%s' % since,
'state': state
})
memcached_dict[context_reference] = value
return status_changed
def _validateXML(self, to_be_validated, xsd_model):
"""Will validate the xml file"""
#We parse the XSD model
......@@ -1015,8 +988,7 @@ class SlapTool(BaseTool):
slap_partition._requested_state = 'stopped'
if state == "start_requested":
slap_partition._requested_state = 'started'
slap_partition._access_status = self._getTextAccessStatus(
software_instance.getReference())
slap_partition._access_status = software_instance.getTextAccessStatus()
slap_partition._software_release_document = SoftwareRelease(
software_release=software_instance.getUrlString().decode("UTF-8"),
......@@ -1061,11 +1033,9 @@ class SlapTool(BaseTool):
Log the software release status
"""
compute_node_document = self._getComputeNodeDocument(compute_node_id)
software_installation_reference = self._getSoftwareInstallationReference(url,
software_installation = self._getSoftwareInstallationForComputeNode(url,
compute_node_document)
user = self.getPortalObject().portal_membership.\
getAuthenticatedMember().getUserName()
self._logAccess(user, software_installation_reference,
software_installation.setAccessStatus(
'#building software release %s' % url, "building")
@convertToREST
......@@ -1074,12 +1044,10 @@ class SlapTool(BaseTool):
Log the software release status
"""
compute_node_document = self._getComputeNodeDocument(compute_node_id)
software_installation_reference = self._getSoftwareInstallationReference(url,
software_installation = self._getSoftwareInstallationForComputeNode(url,
compute_node_document)
user = self.getPortalObject().portal_membership.\
getAuthenticatedMember().getUserName()
self._logAccess(user, software_installation_reference,
'#access software release %s available' % url, "available")
software_installation.setAccessStatus(
'#access software release %s available' % url, "available")
@convertToREST
def _destroyedSoftwareRelease(self, url, compute_node_id):
......@@ -1108,10 +1076,9 @@ class SlapTool(BaseTool):
instance = self._getSoftwareInstanceForComputePartition(
compute_node_id,
compute_partition_id)
user = self.getPortalObject().portal_membership.getAuthenticatedMember()\
.getUserName()
status_changed = self._logAccess(user, instance.getReference(),
'#error while instanciating: %s' % error_log[-80:])
status_changed = instance.setAccessStatus(
'#error while instanciating: %s' % error_log[-80:])
if status_changed:
instance.reindexObject()
......@@ -1126,7 +1093,7 @@ class SlapTool(BaseTool):
for name in [software_instance.getTitle(), new_name]:
# reset request cache
key = '_'.join([hosting, name])
self._storeLastData(key, {})
software_instance.setLastData({}, key=key)
return software_instance.rename(new_name=new_name,
comment="Rename %s into %s" % (software_instance.title, new_name))
......@@ -1140,79 +1107,19 @@ class SlapTool(BaseTool):
software_instance = self._getSoftwareInstanceForComputePartition(
compute_node_id,
compute_partition_id)
user = self.getPortalObject().portal_membership.\
getAuthenticatedMember().getUserName()
self._logAccess(user, software_instance.getReference(),
'#error bang called')
software_instance.setAccessStatus('#error bang called')
timestamp = str(int(software_instance.getModificationDate()))
key = "%s_bangstamp" % software_instance.getReference()
self.getPortalObject().portal_workflow.getInfoFor(
software_instance, 'action', wf_id='instance_slap_interface_workflow')
if (self._getLastData(key) != timestamp):
if (software_instance.getLastData(key) != timestamp):
software_instance.bang(bang_tree=True, comment=message)
self._storeLastData(key, str(int(software_instance.getModificationDate())))
software_instance.setLastData(key, str(int(software_instance.getModificationDate())))
return "OK"
def _getCachedAccessInfo(self, context_reference):
memcached_dict = self.Base_getSlapToolMemcachedDict()
try:
if context_reference is None:
raise KeyError
else:
data = memcached_dict[context_reference]
except KeyError:
return None
return data
def _getTextAccessStatus(self, context_reference):
status_string = self._getCachedAccessInfo(context_reference)
access_status = "#error no data found!"
if status_string is not None:
try:
access_status = json.loads(status_string)['text']
except ValueError:
pass
return access_status
def _getAccessStatus(self, context_reference):
d = self._getCachedAccessInfo(context_reference)
last_modified = rfc1123_date(DateTime())
if d is None:
if context_reference is None:
d = {
"user": "SlapOS Master",
'created_at': '%s' % last_modified,
'since': '%s' % last_modified,
'state': "",
"text": "#error no data found"
}
else:
d = {
"user": "SlapOS Master",
'created_at': '%s' % last_modified,
'since': '%s' % last_modified,
'state': "",
"text": "#error no data found for %s" % context_reference
}
# Prepare for xml marshalling
d["user"] = d["user"].decode("UTF-8")
d["text"] = d["text"].decode("UTF-8")
else:
d = json.loads(d)
# Keep in cache server for 7 days
self.REQUEST.response.setStatus(200)
self.REQUEST.response.setHeader('Cache-Control',
'public, max-age=60, stale-if-error=604800')
self.REQUEST.response.setHeader('Vary',
'REMOTE_USER')
self.REQUEST.response.setHeader('Last-Modified', last_modified)
self.REQUEST.response.setHeader('Content-Type', 'text/xml; charset=utf-8')
self.REQUEST.response.setBody(dumps(d))
return self.REQUEST.response
@convertToREST
def _startedComputePartition(self, compute_node_id, compute_partition_id):
"""
......@@ -1221,10 +1128,7 @@ class SlapTool(BaseTool):
instance = self._getSoftwareInstanceForComputePartition(
compute_node_id,
compute_partition_id)
user = self.getPortalObject().portal_membership.getAuthenticatedMember()\
.getUserName()
status_changed = self._logAccess(user, instance.getReference(),
'#access Instance correctly started', "started")
status_changed = instance.setAccessStatus('#access Instance correctly started', "started")
if status_changed:
instance.reindexObject()
......@@ -1236,10 +1140,7 @@ class SlapTool(BaseTool):
instance = self._getSoftwareInstanceForComputePartition(
compute_node_id,
compute_partition_id)
user = self.getPortalObject().portal_membership.getAuthenticatedMember()\
.getUserName()
status_changed = self._logAccess(user, instance.getReference(),
'#access Instance correctly stopped', "stopped")
status_changed = instance.setAccessStatus('#access Instance correctly stopped', "stopped")
if status_changed:
instance.reindexObject()
......@@ -1288,12 +1189,11 @@ class SlapTool(BaseTool):
compute_partition_id,
slave_reference)
connection_xml = dict2xml(loads(connection_xml))
reference = software_instance.getReference()
if self._getLastData(reference) != connection_xml:
if software_instance.getLastData() != connection_xml:
software_instance.updateConnection(
connection_xml=connection_xml,
)
self._storeLastData(reference, connection_xml)
software_instance.setLastData(connection_xml)
@convertToREST
def _requestComputePartition(self, compute_node_id, compute_partition_id,
......@@ -1368,7 +1268,7 @@ class SlapTool(BaseTool):
value = dict(
hash='_'.join([software_instance_document.getRelativeUrl(), str(kw)]),
)
last_data = self._getLastData(key)
last_data = software_instance_document.getLastData(key)
requested_software_instance = None
if last_data is not None and isinstance(last_data, dict):
requested_software_instance = portal.restrictedTraverse(
......@@ -1381,7 +1281,7 @@ class SlapTool(BaseTool):
if requested_software_instance is not None:
value['request_instance'] = requested_software_instance\
.getRelativeUrl()
self._storeLastData(key, value)
software_instance_document.setLastData(value, key=key)
else:
# requested as root, so done by human
person = portal.portal_membership.getAuthenticatedMember().getUserValue()
......@@ -1396,7 +1296,7 @@ class SlapTool(BaseTool):
value = dict(
hash=str(kw)
)
last_data = self._getLastData(key)
last_data = person.getLastData(key)
if last_data is not None and isinstance(last_data, dict):
requested_software_instance = portal.restrictedTraverse(
last_data.get('request_instance'), None)
......@@ -1408,7 +1308,7 @@ class SlapTool(BaseTool):
if requested_software_instance is not None:
value['request_instance'] = requested_software_instance\
.getRelativeUrl()
self._storeLastData(key, value)
requested_software_instance.setLastData(value, key=key)
if requested_software_instance is None:
raise SoftwareInstanceNotReady
......@@ -1452,7 +1352,7 @@ class SlapTool(BaseTool):
compute_partition_id)
cache_reference = '%s-PREDLIST' % software_instance_document.getReference()
if self._getLastData(cache_reference) != instance_reference_xml:
if software_instance_document.getLastData(cache_reference) != instance_reference_xml:
instance_reference_list = loads(instance_reference_xml)
current_successor_list = software_instance_document.getSuccessorValueList(
......@@ -1470,7 +1370,7 @@ class SlapTool(BaseTool):
compute_node_id, compute_partition_id, successor_list), error=False)
software_instance_document.edit(successor_list=successor_list,
comment='successor_list edited to unlink non commited instances')
self._storeLastData(cache_reference, instance_reference_xml)
software_instance_document.setLastData(instance_reference_xml, key=cache_reference)
####################################################
# Internals methods
......@@ -1678,7 +1578,7 @@ class SlapTool(BaseTool):
else:
software_release_response._requested_state = 'available'
known_state = self._getTextAccessStatus(software_installation.getReference())
known_state = software_installation.getTextAccessStatus()
if known_state.startswith("#access"):
software_release_response._known_state = 'available'
elif known_state.startswith("#building"):
......@@ -1695,11 +1595,8 @@ class SlapTool(BaseTool):
Log the compute_node status
"""
compute_node_document = self._getComputeNodeDocument(compute_node_id)
software_installation_reference = self._getSoftwareInstallationReference(url,
software_installation = self._getSoftwareInstallationForComputeNode(url,
compute_node_document)
user = self.getPortalObject().portal_membership.\
getAuthenticatedMember().getUserName()
self._logAccess(user, software_installation_reference,
'#error while installing %s' % url)
software_installation.setAccessStatus('#error while installing %s' % url)
InitializeClass(SlapTool)
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