#!/usr/bin/env python
# Usage: devops logs [-h] [-r ROLE] {http,system}
# Summary: View logs on remote system

import sys
import os
import subprocess

pylib_dir = os.environ['_DEVOPS_ROOT'] + "/share/devops/"
sys.path.insert(0, pylib_dir)
import devop_config

CONFIG = devop_config.config()

def http_logs(role=None):
    log_files=('/var/log/apache2/access.log',
           '/var/log/apache2/error.log')

    for lf in log_files:
        devop_config.ssh_cmd('tail -n 50 {0}'.format(lf), role=role)


def system_logs(role=None):
    log_files=('/var/log/syslog',
           '/var/log/auth.log')

    for lf in log_files:
        devop_config.ssh_cmd('tail -n 50 {0}'.format(lf), role=role)


def main():
    switcher = {
        'http': http_logs,
        'system' : system_logs,

    }

    parser = devop_config.devop_args()
    parser.add_argument('command', choices=switcher.keys())
    args = parser.parse_args()

    if args.role is not None:
        role=args.role
    else:
        role=CONFIG.get('defaults','role')

    func = switcher.get(args.command, lambda: "nothing")
    func(role=role)



if __name__ == "__main__":
	main()
