Last active
December 28, 2015 03:49
-
-
Save danslimmon/7437681 to your computer and use it in GitHub Desktop.
Script that takes supervisor configs describing lumberjack commands and converts them to a new-style Go-based lumberjack config file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
"""Converts Lumberjack supervisor configs to the new-style JSON configs. | |
USAGE: upgrade_lumberjack_configs.py SUPERVISOR_CONF [SUPERVISOR CONF ...] | |
Assumptions: | |
* All your lumberjack commands use the same --host, --port, and | |
--ssl-ca-path arguments""" | |
import sys | |
import argparse | |
import json | |
SUPERVISOR_CONFIGS = sys.argv[1:] | |
def parse(command): | |
"""Parses a lumberjack invocation and returns an argparse result.""" | |
parser = argparse.ArgumentParser(prog='lumberjack') | |
parser.add_argument('log_file', nargs='+', metavar='LOG_FILE') | |
parser.add_argument('--field', action='append') | |
parser.add_argument('--ssl-ca-path') | |
parser.add_argument('--host') | |
parser.add_argument('--port') | |
parser.add_argument('--window-size') | |
return parser.parse_args(command.split()[1:]) | |
def lumb_network_params(invoc): | |
"""Returns the 'network' section of the new-style lumberjack config. | |
See assumptions in the global docstring.""" | |
servers = [ '{host}:{port}'.format(host=invoc.host, port=invoc.port) ] | |
ssl_ca = invoc.ssl_ca_path | |
return {'servers': servers, 'ssl ca': ssl_ca} | |
def lumb_files_stanza(invoc): | |
"""Converts an argparse result to a new-style lumberjack config "files" stanza. | |
Returns a dict of lumberjack config parameters.""" | |
rslt = {} | |
rslt['paths'] = invoc.log_file | |
rslt['fields'] = {} | |
for field_arg in invoc.field: | |
field_name, field_val = field_arg.split('=', 1) | |
rslt['fields'][field_name] = field_val | |
return rslt | |
# Get all the supervisor configs and make a list of the commands that they supervise | |
commands = [] | |
for sup_conf_path in SUPERVISOR_CONFIGS: | |
command_lines = [line.rstrip() for line in open(sup_conf_path).readlines() | |
if line.startswith('command=')] | |
commands.extend([line.split('=', 1)[1] for line in command_lines]) | |
# Filter down to just lumberjack commands | |
commands = [c for c in commands | |
if c.split()[0].endswith('/lumberjack') or c.split()[0].endswith('/lumberjack.sh')] | |
# Parse each invocation | |
parsed_invocs = [parse(c) for c in commands] | |
# Get the global config parameters from the first invocation | |
rslt = {} | |
rslt['network'] = lumb_network_params(parsed_invocs[0]) | |
# Convert each parsed invocation to a new-style lumberjack config "files" stanza | |
rslt['files'] = [lumb_files_stanza(invoc) for invoc in parsed_invocs] | |
# Print result | |
print json.dumps(rslt, indent=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment