diff --git a/AUTHORS b/AUTHORS index aa258d34af..556856d5d6 100644 --- a/AUTHORS +++ b/AUTHORS @@ -68,6 +68,7 @@ DNF CONTRIBUTORS Christopher Meng Daniel Mach Dave Johansen + Derick Diaz Dominik Mierzejewski Dylan Pindur Eduard Cuba diff --git a/dnf/automatic/emitter.py b/dnf/automatic/emitter.py index f49bb17487..1c8ff6bf87 100644 --- a/dnf/automatic/emitter.py +++ b/dnf/automatic/emitter.py @@ -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') @@ -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")) @@ -72,6 +78,10 @@ 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): @@ -79,7 +89,9 @@ def __init__(self, system_name, conf): 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 @@ -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() @@ -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) diff --git a/dnf/automatic/main.py b/dnf/automatic/main.py index 07760d29cb..c27a75268d 100644 --- a/dnf/automatic/main.py +++ b/dnf/automatic/main.py @@ -230,6 +230,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): @@ -305,6 +306,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() @@ -366,10 +368,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 diff --git a/doc/automatic.rst b/doc/automatic.rst index 2566c9d897..d342c95738 100644 --- a/doc/automatic.rst +++ b/doc/automatic.rst @@ -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 ---------------------