Commit 65e3c3f1 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_cloud: Use script to clean up code duplication arround memcache calls

Introduce this code in order to simplify the code and introduce some kind of reusable API.
parent d679bce2
import json
from DateTime import DateTime
memcached_dict = context.Base_getSlapToolMemcachedDict()
try:
d = memcached_dict[document.getReference()]
except (KeyError, TypeError):
d = {
"user": "SlapOS Master",
"text": "#error no data found for %s" % document.getReference(),
"no_data": 1
}
else:
d = json.loads(d)
last_contact = DateTime(d.get('created_at'))
d["no_data_since_15_minutes"] = 0
d["no_data_since_5_minutes"] = 0
if (DateTime() - last_contact) > 0.005:
d["no_data_since_15_minutes"] = 1
d["no_data_since_5_minutes"] = 1
elif (DateTime() - last_contact) > 0.0025:
d["no_data_since_5_minutes"] = 1
return d
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>document</string> </value>
</item>
<item>
<key> <string>_proxy_roles</string> </key>
<value>
<tuple>
<string>Manager</string>
</tuple>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_getNewsDict</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
from zExceptions import Unauthorized
if REQUEST is not None:
raise Unauthorized
return context.getPortalObject().portal_memcached.getMemcachedDict(
key_prefix='slap_tool',
plugin_path='portal_memcached/default_memcached_plugin')
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>REQUEST=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_getSlapToolMemcacheDict</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
from DateTime import DateTime from DateTime import DateTime
portal = context.getPortalObject()
import json import json
partition = context partition = context
memcached_dict = context.getPortalObject().portal_memcached.getMemcachedDict( memcached_dict = context.Base_getSlapToolMemcacheDict()
key_prefix='slap_tool',
plugin_path='portal_memcached/default_memcached_plugin')
result = "" result = ""
date = None date = None
......
...@@ -22,9 +22,7 @@ comment = '' ...@@ -22,9 +22,7 @@ comment = ''
if can_allocate: if can_allocate:
# Check if computer has error reported # Check if computer has error reported
memcached_dict = portal.portal_memcached.getMemcachedDict( memcached_dict = context.Base_getSlapToolMemcacheDict()
key_prefix='slap_tool',
plugin_path='portal_memcached/default_memcached_plugin')
try: try:
d = memcached_dict[computer.getReference()] d = memcached_dict[computer.getReference()]
except KeyError: except KeyError:
......
from DateTime import DateTime from DateTime import DateTime
import json import json
memcached_dict = context.getPortalObject().portal_memcached.getMemcachedDict( memcached_dict = context.Base_getSlapToolMemcacheDict()
key_prefix='slap_tool',
plugin_path='portal_memcached/default_memcached_plugin')
try: try:
d = memcached_dict[context.getReference()] d = memcached_dict[context.getReference()]
except KeyError: except KeyError:
......
...@@ -5,9 +5,7 @@ has_partition = context.getAggregate(portal_type="Computer Partition") ...@@ -5,9 +5,7 @@ has_partition = context.getAggregate(portal_type="Computer Partition")
result = 'Unable to calculate the status...' result = 'Unable to calculate the status...'
if has_partition: if has_partition:
try: try:
memcached_dict = context.getPortalObject().portal_memcached.getMemcachedDict( memcached_dict = context.Base_getSlapToolMemcacheDict()
key_prefix='slap_tool',
plugin_path='portal_memcached/default_memcached_plugin')
try: try:
d = memcached_dict[context.getReference()] d = memcached_dict[context.getReference()]
except KeyError: except KeyError:
......
...@@ -2,10 +2,6 @@ from DateTime import DateTime ...@@ -2,10 +2,6 @@ from DateTime import DateTime
portal = context.getPortalObject() portal = context.getPortalObject()
import json import json
memcached_dict = context.getPortalObject().portal_memcached.getMemcachedDict(
key_prefix='slap_tool',
plugin_path='portal_memcached/default_memcached_plugin')
error_style = 'background-color: red; display: block; height: 2em; width: 2em; float: left; margin: 5px;' error_style = 'background-color: red; display: block; height: 2em; width: 2em; float: left; margin: 5px;'
access_style = 'background-color: green; display: block; height: 2em; width: 2em; float: left; margin: 5px;' access_style = 'background-color: green; display: block; height: 2em; width: 2em; float: left; margin: 5px;'
...@@ -18,6 +14,7 @@ software_installation = portal.portal_catalog.getResultValue( ...@@ -18,6 +14,7 @@ software_installation = portal.portal_catalog.getResultValue(
if not software_installation or software_installation.getSlapState() == "destroy_requested": if not software_installation or software_installation.getSlapState() == "destroy_requested":
return '<span" style="%s" title="Information not available"></a>' % error_style return '<span" style="%s" title="Information not available"></a>' % error_style
memcached_dict = context.Base_getSlapToolMemcacheDict()
try: try:
d = memcached_dict[software_installation.getReference()] d = memcached_dict[software_installation.getReference()]
except KeyError: except KeyError:
......
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