def Status(self): # Only bother checking once a day. Our certs are valid in the # range of weeks, so there's no need to constantly do this. if (datetime.date.today() < self.last_notification + datetime.timedelta(days=1)): return cmd = ['prodcertstatus', '--check_loas_cert_location', 'sslenrolled'] result = cros_build_lib.sudo_run(cmd, user=self.user, error_code_ok=True, redirect_stdout=True, encoding='utf-8') # Figure out how many days are left. The command should display: # SSL-ENROLLED CERT cert expires in about 22 days m = re.search(r'cert expires in about ([0-9]+) days', result.output) if m: days_left = int(m.group(1)) else: days_left = 0 # Send out one notification a day if there's a week or less left # before our creds expire. if days_left <= 7: alerts.SendEmail('Loas certs expiring soon!', self.email_notify, server=self.email_server, message='Please run:\n %s\n\n%s\n%s' % (self.enroll_msg, result.output, result.error)) self.last_notification = datetime.date.today() else: # We won't expire for a while, so stop the periodic polling. self.last_notification = (datetime.date.today() + datetime.timedelta(days=days_left - 8))
def testGmail(self): """Gmail sanity check.""" send_mock = self.PatchObject(alerts.GmailServer, 'Send') alerts.SendEmail( 'mail', 'root@localhost', server=alerts.GmailServer(token_cache_file='fakefile')) self.assertEqual(send_mock.call_count, 1)
def SendHealthAlert(builder_run, subject, body, extra_fields=None): """Send a health alert. Health alerts are only sent for regular buildbots and Pre-CQ buildbots. Args: builder_run: BuilderRun for the main cbuildbot run. subject: The subject of the health alert email. body: The body of the health alert email. extra_fields: (optional) A dictionary of additional message header fields to be added to the message. Custom field names should begin with the prefix 'X-'. """ if builder_run.InEmailReportingEnvironment(): server = alerts.GmailServer( token_cache_file=constants.GMAIL_TOKEN_CACHE_FILE, token_json_file=constants.GMAIL_TOKEN_JSON_FILE) alerts.SendEmail(subject, GetHealthAlertRecipients(builder_run), server=server, message=body, extra_fields=extra_fields)
def testSmtp(self): """Smtp sanity check.""" send_mock = self.PatchObject(alerts.SmtpServer, 'Send') alerts.SendEmail('mail', 'root@localhost') self.assertEqual(send_mock.call_count, 1)