-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
44 lines (37 loc) · 1.12 KB
/
monitor.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright © 2016 idailybread.com
"""
import docker
import json
import time
import os
__author__ = 'Mush Mo <[email protected]>'
def main():
out_put_path = '/etc/endpoints/endpoints.json'
sync_interval = int(os.getenv('SYNC_INTERVAL', 60))
while True:
node_list = []
client = docker.from_env()
for node in client.nodes.list():
node = node.attrs
if not node['Status']['State'] == 'ready':
continue
node_addr = node['Status']['Addr']
node_name = '{}-{}'.format(
node['Spec']['Role'],
node_addr
)
node_item = {
"Name": node_name,
"URL": "tcp://{}:2375".format(node_addr)
}
node_list.append(node_item)
node_list = sorted(node_list, key=lambda item: item['Name'])
print(node_list)
with open(out_put_path, 'w') as f_endpoints:
f_endpoints.write(json.dumps(node_list))
time.sleep(sync_interval)
if __name__ == '__main__':
main()