From 033eb157b69593e6a86fb27a8ee9a577bac97a6f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Le=20Ninivin?= <cedric.leninivin@tiolive.com>
Date: Thu, 30 May 2013 16:14:55 +0200
Subject: [PATCH] Introduce basic resiliency for slaprunner

---
 software/slaprunner/common.cfg                | 35 +++++++++++++++++--
 .../slaprunner/instance-resilient.cfg.jinja2  | 28 +++++++++++++++
 .../slaprunner/instance-runner-export.cfg.in  | 24 +++++++++++++
 .../slaprunner/instance-runner-import.cfg.in  | 16 +++++++++
 software/slaprunner/instance.cfg              |  9 ++++-
 software/slaprunner/software.cfg              |  4 +--
 6 files changed, 111 insertions(+), 5 deletions(-)
 create mode 100644 software/slaprunner/instance-resilient.cfg.jinja2
 create mode 100644 software/slaprunner/instance-runner-export.cfg.in
 create mode 100644 software/slaprunner/instance-runner-import.cfg.in

diff --git a/software/slaprunner/common.cfg b/software/slaprunner/common.cfg
index 98f094d60..604a10fb4 100644
--- a/software/slaprunner/common.cfg
+++ b/software/slaprunner/common.cfg
@@ -5,8 +5,10 @@ extends =
   ../../component/dropbear/buildout.cfg
   ../../component/git/buildout.cfg
   ../../component/lxml-python/buildout.cfg
+  ../../component/rsync/buildout.cfg
   ../../stack/flask.cfg
   ../../stack/shacache-client.cfg
+  ../../stack/resilient/buildout.cfg
   ../../stack/slapos.cfg
 
 parts =
@@ -14,18 +16,47 @@ parts =
   slapos-cookbook
   eggs
 
+  instance-runner-import
+  instance-runner-export
+
 [template]
 recipe = slapos.recipe.template
 url = ${:_profile_base_location_}/instance.cfg
 output = ${buildout:directory}/template.cfg
 mode = 0644
-md5sum = 5307e4200f044ae57b504ad68444491c
+#md5sum = 5307e4200f044ae57b504ad68444491c
 
 [template-runner]
 recipe = slapos.recipe.template
 url = ${:_profile_base_location_}/instance-runner.cfg
 output = ${buildout:directory}/template-runner.cfg
-md5sum = 91d6550c43b7a43a999724af4650ae40
+#md5sum = 91d6550c43b7a43a999724af4650ae40
+mode = 0644
+
+[instance-resilient]
+recipe = slapos.recipe.template:jinja2
+template = ${:_profile_base_location_}/instance-resilient.cfg.jinja2
+rendered = ${buildout:directory}/instance-resilient.cfg
+
+context = key buildout buildout:bin-directory
+          key develop_eggs_directory buildout:develop-eggs-directory
+          key eggs_directory buildout:eggs-directory
+
+import-list = file parts template-parts:destination
+              file replicated template-replicated:destination
+
+mode = 0644
+
+[instance-runner-import]
+recipe = slapos.recipe.template
+url = ${:_profile_base_location_}/instance-runner-import.cfg.in
+output = ${buildout:directory}/instance-runner-import.cfg
+mode = 0644
+
+[instance-runner-export]
+recipe = slapos.recipe.template
+url = ${:_profile_base_location_}/instance-runner-export.cfg.in
+output = ${buildout:directory}/instance-runner-export.cfg
 mode = 0644
 
 [eggs]
diff --git a/software/slaprunner/instance-resilient.cfg.jinja2 b/software/slaprunner/instance-resilient.cfg.jinja2
new file mode 100644
index 000000000..f0461369d
--- /dev/null
+++ b/software/slaprunner/instance-resilient.cfg.jinja2
@@ -0,0 +1,28 @@
+# vim: set ft=cfg:
+
+{% import 'parts' as parts %}
+{% import 'replicated' as replicated %}
+
+[buildout]
+eggs-directory = {{ eggs_directory }}
+develop-eggs-directory = {{ develop_eggs_directory }}
+offline = true
+
+# += because we need to take up parts (like instance-custom, slapmonitor etc) from the profile we extended
+parts +=
+  {{ parts.replicate("runner", "3") }}
+  publish-connection-informations
+
+{{ replicated.replicate("runner", "3", "runner-export", "runner-import") }}
+
+# Bubble up the parameters
+[request-runner]
+return = url ssh-public-key ssh-url notification-id ip backend_url url cloud9_url ssh_command password_recovery_code
+
+[publish-connection-informations]
+recipe = slapos.cookbook:publish
+backend_url = ${request-runner:connection-backend_url}
+url = ${request-runner:connection-url}
+cloud9_url = ${request-runner:connection-cloud9_url}
+ssh_command = ${request-runner:connection-ssh_command}
+password_recovery_code = ${request-runner:connection-password_recovery_code}
diff --git a/software/slaprunner/instance-runner-export.cfg.in b/software/slaprunner/instance-runner-export.cfg.in
new file mode 100644
index 000000000..e75cf1b5c
--- /dev/null
+++ b/software/slaprunner/instance-runner-export.cfg.in
@@ -0,0 +1,24 @@
+[buildout]
+extends = ${template-runner:output}
+          ${pbsready-export:output}
+
+parts +=
+    urls
+    slaprunner
+    cron-entry-backup
+
+[exporter]
+recipe = slapos.cookbook:slaprunner.export
+wrapper = $${directory:bin}/$${slap-parameter:namebase}-exporter
+srv-directory = $${directory:srv}
+backup-directory = $${directory:backup}
+shell-binary = ${dash:location}/bin/dash
+
+# Extends publish section with resilient parameters
+[urls]
+<= resilient-publish-connection-parameter
+backend_url = $${slaprunner:access-url}
+url =  $${request-frontend:connection-site_url}
+cloud9_url = $${cloud9:access-url}
+ssh_command = ssh $${dropbear-server:host} -p $${dropbear-server:port}
+password_recovery_code = $${recovery-code:passwd}
diff --git a/software/slaprunner/instance-runner-import.cfg.in b/software/slaprunner/instance-runner-import.cfg.in
new file mode 100644
index 000000000..b7256bbd3
--- /dev/null
+++ b/software/slaprunner/instance-runner-import.cfg.in
@@ -0,0 +1,16 @@
+[buildout]
+extends = ${template-runner:output}
+          ${pbsready-import:output}
+
+
+parts +=
+    slaprunner
+# have to repeat the next one, as it's not inherited from pbsready-import
+    import-on-notification
+
+[importer]
+recipe = slapos.cookbook:slaprunner.import
+wrapper = $${directory:bin}/$${slap-parameter:namebase}-exporter
+srv-directory = $${directory:srv}
+backup-directory = $${directory:backup}
+shell-binary = ${dash:location}/bin/dash
\ No newline at end of file
diff --git a/software/slaprunner/instance.cfg b/software/slaprunner/instance.cfg
index 446a7a198..2b0681dd3 100644
--- a/software/slaprunner/instance.cfg
+++ b/software/slaprunner/instance.cfg
@@ -8,4 +8,11 @@ offline = true
 
 [switch_softwaretype]
 recipe = slapos.cookbook:softwaretype
-default = ${template-runner:output}
\ No newline at end of file
+default = ${template-runner:output}
+resilient = ${instance-resilient:rendered}
+runner = ${template-runner:output}
+runner-import = ${instance-runner-import:output}
+runner-export = ${instance-runner-export:output}
+
+frozen = ${instance-frozen:output}
+pull-backup = ${template-pull-backup:output}
diff --git a/software/slaprunner/software.cfg b/software/slaprunner/software.cfg
index c75c2bc03..454ed0c14 100644
--- a/software/slaprunner/software.cfg
+++ b/software/slaprunner/software.cfg
@@ -5,7 +5,7 @@
 # 2/ Define list of trusted certificates for the cache.
 
 [buildout]
-extends = common.cfg
+extends = development.cfg
 
 [networkcache]
 # signature certificates of the following uploaders.
@@ -57,7 +57,7 @@ plone.recipe.command = 1.1
 pycrypto = 2.6
 pytz = 2012j
 slapos.cookbook = 0.71.1
-slapos.core = 0.34
+slapos.core = 0.35.1
 slapos.libnetworkcache = 0.13.4
 slapos.recipe.build = 0.11.5
 slapos.recipe.template = 2.4.2
-- 
2.30.9