Commit 41e5b43f authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_cloud: Normalize Access Status suffix

  Create specific API for status suffixes, since this is a widely used by monitoring system
parent 10d32071
......@@ -35,6 +35,10 @@ from Products.ERP5Type.Cache import DEFAULT_CACHE_SCOPE
import json
ACCESS = "#access"
ERROR = "#error"
BUILDING = "#building"
class SlapOSCacheMixin:
# Declarative security
......@@ -93,7 +97,16 @@ class SlapOSCacheMixin:
return data_dict
def setAccessStatus(self, text, state=""):
def setAccessStatus(self, text, state="", reindex=0):
self._setAccessStatus("%s %s" % (ACCESS, text), state, reindex)
def setErrorStatus(self, text, state="", reindex=0):
self._setAccessStatus("%s %s" % (ERROR, text), state, reindex)
def setBuildingStatus(self, text, state="", reindex=0):
self._setAccessStatus("%s %s" % (BUILDING, text), state, reindex)
def _setAccessStatus(self, text, state="", reindex=0):
user_reference = self.getPortalObject().portal_membership.getAuthenticatedMember()\
.getUserName()
......@@ -121,6 +134,9 @@ class SlapOSCacheMixin:
cache_duration = self._getAccessStatusCacheFactory().cache_duration
self._getAccessStatusPlugin().set(self._getAccessStatusCacheKey(),
DEFAULT_CACHE_SCOPE, value, cache_duration=cache_duration)
if status_changed and reindex:
self.reindexObject()
return status_changed
def getTextAccessStatus(self):
......@@ -134,7 +150,6 @@ class SlapOSCacheMixin:
date = DateTime(data_dict['created_at'])
return date.strftime('%Y/%m/%d %H:%M')
#####################
# SlapOS Last Data
#####################
......
......@@ -814,7 +814,7 @@ class TestComputeNode_hasContactedRecently(SlapOSTestCaseMixinWithAbort):
@simulate('ComputeNode_getCreationDate', '*args, **kwargs','return DateTime() - 32')
def test_ComputeNode_hasContactedRecently_memcached(self):
compute_node = self._makeComputeNode()[0]
compute_node.setAccessStatus("#access ")
compute_node.setAccessStatus("")
self.tic()
compute_node.getCreationDate = self.portal.ComputeNode_getCreationDate
......@@ -827,7 +827,7 @@ class TestComputeNode_hasContactedRecently(SlapOSTestCaseMixinWithAbort):
compute_node = self._makeComputeNode()[0]
try:
self.pinDateTime(DateTime()-32)
compute_node.setAccessStatus("#access ")
compute_node.setAccessStatus("")
finally:
self.unpinDateTime()
......@@ -843,7 +843,7 @@ class TestComputeNode_hasContactedRecently(SlapOSTestCaseMixinWithAbort):
compute_node = self._makeComputeNode()[0]
try:
self.pinDateTime(DateTime()-32)
compute_node.setAccessStatus("#access ")
compute_node.setAccessStatus("")
finally:
self.unpinDateTime()
......@@ -1008,7 +1008,7 @@ class TestSlapOSComputeNode_CheckState(TestCRMSkinsMixin):
try:
d = DateTime() - 1.1
self.pinDateTime(d)
compute_node.setAccessStatus("#access ")
compute_node.setAccessStatus("")
finally:
self.unpinDateTime()
......@@ -1045,7 +1045,7 @@ class TestSlapOSComputeNode_CheckState(TestCRMSkinsMixin):
try:
self.pinDateTime(DateTime()-1.1)
compute_node.setAccessStatus("#access ")
compute_node.setAccessStatus("")
finally:
self.unpinDateTime()
......@@ -1275,7 +1275,7 @@ class TestSlapOSHasError(SlapOSTestCaseMixin):
self._makeComputeNode()
self._makeComputePartitionList()
instance.setAccessStatus("#error ")
instance.setErrorStatus("")
self.assertEqual(instance.SoftwareInstance_hasReportedError(), None)
......@@ -1283,7 +1283,7 @@ class TestSlapOSHasError(SlapOSTestCaseMixin):
self.assertEqual(str(instance.SoftwareInstance_hasReportedError()), '#error ')
instance.setAccessStatus("#access ")
instance.setAccessStatus("")
self.assertEqual(instance.SoftwareInstance_hasReportedError(), None)
def test_SoftwareInstallation_hasReportedError(self):
......@@ -1298,14 +1298,14 @@ class TestSlapOSHasError(SlapOSTestCaseMixin):
error_date = DateTime()
try:
self.pinDateTime(error_date)
installation.setAccessStatus("#error ")
installation.setErrorStatus("")
finally:
self.unpinDateTime()
self.assertEqual(
rfc1123_date(installation.SoftwareInstallation_hasReportedError()),
rfc1123_date(error_date))
installation.setAccessStatus("#building ")
installation.setBuildingStatus("")
self.assertEqual(installation.SoftwareInstallation_hasReportedError(), None)
......@@ -1352,7 +1352,7 @@ class TestSlapOSHasError(SlapOSTestCaseMixin):
"slapos-crm-instance-tree-instance-state.notification"),
instance_tree.InstanceTree_checkSoftwareInstanceState())
instance.setAccessStatus("#access ")
instance.setAccessStatus("")
self.assertEqual(None,
instance_tree.InstanceTree_checkSoftwareInstanceState())
......@@ -1455,7 +1455,7 @@ class TestSlapOSHasError(SlapOSTestCaseMixin):
instance.requestInstance(**kw)
self.tic()
instance.setAccessStatus("#access ")
instance.setAccessStatus("")
self.assertEqual(
'Visited by InstanceTree_createSupportRequestEvent %s %s' % \
......@@ -1490,7 +1490,7 @@ class TestSlapOSHasError(SlapOSTestCaseMixin):
self._makeComputePartitionList()
instance.setAggregateValue(self.compute_node.partition1)
instance.setAccessStatus("#error ")
instance.setErrorStatus("")
self.assertEqual(
None,
instance_tree.InstanceTree_checkSoftwareInstanceState())
......
......@@ -171,7 +171,7 @@ class SlapTool(BaseTool):
user = self.getPortalObject().portal_membership.getAuthenticatedMember().getUserName()
if str(user) == computer_id:
compute_node = self.getPortalObject().portal_membership.getAuthenticatedMember().getUserValue()
compute_node.setAccessStatus('#access %s' % computer_id)
compute_node.setAccessStatus(computer_id)
else:
compute_node = self._getComputeNodeDocument(computer_id)
......@@ -609,7 +609,7 @@ class SlapTool(BaseTool):
Fire up bung on Compute Node
"""
compute_node = self._getComputeNodeDocument(compute_node_id)
compute_node.setAccessStatus('#error bang')
compute_node.setErrorStatus('bang')
return compute_node.reportComputeNodeBang(comment=message)
security.declareProtected(Permissions.AccessContentsInformation,
......@@ -807,8 +807,8 @@ class SlapTool(BaseTool):
compute_node_document = self._getComputeNodeDocument(compute_node_id)
software_installation = self._getSoftwareInstallationForComputeNode(url,
compute_node_document)
software_installation.setAccessStatus(
'#building software release %s' % url, "building")
software_installation.setBuildingStatus(
'software release %s' % url, "building")
@convertToREST
def _availableSoftwareRelease(self, url, compute_node_id):
......@@ -819,7 +819,7 @@ class SlapTool(BaseTool):
software_installation = self._getSoftwareInstallationForComputeNode(url,
compute_node_document)
software_installation.setAccessStatus(
'#access software release %s available' % url, "available")
'software release %s available' % url, "available")
@convertToREST
def _destroyedSoftwareRelease(self, url, compute_node_id):
......@@ -836,24 +836,25 @@ class SlapTool(BaseTool):
software_installation.invalidate(
comment="Software Release destroyed report.")
####
@convertToREST
def _softwareInstanceError(self, compute_node_id,
compute_partition_id, error_log=""):
"""
Add an error for the software Instance Workflow
"""
if error_log is None:
error_log = ""
instance = self._getSoftwareInstanceForComputePartition(
compute_node_id,
compute_partition_id)
status_changed = instance.setAccessStatus(
'#error while instanciating: %s' % error_log[-80:])
if error_log is None:
error_log = ""
instance.setErrorStatus(
'while instanciating: %s' % error_log[-80:], reindex=1)
if status_changed:
instance.reindexObject()
#####
@convertToREST
def _softwareInstanceRename(self, new_name, compute_node_id,
......@@ -876,7 +877,7 @@ class SlapTool(BaseTool):
compute_node_id,
compute_partition_id)
software_instance.setAccessStatus('#error bang called')
software_instance.setErrorStatus('bang called')
timestamp = str(int(software_instance.getModificationDate()))
key = "%s_bangstamp" % software_instance.getReference()
......@@ -896,9 +897,9 @@ class SlapTool(BaseTool):
instance = self._getSoftwareInstanceForComputePartition(
compute_node_id,
compute_partition_id)
status_changed = instance.setAccessStatus('#access Instance correctly started', "started")
if status_changed:
instance.reindexObject()
instance.setAccessStatus(
'Instance correctly started', "started", reindex=1)
@convertToREST
def _stoppedComputePartition(self, compute_node_id, compute_partition_id):
......@@ -908,9 +909,8 @@ class SlapTool(BaseTool):
instance = self._getSoftwareInstanceForComputePartition(
compute_node_id,
compute_partition_id)
status_changed = instance.setAccessStatus('#access Instance correctly stopped', "stopped")
if status_changed:
instance.reindexObject()
instance.setAccessStatus(
'Instance correctly stopped', "stopped", reindex=1)
@convertToREST
def _destroyedComputePartition(self, compute_node_id, compute_partition_id):
......@@ -1257,6 +1257,6 @@ class SlapTool(BaseTool):
compute_node_document = self._getComputeNodeDocument(compute_node_id)
software_installation = self._getSoftwareInstallationForComputeNode(url,
compute_node_document)
software_installation.setAccessStatus('#error while installing %s' % url)
software_installation.setErrorStatus('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