-
Notifications
You must be signed in to change notification settings - Fork 0
/
template-ops.py
1479 lines (1321 loc) · 66.1 KB
/
template-ops.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
ver = "2024-09-29.138"
"""
template-ops tool
Copyright (c) 2024, Juniper Networks, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# on-box operation check
try:
from junos import Junos_Context
import jcs
except:
import sys
sys.dont_write_bytecode = True
import syslog
onbox = False
else:
onbox = True
import argparse
import os
if onbox:
import sys
import traceback
import jinja2
import re
import warnings
import pathlib
import hashlib
import shutil
from threading import Thread
from datetime import datetime
from jnpr.junos import Device
from jnpr.junos.exception import ConfigLoadError, CommitError
from jnpr.junos.utils.config import Config
from time import sleep
import template_ops_conf as template_ops_conf
# default commit timeout
COMMIT_TIMEOUT = 30
from template_ops_conf import *
from template_ops_vars import *
# reduce traceback verbosity
# sys.tracebacklimit=0
if onbox:
try:
USER = Junos_Context["user-context"]["login-name"]
except:
# may happen during event-options execution (no interactive execution)
USER = Junos_Context["user-context"]["user"]
else:
USER = os.getlogin()
# for purposes of template-ops detection from within template
global TEMPLATE_OPS
TEMPLATE_OPS = True
PID = str(os.getpid())
script = "[{user}]{path}[{pid}]".format(
path=os.path.abspath(sys.argv[0]), user=USER, pid=PID
)
threads = []
warnings.filterwarnings(action="ignore", module=".*paramiko.*")
template_thread_data = []
# variable for returning header from exec template
header = "default"
# variables from exec templates
result = "use global result and optionally header variable in exec() template"
# for purposes of passing non-string between templates in mprofile
result_adv = None
TEMPLATE_DATA_HEADER_DEFAULT = [["device", "template operation output"]]
# TEMPLATE_DATA_HEADER can be modified by templates
TEMPLATE_DATA_HEADER = [["device", "template operation output"]]
LIST_PROFILE_HEADER = [["#", "push profile", "comment"]]
LIST_MPROFILE_HEADER = [["#", "multi push profile", "comment"]]
LIST_TEMPLATE_HEADER = [["#", TEMPLATE_SEARCH_PATH + "/*.j2", "md5"]]
SHOW_PROFILE_HEADER = [
[
"profile",
"device",
"template-vars",
"template",
"input",
"exec",
"eph-instance:fmt",
"archival override",
]
]
SHOW_MPROFILE_HEADER = [
[
"multi profile",
"profile",
"pre-delay[s]",
"post-delay[s]",
]
]
def emit_info(msg, use_stdout=True, use_syslog=True, use_snmp=False):
if use_stdout:
print(msg)
if onbox:
if use_syslog:
jcs.syslog("14", "{script} {msg}".format(script=script, msg=msg))
# in case of jcs.syslog reliablity issues use logger instead
# os.system("logger -p user.info {script} {msg}".format(script=script, msg=msg))
if use_snmp:
dev.rpc.request_snmp_spoof_trap(
trap="jnxEventTrap",
variable_bindings="jnxEventTrapDescr = {script}, jnxEventAvAttribute = {msg}".format(
script=script, msg=msg.replace(" ", "_")
),
)
elif use_syslog:
syslog.openlog(facility=syslog.LOG_INFO)
syslog.syslog(syslog.LOG_INFO, "{script} {msg}".format(script=script, msg=msg))
def print_help():
"""Print help"""
if onbox:
help_msg = """
template-ops is a simple yet powerful tool for rendering and uploading Jinja2 templates,
designed for operations with MX/PTX-SRX scale-out, but with general use-case in mind.
arguments:
template-vars pointer to variable gen function in template_ops_vars.py, {TEMPLATE_VARS_STR}
template name of j2 template receiving variables from template-vars
input template seeding data, e.g., sequence number 1-n (or data enclosed in " ")
diff-target device name to retrieve diff between candidate and running config
push-target device name for config template push
exec-target device name for processing template as Python code
profile push to multiple SRX devices using profile [profile-name|# from list]
mprofile multi-profile execution defined by profile [mprofile-name|# from list]
list-profile list push target profiles from template_ops_conf.py (any argument)
list-mprofile list multi-profiles from template_ops_conf.py (any argument)
show-profile show details of push target profile [all|profile-name|# from list]
show-mprofile show multi-profile details [all|mprofile-name|# from list]
list-template list available Jinja2 template files (any argument)
show-template show contents of specific template [template-name|# from list ]
debug [on] enable backtraces to stdout (SYSLOG by default)
template-vars, input and template must be used together, push/diff-target is optional
profile and optionally input (to override config profile) are used as the only arguments for bulk operation
The idea is to preview the template first in a form of set commands:
> op template-ops template-vars srx4600 template srx4600-local-01 input 10
Then optionally diff the configuration against running config:
> op template-ops template-vars srx4600 template srx4600-local-01 input 10 diff-target srx-10
Finaly push to single device and validate:
> op template-ops template-vars srx4600 template srx4600-local-01 input 10 push-target srx-10
where srx-10 is defined along with profile(s) below in template_ops_conf.py
Finally, to upload proven template to multiple devices:
> op template-ops profile p1
Templates are located in {TEMPLATE_SEARCH_PATH} folder.
Both profiles and templates can be listed and viewed using corresponding list and show commands.
version {ver}
""".format(
TEMPLATE_VARS_STR=TEMPLATE_VARS_STR,
TEMPLATE_SEARCH_PATH=TEMPLATE_SEARCH_PATH,
ver=ver,
)
else:
help_msg = """
template-ops is a simple yet powerful tool for rendering and uploading Jinja2 templates,
designed for operations with MX/PTX-SRX scale-out, but with general use-case in mind.
arguments:
--template-vars pointer to variable gen function in template_ops_vars.py, {TEMPLATE_VARS_STR}
--template name of j2 template receiving variables from --template-vars
--input template seeding data, e.g., sequence number 1-n (or data enclosed in " ")
--diff-target device name to retrieve diff between candidate and running config
--push-target device name for config template push
--exec-target device name for processing template as Python code
--profile push to multiple SRX devices using profile [profile-name|# from list]
--mprofile multi-profile execution defined by profile [mprofile-name|# from list]
--list-profile list push target profiles from template_ops_conf.py
--list-mprofile list multi-profiles from template_ops_conf.py
--show-profile show details of push target profile [all|profile-name|# from list]
--show-mprofile show multi-profile details [all|mprofile-name|# from list]
--list-template list available Jinja2 template files
--show-template show contents of specific template [template-name|# from list ]
--debug [on] enable backtraces to stdout (SYSLOG by default)
template-vars, input and template must be used together, push/diff-target is optional
profile and optionally input (to override config profile) are used as the only arguments for bulk operation
The idea is to preview the template first in a form of set commands:
template-ops --template-vars srx4600 --template srx4600-local-01 --input 10
Then optionally diff the configuration against running config:
template-ops --template-vars srx4600 --template srx4600-local-01 --input 10 --diff-target srx-10
Finaly push to single device and validate:
template-ops --template-vars srx4600 --template srx4600-local-01 --input 10 --push-target srx-10
where srx-10 is defined along with profile(s) below in template_ops_conf.py
Finally, to upload proven template to multiple devices:
template-ops --profile p1
Templates are located in {TEMPLATE_SEARCH_PATH} folder.
Both profiles and templates can be listed and viewed using corresponding list and show commands.
version {ver}
""".format(
TEMPLATE_VARS_STR=TEMPLATE_VARS_STR,
TEMPLATE_SEARCH_PATH=TEMPLATE_SEARCH_PATH,
ver=ver,
)
print(help_msg)
def file_md5(path):
try:
with open(path, "rb") as f:
file_hash = hashlib.md5()
chunk = f.read(8192)
while chunk:
file_hash.update(chunk)
chunk = f.read(8192)
return file_hash.hexdigest()
except Exception:
traceback_msg = str(traceback.format_exc())
emit_info(
"error calculating md5, traceback: {traceback_msg}".format(
traceback_msg=traceback_msg
),
debug,
)
return "error calculating md5"
def parse_args():
"""Parse arguments"""
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--template")
parser.add_argument("--template-vars", dest="template_vars")
parser.add_argument("--input")
parser.add_argument("--push-target", dest="push_target")
parser.add_argument("--exec-target", dest="exec_target")
parser.add_argument("--diff-target", dest="diff_target")
parser.add_argument("--profile", dest="profile")
parser.add_argument("--mprofile", dest="mprofile")
parser.add_argument("--debug", default=False)
parser.add_argument("--list-profile", nargs="?", const="all", dest="list_profile")
parser.add_argument("--list-mprofile", nargs="?", const="all", dest="list_mprofile")
parser.add_argument("--list-template", nargs="?", const="all", dest="list_template")
parser.add_argument("--show-profile", dest="show_profile")
parser.add_argument("--show-mprofile", dest="show_mprofile")
parser.add_argument("--show-template", dest="show_template")
parser.add_argument("--eph-instance", dest="eph_instance")
parsed_args = parser.parse_args()
return parsed_args
def list_profile(arg):
try:
table_width = 95
print("-" * table_width)
for row in LIST_PROFILE_HEADER:
print("| {:>3} | {:^21} | {:^60} |".format(*row))
print("-" * table_width)
for row_nr, (profile, profile_detail) in enumerate(
template_ops_conf.push_profiles.items(), start=1
):
print(
"| {:>3} | {:>21} | {:>60} |".format(
row_nr, profile, profile_detail["comment"][0]
)
)
print("-" * table_width)
except Exception:
traceback_msg = str(traceback.format_exc())
emit_info(
"error listing profiles, traceback: {traceback_msg}".format(
traceback_msg=traceback_msg
),
debug,
)
emit_info("error listing profiles, use debug on/see log", not debug, not debug)
def show_profile(arg):
by_index = True
try:
if int(arg) > 0:
index = int(arg)
for row_nr, (profile, profile_detail) in enumerate(
template_ops_conf.push_profiles.items(), start=1
):
if index == row_nr:
arg_profile = profile
break
except:
by_index = False
arg_profile = arg
finally:
try:
if arg_profile in template_ops_conf.push_profiles or arg_profile == "all":
profile_data = []
for profile in template_ops_conf.push_profiles:
if profile == arg_profile or arg_profile == "all":
profile_dict = getattr(template_ops_conf, profile)
for profile_dev in profile_dict.keys():
# merge with default profile, pre-python 3.9 style
if "default" in profile_dict.keys():
profile_dev_detail = {
**profile_dict["default"],
**profile_dict[profile_dev],
}
else:
profile_dev_detail = profile_dict[profile_dev]
# build a list with profile settings for printing
if not "default" in profile_dev:
profile_data.append(
[
profile,
profile_dev,
profile_dev_detail["template_vars"][0],
profile_dev_detail["template"][0],
profile_dev_detail["input"][0],
# Y if explicit True, N for explicit False and no setting
"Y"
if bool(
profile_dev_detail.get("exec", [False])[0]
)
else "N",
# eph_instance:format if set, N when eph_instance is not set
f"{profile_dev_detail.get('eph_inst')[0]}:{profile_dev_detail.get('eph_inst')[1]}"
if bool(
profile_dev_detail.get("eph_inst", [False])[
0
]
)
else "N",
# save rendered, j2
f"rendered:{'Y' if profile_dev_detail.get('save_rendered_j2')[0] else 'N'}, J2:{'Y' if profile_dev_detail.get('save_rendered_j2')[1] else 'N'}"
if profile_dev_detail.get("save_rendered_j2")
else "N",
]
)
table_width = 168
for row in SHOW_PROFILE_HEADER:
if row[0] != "":
print("-" * table_width)
print(
"| {:^21} | {:^13} | {:^15} | {:^26} | {:^25} | {:>4} | {:^20} | {:^19} |".format(
*row
)
)
prev_profile = ""
for row in profile_data:
if row[0] == prev_profile:
row[0] = ""
if row[0] != "":
prev_profile = row[0]
print("-" * table_width)
print(
"| {:>21} | {:>13} | {:>15} | {:>26} | {:>25} | {:>4} | {:>20} | {:>19} |".format(
*row
)
)
print("-" * table_width)
else:
emit_info("Profile doesn't exist")
except Exception:
traceback_msg = str(traceback.format_exc())
emit_info(
"show-profile error, traceback: {traceback_msg}".format(
traceback_msg=traceback_msg
),
debug,
)
emit_info("show-profile error, use debug on/see log", not debug, not debug)
def show_mprofile(arg):
by_index = True
try:
if int(arg) > 0:
index = int(arg)
for row_nr, (mprofile, mprofile_detail) in enumerate(
template_ops_conf.multi_profiles.items(), start=1
):
if index == row_nr:
arg_mprofile = mprofile
break
except:
by_index = False
arg_mprofile = arg
finally:
try:
if (
arg_mprofile in template_ops_conf.multi_profiles
or arg_mprofile == "all"
):
mprofile_data = []
for mprofile in template_ops_conf.multi_profiles:
if mprofile == arg_mprofile or arg_mprofile == "all":
for push_profiles in template_ops_conf.multi_profiles[mprofile][
"push_profiles"
]:
for (
push_profile,
push_profile_settings,
) in push_profiles.items():
# build a list with mprofile settings for printing
mprofile_data.append(
[
mprofile,
push_profile,
# " " if 0 or empty, value for non-0 setting
""
if not bool(
push_profile_settings.get("pre-delay", 0)
)
else push_profile_settings.get("pre-delay"),
# " " if 0 or empty, value for non-0 setting
""
if not bool(
push_profile_settings.get("post-delay", 0)
)
else push_profile_settings.get("post-delay"),
]
)
table_width = 83
for row in SHOW_MPROFILE_HEADER:
if row[0] != "":
print("-" * table_width)
print("| {:^21} | {:^21} | {:^14} | {:^14} | ".format(*row))
prev_mprofile = ""
for row in mprofile_data:
if row[0] == prev_mprofile:
row[0] = ""
if row[0] != "":
prev_mprofile = row[0]
print("-" * table_width)
print("| {:>21} | {:>21} | {:>14} | {:>14} |".format(*row))
print("-" * table_width)
else:
emit_info("Multi-profile doesn't exist")
except Exception:
traceback_msg = str(traceback.format_exc())
emit_info(
"show-profile error, traceback: {traceback_msg}".format(
traceback_msg=traceback_msg
),
debug,
)
emit_info("show-mprofile error, use debug on/see log", not debug, not debug)
def list_mprofile(arg):
try:
table_width = 95
print("-" * table_width)
for row in LIST_MPROFILE_HEADER:
print("| {:>3} | {:^21} | {:^60} |".format(*row))
print("-" * table_width)
for row_nr, (mprofile, mprofile_detail) in enumerate(
template_ops_conf.multi_profiles.items(), start=1
):
print(
"| {:>3} | {:>21} | {:>60} |".format(
row_nr, mprofile, mprofile_detail["comment"][0]
)
)
print("-" * table_width)
except Exception:
traceback_msg = str(traceback.format_exc())
emit_info(
"error listing multi profiles, traceback: {traceback_msg}".format(
traceback_msg=traceback_msg
),
debug,
)
emit_info(
"error listing multi profiles, use debug on/see log", not debug, not debug
)
def list_template(return_only):
try:
template_files = [f for f in pathlib.Path(TEMPLATE_SEARCH_PATH).glob("*.j2")]
template_files.sort()
if not return_only:
table_width = 87
print("-" * table_width)
for row in LIST_TEMPLATE_HEADER:
print("| {:>3} | {:^40} | {:^34} |".format(*row))
print("-" * table_width)
for row_nr, row in enumerate(template_files, start=1):
print(
"| {:>3} | {:>40} | {:>34} |".format(
row_nr, str(row.stem), file_md5(row)
)
)
print("-" * table_width)
# show-template use
else:
return template_files
except Exception:
traceback_msg = str(traceback.format_exc())
emit_info(
"error listing templates, traceback: {traceback_msg}".format(
traceback_msg=traceback_msg
),
debug,
)
emit_info("Error listing templates, use debug on/see log", not debug, not debug)
def show_template(arg):
by_index = True
templates_path_file = list_template(True)
templates_file = [template.stem for template in templates_path_file]
try:
if int(arg) > 0:
index = int(arg) - 1
arg_template = templates_file[index]
except:
by_index = False
arg_template = arg
finally:
if not by_index and arg_template in templates_file:
index = templates_file.index(arg_template)
try:
with open(templates_path_file[index], "r") as f:
print(
"#\n####################################### BEGIN {} #######################################\n#".format(
templates_file[index]
)
)
print(f.read())
print(
"#\n####################################### END {} ########################################\n#".format(
templates_file[index]
)
)
except Exception:
traceback_msg = str(traceback.format_exc())
emit_info(
"error opening template, traceback: {traceback_msg}".format(
traceback_msg=traceback_msg
),
debug,
)
emit_info(
"Error opening template, use debug on/see log", not debug, not debug
)
def template_thread(parsed_args, profile_operation, profile, device="", timestamp=""):
def eph_settings(eph_param):
# function to set data type for load to eph, set is default, json/xml/text are loaded with overwrite = True to overcome slow delete in eph
if eph_param == None:
return None, "set", False
eph_instance = eph_param[0]
eph_conf_type = eph_param[1]
if eph_conf_type in ["json", "xml", "text"]:
return eph_instance, eph_conf_type, True
return eph_instance, "set", False
# sets eph paramaters for CLI param, it is called during profile operation too
eph_instance, eph_conf_type, eph_load_overwrite = eph_settings(
parsed_args.eph_instance
)
if parsed_args.exec_target:
exec_template = True
else:
exec_template = False
templateLoader = jinja2.FileSystemLoader(searchpath=TEMPLATE_SEARCH_PATH)
templateEnv = jinja2.Environment(loader=templateLoader)
diff_only = False
init_error = False
template_file = ""
status = ""
# read archival constants, setting may be overriden in profile operations
save_exec_py_enable = SAVE_EXEC_PY_ENABLE
save_exec_j2_enable = SAVE_EXEC_J2_ENABLE
save_commit_cfg_enable = SAVE_COMMIT_CFG_ENABLE
save_commit_j2_enable = SAVE_COMMIT_J2_ENABLE
# either diff or push to single device (or set render)
if not profile_operation:
template_file = parsed_args.template + ".j2"
_input = parsed_args.input
template_vars = parsed_args.template_vars
# diff
if parsed_args.diff_target:
diff_only = True
push_target = parsed_args.diff_target
# push or no diff/push
else:
if parsed_args.push_target:
push_target = parsed_args.push_target
elif parsed_args.exec_target:
push_target = parsed_args.exec_target
# neither diff/push - set rendering
else:
push_target = "N/A"
# profile operation
else:
try:
push_target = device
profile_dict = getattr(template_ops_conf, profile)
# merge with default profile, pre-python 3.9 style
if "default" in profile_dict.keys():
profile_dev = {**profile_dict["default"], **profile_dict[device]}
else:
profile_dev = profile_dict[device]
template_file = profile_dev["template"][0] + ".j2"
template_vars = profile_dev["template_vars"][0]
# profile input override
if parsed_args.input:
_input = parsed_args.input
else:
_input = profile_dev["input"][0]
exec_template = True if bool(profile_dev.get("exec", [False])[0]) else False
eph_instance, eph_conf_type, eph_load_overwrite = eph_settings(
profile_dev.get("eph_inst", None)
)
# override template render and j2 save default archival settings
if "save_rendered_j2" in profile_dev.keys():
save_exec_py_enable = profile_dev.get("save_rendered_j2")[0]
save_exec_j2_enable = profile_dev.get("save_rendered_j2")[1]
save_commit_cfg_enable = save_exec_py_enable
save_commit_j2_enable = save_exec_j2_enable
except Exception:
init_error = True
traceback_msg = str(traceback.format_exc())
emit_info(
"error reading device profile, traceback: {traceback_msg}".format(
traceback_msg=traceback_msg
),
debug,
)
status = "Error reading device profile, use debug on/see log"
template_thread_data.append([push_target, status])
template_abs = TEMPLATE_SEARCH_PATH + "/" + template_file
# missing/inaccesible file for both single/profile operation
if not os.path.isfile(template_abs) and not init_error:
status = (
"Error opening template file {template_file}, file doesn't exist".format(
template_file=template_file.replace(".j2", "")
)
)
template_thread_data.append([push_target, status])
emit_info(status, False)
# proceed if there is no init error recorded above
elif not init_error:
try:
template = templateEnv.get_template(template_file)
template_md5 = file_md5(template_abs)
template_vars_for_render = template_vars_get(template_vars, _input)
template_output = template.render(template_vars_for_render)
except Exception:
traceback_msg = str(traceback.format_exc())
emit_info(
"{push_target} error rendering template {template_file}, traceback: {traceback_msg}".format(
push_target=push_target,
template_file=template_file,
traceback_msg=traceback_msg,
),
debug,
)
status = (
"Error rendering template {template_file}, use debug on/see log".format(
template_file=template_file.replace(".j2", "")
)
)
template_thread_data.append([push_target, status])
# no exception during template rendering
else:
# determine if local on-box mode without SSH
if push_target in ["local", "localhost"]:
local_onbox_ops = True
else:
local_onbox_ops = False
# diff/push(includes exec) operation (not set rendering) to eligible template vars
if push_target != "N/A" and template_vars in DIFF_PUSH_ELIGIBLE_LIST:
try:
if not local_onbox_ops:
# merge with default profile, pre-python 3.9 style
if "default" in template_ops_conf.auth_profiles:
netconf_param = {
**template_ops_conf.auth_profiles["default"],
**template_ops_conf.auth_profiles[push_target],
}
else:
netconf_param = template_ops_conf.auth_profiles[push_target]
except Exception:
traceback_msg = str(traceback.format_exc())
emit_info(
"{push_target} netconf config lookup error {template_file}, traceback: {traceback_msg}".format(
push_target=push_target,
template_file=template_abs,
traceback_msg=traceback_msg,
),
debug,
)
template_thread_data.append(
[push_target, "netconf config lookup error"]
)
# no netconf lookup error
else:
# localhost operation, no SSH
if local_onbox_ops:
dev = Device(gather_facts=False)
# SSH operation
else:
dev = Device(
user=netconf_param["user"][0],
host=netconf_param["host"][0],
port=netconf_param["port"][0],
ssh_private_key_file=netconf_param["ssh_key"][0],
gather_facts=False,
)
try:
# probe doesn't work with local operation, only SSH
if local_onbox_ops:
dev.open()
else:
dev.open(auto_probe=2)
except Exception:
traceback_msg = str(traceback.format_exc())
emit_info(
"{push_target} error connecting to the device, {traceback_msg}".format(
push_target=push_target,
traceback_msg=traceback_msg,
),
debug,
)
template_thread_data.append(
[push_target, "error connecting to the device"]
)
# device connection OK
else:
# config operation, no exec()
if not exec_template:
if not diff_only:
emit_info(
"{push_target} template push start {template_file} (md5: {md5})".format(
push_target=push_target,
template_file=template_abs,
md5=template_md5,
),
False,
)
else:
emit_info(
"{push_target} template diff start {template_file} (md5: {md5})".format(
push_target=push_target,
template_file=template_abs,
md5=template_md5,
),
False,
)
set_cmd = template_output
no_exception = False
removed_del_cmds = False
try:
def load_or_diff(eph=False):
if eph:
# overwrite with json/xml/text
cu.load(
set_cmd,
format=eph_conf_type,
overwrite=eph_load_overwrite,
)
else:
cu.load(set_cmd, format="set")
# can't happen with ephemeral, parameter check prevents that
if diff_only:
diff = cu.diff()
cu.rollback()
return diff
else:
cu.commit(timeout=COMMIT_TIMEOUT)
if eph_instance is not None:
with Config(
dev,
mode="ephemeral",
ephemeral_instance=eph_instance,
) as cu:
diff = load_or_diff(eph=True)
else:
with Config(dev) as cu:
diff = load_or_diff()
# handle diff/push differently by removing delete which may not be present, if configured
except (ConfigLoadError, CommitError):
if eph_instance is not None:
cu.rollback()
# check if removing delete commands is configured for diff/push re-try
if REMOVE_DEL_CMDS_DURING_PUSH_DIFF_ERR_ENABLE:
try:
# items for delete might not be present, remove and commit without
# ^delete pattern is not used as that doesn't apply for multiple lines!!
set_cmd = re.sub(r"delete\ .*\n", "", set_cmd)
# unless repeated re-open of ephemeral, commit is to regular config
# TBD re-factor repeated code, handle exception in load_or_diff function
if eph_instance is not None:
with Config(
dev,
mode="ephemeral",
ephemeral_instance=eph_instance,
) as cu:
diff = load_or_diff(eph=True)
else:
with Config(dev) as cu:
diff = load_or_diff()
# for purposes of console and log message
removed_del_cmds = True
# neither regular/without delete push/diff passed
except Exception:
cu.rollback()
traceback_msg = str(traceback.format_exc())
if diff_only:
template_thread_data.append(
[
push_target,
"error during template diff (-del cmds), rollback..., use debug on/see log",
]
)
emit_info(
"{push_target} error during template diff (-del cmds), rollback..., {template_file} (md5: {md5}), traceback: {traceback_msg}".format(
push_target=push_target,
template_file=template_abs,
md5=template_md5,
traceback_msg=traceback_msg,
),
debug,
)
else:
template_thread_data.append(
[
push_target,
"error during template commit (-del cmds), rollback..., use debug on/see log",
]
)
emit_info(
"{push_target} error during template commit (-del cmds), rollback..., {template_file} (md5: {md5}), traceback: {traceback_msg}".format(
push_target=push_target,
template_file=template_abs,
md5=template_md5,
traceback_msg=traceback_msg,
),
debug,
)
# no exception during push/diff without del cmds
else:
no_exception = True
# exception and REMOVE_DEL_CMDS_DURING_PUSH_DIFF_ERR_ENABLE = 0
else:
traceback_msg = str(traceback.format_exc())
if diff_only:
template_thread_data.append(
[
push_target,
"error during template diff, rollback..., use debug on/see log",
]
)
emit_info(
"{push_target} error during template diff, rollback..., {template_file} (md5: {md5}), traceback: {traceback_msg}".format(
push_target=push_target,
template_file=template_abs,
md5=template_md5,
traceback_msg=traceback_msg,
),
debug,
)
else:
template_thread_data.append(
[
push_target,
"error during template commit, rollback..., use debug on/see log",
]
)
emit_info(
"{push_target} error during template commit, rollback..., {template_file} (md5: {md5}), traceback: {traceback_msg}".format(
push_target=push_target,
template_file=template_abs,
md5=template_md5,
traceback_msg=traceback_msg,
),
debug,
)
# no exception during regular push/diff
else:
no_exception = True
if no_exception:
# log if there was diff
if diff_only and not (diff is None):
template_thread_data.append(["diff", diff])
emit_info(
"{push_target} diff exist between candidate and current config {template_file} (md5: {md5})".format(
push_target=push_target,
template_file=template_abs,
md5=template_md5,
),
False,
)
# log if there was no diff
elif diff_only:
if removed_del_cmds:
template_thread_data.append(
[
push_target,
"No diff between candidate and current config (after removing delete cmds)",
]
)
emit_info(
"{push_target} no diff between candidate and current config (after removing delete cmds) {template_file} (md5: {md5})".format(
push_target=push_target,
template_file=template_abs,