From f96f075528eddbc050a05d95bb3980893ca57a49 Mon Sep 17 00:00:00 2001
From: Rafael Monnerat <rafael@nexedi.com>
Date: Thu, 7 Apr 2016 15:32:45 +0000
Subject: [PATCH] slapos_ecoallocation: Add scripts for suggest better
 eco-friendly allocation

Those scripts are only usefull for resilient (kvm, webrunner) instances.
---
 ...ubscript_getEcoReallocationInformation.xml | 83 ++++++++++++++++++
 ...on_getMostEcoEfficientSoftwareInstance.xml | 85 +++++++++++++++++++
 2 files changed, 168 insertions(+)
 create mode 100644 master/bt5/slapos_ecoallocation/SkinTemplateItem/portal_skins/slapos_ecoallocation/HostingSubscript_getEcoReallocationInformation.xml
 create mode 100644 master/bt5/slapos_ecoallocation/SkinTemplateItem/portal_skins/slapos_ecoallocation/HostingSubscription_getMostEcoEfficientSoftwareInstance.xml

diff --git a/master/bt5/slapos_ecoallocation/SkinTemplateItem/portal_skins/slapos_ecoallocation/HostingSubscript_getEcoReallocationInformation.xml b/master/bt5/slapos_ecoallocation/SkinTemplateItem/portal_skins/slapos_ecoallocation/HostingSubscript_getEcoReallocationInformation.xml
new file mode 100644
index 000000000..bdb6eac36
--- /dev/null
+++ b/master/bt5/slapos_ecoallocation/SkinTemplateItem/portal_skins/slapos_ecoallocation/HostingSubscript_getEcoReallocationInformation.xml
@@ -0,0 +1,83 @@
+<?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>instance, delta_co2 = context.HostingSubscription_getMostEcoEfficientSoftwareInstance()\n
+\n
+if instance is None:\n
+  return None \n
+\n
+master_node = instance.SoftwareInstance_getResilientMasterNode()\n
+\n
+if master_node is None:\n
+  return None \n
+\n
+if instance.getRelativeUrl() != master_node.getRelativeUrl():\n
+  master_delta_co2 = master_node.SoftwareInstance_getFutureDeltaCO2()\n
+  saving_ratio = (master_delta_co2-delta_co2)/master_delta_co2\n
+  return "Improve Power efficiency in %s%% by using %s instance as Main Node. We recommend you to a take over." % (int(saving_ratio*100), instance.getTitle())\n
+\n
+\n
+return None\n
+</string> </value>
+        </item>
+        <item>
+            <key> <string>_params</string> </key>
+            <value> <string></string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>HostingSubscript_getEcoReallocationInformation</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/master/bt5/slapos_ecoallocation/SkinTemplateItem/portal_skins/slapos_ecoallocation/HostingSubscription_getMostEcoEfficientSoftwareInstance.xml b/master/bt5/slapos_ecoallocation/SkinTemplateItem/portal_skins/slapos_ecoallocation/HostingSubscription_getMostEcoEfficientSoftwareInstance.xml
new file mode 100644
index 000000000..563b6b9e7
--- /dev/null
+++ b/master/bt5/slapos_ecoallocation/SkinTemplateItem/portal_skins/slapos_ecoallocation/HostingSubscription_getMostEcoEfficientSoftwareInstance.xml
@@ -0,0 +1,85 @@
+<?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[
+
+partition_co2_dict = {}\n
+min_delta_co2 = 2000\n
+minimal_candidate = None\n
+\n
+for software_instance in context.getSpecialiseRelatedValueList(portal_type="Software Instance"):\n
+  delta_co2 = software_instance.SoftwareInstance_getFutureDeltaCO2()     \n
+  if delta_co2 != "Not Applicable":\n
+    if delta_co2 < min_delta_co2:\n
+      minimal_candidate = software_instance\n
+      min_delta_co2 = delta_co2\n
+    elif (delta_co2 == min_delta_co2) and \\\n
+           (software_instance.getTitle() in ["kvm0", "runner0"]):\n
+      minimal_candidate = software_instance\n
+\n
+return minimal_candidate, min_delta_co2\n
+
+
+]]></string> </value>
+        </item>
+        <item>
+            <key> <string>_params</string> </key>
+            <value> <string></string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>HostingSubscription_getMostEcoEfficientSoftwareInstance</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
-- 
2.30.9