This repository has been archived by the owner on Nov 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
executable file
·69 lines (61 loc) · 1.95 KB
/
main.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
#!/usr/bin/env python
# import helpers
from napalm_base import get_network_driver
from lib.helpers import build_help
from lib.helpers import configure_logging
from lib.helpers import parse_optional_args
from lib.device_info import get_device_information
from lib.device_info import build_json
from lib.validation import validate
from lib.output_html import main as output_html_main
from lib.output_md import main as output_md_main
from lib.output_text import main as output_text_main
import sys
import yaml
import json
import logging
import tabulate
logger = logging.getLogger('yact')
def get_file_content(config):
try:
with open(config, 'r') as f:
return yaml.load(f.read())
except IOError:
print "ERROR"
def get_devices_from_config(config, limit):
devices = {}
config = get_file_content(config)
for device in config.get('devices'):
temp = {}
if limit and device.get('hostname') == limit:
new_temp = {}
temp.update(config.get('credentials')[device['credentials']])
temp['driver'] = device['driver']
temp['ip'] = device['ip']
new_temp[device['hostname']] = temp
return new_temp
else:
temp.update(config.get('credentials')[device['credentials']])
temp['driver'] = device['driver']
temp['ip'] = device['ip']
devices[device['hostname']] = temp
return devices
def run(config, limit, scope, debug):
devices = get_devices_from_config(config, limit)
if scope == 'getters':
try:
build_json(devices)
output_html_main()
output_md_main()
output_text_main()
except:
raise
elif scope == 'validate':
validate(devices, debug)
def main():
args = build_help()
configure_logging(logger, args.debug)
run(args.config_file, args.limit, args.scope, args.debug)
sys.exit(0)
if __name__ == '__main__':
main()