forked from ragsden/cds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_out.py
75 lines (66 loc) · 2.39 KB
/
message_out.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys
import json
import time
import logging
import requests
class MessageOut(object):
def __init__(self, module, config):
self.log = None
self.module = module
self.config = config
self.vortex_url = self.config['SHIPPABLE_VORTEX_URL']
self.__setup_logging()
def __setup_logging(self):
logging.basicConfig(level=logging.DEBUG)
self.log = logging.getLogger(self.module)
self.log.setLevel(self.config['LOG_LEVEL'])
def console(self, message):
self.log.debug('Posting message : {0}'.format(message))
post_data = {
"where": "micro.cu",
"payload": {
"headers": message.get('headers'),
"console": message.get('console')
}
}
console_size_bytes = sys.getsizeof(
json.dumps(post_data['payload']['console']))
if console_size_bytes > self.config['MAX_USER_LOG_SIZE_BYTES']:
self.log.warn('Console size exceeding max limit: {0}'.format(
console_size_bytes))
return
headers = {
'Authorization': 'apiToken {0}'.format(
self.config['SHIPPABLE_API_TOKEN']),
'content-type': 'application/json'
}
self.__push_to_vortex(headers, post_data)
def status(self, headers, status):
self.log.debug('Posting status: {0}'.format(status))
post_data = {
"where": "micro.su",
"payload": {
"headers": headers,
"status": status
}
}
headers = {
'Authorization': 'apiToken {0}'.format(
self.config['SHIPPABLE_API_TOKEN']),
'content-type': 'application/json'
}
self.__push_to_vortex(headers, post_data)
def __push_to_vortex(self, headers, post_data):
self.log.debug('Pushing to vortex')
while True:
try:
request = requests.post(
self.vortex_url,
data=json.dumps(post_data),
headers=headers)
self.log.debug('post status response : {0}'.format(request))
break
except Exception as exc:
self.log.error('{0}\n{1}\nretrying...'.format(
post_data, str(exc)))
time.sleep(self.config['SHIPPABLE_VORTEX_RETRY_INTERVAL'])