1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
17 from email.mime.text import MIMEText
18 from email.mime.multipart import MIMEMultipart
19 from os.path import isdir
21 from errors import PresentationError
24 class AlertingError(PresentationError):
25 """Exception(s) raised by the alerting module.
27 When raising this exception, put this information to the message in this
29 - short description of the encountered problem (parameter msg),
30 - relevant messages if there are any collected, e.g., from caught
31 exception (optional parameter details),
32 - relevant data if there are any collected (optional parameter details).
35 def __init__(self, msg, details='', level="CRITICAL"):
36 """Sets the exception message and the level.
38 :param msg: Short description of the encountered problem.
39 :param details: Relevant messages if there are any collected, e.g.,
40 from caught exception (optional parameter details), or relevant data
41 if there are any collected (optional parameter details).
42 :param level: Level of the error, possible choices are: "DEBUG", "INFO",
43 "WARNING", "ERROR" and "CRITICAL".
49 super(AlertingError, self).__init__(
50 "Alerting: {0}".format(msg), details, level)
54 "AlertingError(msg={msg!r},details={dets!r},level={level!r})".
55 format(msg=self._msg, dets=self._details, level=self._level))
58 class Alerting(object):
59 """Class implementing the alerting mechanism.
62 def __init__(self, spec):
65 :param spec: The CPTA specification.
66 :type spec: Specification
70 self._ALERTS = ("failed-tests", )
72 self._spec = spec.alerting
73 self._path_failed_tests = spec.environment["paths"]["DIR[STATIC,VPP]"]
75 # Verify and validate input specification:
76 self.configs = self._spec.get("configurations", None)
78 raise AlertingError("No alert configuration is specified.")
79 for config_type, config_data in self.configs.iteritems():
80 if config_type == "email":
81 if not config_data.get("server", None):
82 raise AlertingError("Parameter 'server' is missing.")
83 if not config_data.get("address-to", None):
84 raise AlertingError("Parameter 'address-to' (recipient) is "
86 if not config_data.get("address-from", None):
87 raise AlertingError("Parameter 'address-from' (sender) is "
89 elif config_type == "jenkins":
90 if not isdir(config_data.get("output-dir", "")):
91 raise AlertingError("Parameter 'output-dir' is "
92 "missing or it is not a directory.")
93 if not config_data.get("output-file", None):
94 raise AlertingError("Parameter 'output-file' is missing.")
96 raise AlertingError("Alert of type '{0}' is not implemented.".
99 self.alerts = self._spec.get("alerts", None)
101 raise AlertingError("No alert is specified.")
102 for alert, alert_data in self.alerts.iteritems():
103 if not alert_data.get("title", None):
104 raise AlertingError("Parameter 'title' is missing.")
105 if not alert_data.get("type", None) in self._ALERTS:
106 raise AlertingError("Parameter 'failed-tests' is missing or "
108 if not alert_data.get("way", None) in self.configs.keys():
109 raise AlertingError("Parameter 'way' is missing or incorrect.")
110 if not alert_data.get("include", None):
111 raise AlertingError("Parameter 'include' is missing or the "
115 """Return string with human readable description of the alert.
117 :returns: Readable description.
120 return "configs={configs}, alerts={alerts}".format(
121 configs=self.configs, alerts=self.alerts)
124 """Return string executable as Python constructor call.
126 :returns: Executable constructor call.
129 return "Alerting(spec={spec})".format(
132 def generate_alerts(self):
133 """Generate alert(s) using specified way(s).
136 for alert, alert_data in self.alerts.iteritems():
137 if alert_data["way"] == "email":
138 text, html = self._create_alert_message(alert_data)
139 conf = self.configs["email"]
140 self._send_email(server=conf["server"],
141 addr_from=conf["address-from"],
142 addr_to=conf["address-to"],
143 subject=alert_data["title"],
146 elif alert_data["way"] == "jenkins":
147 self._generate_files_for_jenkins(alert_data)
149 raise AlertingError("Alert with way '{0}' is not implemented.".
150 format(alert_data["way"]))
153 def _send_email(server, addr_from, addr_to, subject, text=None, html=None):
154 """Send an email using predefined configuration.
156 :param server: SMTP server used to send email.
157 :param addr_from: Sender address.
158 :param addr_to: Recipient address(es).
159 :param subject: Subject of the email.
160 :param text: Message in the ASCII text format.
161 :param html: Message in the HTML format.
170 if not text and not html:
171 raise AlertingError("No text/data to send.")
173 msg = MIMEMultipart('alternative')
174 msg['Subject'] = subject
175 msg['From'] = addr_from
176 msg['To'] = ", ".join(addr_to)
179 msg.attach(MIMEText(text, 'plain'))
181 msg.attach(MIMEText(html, 'html'))
185 logging.info("Trying to send alert '{0}' ...".format(subject))
186 logging.debug("SMTP Server: {0}".format(server))
187 logging.debug("From: {0}".format(addr_from))
188 logging.debug("To: {0}".format(", ".join(addr_to)))
189 logging.debug("Message: {0}".format(msg.as_string()))
190 smtp_server = smtplib.SMTP(server)
191 smtp_server.sendmail(addr_from, addr_to, msg.as_string())
192 except smtplib.SMTPException as err:
193 raise AlertingError("Not possible to send the alert via email.",
199 def _create_alert_message(self, alert):
200 """Create the message which is used in the generated alert.
202 :param alert: Message is created for this alert.
204 :returns: Message in the ASCII text and HTML format.
205 :rtype: tuple(str, str)
208 if alert["type"] == "failed-tests":
210 html = "<html><body>"
211 for item in alert["include"]:
212 file_name = "{path}/{name}".format(
213 path=self._path_failed_tests, name=item)
215 with open("{0}.txt".format(file_name), 'r') as txt_file:
216 text += "{0}:\n\n".format(
217 item.replace("failed-tests-", ""))
218 text += txt_file.read() + "\n" * 2
220 logging.error("Not possible to read the file '{0}.txt'.".
223 with open("{0}.rst".format(file_name), 'r') as rst_file:
224 html += "<h2>{0}:</h2>".format(
225 item.replace("failed-tests-", ""))
226 html += rst_file.readlines()[2].\
227 replace("../trending", alert.get("url", ""))
230 logging.error("Not possible to read the file '{0}.rst'.".
232 html += "</body></html>"
234 raise AlertingError("Alert of type '{0}' is not implemented.".
235 format(alert["type"]))
238 def _generate_files_for_jenkins(self, alert):
239 """Create the file which is used in the generated alert.
241 :param alert: Files are created for this alert.
245 config = self.configs[alert["way"]]
247 if alert["type"] == "failed-tests":
248 text, html = self._create_alert_message(alert)
249 file_name = "{0}/{1}".format(config["output-dir"],
250 config["output-file"])
251 logging.info("Writing the file '{0}.txt' ...".format(file_name))
253 with open("{0}.txt".format(file_name), 'w') as txt_file:
256 logging.error("Not possible to write the file '{0}.txt'.".
258 logging.info("Writing the file '{0}.html' ...".format(file_name))
260 with open("{0}.html".format(file_name), 'w') as html_file:
261 html_file.write(html)
263 logging.error("Not possible to write the file '{0}.html'.".
266 raise AlertingError("Alert of type '{0}' is not implemented.".
267 format(alert["type"]))