-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
60 lines (44 loc) · 1.71 KB
/
agent.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
import logging
from queue import Queue
import sys
import time
VERSION = '0.1.0'
import requests
from halti_agent import comms, halti_agent_info
from halti_agent import containers as container_client
from halti_agent.state import load_state
from halti_agent.statekeeper import StatekeeperWorker
logger = logging.getLogger('halti-agent')
desired_state_queue = Queue()
def heartbeat():
"""Perform a single Halti Heartbeat."""
logger.debug('Heartbeat!')
try:
payload = {'containers': container_client.list_containers()}
return comms.heartbeat(payload)
except requests.RequestException as e:
logger.error('Heartbeat failed: {}'.format(e))
return None
def main_loop(state, statekeeper):
"""Check that statekeeper is running and perform Halti Heartbeats."""
while statekeeper.is_alive():
hb = heartbeat()
if hb:
desired_state_queue.put(hb)
time.sleep(state['heartbeat_interval'])
logger.error('Statekeeper has crashed. Exiting Halti-Agent.')
sys.exit(-1)
if __name__ == '__main__':
logger.info('Starting Halti-Agent...')
logger.info('VERSION:'+VERSION)
logger.info('Information: {}'.format(halti_agent_info()))
# load state from STATE_FILE or Halti Master
state = load_state(container_client)
# save instance_id to a global so comms has access to it
# this ID never mutates after this when the agent is running
comms.INSTANCE_ID = state['instance_id']
statekeeper = StatekeeperWorker(desired_state_queue, container_client=container_client)
statekeeper.daemon = True
statekeeper.start()
logger.info('Starting Halti-Agent main loop (health checks and heartbeat).')
main_loop(state, statekeeper)