Merge ~ines-almeida/launchpad-layers:add-pkgupload-interface into launchpad-layers:main

Proposed by Ines Almeida
Status: Merged
Approved by: Ines Almeida
Approved revision: 5b890f91070864c5eefc45f974298756e39c53a5
Merge reported by: Ines Almeida
Merged at revision: 3618c237db8fd7a7d44f8872d27e7b6b22b703ab
Proposed branch: ~ines-almeida/launchpad-layers:add-pkgupload-interface
Merge into: launchpad-layers:main
Diff against target: 109 lines (+91/-0)
3 files modified
upload-queue-processor/interface.yaml (+3/-0)
upload-queue-processor/provides.py (+37/-0)
upload-queue-processor/requires.py (+51/-0)
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Review via email: mp+445989@code.launchpad.net

Commit message

Create interface for txpkgupload charm

This interface has 2 main goals:
 - Set the `<interface>.available` and `<interface>.configuration.available` flags within the charms
 - Send configuration data from the upload processor to the txpkgupload using `set_config()` in `provides.py`, and fetching it from the txpkgupload charm directly once the `<interface>.configuration.available` flag is on.

Description of the change

This interface should be used between txpkgupload and the ppa/ftpmaster upload processors charms

Currently the ppa/ftpmaster upload processors are the providers of the interface, and the txpkgupload is the one that requires it

To post a comment you must log in.
Revision history for this message
Ines Almeida (ines-almeida) :
Revision history for this message
Ines Almeida (ines-almeida) wrote :

Added some of my own thoughts and questions about the code

Revision history for this message
Colin Watson (cjwatson) :
review: Approve
Revision history for this message
Ines Almeida (ines-almeida) wrote :

Since we are on it, I'll wait for the txpkgupload charm MP to be ready for approval before merging this one, in case we want to make any changes. I'll leave it open for review in the meantime

Revision history for this message
Guruprasad (lgp171188) :
Revision history for this message
Ines Almeida (ines-almeida) wrote :

I addressed your comments Guruprasad. Good thing this didn't merge automatically when I hit approve :)

Revision history for this message
Guruprasad (lgp171188) wrote :

Thanks Inês!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/upload-queue-processor/interface.yaml b/upload-queue-processor/interface.yaml
2new file mode 100644
3index 0000000..2442dd8
4--- /dev/null
5+++ b/upload-queue-processor/interface.yaml
6@@ -0,0 +1,3 @@
7+name: upload-queue-processor
8+summary: Package Uploader interface
9+version: 1
10diff --git a/upload-queue-processor/provides.py b/upload-queue-processor/provides.py
11new file mode 100644
12index 0000000..1c0c05b
13--- /dev/null
14+++ b/upload-queue-processor/provides.py
15@@ -0,0 +1,37 @@
16+# Copyright 2023 Canonical Ltd. This software is licensed under the
17+# GNU Affero General Public License version 3 (see the file LICENSE).
18+
19+from charmhelpers.core import hookenv
20+from charms.reactive import Endpoint, clear_flag, set_flag, when
21+
22+
23+class UploadProcessorProvides(Endpoint):
24+ @when("endpoint.{endpoint_name}.joined")
25+ def handle_joined_unit(self):
26+ set_flag(self.expand_name("{endpoint_name}.available"))
27+
28+ @when("endpoint.{endpoint_name}.changed")
29+ def handle_changed_unit(self):
30+ set_flag(self.expand_name("{endpoint_name}.available"))
31+ clear_flag(self.expand_name("changed"))
32+
33+ @when("endpoint.{endpoint_name}.departed")
34+ def handle_departed_unit(self):
35+ clear_flag(self.expand_name("{endpoint_name}.available"))
36+ self.all_departed_units.clear()
37+ clear_flag(self.expand_name("departed"))
38+
39+ def set_config(self, fsroot: str):
40+ """Called from the provider of the interface to configure txpkgupload.
41+ This will trigger a 'changed' flag in the `requires` side of the
42+ relation.
43+
44+ :param fsroot: directory for the uploads to live within the filesystem
45+ """
46+ hookenv.log("Publishing configuration data txpkgupload interface")
47+ for relation in self.relations:
48+ relation.to_publish.update(
49+ {
50+ "fsroot": fsroot,
51+ }
52+ )
53diff --git a/upload-queue-processor/requires.py b/upload-queue-processor/requires.py
54new file mode 100644
55index 0000000..3f3df36
56--- /dev/null
57+++ b/upload-queue-processor/requires.py
58@@ -0,0 +1,51 @@
59+# Copyright 2023 Canonical Ltd. This software is licensed under the
60+# GNU Affero General Public License version 3 (see the file LICENSE).
61+
62+from charmhelpers.core import hookenv
63+from charms.reactive import Endpoint, clear_flag, set_flag, when
64+
65+
66+class UploadProcessorRequires(Endpoint):
67+ def __init__(self, *args, **kwargs):
68+ super().__init__(*args, **kwargs)
69+ self.fsroot = None
70+
71+ @when("endpoint.{endpoint_name}.joined")
72+ def handle_joined_unit(self):
73+ # We don't want to make it available before it's configured
74+ # It is configured, when data is added from the provider side,
75+ # triggering the 'changed' endpoint
76+ hookenv.log("Txpkgupload interface available.")
77+
78+ @when("endpoint.{endpoint_name}.changed")
79+ def handle_changed_unit(self):
80+ clear_flag(self.expand_name("{endpoint_name}.configured"))
81+
82+ # We expect exactly one relation between a txpkgupload and a queue
83+ # processor (where the txpkgupload a subordinate to the latter)
84+ if len(self.relations) != 1:
85+ endpoint_name = self.expand_name("{endpoint_name}")
86+ clear_flag(self.expand_name("changed"))
87+ raise Exception(
88+ f"Expected exactly 1 relation in {endpoint_name} but found "
89+ f"{len(self.relations)} instead."
90+ )
91+
92+ # Use `fsroot` values received from the queue processor
93+ # If none were received, use the ones received previously
94+ received_data = self.relations[0].units.received
95+ self.fsroot = received_data.get("fsroot", self.fsroot)
96+
97+ if self.fsroot:
98+ hookenv.log("Configuration from queue processor received")
99+ set_flag(self.expand_name("{endpoint_name}.configured"))
100+ else:
101+ hookenv.log("Waiting for configuration from queue processor")
102+
103+ clear_flag(self.expand_name("changed"))
104+
105+ @when("endpoint.{endpoint_name}.departed")
106+ def handle_departed_unit(self):
107+ clear_flag(self.expand_name("{endpoint_name}.configured"))
108+ self.all_departed_units.clear()
109+ clear_flag(self.expand_name("departed"))

Subscribers

People subscribed via source and target branches