2013-03-24 22:31:46 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
2015-06-01 17:50:06 -07:00
|
|
|
import re
|
2013-06-09 23:01:58 -04:00
|
|
|
import time
|
2013-03-24 22:31:46 -07:00
|
|
|
import select
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2021-03-03 11:15:09 -05:00
|
|
|
import json
|
2013-06-09 23:01:58 -04:00
|
|
|
from requests.exceptions import ConnectionError
|
2013-03-24 22:31:46 -07:00
|
|
|
|
2013-05-20 15:52:11 -07:00
|
|
|
sys.path.insert(0, '/srv/newsblur')
|
2021-01-12 15:52:10 -05:00
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'newsblur_web.settings'
|
2013-03-24 22:31:46 -07:00
|
|
|
|
2021-03-03 11:15:09 -05:00
|
|
|
NEWSBLUR_USERNAME = 'nb'
|
2013-03-24 22:31:46 -07:00
|
|
|
IGNORE_HOSTS = [
|
2021-03-03 11:15:09 -05:00
|
|
|
'app-push',
|
2013-03-24 22:31:46 -07:00
|
|
|
]
|
|
|
|
|
2021-03-16 15:20:24 -04:00
|
|
|
def main(role="app", role2="app", command=None, path=None):
|
2013-06-09 23:01:58 -04:00
|
|
|
delay = 1
|
|
|
|
|
2021-03-03 11:15:09 -05:00
|
|
|
hosts = subprocess.check_output(['ansible-inventory', '--list'])
|
|
|
|
if not hosts:
|
|
|
|
print(" ***> Could not load ansible-inventory!")
|
|
|
|
return
|
|
|
|
hosts = json.loads(hosts)
|
|
|
|
|
2013-06-09 23:01:58 -04:00
|
|
|
while True:
|
|
|
|
try:
|
2021-03-03 11:15:09 -05:00
|
|
|
streams = create_streams_for_roles(hosts, role, role2, command=command, path=path)
|
|
|
|
print(" --- Loading %s %s/%s Log Tails ---" % (len(streams), role, role2))
|
2013-06-09 23:01:58 -04:00
|
|
|
read_streams(streams)
|
2016-02-04 21:07:28 -08:00
|
|
|
# except UnicodeDecodeError: # unexpected end of data
|
|
|
|
# print " --- Lost connections - Retrying... ---"
|
|
|
|
# time.sleep(1)
|
|
|
|
# continue
|
2013-06-09 23:01:58 -04:00
|
|
|
except ConnectionError:
|
2020-06-19 02:27:48 -04:00
|
|
|
print(" --- Retrying in %s seconds... ---" % delay)
|
2013-06-09 23:01:58 -04:00
|
|
|
time.sleep(delay)
|
|
|
|
delay += 1
|
|
|
|
continue
|
|
|
|
except KeyboardInterrupt:
|
2020-06-19 02:27:48 -04:00
|
|
|
print(" --- End of Logging ---")
|
2013-06-09 23:01:58 -04:00
|
|
|
break
|
|
|
|
|
2021-03-03 11:15:09 -05:00
|
|
|
def create_streams_for_roles(hosts, role, role2, command=None, path=None):
|
2013-03-24 22:31:46 -07:00
|
|
|
streams = list()
|
2013-06-09 23:01:58 -04:00
|
|
|
found = set()
|
|
|
|
|
2013-04-15 17:59:06 -07:00
|
|
|
if not path:
|
|
|
|
path = "/srv/newsblur/logs/newsblur.log"
|
|
|
|
if not command:
|
|
|
|
command = "tail -f"
|
2015-06-01 17:50:06 -07:00
|
|
|
if role in hosts:
|
2021-03-03 11:15:09 -05:00
|
|
|
for hostname in (hosts[role]['hosts'] + hosts[role2]['hosts']):
|
2015-06-01 17:50:06 -07:00
|
|
|
if any(h in hostname for h in IGNORE_HOSTS) and role != 'push': continue
|
2021-03-03 11:15:09 -05:00
|
|
|
follow_host(hosts, streams, found, hostname, command, path)
|
2015-06-01 17:50:06 -07:00
|
|
|
else:
|
|
|
|
host = role
|
|
|
|
role = re.search(r'([^0-9]+)', host).group()
|
|
|
|
for hostname in hosts[role]:
|
|
|
|
if hostname['name'] == host:
|
2021-03-03 11:15:09 -05:00
|
|
|
follow_host(hosts, streams, found, hostname, command, path)
|
2015-06-01 17:50:06 -07:00
|
|
|
|
2013-06-09 23:01:58 -04:00
|
|
|
return streams
|
2013-03-24 22:31:46 -07:00
|
|
|
|
2021-03-03 11:15:09 -05:00
|
|
|
def follow_host(hosts, streams, found, hostname, command=None, path=None):
|
2015-06-01 17:50:06 -07:00
|
|
|
if isinstance(hostname, dict):
|
|
|
|
address = hostname['address']
|
|
|
|
hostname = hostname['name']
|
|
|
|
elif ':' in hostname:
|
|
|
|
hostname, address = hostname.split(':', 1)
|
|
|
|
elif isinstance(hostname, tuple):
|
|
|
|
hostname, address = hostname[0], hostname[1]
|
|
|
|
else:
|
2021-03-03 11:15:09 -05:00
|
|
|
address = hosts['_meta']['hostvars'][hostname]['ansible_host']
|
|
|
|
print(" ---> Following %s %s" % (hostname, address))
|
2015-06-01 17:50:06 -07:00
|
|
|
if hostname in found: return
|
2021-03-03 11:15:09 -05:00
|
|
|
s = subprocess.Popen(["ssh", "-l", NEWSBLUR_USERNAME,
|
|
|
|
"-i", os.path.expanduser("/srv/secrets-newsblur/keys/docker.key"),
|
|
|
|
address, "%s %s" % (command, path)], stdout=subprocess.PIPE)
|
2015-06-01 17:50:06 -07:00
|
|
|
s.name = hostname
|
|
|
|
streams.append(s)
|
|
|
|
found.add(hostname)
|
|
|
|
|
2013-06-09 23:01:58 -04:00
|
|
|
def read_streams(streams):
|
|
|
|
while True:
|
|
|
|
r, _, _ = select.select(
|
|
|
|
[stream.stdout.fileno() for stream in streams], [], [])
|
|
|
|
for fileno in r:
|
|
|
|
for stream in streams:
|
|
|
|
if stream.stdout.fileno() != fileno:
|
|
|
|
continue
|
|
|
|
data = os.read(fileno, 4096)
|
|
|
|
if not data:
|
|
|
|
streams.remove(stream)
|
2013-03-24 22:31:46 -07:00
|
|
|
break
|
2016-11-02 22:07:10 -07:00
|
|
|
try:
|
2021-03-03 11:15:09 -05:00
|
|
|
combination_message = "[%-12s] %s" % (stream.name[:12], data.decode())
|
2016-11-02 22:07:10 -07:00
|
|
|
except UnicodeDecodeError:
|
|
|
|
continue
|
2013-06-09 23:01:58 -04:00
|
|
|
sys.stdout.write(combination_message)
|
2021-03-03 11:15:09 -05:00
|
|
|
sys.stdout.flush()
|
2013-06-09 23:01:58 -04:00
|
|
|
break
|
2013-03-24 22:31:46 -07:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2013-05-20 12:57:45 -07:00
|
|
|
main(*sys.argv[1:])
|