-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
146 lines (118 loc) · 5.17 KB
/
app.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import json
import logging
import os
import sys
from collections import OrderedDict
from json import JSONDecodeError
from multiprocessing.pool import ThreadPool
from netmiko import Netmiko, NetMikoTimeoutException, NetMikoAuthenticationException
from tqdm import tqdm
logger = logging.getLogger(__name__)
g_log_dir_name = "log"
def init_logger():
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
s_handler = logging.StreamHandler(sys.stdout)
f_handler = logging.FileHandler(str(os.path.basename(__file__).split(".")[0]) + ".log")
s_handler.setFormatter(formatter)
f_handler.setFormatter(formatter)
logger.addHandler(s_handler)
logger.addHandler(f_handler)
if not os.path.exists(g_log_dir_name):
logger.debug("Creating {0} directory".format(g_log_dir_name))
os.mkdir(g_log_dir_name)
def get_worker_logger(f_name):
lgr = logging.getLogger(f_name)
lgr.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
f_handler = logging.FileHandler(os.path.join(g_log_dir_name, f_name + ".log"))
f_handler.setFormatter(formatter)
lgr.addHandler(f_handler)
return lgr
def parse_config():
conf_file = "config.json"
logger.info("Reading configuration file [{0}]".format(conf_file))
with open(conf_file, "r") as json_file:
return json.load(json_file, object_pairs_hook=OrderedDict)
def write_memory(conn):
if conn.is_alive():
logger.info("Saving configuration to memory")
logger.info(conn.send_command_expect('write memory'))
def execute_on_device_wrapper(args):
execute_on_device(*args)
def execute_on_device(lgr, global_cmd, k_device, v_device):
lgr.info("++++++++Connecting to [{0}] device++++++++".format(k_device))
try:
pass
with Netmiko(**v_device.get("conn")) as conn:
lgr.info("Device Promt: {0}".format(conn.find_prompt()))
if not conn.check_enable_mode():
lgr.info("Not in privileged mode, switching to enable")
conn.enable()
lgr.info("Promt: {0}".format(conn.find_prompt()))
if len(v_device.get("cmd")) > 0:
lgr.info("Executing local commands")
lgr.info(conn.send_config_set(v_device.get("cmd")))
if v_device.get("write_memory"):
write_memory(conn)
elif len(global_cmd) > 0:
lgr.info("Executing global commands")
lgr.info(conn.send_config_set(global_cmd))
if v_device.get("write_memory"):
write_memory(conn)
else:
lgr.warning("No global or local commands found for [{0}] device".format(k_device))
except ValueError as e:
lgr.error(e)
except (NetMikoTimeoutException, NetMikoAuthenticationException) as e:
lgr.error("Failed to connect to [{0}] device, reason: {1}".format(k_device, e))
lgr.info("++++++++Disconnected from [{0}] device++++++++".format(k_device))
def get_number_of_threads(conf_threads, number_of_devices):
rc = os.cpu_count()
if number_of_devices > rc:
rc *= 2
else:
rc = number_of_devices
if isinstance(conf_threads, str) and conf_threads == "auto":
logger.info("Using auto for multiprocessing setting")
elif isinstance(conf_threads, str):
try:
rc = int(conf_threads)
except ValueError:
logger.warning("Failed to convert {0} to int".format(conf_threads))
elif isinstance(conf_threads, int):
rc = conf_threads
else:
logger.warning("{0} unknown variable, using auto setting".format(threads))
logger.info("Initializing {0} workers".format(rc))
return rc
def run_config(conf):
is_multi = conf.get("options").get("use_multiprocessing")
logger.info("Running{0} automation according to config file".format(" multiprocess" if is_multi else ""))
if is_multi:
with ThreadPool(processes=get_number_of_threads(conf.get("options").get("threads"),
len(conf.get("devices")))) as workers_pool:
for _ in tqdm(workers_pool.imap_unordered(execute_on_device_wrapper, [
(get_worker_logger(k_device), conf.get("global_cmd"),
k_device, v_device) for k_device, v_device in conf.get("devices").items()]),
desc="Working on devices", unit="device", total=len(conf.get("devices"))):
pass
workers_pool.join()
else:
for k_device, v_device in conf.get("devices").items():
execute_on_device(get_worker_logger(k_device), conf.get("global_cmd"), k_device, v_device)
def main():
logger.info("---------------------------")
logger.info("Welcome to Network automation")
try:
conf = parse_config()
run_config(conf)
except FileNotFoundError as e:
logger.error(e)
except JSONDecodeError as e:
logger.error("Failed to parse config file: [{0}]".format(e))
logger.info("Finished execution, goodbye")
logger.info("---------------------------")
if __name__ == "__main__":
init_logger()
main()