2013-03-24 22:31:46 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import select
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2013-05-20 15:52:11 -07:00
|
|
|
sys.path.insert(0, '/srv/newsblur')
|
2013-05-20 12:57:45 -07:00
|
|
|
|
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
|
2013-05-20 15:52:11 -07:00
|
|
|
import fabfile
|
2013-03-24 22:31:46 -07:00
|
|
|
|
|
|
|
IGNORE_HOSTS = [
|
|
|
|
'push',
|
|
|
|
]
|
|
|
|
|
2013-06-09 02:45:17 -04:00
|
|
|
def main(role="app", role2="app", command=None, path=None):
|
2013-03-24 22:31:46 -07:00
|
|
|
streams = list()
|
2013-04-15 17:59:06 -07:00
|
|
|
if not path:
|
|
|
|
path = "/srv/newsblur/logs/newsblur.log"
|
|
|
|
if not command:
|
|
|
|
command = "tail -f"
|
2013-05-20 12:57:45 -07:00
|
|
|
|
2013-06-09 02:45:17 -04:00
|
|
|
hosts = fabfile.do(split=True)
|
|
|
|
found = set()
|
|
|
|
for hostname in (hosts[role] + hosts[role2]):
|
|
|
|
if isinstance(hostname, dict):
|
|
|
|
address = hostname['address']
|
|
|
|
hostname = hostname['name']
|
|
|
|
elif ':' in hostname:
|
2013-03-24 22:31:46 -07:00
|
|
|
hostname, address = hostname.split(':', 1)
|
2013-05-20 15:52:11 -07:00
|
|
|
elif isinstance(hostname, tuple):
|
|
|
|
hostname, address = hostname[0], hostname[1]
|
2013-03-24 22:31:46 -07:00
|
|
|
else:
|
|
|
|
address = hostname
|
2013-05-20 15:52:11 -07:00
|
|
|
if any(h in hostname for h in IGNORE_HOSTS): continue
|
2013-06-09 02:45:17 -04:00
|
|
|
if hostname in found: continue
|
2013-03-24 22:31:46 -07:00
|
|
|
if 'ec2' in hostname:
|
|
|
|
s = subprocess.Popen(["ssh", "-i", os.path.expanduser("~/.ec2/sclay.pem"),
|
2013-04-15 17:59:06 -07:00
|
|
|
address, "%s %s" % (command, path)], stdout=subprocess.PIPE)
|
2013-03-24 22:31:46 -07:00
|
|
|
else:
|
2013-04-15 17:59:06 -07:00
|
|
|
s = subprocess.Popen(["ssh", address, "%s %s" % (command, path)], stdout=subprocess.PIPE)
|
2013-03-24 22:31:46 -07:00
|
|
|
s.name = hostname
|
|
|
|
streams.append(s)
|
2013-06-09 02:45:17 -04:00
|
|
|
found.add(hostname)
|
2013-03-24 22:31:46 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
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)
|
|
|
|
break
|
|
|
|
combination_message = "[%-6s] %s" % (stream.name[:6], data)
|
|
|
|
sys.stdout.write(combination_message)
|
|
|
|
break
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print " --- End of Logging ---"
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2013-05-20 12:57:45 -07:00
|
|
|
main(*sys.argv[1:])
|