Merge ~cjwatson/launchpad-layers:rabbitmq-optional-relation into launchpad-layers:main

Proposed by Colin Watson
Status: Merged
Merged at revision: 7287e67e5435d7567abf74c411d5ead7d58a3420
Proposed branch: ~cjwatson/launchpad-layers:rabbitmq-optional-relation
Merge into: launchpad-layers:main
Diff against target: 84 lines (+35/-14)
2 files modified
launchpad-base/config.yaml (+5/-0)
launchpad-base/reactive/launchpad-base.py (+30/-14)
Reviewer Review Type Date Requested Status
Guruprasad Approve
Review via email: mp+439193@code.launchpad.net

Commit message

Allow non-relation-driven RabbitMQ configuration

Description of the change

This makes it easier to migrate a non-charmed Launchpad environment to a charmed deployment: everything in a given environment should use the same RabbitMQ server, so we probably want to start by using the existing one rather than demanding that the broker URLs be provided using a Juju relation.

To post a comment you must log in.
Revision history for this message
Guruprasad (lgp171188) wrote :

LGTM 👍🏼

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/launchpad-base/config.yaml b/launchpad-base/config.yaml
2index 4f2b83b..868c93b 100644
3--- a/launchpad-base/config.yaml
4+++ b/launchpad-base/config.yaml
5@@ -206,6 +206,11 @@ options:
6 type: string
7 description: External base URL for private PPAs.
8 default: http://private-ppa.launchpad.test/
9+ rabbitmq_broker_urls:
10+ type: string
11+ description: >
12+ Space-separated list of RabbitMQ broker URLs. If unset, rely on the
13+ rabbitmq relation instead.
14 rabbitmq_user:
15 type: string
16 description: Username to use with RabbitMQ.
17diff --git a/launchpad-base/reactive/launchpad-base.py b/launchpad-base/reactive/launchpad-base.py
18index 0015a78..a6c6fa9 100644
19--- a/launchpad-base/reactive/launchpad-base.py
20+++ b/launchpad-base/reactive/launchpad-base.py
21@@ -18,6 +18,8 @@ from charms.reactive import (
22 remove_state,
23 set_state,
24 when,
25+ when_any,
26+ when_none,
27 when_not,
28 )
29 from ols import base, postgres
30@@ -43,19 +45,33 @@ def prepare_rabbitmq():
31
32
33 def get_rabbitmq_uris(rabbitmq):
34- for conversation in rabbitmq.conversations():
35- for relation_id in conversation.relation_ids:
36- for unit in hookenv.related_units(relation_id):
37- hostname = hookenv.relation_get(
38- "private-address", unit, relation_id
39- )
40- vhost = rabbitmq.vhost()
41- username = rabbitmq.username()
42- password = rabbitmq.password()
43- yield f"amqp://{username}:{password}@{hostname}/{vhost}"
44-
45-
46-@when("ols.configured", "db.master.available", "rabbitmq.available")
47+ config = hookenv.config()
48+ if config["rabbitmq_broker_urls"]:
49+ yield from config["rabbitmq_broker_urls"].split()
50+ else:
51+ for conversation in rabbitmq.conversations():
52+ for relation_id in conversation.relation_ids:
53+ for unit in hookenv.related_units(relation_id):
54+ hostname = hookenv.relation_get(
55+ "private-address", unit, relation_id
56+ )
57+ vhost = rabbitmq.vhost()
58+ username = rabbitmq.username()
59+ password = rabbitmq.password()
60+ yield f"amqp://{username}:{password}@{hostname}/{vhost}"
61+
62+
63+@when_any("rabbitmq.available", "config.set.rabbitmq_broker_urls")
64+def rabbitmq_available():
65+ set_state("launchpad.rabbitmq.available")
66+
67+
68+@when_none("rabbitmq.available", "config.set.rabbitmq_broker_urls")
69+def rabbitmq_unavailable():
70+ remove_state("launchpad.rabbitmq.available")
71+
72+
73+@when("ols.configured", "db.master.available", "launchpad.rabbitmq.available")
74 @when_not("launchpad.base.configured")
75 def configure():
76 db = endpoint_from_flag("db.master.available")
77@@ -114,6 +130,6 @@ def config_changed():
78
79
80 @hook("{requires:rabbitmq}-relation-changed")
81-def rabbitmq_relation_changed():
82+def rabbitmq_relation_changed(*args):
83 remove_state("launchpad.base.configured")
84 remove_state("service.configured")

Subscribers

People subscribed via source and target branches