def send_messages(self, messages): sent_messages = 0 if self.http_method.lower() not in ['put', 'post']: raise ValueError("HTTP method must be either 'POST' or 'PUT'.") chosen_method = getattr(requests, self.http_method.lower(), None) for m in messages: auth = None if self.username or self.password: auth = (self.username, self.password) r = chosen_method( "{}".format(m.recipients()[0]), auth=auth, data=json.dumps(m.body, ensure_ascii=False).encode('utf-8'), headers=dict( list(get_awx_http_client_headers().items()) + list((self.headers or {}).items())), verify=(not self.disable_ssl_verification), ) if r.status_code >= 400: logger.error( smart_str( _("Error sending notification webhook: {}").format( r.status_code))) if not self.fail_silently: raise Exception( smart_str( _("Error sending notification webhook: {}").format( r.status_code))) sent_messages += 1 return sent_messages
def send_messages(self, messages): sent_messages = 0 for m in messages: payload = {"text": m.subject} for opt, optval in { 'rocketchat_icon_url': 'icon_url', 'rocketchat_username': '******' }.items(): optvalue = getattr(self, opt) if optvalue is not None: payload[optval] = optvalue.strip() r = requests.post("{}".format(m.recipients()[0]), data=json.dumps(payload), headers=get_awx_http_client_headers(), verify=(not self.rocketchat_no_verify_ssl)) if r.status_code >= 400: logger.error( smart_str( _("Error sending notification rocket.chat: {}").format( r.status_code))) if not self.fail_silently: raise Exception( smart_str( _("Error sending notification rocket.chat: {}"). format(r.status_code))) sent_messages += 1 return sent_messages
def ship(path): """ Ship gathered metrics to the Insights API """ if not path: logger.error('Automation Analytics TAR not found') return if not os.path.exists(path): logger.error('Automation Analytics TAR {} not found'.format(path)) return if "Error:" in str(path): return try: logger.debug('shipping analytics file: {}'.format(path)) url = getattr(settings, 'AUTOMATION_ANALYTICS_URL', None) if not url: logger.error('AUTOMATION_ANALYTICS_URL is not set') return rh_user = getattr(settings, 'REDHAT_USERNAME', None) rh_password = getattr(settings, 'REDHAT_PASSWORD', None) if not rh_user: return logger.error('REDHAT_USERNAME is not set') if not rh_password: return logger.error('REDHAT_PASSWORD is not set') with open(path, 'rb') as f: files = { 'file': (os.path.basename(path), f, settings.INSIGHTS_AGENT_MIME) } s = requests.Session() s.headers = get_awx_http_client_headers() s.headers.pop('Content-Type') with set_environ(**settings.AWX_TASK_ENV): response = s.post( url, files=files, verify="/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", auth=(rh_user, rh_password), headers=s.headers, timeout=(31, 31), ) # Accept 2XX status_codes if response.status_code >= 300: return logger.exception( 'Upload failed with status {}, {}'.format( response.status_code, response.text)) finally: # cleanup tar.gz if os.path.exists(path): os.remove(path)