Completed
Push — master ( c5f393...12467b )
by Ryan
01:17
created

cli()   A

Complexity

Conditions 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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