2013-04-15 18:32:13 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
2014-11-11 22:45:32 -08:00
|
|
|
import time
|
2013-04-15 18:32:13 -07:00
|
|
|
import select
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2014-11-11 22:45:32 -08:00
|
|
|
from optparse import OptionParser
|
|
|
|
from requests.exceptions import ConnectionError
|
2013-04-15 18:32:13 -07:00
|
|
|
|
2014-11-11 22:45:32 -08:00
|
|
|
sys.path.insert(0, '/srv/newsblur')
|
2021-01-19 09:40:13 -05:00
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'newsblur_web.settings'
|
2014-11-11 22:45:32 -08:00
|
|
|
import fabfile
|
2013-04-15 18:32:13 -07:00
|
|
|
|
2014-11-11 22:45:32 -08:00
|
|
|
NEWSBLUR_USERNAME = 'sclay'
|
2013-04-15 18:32:13 -07:00
|
|
|
IGNORE_HOSTS = [
|
|
|
|
'push',
|
|
|
|
]
|
|
|
|
|
2016-08-15 16:27:17 -07:00
|
|
|
def main(role="app", role2="work", command=None):
|
2014-11-11 22:45:32 -08:00
|
|
|
delay = 1
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
2016-08-15 16:27:17 -07:00
|
|
|
streams = create_streams_for_roles(role, role2, command=command)
|
2020-06-19 02:27:48 -04:00
|
|
|
print(" --- Loading %s App Log Tails ---" % len(streams))
|
2014-11-11 22:45:32 -08:00
|
|
|
read_streams(streams)
|
2016-08-15 16:27:17 -07:00
|
|
|
except (UnicodeDecodeError, ConnectionError, KeyboardInterrupt):
|
2020-06-19 02:27:48 -04:00
|
|
|
print(" --- End of Logging ---")
|
2014-11-11 22:45:32 -08:00
|
|
|
break
|
|
|
|
|
2016-08-15 16:27:17 -07:00
|
|
|
def create_streams_for_roles(role, role2, command=None):
|
2013-04-15 18:32:13 -07:00
|
|
|
streams = list()
|
2016-02-09 16:34:59 -08:00
|
|
|
hosts = fabfile.assign_digitalocean_roledefs(split=True)
|
2014-11-11 22:45:32 -08:00
|
|
|
found = set()
|
|
|
|
|
2013-04-15 18:32:13 -07:00
|
|
|
if not command:
|
|
|
|
command = "tail -f"
|
2014-11-11 22:45:32 -08:00
|
|
|
for hostname in (hosts[role] + hosts[role2]):
|
|
|
|
if isinstance(hostname, dict):
|
|
|
|
address = hostname['address']
|
|
|
|
hostname = hostname['name']
|
|
|
|
elif ':' in hostname:
|
2013-04-15 18:32:13 -07:00
|
|
|
hostname, address = hostname.split(':', 1)
|
2014-11-11 22:45:32 -08:00
|
|
|
elif isinstance(hostname, tuple):
|
|
|
|
hostname, address = hostname[0], hostname[1]
|
2013-04-15 18:32:13 -07:00
|
|
|
else:
|
|
|
|
address = hostname
|
2014-11-11 22:45:32 -08:00
|
|
|
if any(h in hostname for h in IGNORE_HOSTS): continue
|
|
|
|
if hostname in found: continue
|
2013-04-15 18:32:13 -07:00
|
|
|
if 'ec2' in hostname:
|
2014-11-11 22:45:32 -08:00
|
|
|
s = subprocess.Popen(["ssh",
|
|
|
|
"-i", os.path.expanduser(os.path.join(fabfile.env.SECRETS_PATH,
|
|
|
|
"keys/ec2.pem")),
|
2016-08-15 16:27:17 -07:00
|
|
|
address, command], stdout=subprocess.PIPE)
|
2013-04-15 18:32:13 -07:00
|
|
|
else:
|
2014-11-11 22:45:32 -08:00
|
|
|
s = subprocess.Popen(["ssh", "-l", NEWSBLUR_USERNAME,
|
|
|
|
"-i", os.path.expanduser(os.path.join(fabfile.env.SECRETS_PATH,
|
|
|
|
"keys/newsblur.key")),
|
2016-08-15 16:27:17 -07:00
|
|
|
address, command], stdout=subprocess.PIPE)
|
2013-04-15 18:32:13 -07:00
|
|
|
s.name = hostname
|
|
|
|
streams.append(s)
|
2014-11-11 22:45:32 -08:00
|
|
|
found.add(hostname)
|
|
|
|
|
|
|
|
return streams
|
2013-04-15 18:32:13 -07:00
|
|
|
|
2014-11-11 22:45:32 -08: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-04-15 18:32:13 -07:00
|
|
|
break
|
2014-11-11 22:45:32 -08:00
|
|
|
combination_message = "[%-6s] %s" % (stream.name[:6], data)
|
|
|
|
sys.stdout.write(combination_message)
|
2013-04-15 18:32:13 -07:00
|
|
|
break
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("-f", "--find", dest="find")
|
|
|
|
parser.add_option("-p", "--path", dest="path")
|
2015-08-21 13:12:07 -07:00
|
|
|
parser.add_option("-r", "--role", dest="role")
|
2016-08-15 16:27:17 -07:00
|
|
|
parser.add_option("-e", "--exclude", dest="exclude")
|
2013-04-15 18:32:13 -07:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2016-08-15 16:27:17 -07:00
|
|
|
path = options.path or "/srv/newsblur/logs/newsblur.log"
|
2013-04-15 18:32:13 -07:00
|
|
|
find = options.find
|
2015-08-21 13:12:07 -07:00
|
|
|
role = options.role or 'app'
|
2016-08-15 16:27:17 -07:00
|
|
|
exclude = options.exclude or None
|
|
|
|
command = "zgrep \"%s\" %s" % (find, path)
|
|
|
|
if exclude:
|
|
|
|
command += " | zgrep -v \"%s\"" % exclude
|
2020-06-19 02:27:48 -04:00
|
|
|
print(command)
|
2016-08-15 16:27:17 -07:00
|
|
|
main(role=role, command=command)
|