Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport dnf-automatic sending notification on error for RHEL 9.6 #2171

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ DNF CONTRIBUTORS
Christopher Meng <[email protected]>
Daniel Mach <[email protected]>
Dave Johansen <[email protected]>
Derick Diaz <[email protected]>
Dylan Pindur <[email protected]>
Eduard Cuba <[email protected]>
Evan Goode <[email protected]>
Expand Down
22 changes: 18 additions & 4 deletions dnf/automatic/emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
APPLIED_TIMESTAMP = _("Updates completed at %s")
AVAILABLE = _("The following updates are available on '%s':")
DOWNLOADED = _("The following updates were downloaded on '%s':")
ERROR = _("An error has occured on: '%s'")

logger = logging.getLogger('dnf')

Expand All @@ -44,10 +45,15 @@ def __init__(self, system_name):
self._downloaded = False
self._system_name = system_name
self._trans_msg = None
self._error = False
self._error_msg = None

def _prepare_msg(self):
msg = []
if self._applied:
if self._error:
msg.append(ERROR % self._system_name)
msg.append(self._error_msg)
elif self._applied:
msg.append(APPLIED % self._system_name)
msg.append(self._available_msg)
msg.append(APPLIED_TIMESTAMP % time.strftime("%c"))
Expand All @@ -72,14 +78,20 @@ def notify_downloaded(self):
assert self._available_msg
self._downloaded = True

def notify_error(self, msg):
self._error = True
self._error_msg = msg


class EmailEmitter(Emitter):
def __init__(self, system_name, conf):
super(EmailEmitter, self).__init__(system_name)
self._conf = conf

def _prepare_msg(self):
if self._applied:
if self._error:
subj = _("An error has occured on '%s'.") % self._system_name
elif self._applied:
subj = _("Updates applied on '%s'.") % self._system_name
elif self._downloaded:
subj = _("Updates downloaded on '%s'.") % self._system_name
Expand All @@ -95,6 +107,8 @@ def commit(self):
message.set_charset('utf-8')
email_from = self._conf.email_from
email_to = self._conf.email_to
email_host = self._conf.email_host
email_port = self._conf.email_port
message['Date'] = email.utils.formatdate()
message['From'] = email_from
message['Subject'] = subj
Expand All @@ -103,12 +117,12 @@ def commit(self):

# Send the email
try:
smtp = smtplib.SMTP(self._conf.email_host, timeout=300)
smtp = smtplib.SMTP(email_host, email_port, timeout=300)
smtp.sendmail(email_from, email_to, message.as_string())
smtp.close()
except OSError as exc:
msg = _("Failed to send an email via '%s': %s") % (
self._conf.email_host, exc)
email_host, exc)
logger.error(msg)


Expand Down
8 changes: 6 additions & 2 deletions dnf/automatic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def __init__(self):
libdnf.conf.VectorString(['email', 'stdio'])))
self.add_option('output_width', libdnf.conf.OptionNumberInt32(80))
self.add_option('system_name', libdnf.conf.OptionString(socket.gethostname()))
self.add_option('send_error_messages', libdnf.conf.OptionBool(False))


def gpgsigcheck(base, pkgs):
Expand Down Expand Up @@ -314,6 +315,7 @@ def main(args):
try:
conf = AutomaticConfig(opts.conf_path, opts.downloadupdates,
opts.installupdates)
emitters = None
with dnf.Base() as base:
cli = dnf.cli.Cli(base)
cli._read_conf_file()
Expand Down Expand Up @@ -375,10 +377,12 @@ def main(args):
(conf.commands.reboot == 'when-needed' and base.reboot_needed())):
exit_code = os.waitstatus_to_exitcode(os.system(conf.commands.reboot_command))
if exit_code != 0:
logger.error('Error: reboot command returned nonzero exit code: %d', exit_code)
return 1
raise dnf.exceptions.Error('reboot command returned nonzero exit code: %d', exit_code)
except dnf.exceptions.Error as exc:
logger.error(_('Error: %s'), ucd(exc))
if conf.emitters.send_error_messages and emitters != None:
emitters.notify_error(_('Error: %s') % str(exc))
emitters.commit()
return 1
return 0

Expand Down
5 changes: 5 additions & 0 deletions doc/automatic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ Choosing how the results should be reported.

How the system is called in the reports.

``send_error_messages``
boolean, default: False

Invokes emitters when an error occurs.

---------------------
``[command]`` section
---------------------
Expand Down
Loading