Completed
Push — master ( 091708...b877bb )
by Ryan
01:13
created

cli()   F

Complexity

Conditions 9

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
c 3
b 0
f 0
dl 0
loc 33
rs 3
1
#!/usr/bin/env python
2
3
import click
4
import yaml
5
import json
6
import logging
7
import os
8
import sys
9
from config import Config, create_config
10
11
12
@click.group()
13
@click.option("-c", "--configfile", type=str, help="path to yaml config file")
14
@click.option("-d", "--debug", is_flag=True, default=False, help="debug mode")
15
@click.option("-S", "--simulate", is_flag=True, default=False, help="take no actions")
16
@click.option("-v", "--verbose", default=0, count=True, help="verbose, can be given multiple times")
17
@click.pass_context
18
def cli(ctx, configfile, debug, simulate, verbose):
19
    config = ctx.obj["config"]
20
    if configfile:
21
        with open(configfile, "r") as f:
22
            config.update(yaml.safe_load(f))
23
    elif os.path.isfile(os.path.expanduser("~/.trdb.yaml")):
24
        with open(os.path.expanduser("~/.trdb.yaml"), "r") as f:
25
            config.update(yaml.safe_load(f))
26
    elif os.path.isfile("/etc/trdb/conf.yaml"):
27
        with open("/etc/trdb/conf.yaml", "r") as f:
28
            config.update(yaml.safe_load(f))
29
    else:
30
        create_config()
31
        print "wrote config out to ~/.trdb.yaml"
32
        sys.exit(1)
33
    config.verbose = verbose
34
    config.debug = debug
35
    config.simulate = simulate
36
    if config.debug:
37
        logging.basicConfig(level=logging.DEBUG)
38
        print "config:"
39
        print json.dumps(config, indent=2)
40
    elif verbose:
41
        lvl = logging.ERROR - (verbose * 10)
42
        logging.basicConfig(level=lvl)
43
    else:
44
        logging.basicConfig(level=logging.ERROR)
45
46
47
@click.command()
48
@click.option("-f", "--ips-file", type=str, help="read ips from file one per line")
49
@click.option("--mmdb", type=str, help="path to mmdb for isp lookup")
50
@click.pass_context
51
def server(ctx, ips_file, mmdb):
52
    config = ctx.obj["config"]
53
    config.update(config.pop("server", {}))
54
    if ips_file:
55
        config.ips_file = ips_file
56
    if mmdb:
57
        config.mmdb = mmdb
58
    from traceroutedb.server import run_server
59
    run_server(config)
60
61
62
@click.command()
63
@click.option("-f", "--ips-file", type=str, help="read ips from file one per line")
64
@click.option("-n", "--hostname", default=None, help="reported hostname of this machine (reporter)")
65
@click.option("-R", "--remote-ips", is_flag=True, help="Use ips pulled from server")
66
@click.option("-i", "--ip", multiple=True, type=str, help="dst ip for trace, can be given multiple times")
67
@click.option("-s", "--server_url", help="server to send traces to (http://$server_port)")
68
@click.option("-N", "--note", help="trace note")
69
@click.option("-P", "--procs", type=int, help="num procs")
70
@click.pass_context
71
def runner(ctx, ips_file, hostname, remote_ips, ip, server_url, note, procs):
72
    config = ctx.obj["config"]
73
    config.update(config.pop("runner", {}))
74
    if ips_file:
75
        config.ips_file = ips_file
76
    if hostname:
77
        config.hostname = hostname
78
    if remote_ips:
79
        config.remote_ips = remote_ips
80
    if ip:
81
        config.ips = ip
82
    if server_url:
83
        config.server_url = server_url
84
    if note:
85
        config.note = note
86
    if procs:
87
        config.procs = procs
88
    else:
89
        config.procs = 10
90
    from traceroutedb.runner import runner_entry
91
    runner_entry(config)
92
93
94
def cli_entry():
95
    cli.add_command(server)
96
    cli.add_command(runner)
97
    cli(obj={"config": Config()})
98
99
100
if __name__ == "__main__":
101
    cli_entry()
102