blob: 997c7b8f54abcbebb9c9e4b09ed527752b8fe71a [file] [log] [blame]
Mike Frysingerd03e6b52019-08-03 12:49:01 -04001#!/usr/bin/python2
Fang Deng2e19bcf2015-03-18 17:49:29 -07002# Copyright 2015 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""
7Mail the content of standard input.
8
9Example usage:
10 Use pipe:
11 $ echo "Some content" |./gmail_lib.py -s "subject" abc@bb.com xyz@gmail.com
12
13 Manually input:
14 $ ./gmail_lib.py -s "subject" abc@bb.com xyz@gmail.com
15 > Line 1
16 > Line 2
17 Ctrl-D to end standard input.
18"""
19import argparse
20import base64
21import httplib2
22import logging
23import sys
24import os
Aviv Keshetcf1f9a52017-12-04 15:46:04 -080025import random
Fang Deng2e19bcf2015-03-18 17:49:29 -070026from email.mime.text import MIMEText
27
28import common
Sean Paul97d85b62017-06-28 10:50:34 -040029from autotest_lib.client.bin import utils
Fang Deng2e19bcf2015-03-18 17:49:29 -070030from autotest_lib.client.common_lib import global_config
Ningning Xiac0c10612016-11-23 13:15:26 -080031from autotest_lib.server import site_utils
Fang Deng2e19bcf2015-03-18 17:49:29 -070032
33try:
34 from apiclient.discovery import build as apiclient_build
35 from apiclient import errors as apiclient_errors
36 from oauth2client import file as oauth_client_fileio
37except ImportError as e:
38 apiclient_build = None
Dan Shi8fb23d22016-06-24 16:56:16 +000039 logging.debug("API client for gmail disabled. %s", e)
Fang Deng2e19bcf2015-03-18 17:49:29 -070040
Aviv Keshet00dff1f2019-11-22 14:05:08 -080041# Note: These imports needs to come after the apiclient imports, because
Aviv Keshetd52a4912016-06-23 23:57:23 -070042# of a sys.path war between chromite and autotest crbug.com/622988
43from autotest_lib.server import utils as server_utils
44from chromite.lib import retry_util
Dan Shi5e2efb72017-02-07 11:40:23 -080045
46try:
47 from chromite.lib import metrics
48except ImportError:
Sean Paul97d85b62017-06-28 10:50:34 -040049 metrics = utils.metrics_mock
Aviv Keshetd52a4912016-06-23 23:57:23 -070050
Fang Deng2e19bcf2015-03-18 17:49:29 -070051
Fang Dengf8a94e22015-12-07 13:39:13 -080052DEFAULT_CREDS_FILE = global_config.global_config.get_config_value(
53 'NOTIFICATIONS', 'gmail_api_credentials', default=None)
Fang Dengf3efacb2015-09-25 14:20:51 -070054RETRY_DELAY = 5
55RETRY_BACKOFF_FACTOR = 1.5
56MAX_RETRY = 10
57RETRIABLE_MSGS = [
58 # User-rate limit exceeded
59 r'HttpError 429',]
Fang Deng2e19bcf2015-03-18 17:49:29 -070060
61class GmailApiException(Exception):
62 """Exception raised in accessing Gmail API."""
63
64
65class Message():
66 """An email message."""
67
68 def __init__(self, to, subject, message_text):
69 """Initialize a message.
70
71 @param to: The recievers saperated by comma.
72 e.g. 'abc@gmail.com,xyz@gmail.com'
73 @param subject: String, subject of the message
74 @param message_text: String, content of the message.
75 """
76 self.to = to
77 self.subject = subject
78 self.message_text = message_text
79
80
81 def get_payload(self):
82 """Get the payload that can be sent to the Gmail API.
83
84 @return: A dictionary representing the message.
85 """
86 message = MIMEText(self.message_text)
87 message['to'] = self.to
88 message['subject'] = self.subject
89 return {'raw': base64.urlsafe_b64encode(message.as_string())}
90
91
92class GmailApiClient():
93 """Client that talks to Gmail API."""
94
95 def __init__(self, oauth_credentials):
96 """Init Gmail API client
97
98 @param oauth_credentials: Path to the oauth credential token.
99 """
100 if not apiclient_build:
101 raise GmailApiException('Cannot get apiclient library.')
102
103 storage = oauth_client_fileio.Storage(oauth_credentials)
104 credentials = storage.get()
105 if not credentials or credentials.invalid:
106 raise GmailApiException('Invalid credentials for Gmail API, '
107 'could not send email.')
108 http = credentials.authorize(httplib2.Http())
109 self._service = apiclient_build('gmail', 'v1', http=http)
110
111
Fang Dengf3efacb2015-09-25 14:20:51 -0700112 def send_message(self, message, ignore_error=True):
113 """Send an email message.
Fang Deng2e19bcf2015-03-18 17:49:29 -0700114
Fang Dengf3efacb2015-09-25 14:20:51 -0700115 @param message: Message to be sent.
116 @param ignore_error: If True, will ignore any HttpError.
117 """
118 try:
119 # 'me' represents the default authorized user.
120 message = self._service.users().messages().send(
121 userId='me', body=message.get_payload()).execute()
122 logging.debug('Email sent: %s' , message['id'])
123 except apiclient_errors.HttpError as error:
124 if ignore_error:
125 logging.error('Failed to send email: %s', error)
126 else:
127 raise
Fang Deng2e19bcf2015-03-18 17:49:29 -0700128
129
Fang Dengf8a94e22015-12-07 13:39:13 -0800130def send_email(to, subject, message_text, retry=True, creds_path=None):
Fang Deng2e19bcf2015-03-18 17:49:29 -0700131 """Send email.
132
133 @param to: The recipients, separated by comma.
134 @param subject: Subject of the email.
135 @param message_text: Text to send.
Fang Deng592640c2015-10-19 13:22:40 -0700136 @param retry: If retry on retriable failures as defined in RETRIABLE_MSGS.
Fang Dengf8a94e22015-12-07 13:39:13 -0800137 @param creds_path: The credential path for gmail account, if None,
138 will use DEFAULT_CREDS_FILE.
Fang Deng2e19bcf2015-03-18 17:49:29 -0700139 """
Fang Dengf8a94e22015-12-07 13:39:13 -0800140 auth_creds = server_utils.get_creds_abspath(
141 creds_path or DEFAULT_CREDS_FILE)
142 if not auth_creds or not os.path.isfile(auth_creds):
Aviv Keshet74216952017-12-04 15:31:33 -0800143 logging.error('Failed to send email to %s: Credential file does not '
144 'exist: %s. If this is a prod server, puppet should '
Fang Deng2e19bcf2015-03-18 17:49:29 -0700145 'install it. If you need to be able to send email, '
146 'find the credential file from chromeos-admin repo and '
147 'copy it to %s', to, auth_creds, auth_creds)
148 return
149 client = GmailApiClient(oauth_credentials=auth_creds)
150 m = Message(to, subject, message_text)
Fang Deng592640c2015-10-19 13:22:40 -0700151 retry_count = MAX_RETRY if retry else 0
Fang Dengf3efacb2015-09-25 14:20:51 -0700152
153 def _run():
154 """Send the message."""
155 client.send_message(m, ignore_error=False)
156
157 def handler(exc):
158 """Check if exc is an HttpError and is retriable.
159
160 @param exc: An exception.
161
162 @return: True if is an retriable HttpError.
163 """
164 if not isinstance(exc, apiclient_errors.HttpError):
165 return False
166
167 error_msg = str(exc)
168 should_retry = any([msg in error_msg for msg in RETRIABLE_MSGS])
169 if should_retry:
170 logging.warning('Will retry error %s', exc)
171 return should_retry
172
Aviv Keshetd885d782016-12-11 16:18:41 -0800173 success = False
Fang Deng592640c2015-10-19 13:22:40 -0700174 try:
175 retry_util.GenericRetry(
176 handler, retry_count, _run, sleep=RETRY_DELAY,
177 backoff_factor=RETRY_BACKOFF_FACTOR)
Aviv Keshetd885d782016-12-11 16:18:41 -0800178 success = True
179 finally:
180 metrics.Counter('chromeos/autotest/send_email/count').increment(
181 fields={'success': success})
Fang Deng2e19bcf2015-03-18 17:49:29 -0700182
183
184if __name__ == '__main__':
185 logging.basicConfig(level=logging.DEBUG)
186 parser = argparse.ArgumentParser(
187 description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
188 parser.add_argument('-s', '--subject', type=str, dest='subject',
189 required=True, help='Subject of the mail')
Aviv Keshetcf1f9a52017-12-04 15:46:04 -0800190 parser.add_argument('-p', type=float, dest='probability',
191 required=False, default=0,
192 help='(optional) per-addressee probability '
193 'with which to send email. If not specified '
194 'all addressees will receive message.')
Fang Deng2e19bcf2015-03-18 17:49:29 -0700195 parser.add_argument('recipients', nargs='*',
196 help='Email addresses separated by space.')
197 args = parser.parse_args()
198 if not args.recipients or not args.subject:
199 print 'Requires both recipients and subject.'
200 sys.exit(1)
201
202 message_text = sys.stdin.read()
Ningning Xiac0c10612016-11-23 13:15:26 -0800203
Aviv Keshetcf1f9a52017-12-04 15:46:04 -0800204 if args.probability:
205 recipients = []
206 for r in args.recipients:
207 if random.random() < args.probability:
208 recipients.append(r)
209 if recipients:
210 print 'Randomly selected recipients %s' % recipients
211 else:
212 print 'Random filtering removed all recipients. Sending nothing.'
213 sys.exit(0)
214 else:
215 recipients = args.recipients
216
217
Ningning Xiac0c10612016-11-23 13:15:26 -0800218 with site_utils.SetupTsMonGlobalState('gmail_lib', short_lived=True):
Aviv Keshetcf1f9a52017-12-04 15:46:04 -0800219 send_email(','.join(recipients), args.subject , message_text)