<?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>_body</string> </key>
            <value> <string encoding="cdata"><![CDATA[

import json\n
computer = context\n
portal = context.getPortalObject()\n
\n
if computer.getAllocationScope() != \'open/public\':\n
  # Don\'t update non public computer\n
  return\n
\n
can_allocate = True\n
comment = \'\'\n
\n
# First and simple way to see if computer is dead\n
# modification_date = portal.portal_workflow.getInfoFor(computer, \'time\', wf_id=\'edit_workflow\')\n
# if (DateTime() - modification_date) > 1:\n
#   # Computer didn\'t talk to vifib for 1 days, do not consider it as a trustable public server for now\n
#   # slapformat is supposed to run at least once per day\n
#   can_allocate = False\n
#   comment = "Computer didn\'t contact the server for more than one day"\n
\n
if can_allocate:\n
  # Check if computer has error reported\n
  memcached_dict = portal.portal_memcached.getMemcachedDict(\n
    key_prefix=\'slap_tool\',\n
    plugin_path=\'portal_memcached/default_memcached_plugin\')\n
  try:\n
    d = memcached_dict[computer.getReference()]\n
  except KeyError:\n
    can_allocate = False\n
    comment = "Computer didn\'t contact the server"\n
  else:\n
    log_dict = json.loads(d)\n
    if \'#error\' in log_dict.get(\'text\', \'#error\'):\n
      can_allocate = False\n
      comment = \'Computer reported an error\'\n
    # XXX TODO: compare creation date of #ok message\n
    elif int(DateTime()) - int(DateTime(log_dict.get(\'created_at\'))) > 600:\n
      can_allocate = False\n
      comment = "Computer didn\'t contact for more than 10 minutes"\n
\n
if can_allocate:\n
  # Check the computer capacity.\n
  # there is a arbitrary hardcoded default value: not more than 1000000 (!) instances on\n
  # a computer\n
  computer_capacity_quantity = computer.getCapacityQuantity(1000000)\n
  software_release_capacity_dict = {}\n
  consumed_capacity = 0\n
\n
  for instance in portal.portal_catalog.portal_catalog(\n
      default_aggregate_relative_url=\'%s/%%\' % computer.getRelativeUrl(),\n
      portal_type=[\'Software Instance\', \'Slave Instance\'],\n
      validation_state=\'validated\'):\n
\n
    instance = instance.getObject()\n
\n
    software_release_url = instance.getUrlString()\n
    if software_release_url in software_release_capacity_dict:\n
      software_release_capacity = software_release_capacity_dict[software_release_url]\n
    else:\n
      software_release = portal.portal_catalog.getResultValue(\n
          portal_type=\'Software Release\',\n
          url_string={\'query\': software_release_url, \'key\': \'ExactMatch\'})\n
      if software_release is not None:\n
        software_release_capacity = software_release.getCapacityQuantity(1)\n
      else:\n
        software_release_capacity = 1\n
      software_release_capacity_dict[software_release_url] = software_release_capacity\n
    consumed_capacity += software_release_capacity\n
    if consumed_capacity >= computer_capacity_quantity:\n
      can_allocate = False\n
      comment = \'Computer capacity limit exceeded\'\n
      break\n
\n
# if can_allocate:\n
#   result_list = portal.portal_catalog.portal_catalog(\n
#     parent_uid=computer.getUid(),\n
#     portal_type=\'Computer Partition\',\n
#     free_for_request=1,\n
#     limit=1)\n
#   if len(result_list) == 0:\n
#     can_allocate = False\n
#     comment = \'No free partition left\'\n
\n
new_value = None\n
if can_allocate:\n
  if computer.getCapacityScope() == \'close\':\n
    new_value = \'open\'\n
else:\n
  if computer.getCapacityScope() == \'open\':\n
    new_value = \'close\'\n
\n
if new_value is not None:\n
  computer.edit(capacity_scope=new_value)\n
  if comment:\n
    portal.portal_workflow.doActionFor(computer, \'edit_action\', comment=comment)\n


]]></string> </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string></string> </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>Computer_checkAndUpdateCapacityScope</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
