forked from cooperlees/peerme
/
peerme.py
executable file
·121 lines (103 loc) · 3.3 KB
/
peerme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
'''
Discover and Generate IX Peerings
'''
import asyncio
import click
import logging
import sys
import time
from os.path import expanduser
from peerme import config as peerme_config
from peerme import peeringdb_mysql
from peerme import peeringdb_api
from peerme import euroix_json
from peerme.commands.generate import GenerateConfigCli
from peerme.commands.discover import DiscoverCli
from peerme.commands.request import RequestCli
CLICK_CONTEXT_SETTINGS = {'help_option_names': ('-h', '--help')}
class Options():
''' Object for holding shared object between subcommands '''
def __init__(self, debug, start_time, db, loop, config):
self.config = config
self.db = db
self.debug = debug
self.loop = loop
self.start_time = start_time
self.human_start_time = time.strftime('%Y%m%d%H%M%S',
time.gmtime(start_time))
logging.debug("{} started @ {}".format(
sys.argv[0],
self.human_start_time,
))
def __repr__(self):
''' String Representation for debugging '''
return 'CLI stated @ {} - Debug is {}'.format(
self.debug,
self.human_start_time
)
def _handle_debug(ctx, param, debug):
'''Turn on debugging if asked otherwise INFO default'''
log_level = logging.DEBUG if debug else logging.INFO
logging.basicConfig(
format=(
'[%(asctime)s] %(levelname)s: %(message)s (%(filename)s:%(lineno)d)'
),
level=log_level
)
return debug
@click.group(context_settings=CLICK_CONTEXT_SETTINGS)
@click.option(
'-c',
'--config',
default='{}/.peerme.conf'.format(expanduser('~')),
help='Config File Location - Default: ~/.peerme.conf',
)
@click.option(
'-d',
'--debug',
is_flag=True,
help='Turn on verbose logging',
callback=_handle_debug,
)
@click.option(
'--refresh-data',
is_flag=True,
help='Fetch fresh data from Internet sources (EuroIX only)',
)
@click.option(
'-s',
'--data-source',
help='Choose Peering datasource (pdbapi [default], pdbsql, euroix)',
default='pdbapi'
)
@click.pass_context
def main(ctx, config, debug, data_source, refresh_data):
'''
Discover and generate potential peering endpoints @ IXs
TODO: Support API calls in ther future as well as a local DB
'''
loop = asyncio.get_event_loop()
config_obj = peerme_config.PeermeConfig(config)
if data_source == 'pdbsql':
peering_api = peeringdb_mysql.PeermeDb(config_obj, loop)
elif data_source == 'pdbapi':
peering_api = peeringdb_api.PeermeDb(config_obj, loop)
elif data_source == 'euroix':
peering_api = euroix_json.PeermeDb(
config_obj, refresh_data, loop
)
else:
raise Exception('Invalid option "{}" for data source.'.format(
data_source))
# Class to hold all shared options
ctx.obj = Options(debug, time.time(), peering_api, loop, config_obj)
def add_internal_modules():
''' Add internal modules to main parser '''
main.add_command(DiscoverCli().discover)
main.add_command(GenerateConfigCli().generate)
main.add_command(RequestCli().pinder)
if __name__ == '__main__':
# Add subcommands
add_internal_modules()
sys.exit(main())