From 569439d51596ff2d04792439131d161133ce27ec Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Sun, 22 Mar 2020 01:10:25 +0100
Subject: [PATCH] resilient: fix notifier.callback parts installing the same
 path

---
 setup.py                                      |  2 +-
 slapos/recipe/librecipe/generic.py            | 16 ----------------
 slapos/recipe/notifier.py                     | 14 +++++---------
 software/slaprunner/buildout.hash.cfg         |  2 +-
 .../slaprunner/instance-runner-import.cfg.in  |  2 --
 stack/resilient/README.rst                    |  2 +-
 stack/resilient/buildout.hash.cfg             |  2 +-
 stack/resilient/pbsready-import.cfg.in        | 19 ++++++++-----------
 stack/slapos.cfg                              |  2 +-
 9 files changed, 18 insertions(+), 43 deletions(-)

diff --git a/setup.py b/setup.py
index 9c7a4eb86..b36ec5cca 100755
--- a/setup.py
+++ b/setup.py
@@ -28,7 +28,7 @@ from setuptools import setup, find_packages
 import glob
 import os
 
-version = '1.0.142'
+version = '1.0.143'
 name = 'slapos.cookbook'
 long_description = open("README.rst").read()
 
diff --git a/slapos/recipe/librecipe/generic.py b/slapos/recipe/librecipe/generic.py
index 6a61f630f..181bc80a8 100644
--- a/slapos/recipe/librecipe/generic.py
+++ b/slapos/recipe/librecipe/generic.py
@@ -27,7 +27,6 @@
 #
 ##############################################################################
 import errno
-import io
 import logging
 import os
 import sys
@@ -124,21 +123,6 @@ class GenericBaseRecipe(object):
   def createExecutable(self, name, content, mode=0o700):
     return self.createFile(name, content, mode)
 
-  def addLineToFile(self, filepath, line, encoding='utf8'):
-    """Append a single line to a text file, if the line does not exist yet.
-
-    line must be unicode."""
-
-    if os.path.exists(filepath):
-      lines = [l.rstrip('\n') for l in io.open(filepath, 'r', encoding=encoding)]
-    else:
-      lines = []
-
-    if not line in lines:
-      lines.append(line)
-      with io.open(filepath, 'w+', encoding=encoding) as f:
-        f.write(u'\n'.join(lines))
-
   def createPythonScript(self, name, absolute_function, args=(), kw={}):
     """Create a python script using zc.buildout.easy_install.scripts
 
diff --git a/slapos/recipe/notifier.py b/slapos/recipe/notifier.py
index 1ef5fa135..cc576906e 100644
--- a/slapos/recipe/notifier.py
+++ b/slapos/recipe/notifier.py
@@ -46,20 +46,16 @@ class Recipe(GenericBaseRecipe):
 
 class Callback(GenericBaseRecipe):
 
-  def createCallback(self, notification_id, callback):
+  def install(self):
+    options = self.options
+    notification_id = options['on-notification-id']
     # XXX: hashing the name here and in
     # slapos.toolbox/slapos/pubsub/__init__.py is completely messed up and
     # prevent any debug.
     callback_id = sha512(str2bytes(notification_id)).hexdigest()
 
-    filepath = os.path.join(self.options['callbacks'], callback_id)
-    self.addLineToFile(filepath, callback)
-    return filepath
-
-  def install(self):
-    # XXX this path is returned multiple times, one for each callback that has been added.
-    return [self.createCallback(self.options['on-notification-id'],
-                                self.options['callback'])]
+    return self.createFile(os.path.join(options['directory'], callback_id),
+                           options['callbacks'])
 
 class Notify(GenericBaseRecipe):
 
diff --git a/software/slaprunner/buildout.hash.cfg b/software/slaprunner/buildout.hash.cfg
index 8653f4d83..f321e730b 100644
--- a/software/slaprunner/buildout.hash.cfg
+++ b/software/slaprunner/buildout.hash.cfg
@@ -26,7 +26,7 @@ md5sum = 2852689640b632fc713d7a603ce66207
 
 [instance-runner-import]
 filename = instance-runner-import.cfg.in
-md5sum = 9a3adacb4a833b1af3715e7097d31aad
+md5sum = a582be15f0fb797fc75cfc39cd1d12ab
 
 [instance-runner-export]
 filename = instance-runner-export.cfg.in
diff --git a/software/slaprunner/instance-runner-import.cfg.in b/software/slaprunner/instance-runner-import.cfg.in
index ece018e5c..00d4927df 100644
--- a/software/slaprunner/instance-runner-import.cfg.in
+++ b/software/slaprunner/instance-runner-import.cfg.in
@@ -29,8 +29,6 @@ parts +=
   software-release-deployment-promise
 
   resilient-software-release-information
-# have to repeat the next one, as it's not inherited from pbsready-import
-  import-on-notification
 
   monitor-base
 
diff --git a/stack/resilient/README.rst b/stack/resilient/README.rst
index 491ba2168..78c4d2564 100644
--- a/stack/resilient/README.rst
+++ b/stack/resilient/README.rst
@@ -94,7 +94,7 @@ extends = ${instance-mysoftware:output}
 
 parts +=
     mysoftware
-    import-on-notification
+    notify-callback
 
 [importer]
 recipe = YourImportRecipe
diff --git a/stack/resilient/buildout.hash.cfg b/stack/resilient/buildout.hash.cfg
index be4cf771a..bca9dd12a 100644
--- a/stack/resilient/buildout.hash.cfg
+++ b/stack/resilient/buildout.hash.cfg
@@ -18,7 +18,7 @@ md5sum = 66331047b7dbf2513c5726d5d1647320
 
 [pbsready-import]
 filename = pbsready-import.cfg.in
-md5sum = d813c43ed00eff868fb13bc75b045336
+md5sum = 6f32cede10cf0d6430c017f49fe5aa30
 
 [pbsready-export]
 filename = pbsready-export.cfg.in
diff --git a/stack/resilient/pbsready-import.cfg.in b/stack/resilient/pbsready-import.cfg.in
index b608333af..04e687e52 100644
--- a/stack/resilient/pbsready-import.cfg.in
+++ b/stack/resilient/pbsready-import.cfg.in
@@ -7,8 +7,7 @@ parts +=
   resilient-web-takeover-httpd-wrapper
   resilient-web-takeover-httpd-promise
 
-  check-backup-integrity-on-notification
-  import-on-notification
+  notify-callback
   backup-checksum-integrity-promise
   resilient-publish-connection-parameter
 
@@ -39,13 +38,17 @@ minimum = 65516
 maximum = 65525
 ip = $${notifier:host}
 
-[import-on-notification]
+[notify-callback]
 # notifier.callback runs a script when a notification (sent by a parent PBS)
 # is received
-<= notifier
 recipe = slapos.cookbook:notifier.callback
+directory = $${notifier:callbacks}
 on-notification-id = $${slap-parameter:on-notification}
-callback = $${importer:wrapper}
+callbacks =
+# import on notification
+  $${importer:wrapper}
+# check backup integrity on notification
+  $${post-notification-run:output}
 
 [post-notification-run]
 recipe = collective.recipe.template
@@ -59,12 +62,6 @@ input = inline:
 output = $${rootdirectory:bin}/post-notification-run
 mode = 0700
 
-[check-backup-integrity-on-notification]
-<= notifier
-recipe = slapos.cookbook:notifier.callback
-on-notification-id = $${slap-parameter:on-notification}
-callback = $${post-notification-run:output}
-
 [backup-checksum-integrity-promise-bin]
 recipe = slapos.recipe.template:jinja2
 template = inline:
diff --git a/stack/slapos.cfg b/stack/slapos.cfg
index cfd6fc7f0..018fb5bc8 100644
--- a/stack/slapos.cfg
+++ b/stack/slapos.cfg
@@ -137,7 +137,7 @@ pyparsing = 2.2.0
 pytz = 2016.10
 requests = 2.13.0
 six = 1.12.0
-slapos.cookbook = 1.0.142
+slapos.cookbook = 1.0.143
 slapos.core = 1.5.9
 slapos.extension.strip = 0.4
 slapos.extension.shared = 1.0
-- 
2.30.9