forked from ragsden/cds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten.py
executable file
·67 lines (60 loc) · 2.51 KB
/
listen.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
import sys
import traceback
from base import Base
from execute import Execute
from message_reader import MessageReader
class Listen(Base):
def __init__(self):
Base.__init__(self, __name__)
self.log.info('Inside DS listener')
self.message_reader = MessageReader(
self, self.config['BUILD_AMQP_URL'],
self.config['LISTEN_QUEUE'],
self.config['DEFAULT_EXCHANGE'])
self.itinerary = None
self.execute = None
self.log.info('Boot successful')
def main(self):
self.log.info('inside Listen main')
try:
self.message_reader.connect_and_read(self.handle_message_callback)
except Exception as exc:
self.log.error('Error starting listener - {0}'.format(exc))
finally:
self.log.debug('Cleaning up the listener')
handlers = self.log.handlers[:]
for handler in handlers:
handler.close()
self.log.remove_handler(handler)
sys.stdout.flush()
def handle_message_callback(self, message, is_redelivered=True):
self.log.info('Message callback invoked: {0}'.format(is_redelivered))
response = {'success': True}
try:
self.execute = Execute(message)
if self.execute.container_name != self.config['CONTAINER_NAME']:
error_message = 'Invalid DS message received. ' \
'Unmatched container names. \n CONFIG: {0}' \
'\nCONTAINER_NAME {1}'.format(
self.config, self.execute.container_name)
self.log.error(error_message)
response['success'] = False
response['error'] = error_message
raise Exception(error_message)
else:
self.log.info('Valid DS message : {0}'.format(
self.config['BOOT_SUCCESS_MESSAGE']))
self.log.info(self.execute.message)
# this is read by HDQ to decide whether DS container
# boot was successful or not
print self.config['BOOT_SUCCESS_MESSAGE']
boot_file = open('/home/shippable/bootinfo.txt', 'a')
boot_file.write('success')
boot_file.close()
sys.stdout.flush()
self.execute.run()
except Exception as exc:
self.log.error(str(exc))
trace = traceback.format_exc()
self.log.debug(trace)
return response