From bcf34071357e2c8a130649a12497dcd9cdbedb45 Mon Sep 17 00:00:00 2001
From: Nicolas Wavrant <nicolas.wavrant@tiolive.com>
Date: Mon, 21 Oct 2013 15:51:59 +0200
Subject: [PATCH] slaprunner: isn't started by the development server anymore
 but thanks to Gunicorn, as a wsgi software

---
 software/slaprunner/common.cfg          | 13 ++++-----
 software/slaprunner/instance-runner.cfg | 36 ++++++++++++++++++-------
 software/slaprunner/nginx_conf.in       | 28 +++++++++----------
 software/slaprunner/software.cfg        |  3 +++
 4 files changed, 46 insertions(+), 34 deletions(-)

diff --git a/software/slaprunner/common.cfg b/software/slaprunner/common.cfg
index 226e4ac01..e98226be1 100644
--- a/software/slaprunner/common.cfg
+++ b/software/slaprunner/common.cfg
@@ -92,7 +92,7 @@ mode = 0644
 recipe = slapos.recipe.template
 url = ${:_profile_base_location_}/instance-runner.cfg
 output = ${buildout:directory}/template-runner.cfg
-#md5sum = 1355283f0e79a890aeb687cedd8220ee
+md5sum = 8054f653b00c1fdd50e70b23c5a861a7
 mode = 0644
 
 [instance-runner-import]
@@ -128,7 +128,7 @@ mode = 0644
 recipe = hexagonit.recipe.download
 url = ${:_profile_base_location_}/nginx_conf.in
 download-only = true
-#md5sum = 09b7677dfc6b23c1f58e67fd06a7625e
+md5sum = df4cf59f160b429a1eee43eea9e6aff8
 filename = nginx_conf.in
 mode = 0644
 
@@ -143,7 +143,7 @@ location = ${buildout:parts-directory}/${:_buildout_section_name_}
 [slapos-cfg-template]
 recipe = hexagonit.recipe.download
 url = ${:_profile_base_location_}/template/${:filename}
-#md5sum = 28ceadbeaa3b687f7ba39d87cd6772f3
+md5sum = fa0f89e2b68d829452edcaebde9f785d
 location = ${buildout:parts-directory}/${:_buildout_section_name_}
 filename = slapos.cfg.in 
 download-only = true
@@ -165,7 +165,8 @@ eggs =
   slapos.core
   xml_marshaller
   pytz
-<<<<<<< HEAD
+  collective.recipe.environment
+  Gunicorn
 
 # Add slapos.libnetworkcache to path of slapos.core so that slaprunner can build SRs using cache
 [slapos-cookbook]
@@ -183,7 +184,3 @@ eggs =
   xml_marshaller
   pytz
   slapos.libnetworkcache
-
-=======
-  collective.recipe.environment
->>>>>>> 3bf4e1a... slaprunner : is now deployed by buildout
diff --git a/software/slaprunner/instance-runner.cfg b/software/slaprunner/instance-runner.cfg
index e3af9706c..f1b94f450 100644
--- a/software/slaprunner/instance-runner.cfg
+++ b/software/slaprunner/instance-runner.cfg
@@ -7,6 +7,8 @@ parts =
   ca-nginx
   ca-node-frontend
   ca-shellinabox
+  gunicorn-launcher
+  gunicorn-graceful
   test-runner
   sshkeys-dropbear-runner
   dropbear-server-add-authorized-key
@@ -25,7 +27,6 @@ parts =
   shellinabox
   environ
   slapos-cfg
-  slaprunner-wrapper
   slapos-repo-config
   prepare-software
 
@@ -33,7 +34,6 @@ eggs-directory = ${buildout:eggs-directory}
 develop-eggs-directory = ${buildout:develop-eggs-directory}
 offline = true
 
-
 # Create all needed directories
 [directory]
 recipe = slapos.cookbook:mkdirectory
@@ -248,6 +248,7 @@ context =
     key shellinabox_port shellinabox:port
     section param_nginx_frontend nginx-frontend
     section param_tempdir tempdirectory
+    key socket gunicorn-frontend:socket
 
 [nginx-launcher]
 recipe = slapos.recipe.template:jinja2
@@ -257,6 +258,29 @@ mode = 700
 context =
     section param_nginx_frontend nginx-frontend
 
+#--------------------
+#--
+#-- WSGI
+
+[gunicorn-frontend]
+bin_gunicorn = $${directory:bin}/gunicorn
+bin_launcher = $${directory:services}/gunicorn
+path_shell = ${dash:location}/bin/dash
+socket = $${directory:tmp}/flaskserver.sock
+path_pid = $${directory:run}/gunicorn.pid
+
+[gunicorn-launcher]
+recipe = slapos.cookbook:wrapper
+command-line = $${gunicorn-frontend:bin_gunicorn} slapos.runner:app -p $${gunicorn-frontend:path_pid} -b unix:$${gunicorn-frontend:socket} -e RUNNER_CONFIG=$${slaprunner:slapos.cfg} --preload
+wrapper-path = $${gunicorn-frontend:bin_launcher}
+environment = PATH=$${environ:PATH}
+  RUNNER_CONFIG=$${slaprunner:slapos.cfg}
+
+[gunicorn-graceful]
+recipe = slapos.cookbook:wrapper
+command-line = $${directory:bin}/killpidfromfile $${gunicorn-frontend:path_pid} SIGHUP
+wrapper-path = $${directory:scripts}/gunicorn-graceful
+
 #--------------------
 #--
 #-- ssl certificates
@@ -480,14 +504,6 @@ path =
 [environ]
 recipe = collective.recipe.environment
 
-[slaprunner-wrapper]
-recipe = slapos.cookbook:wrapper
-command-line = $${slaprunner:slaprunner} $${slaprunner:slapos.cfg} --log_file $${:log-file}
-log-file = $${slaprunner:log_dir}/slaprunner.log
-wrapper-path = $${slaprunner:wrapper}
-environment = PATH=$${slaprunner:git-binary}:$${environ:PATH}
-  GIT_SSH=$${slaprunner:ssh_client}
-
 [slapos-repo]
 recipe = slapos.recipe.build:gitclone
 repository = $${slap-parameter:slapos-repository}
diff --git a/software/slaprunner/nginx_conf.in b/software/slaprunner/nginx_conf.in
index 5f0c204d7..23728fe3b 100644
--- a/software/slaprunner/nginx_conf.in
+++ b/software/slaprunner/nginx_conf.in
@@ -57,28 +57,24 @@ http {
         scgi_temp_path {{ param_tempdir['scgi_temp_path'] }};
 	error_page 401 /login;
         location / {
-            proxy_pass http://{{ param_nginx_frontend['runner-ip'] }}:{{ param_nginx_frontend['runner-port'] }};
-            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
             auth_basic "Restricted";
             auth_basic_user_file {{ param_nginx_frontend['etc_dir'] }}/.htpasswd;
             proxy_redirect off;
-            proxy_buffering off;
-            proxy_set_header        Host              $host;
-            proxy_set_header        X-Real-IP         $remote_addr;
-            proxy_set_header        X-Forwarded-Proto $scheme;
-            proxy_set_header        X-Forwarded-For   $proxy_add_x_forwarded_for;
-            proxy_set_header        X-Forwarded-Host  $http_host;
+            proxy_set_header X-Forwarded-Proto $scheme;
+            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+            proxy_set_header X-Forwarded-Host $http_host;
+            proxy_set_header X-Accel-Mapping /private/;
+
+            proxy_pass http://unix:{{ socket }};
         }
         location ~ ^(/login|/doLogin|/static|/setAccount|/configAccount|/slapgridResult) {
-            proxy_pass http://{{ param_nginx_frontend['runner-ip'] }}:{{ param_nginx_frontend['runner-port'] }};
-            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
             proxy_redirect off;
-            proxy_buffering off;
-            proxy_set_header        Host              $host;
-            proxy_set_header        X-Real-IP         $remote_addr;
-            proxy_set_header        X-Forwarded-Proto $scheme;
-            proxy_set_header        X-Forwarded-For   $proxy_add_x_forwarded_for;
-            proxy_set_header        X-Forwarded-Host  $http_host;
+            proxy_set_header X-Forwarded-Proto $scheme;
+            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+            proxy_set_header X-Forwarded-Host $http_host;
+            proxy_set_header X-Accel-Mapping /private/;
+
+            proxy_pass http://unix:{{ socket }};
         }
 	location /shellinabox {
             proxy_pass  http://[::1]:{{ shellinabox_port }}/;
diff --git a/software/slaprunner/software.cfg b/software/slaprunner/software.cfg
index 75b5ea637..d796cd3fd 100644
--- a/software/slaprunner/software.cfg
+++ b/software/slaprunner/software.cfg
@@ -135,3 +135,6 @@ unittest2 = 0.5.1
 # Required by:
 # slapos.core==0.34
 zope.interface = 4.0.3
+
+# WSGI server
+gunicorn = 18.0
-- 
2.30.9