← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~pappacena/turnip:celery-worker-charm into turnip:master

 

Thiago F. Pappacena has proposed merging ~pappacena/turnip:celery-worker-charm into turnip:master with ~pappacena/turnip:celery-repo-creation as a prerequisite.

Commit message:
WIP: Adding juju setup for celery and rabbitmq

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~pappacena/turnip/+git/turnip/+merge/387362
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~pappacena/turnip:celery-worker-charm into turnip:master.
diff --git a/charm/bundle.yaml.in b/charm/bundle.yaml.in
index 8078e4b..c57e878 100644
--- a/charm/bundle.yaml.in
+++ b/charm/bundle.yaml.in
@@ -25,6 +25,9 @@ applications:
           service_port: 9419
       ssl_cert: "%SSL_CERT%"
       ssl_key: "%SSL_KEY%"
+  rabbitmq-server:
+    charm: cs:rabbitmq-server
+    num_units: 1
   turnip-pack-backend:
     charm: ./dist/builds/turnip-pack-backend
     num_units: 1
@@ -85,3 +88,4 @@ relations:
   - ["haproxy", "turnip-pack-frontend-http:turnip-pack-virt"]
   - ["haproxy", "turnip-pack-frontend-http:turnip-pack-frontend-http"]
   - ["haproxy", "turnip-api"]
+  - ['rabbitmq-server:amqp', "turnip-api:amqp"]
diff --git a/charm/turnip-api/config.yaml b/charm/turnip-api/config.yaml
index e773d0f..cff36b9 100644
--- a/charm/turnip-api/config.yaml
+++ b/charm/turnip-api/config.yaml
@@ -45,3 +45,7 @@ options:
       - option httplog
       - option httpchk /repo
       - balance leastconn
+  celery_broker:
+    type: string
+    default: pyamqp://guest@localhost//
+    description: Celery broker URL
diff --git a/charm/turnip-api/metadata.yaml b/charm/turnip-api/metadata.yaml
index 7a10773..f506fcb 100644
--- a/charm/turnip-api/metadata.yaml
+++ b/charm/turnip-api/metadata.yaml
@@ -19,3 +19,7 @@ provides:
   nrpe-external-master:
     interface: nrpe-external-master
     scope: container
+requires:
+  amqp:
+    interface: rabbitmq
+    optional: true
diff --git a/charm/turnip-api/reactive/turnip-api.py b/charm/turnip-api/reactive/turnip-api.py
index 565ba92..caee26a 100644
--- a/charm/turnip-api/reactive/turnip-api.py
+++ b/charm/turnip-api/reactive/turnip-api.py
@@ -8,8 +8,10 @@ from charms.layer import status
 from charms.reactive import (
     clear_flag,
     endpoint_from_flag,
+    endpoint_from_name,
     set_flag,
     when,
+    when_any,
     when_not,
     )
 
@@ -17,6 +19,7 @@ from charms.turnip.api import configure_wsgi
 from charms.turnip.base import (
     configure_service,
     deconfigure_service,
+    find_git_service,
     publish_website,
     )
 
@@ -41,6 +44,21 @@ def deconfigure_turnip():
     status.blocked('Waiting for storage to be available')
 
 
+@when_any('amqp-relation-joined',
+          'amqp-relation-changed')
+def rabbitmq_available():
+    config = hookenv.config()
+    with open("/tmp/banana.txt", "w") as fd:
+        fd.write(str(config))
+    config['celery_broker'] = "??"
+    rabbitmq = endpoint_from_name('rabbitmq-server')
+    address, port = find_git_service(rabbitmq, 'rabbitmq-server')
+    if address is not None and port is not None:
+        url = "pyamqp://guest@%s:%s//" % (address, port)
+        config['celery_broker'] = url
+    configure_wsgi()
+
+
 @when('nrpe-external-master.available', 'turnip.configured')
 @when_not('turnip.nrpe-external-master.published')
 def nrpe_available():
diff --git a/charm/turnip-api/templates/turnip-api.service.j2 b/charm/turnip-api/templates/turnip-api.service.j2
index 3bb4336..2cecf01 100644
--- a/charm/turnip-api/templates/turnip-api.service.j2
+++ b/charm/turnip-api/templates/turnip-api.service.j2
@@ -13,6 +13,7 @@ WorkingDirectory={{ code_dir }}
 Environment=PATH={{ venv_dir }}/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
 Environment=REPO_STORE={{ data_dir }}/repos
 Environment=TURNIP_LOG_DIR={{ logs_dir }}
+Environment=CELERY_BROKER={{ celery_broker }}
 ExecStart={{ venv_dir }}/bin/gunicorn --config {{ config_file }} --paste api.ini
 ExecReload=/bin/kill -s HUP $MAINPID
 ExecStop=/bin/kill -s TERM $MAINPID

References