← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad-layers:mail-configure into launchpad-layers:main

 

Colin Watson has proposed merging ~cjwatson/launchpad-layers:mail-configure into launchpad-layers:main.

Commit message:
Configure outbound email if send_email is true

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #2017136 in launchpad-layers: "No email received after changing the OpenPGP key"
  https://bugs.launchpad.net/launchpad-layers/+bug/2017136

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad-layers/+git/launchpad-layers/+merge/441577

Most of Launchpad's email is sent using immediate delivery, which uses `smtplib` directly; but some, such as email sent by garbo jobs, instead queues it up until the end of the transaction.  The latter mode wasn't configured properly on production, so outgoing email ended up in a default "stub" mailer which sent everything to root@localhost.  Add configuration to send queued production email to localhost:25 as well.

Individual charms must call `configure_email` to install `mail-configure.zcml` in the appropriate configuration directory, or remove it if `send_email` is false.

See also https://code.launchpad.net/~cjwatson/lp-production-configs/production-smtp-mailer/+merge/411555 in our old private configs branch.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad-layers:mail-configure into launchpad-layers:main.
diff --git a/launchpad-base/config.yaml b/launchpad-base/config.yaml
index 3fa13a5..14feeca 100644
--- a/launchpad-base/config.yaml
+++ b/launchpad-base/config.yaml
@@ -219,6 +219,13 @@ options:
     type: string
     description: Username to use with RabbitMQ.
     default: test
+  send_email:
+    type: boolean
+    description: >
+      Is this service allowed to send email?  (Email is always sent via a
+      mail transport agent running on localhost; deploy a postfix-relay
+      subordinate charm to handle this.)
+    default: false
   session_cookie_name:
     type: string
     description: The ID of the cookie used to store the session token.
diff --git a/launchpad-base/lib/charms/launchpad/base.py b/launchpad-base/lib/charms/launchpad/base.py
index fbdf2fd..7768c9b 100644
--- a/launchpad-base/lib/charms/launchpad/base.py
+++ b/launchpad-base/lib/charms/launchpad/base.py
@@ -136,6 +136,25 @@ def lazr_config_files():
     ]
 
 
+def configure_email(config, directory_name):
+    mail_configure_path = config_file_path(
+        f"{directory_name}/mail-configure.zcml"
+    )
+    if config["send_email"]:
+        hookenv.log("Writing email configuration.")
+        templating.render(
+            "mail-configure.zcml",
+            mail_configure_path,
+            config,
+            owner="root",
+            group=base.user(),
+            perms=0o444,
+        )
+    elif os.path.exists(mail_configure_path):
+        hookenv.log("Removing email configuration.")
+        os.unlink(mail_configure_path)
+
+
 def configure_rsync(config, template, name):
     hookenv.log("Writing rsync configuration.")
     rsync_path = os.path.join("/etc/rsync-juju.d", name)
diff --git a/launchpad-base/templates/launchpad-base-lazr.conf b/launchpad-base/templates/launchpad-base-lazr.conf
index 71c456e..b9707cb 100644
--- a/launchpad-base/templates/launchpad-base-lazr.conf
+++ b/launchpad-base/templates/launchpad-base-lazr.conf
@@ -57,6 +57,9 @@ oops_prefix: {{ oops_prefix }}
 [gpghandler]
 upload_keys: {{ gpg_upload_keys }}
 
+[immediate_mail]
+{{- opt("send_email", send_email) }}
+
 [launchpad]
 bugs_domain: bugs.{{ domain }}
 {{- opt("bzr_imports_root_url", bzr_imports_root_url) }}
diff --git a/launchpad-base/templates/mail-configure.zcml b/launchpad-base/templates/mail-configure.zcml
new file mode 100644
index 0000000..380a530
--- /dev/null
+++ b/launchpad-base/templates/mail-configure.zcml
@@ -0,0 +1,8 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope";
+    xmlns:mail="http://namespaces.zope.org/mail";
+    i18n_domain="zope">
+    <mail:smtpMailer name="localhost" hostname="localhost" port="25" />
+    <mail:directDelivery permission="zope.SendMail" mailer="localhost" />
+</configure>
+

Follow ups