Skip to content

Commit

Permalink
Added feature to allow emitters to invoke on dnf error
Browse files Browse the repository at this point in the history
  • Loading branch information
derickdiaz committed Oct 17, 2023
1 parent 2c1a388 commit 14882a9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
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 <cickumqt@gmail.com>
Daniel Mach <dmach@redhat.com>
Dave Johansen <davejohansen@gmail.com>
Derick Diaz <derickdiaz123@outlook.com>
Dominik Mierzejewski <dominik@greysector.net>
Dylan Pindur <dylanpindur@gmail.com>
Eduard Cuba <ecuba@redhat.com>
Expand Down
27 changes: 20 additions & 7 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,7 @@ 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
email_tls = self._conf.email_tls
message['Date'] = email.utils.formatdate()
Expand All @@ -105,17 +118,17 @@ def commit(self):

# Send the email
try:
if self._conf.email_tls == 'yes':
smtp = smtplib.SMTP_SSL(self._conf.email_host, self._conf.email_port, timeout=300)
if email_tls == 'yes':
smtp = smtplib.SMTP_SSL(email_host, email_port, timeout=300)
else:
smtp = smtplib.SMTP(self._conf.email_host, self._conf.email_port, timeout=300)
if self._conf.email_tls == 'starttls':
smtp = smtplib.SMTP(email_host, email_port, timeout=300)
if email_tls == 'starttls':
smtp.starttls()
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
5 changes: 5 additions & 0 deletions dnf/automatic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,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 @@ -367,9 +368,13 @@ def main(args):
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)
emitters.notify_error('Error: reboot command returned nonzero exit code: %d', exit_code)
emitters.commit()
return 1
except dnf.exceptions.Error as exc:
logger.error(_('Error: %s'), ucd(exc))
emitters.notify_error(_('Error: %s') % str(exc))
emitters.commit()
return 1
return 0

Expand Down

0 comments on commit 14882a9

Please sign in to comment.