-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.py
executable file
·1040 lines (845 loc) · 40.1 KB
/
builder.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
import cmd
import csv
from backports.configparser import ConfigParser
import urllib3
import re
from ipaddress import ip_address
import os, ssl
import sys
import datetime
import xlrd
from itertools import chain
LOGGER = None
CVP = None
CONFIG = {}
TEMPLATES = {}
DEVICES = {}
COMPILE_FOR = []
ASSIGN_TO = []
HOST_TO_DEVICE = {}
SUPPLEMENT_FILES = {}
SPINES = []
DEBUG = False
class Log():
def __init__(self):
self.fabric_builder_log = open('fabric_builder_log.txt', 'a')
def log(self,string):
string = "{0}: {1}\n".format( datetime.datetime.now().strftime('%a %b %d %H:%M'), string )
sys.stderr.write(string)
self.fabric_builder_log.write(string)
class Cvp():
def __init__(self):
self.cvprac = None
self.containerTree = {}
self.CvpApiError = None
self.devices = {}
self.host_to_device = {}
self.containers = {}
self.configlets = {}
try:
from cvprac.cvp_client import CvpClient
from cvprac.cvp_client_errors import CvpClientError
from cvprac.cvp_client_errors import CvpApiError
self.CvpClientError = CvpClientError
self.CvpApiError = CvpApiError
self.cvprac = CvpClient()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # to supress the warnings for https
self.cvprac.connect([searchConfig('cvp_server')], searchConfig('cvp_user'), searchConfig('cvp_pass'))
LOGGER.log("Successfully authenticated to CVP")
except (ImportError, self.CvpClientError) as e:
LOGGER.log("Unable to Init CVP; forcing debug mode")
LOGGER.log("ERROR: {0}".format(e))
global DEBUG
DEBUG = True
def populate(self):
try:
LOGGER.log("-loading containers; please wait...")
self.containers = {item['name'].lower():item for item in self.cvprac.api.get_containers()['data']}
LOGGER.log("-loading configlets; please wait...")
self.configlets = {item['name'].lower():item for item in self.cvprac.api.get_configlets()['data']}
for name, cont in self.containers.items():
self.containerTree[name] = [_name for _name, _cont in self.containers.items() if _cont['parentName'] == cont['name']]
LOGGER.log("-loading devices; please wait...")
for device in self.cvprac.api.get_inventory():
sn = device['serialNumber'].lower()
host = device['hostname'].lower()
LOGGER.log("-loading {0} configlets; please wait...".format(host))
configlets = self.cvprac.api.get_configlets_by_device_id(device['systemMacAddress'])
device['configlets'] = {item['name'].lower():item for item in configlets}
self.devices[sn] = device
self.host_to_device[host] = self.devices[sn]
except:
LOGGER.log("Unable to connect to CVP Server")
sys.exit(0)
def getBySerial(self, sn):
return self.devices.get(sn.lower(), None)
def getByHostname(self, hostname):
return self.host_to_device.get(hostname.lower(), None)
def getContainerByName(self, name):
return self.containers.get(name.lower(), None)
def getContainerDevices(self, containerName, follow = False):
containerName = containerName.lower()
tree = [containerName] + self.containerTree[containerName] if follow else [containerName]
return [device for device in self.devices.values() if device['containerName'].lower() in tree]
def fetchDevices(self, search, follow_child_containers = False):
search = search if type(search) == list else [search]
devices = []
for _search in search:
try:
device = CVP.getBySerial(_search) or CVP.getByHostname(_search)
if device:
devices.append((device,))
continue
else:
devices.append(CVP.getContainerDevices(_search, follow_child_containers))
except KeyError as e:
LOGGER.log("Could not find {0}".format(_search))
return list(chain.from_iterable(devices))
def createConfiglet(self, configlet_name, configlet_content):
# Configlet doesn't exist let's create one
LOGGER.log("--creating configlet {0}; please wait...".format(configlet_name))
self.cvprac.api.add_configlet(configlet_name, configlet_content)
return self.cvprac.api.get_configlet_by_name(configlet_name)
def updateConfiglet(self, configlet, new_configlet_content):
# Configlet does exist, let's update the content only if not the same (avoid empty task)
configlet_name = configlet['name']
LOGGER.log("--found configlet {0}".format(configlet_name))
if configlet['config'] != new_configlet_content:
LOGGER.log("---updating configlet {0}; please wait...".format(configlet_name))
self.cvprac.api.update_configlet(new_configlet_content, configlet['key'], configlet_name)
return self.cvprac.api.get_configlet_by_name(configlet_name)
def deployDevice(self, device, container, configlets_to_deploy):
try:
ids = self.cvprac.api.deploy_device(device.cvp, container, configlets_to_deploy)
except self.CvpApiError as e:
LOGGER.log("---deploying device {0}: failed, could not get task id from CVP".format(device.hostname))
else:
ids = ','.join(map(str, ids['data']['taskIds']))
LOGGER.log("---deploying device {0}: {1} to {2} container".format(device.hostname, device.mgmt_ip, device.container))
LOGGER.log("---CREATED TASKS {0}".format(ids))
def applyConfiglets(self, to, configlets):
app_name = "CVP Configlet Builder"
to = to if type(to) == list else [to]
configlets = configlets if type(configlets) == list else [configlets]
toContainer = None
toDevice = None
#dest is a container, sn. or hostname string
for dest in to:
toContainer = self.getContainerByName(dest)
if toContainer:
LOGGER.log("---applying configlets to {0}; please wait...".format(toContainer.name))
_result = self.cvprac.api.apply_configlets_to_container(app_name, toContainer, configlets)
dest = toContainer
else:
#apply to device
toDevice = getBySerial(dest) or getByHostname(dest)
dest = toDevice.hostname
LOGGER.log("---applying configlets to {0}; please wait...".format(dest))
_result = self.cvprac.api.apply_configlets_to_device(app_name, toDevice.cvp, configlets) if toDevice else None
if not (toDevice or toContainer):
errorOn = [_conf['name'] for _conf in configlets]
LOGGER.log("---failed to push {0}; {1} not found".format(','.join(errorOn), dest))
elif _result and _result['data']['status'] == 'success':
LOGGER.log("---CREATED TASKS {0}".format(','.join(map(str, _result['data']['taskIds']))))
return None
class Task():
def __init__(self, device = None, template = None, mode = None):
self.device = device
self.template = template
self.singleton = True if template else False
self.mode = mode
#the task finally figures out what to assign and compile
def execute(self):
configlet_keys = []
apply_configlets = searchConfig('apply_configlets')
def pushToCvp():
container = searchSource('container', self.device)
if self.device.cvp['containerName'] == 'Undefined' and container:
CVP.deployDevice(self.device, container, configlet_keys)
elif self.device.cvp['containerName'] == 'Undefined' and not container:
LOGGER.log("---cannot deploy {0}; non-provisioned device with no destination container defined".format(self.device.hostname))
else:
CVP.applyConfiglets(self.device.sn, configlet_keys)
if self.singleton:
#deal with singletons
name = "{0}-{1}".format(self.template.injectSection, self.template.name)
name_lower = name.lower()
LOGGER.log('COMPILING {0}'.format(name))
new_configlet_content = self.template.compile({})
assign_to = searchConfig('assign_to', self.template.injectSection)
if not DEBUG:
print('-'*50)
LOGGER.log("EXECUTING TASKS FOR SINGLETON {0}".format(name))
print('-'*50)
else:
print('-'*50)
print('DEBUG SINGLETON OUTPUT: '+ name)
print('-'*50)
print("assign to: "+ ','.join(assign_to))
print('-'*50)
print(new_configlet_content)
return
exists = searchSource(name_lower, CVP.configlets, False)
if not exists:
configlet_keys.append(CVP.createConfiglet(name, new_configlet_content))
else:
CVP.updateConfiglet(exists, new_configlet_content)
if apply_configlets and assign_to and configlet_keys:
createdTasks = CVP.applyConfiglets(assign_to, configlet_keys) if assign_to else []
if createdTasks:
LOGGER.log("---successfully created tasks {0}".format(','.join(map(str, createdTasks))))
#DAY1 and DAY2 EXECUTION HAPPENS HERE
else:
if not DEBUG:
print('-'*50)
LOGGER.log("EXECUTING TASKS FOR DEVICE {0}/{1}".format(self.device.hostname, self.device.sn))
print('-'*50)
configlet_keys = []
for name, configlet in self.device.to_deploy:
#IF DEBUG IS ON THEN JUST PRINT TO SCREEN
if DEBUG:
print('-'*50)
print('DEBUG OUTPUT: '+ name)
print('-'*50)
print(configlet.compile(self.device))
continue
#ELSE DOES IT EXIST AND ASSIGNED?
name_lower = name.lower()
exists = searchSource(name_lower, CVP.configlets, False)
assigned = searchSource(name_lower, self.device.cvp['configlets'], False)
LOGGER.log('COMPILING {0}'.format(name))
new_configlet_content = configlet.compile(self.device)
if not exists:
configlet_keys.append(CVP.createConfiglet(name, new_configlet_content))
elif not assigned:
configlet_keys.append(CVP.updateConfiglet(exists, new_configlet_content))
else:
CVP.updateConfiglet(exists, new_configlet_content)
#DEVICES IN ASSIGN_TO ALWAYS FOLLOW CHILD CONTAINERS
if not DEBUG and apply_configlets and configlet_keys:
if self.mode == 2:
if ASSIGN_TO:
if self.device.cvp in ASSIGN_TO:
pushToCvp()
else:
pushToCvp()
elif self.mode == 1:
pushToCvp()
self.device.to_deploy = []
class Switch():
def __init__(self, params={}, cvpDevice={}, injectSection = None, implicitRole = None):
#list to hold leaf compiled spine underlay interface init
self.underlay_inject = []
self.to_deploy = []
self.injectSection = injectSection
self.cvp = None
for k, v in params.items():
setattr(self, k, str(v).replace("|",","))
self.hostname = searchSource('hostname', self) or searchSource('hostname', cvpDevice)
self.sn = searchSource('sn', self) or searchSource('serialNumber', cvpDevice)
if implicitRole:
self.role = implicitRole
elif not params and cvpDevice:
self.role = 'cvp_device'
if cvpDevice:
self.cvp = cvpDevice
LOGGER.log("Device init {0}, Role: {1}, Container: {2}, CVP found: ({3})".format(self.sn, self.role, self.cvp['containerName'], 'x'))
else:
LOGGER.log("Device init {0}, Role: {1}, CVP found: ({2})".format(self.sn, self.role, ''))
def searchConfig(self, key):
return searchConfig(key, self.injectSection)
def assign_configlet(self, template):
#TODO: MAKE HANDLE LIST LOOKUPS, RIGHT NOW ONLY WORKS FOR ONE CONTAINER OR ONE DEVICE i.e. USELESS
exception = getattr(template, "skip_container", None)
if exception == self.role:
return None
exception = getattr(template, "skip_device", None)
if exception == self.sn:
return None
configlet_name = "{0}-{1}-{2}-CONFIG".format(self.injectSection.upper(), template.name.upper(), self.hostname.upper())
self.to_deploy.append((configlet_name, template))
def compile_configlet(self, template):
#TODO: MAKE HANDLE LIST LOOKUPS, RIGHT NOW ONLY WORKS FOR ONE CONTAINER OR ONE DEVICE i.e. USELESS
exception = getattr(template, "skip_container", None)
if exception == self.role:
return ' '
exception = getattr(template, "skip_device", None)
if exception == self.sn:
return ' '
return template.compile(self)
@property
def peer_desc(self, peer):
return "TO-{0}".format(peer.hostname)
@property
def mlag_address(self):
try:
neighbor = getByHostname(self.mlag_neighbor)
mgmt_ip = ip_address(unicode(self.mgmt_ip[:-3]))
neighbor_mgmt = ip_address(unicode(neighbor.mgmt_ip[:-3]))
global_mlag_address = ip_address(unicode(self.searchConfig('mlag_address')))
if mgmt_ip > neighbor_mgmt:
return global_mlag_address + 1
else:
return global_mlag_address
except:
return 'ERROR'
@property
def mlag_peer_address(self):
try:
neighbor = getByHostname(self.mlag_neighbor)
return str(neighbor.mlag_address)
except:
return 'ERROR'
@property
def reload_delay_0(self):
if getattr(self, "is_jericho", None):
return self.searchConfig('reload_delay_jericho')[0]
else:
return self.searchConfig('reload_delay')[0]
@property
def reload_delay_1(self):
if getattr(self, "is_jericho", None):
return self.searchConfig('reload_delay_jericho')[1]
else:
return self.searchConfig('reload_delay')[1]
@property
def underlay(self):
template = TEMPLATES.get('underlay_private')
i = 0
if len(self.underlay_inject):
return "\n".join(self.underlay_inject)
for i, spine in enumerate(SPINES, start = 1):
#compile p2p link to spine
try:
ipAddress = ip_address(unicode(getattr(self, "sp{0}_ip".format(i))))
spine_args = {
"interface" : getattr(self, "sp{0}_int".format(i)),
"address" : ipAddress,
"interface_speed" : getattr(self, "sp{0}_speed".format(i), self.searchConfig('fabric_speed')),
"description" : "TO-{0}-UNDERLAY Ethernet{1}".format(self.hostname, getattr(self, "lf{0}_int".format(i)))
}
spine.underlay_inject.append(template.compile(spine_args))
self_args = {
"interface" : getattr(self, "lf{0}_int".format(i)),
"address" : ipAddress + 1,
"interface_speed" : getattr(self, "sp{0}_speed".format(i), self.searchConfig('fabric_speed')),
"description" : "TO-{0}-UNDERLAY Ethernet{1}".format(spine.hostname, getattr(self, "sp{0}_int".format(i)))
}
self.underlay_inject.append(template.compile(self_args))
except Exception as e:
LOGGER.log("-error building configlet section underlay for {0}<->{1}: {2}".format(spine.hostname, self.hostname, e))
sys.exit(0)
return "\n".join(self.underlay_inject)
@property
def spine_asn(self):
if len(SPINES) >= 1:
return SPINES[0].asn
else:
return 'ERROR'
@property
def spine_lo0_list(self):
return [spine.lo0 for spine in SPINES]
@property
def spine_lo1_list(self):
return [spine.lo1 for spine in SPINES]
@property
def spine_ipv4_list(self):
ipAddresses = []
for i, spine in enumerate(SPINES, start = 1):
#compile p2p link to spine
ipAddresses.append(getattr(self, "sp{0}_ip".format(i)))
return ipAddresses
@property
def spine_hostname_list(self):
return [spine.hostname for spine in SPINES]
@property
def vrf_ibgp_peer_address(self):
ip = self.searchConfig('vrf_ibgp_ip')
return ip_address(unicode(ip)) + 1 if ip else 'ERROR'
class Math():
def __init__(self, start, op, qty):
self.iter = None
self.counter = None
if type(start) == list:
self.iter = iter(start)
else:
self.counter = int(start)
if op == '+':
self.do = self.increment
self.qty = int(qty) if qty else 1
elif op == '++':
self.do = self.increment
self.qty = int(qty) if qty else 10
elif op == '*':
self.do = self.multiply
self.qty = int(qty) if qty else 1
def current(self):
return int(next(self.iter)) if self.iter else self.counter
def increment(self):
current = self.current()
if self.iter:
return current + self.qty
else:
self.counter += self.qty
return current
def multiply(self):
current = self.current()
if self.iter:
return current * self.qty
else:
self.counter *= self.qty
return current
class Configlet():
def __init__(self, name, params = {}, injectSection = None):
self.name = name
self.injectSection = injectSection
for k, v in params.items():
setattr(self, k, v)
def compileIterables(self, source, baseTemplate):
compiled = {}
compiled['error'] = []
iterables = parseForIterables(baseTemplate)
for template in iterables:
extractedTemplates = [v.strip('[]') for v in template.split('else')]
for i, _template in enumerate(extractedTemplates):
valueDict = buildValueDict(source, _template, self.injectSection)
errorKeys = valueDict.pop('error')
if not errorKeys:
#values is a dict
keys = valueDict.keys()
values_list = valueDict.values()
#basically turn lists into iterables and static values into functions which return the same thing everytime
#this way we can exhause iterators until they fail as we build new dicts to pass as args
#if the flag is never set i.e. no lists are found just return one
values_and_getters = []
_compiled = []
flag = False
for item in values_list:
#if the item is just a list without math then use StopIteration exception to stop iterations
if type(item) == list:
#found at least one list
flag = not flag if not flag else flag
values_and_getters.append((iter(item), lambda item:next(item)))
#if the item is a tuple then it wraps the item inside the tuple with math ops to be done e.g. (value, op, qty) where value can be a list, if so compile until exhausted
elif type(item) == tuple:
if type(item[0]) == list:
flag = not flag if not flag else flag
values_and_getters.append((Math(*item), lambda item:item.do()))
#this is a single value, no math, compile once
else:
values_and_getters.append((item, lambda item:item))
#sanitize format syntax from templates and replase actual keys with positionals
_keys = []
#don't modify existing i; this is to sanitize and replace invalid keys for the format function used later
for x, key in enumerate(keys, 0):
x = 'i'+str(x)
_template = _template.replace('{'+key+'}', '{'+x+'}')
_keys.append(x)
#exhaust iterators
try:
#if flag is tripped then we know to iterate until the exception
while flag:
_compiled.append(_template.format(**dict(zip(_keys, [function(value) for value, function in values_and_getters]))))
else:
#no lists were found return once
compiled[template] = _template.format(**dict(zip(_keys, [function(value) for value, function in values_and_getters])))
except StopIteration as e:
compiled[template] = '\n'.join(_compiled)
if i == 0:
break
if i == 1:
compiled['error'].pop()
else:
compiled['error'].append((template, errorKeys))
return compiled
#source can be either dict or object class i.e. getattr(CLASS, VALUE, None) or DICT.get(value,None)
#will be used accordingly
def compile(self, source):
#TODO: Right now all the string replacements happen literally carrying the groups as the toReplace parameters
#can definitely do this better
baseTemplate = self.basetemplate
#parse for sections @...@{test}
#and recurse on stripped sections
sections = parseForSections(baseTemplate)
for section in sections:
#has clause to enable/disable
_section, _test = section
__section = _section.strip('@')
compiledIterables = self.compileIterables(source, __section)
errorIterables = compiledIterables.pop('error')
#test the "tests" arguments i.e @...@{tests}
#parseCondition returns a (value, function) tuple the fn(value) will return true/false if the test passes
#here we collect the key which failed a test
failedTests = [v[0] for v, fn in buildConditionTest(_test.strip('{}')) if not fn(*v, source = source, section = self.injectSection)]
if _test and not (failedTests or errorIterables):
#there is a test and iterables with no errors -> COMPILE
for toReplace, compiled in compiledIterables.items():
__section = __section.replace(toReplace, compiled)
elif _test and failedTests:
#there is a test but failed WIPE
LOGGER.log("-skipping configlet section {0} in {1}: test condition for {2} failed".format(
_section.replace('\n','')[:15],
self.name,
','.join(failedTests)
))
__section = ''
elif compiledIterables and not errorIterables:
#there is no test, and all iterables passed COMPILE
for toReplace, compiled in compiledIterables.items():
__section = __section.replace(toReplace, compiled)
else:
#no test, iterables failed WIPE
for toReplace, errorKeys in errorIterables:
LOGGER.log("-skipping configlet section {0} in {1}: iterations failed on {2}".format(
_section.replace('\n','')[:15],
self.name,
','.join(errorKeys)
))
__section = ''
baseTemplate = baseTemplate.replace(_section + _test, __section)
#parse stuff in [] for iterations outside of sections
#support only one iterable for now from the global space
compiledIterables = self.compileIterables(source, baseTemplate)
errorIterables = compiledIterables.pop('error')
for toReplace, errorKeys in errorIterables:
LOGGER.log("-skipping configlet option {0} in {1}: variable {2} undefined".format(
toReplace.replace('\n','')[:15] + '...',
self.name,
','.join(errorKeys)
))
for toReplace, compiled in compiledIterables.items():
baseTemplate = baseTemplate.replace(toReplace, compiled)
for toReplace, errorKeys in errorIterables:
baseTemplate = baseTemplate.replace(toReplace, '')
#now deal with the base template after sections/iterables are worked out
valueDict = buildValueDict(source, baseTemplate, self.injectSection)
errorKeys = valueDict.pop('error')
if errorKeys:
LOGGER.log("-error building configlet {0}: global/device definition for {1} undefined".format(self.name, ','.join(errorKeys)))
return ' '
#this is to sanitize and replace invalid keys in the format function
_keys = []
for i, key in enumerate(valueDict.keys(), 0):
i = 'i'+str(i)
baseTemplate = baseTemplate.replace('{'+key+'}', '{'+i+'}')
_keys.append(i)
try:
baseTemplate = baseTemplate.format(**dict(zip(_keys, valueDict.values())))
except KeyError as e:
LOGGER.log("-error building configlet {0}: global/device definition for {1} undefined".format(self.name, e))
#must return a value which passes a boolean test
#we will usually get here if the parent configlet requires device @property functions but the
return ' '
return baseTemplate.replace("~","\t").strip()
class Manager():
def __init__(self):
self.tasks_to_deploy = []
def deploy(self, section):
recipe = searchConfig("recipe", section)
mode = searchConfig('mode', section)
#control what tasks get created, DEVICES are already loaded accordingly
if mode == 'day1':
for sn, device in DEVICES.items():
for template in recipe:
template = TEMPLATES[template]
device.assign_configlet(template)
if device.role == "spine":
self.tasks_to_deploy.append(Task(device, mode = 1))
else:
self.tasks_to_deploy.insert(0,Task(device, mode = 1))
elif mode == 'day2':
singleton = searchConfig('singleton', section)
if singleton:
for template in recipe:
template = TEMPLATES[template]
self.tasks_to_deploy.append(Task(template = template, mode = 2))
else:
for device in COMPILE_FOR:
for template in recipe:
template = TEMPLATES[template]
device.assign_configlet(template)
if device.role == "spine":
self.tasks_to_deploy.append(Task(device, mode = 2))
else:
self.tasks_to_deploy.insert(0,Task(device, mode = 2))
for task in self.tasks_to_deploy:
task.execute()
self.tasks_to_deploy = []
class FabricBuilder(cmd.Cmd):
"""Arista Fabric Initializer"""
intro = 'Type ? for available commands'
prompt = 'builder>'
def help_deploy(self):
print('Use deploy NAME where NAME is the user-defined section with a defined recipe.')
#check recipe syntax and variables
def do_deploy(self, section):
mode = searchConfig('mode', section)
if mode == 'day2':
spines = searchConfig('spines', section)
leafs = searchConfig('leafs', section)
if not (spines and leafs):
LOGGER.log('Recipe error: for mode = day2, spines and leaves must be defined in the global or recipe config')
return True
singleton = searchConfig('singleton', section)
compile_for = searchConfig('compile_for', section)
if not singleton and not compile_for:
LOGGER.log('Recipe error: for singleton = False, compile_for must be defined in the global or recipe config')
return True
buildGlobalData(section)
MANAGER.deploy(section)
def do_EOF(self, line):
return True
#get if dict, getattr if else
def searchSource(key, source, default = None):
return source.get(key, default) if type(source) is dict else getattr(source, key, default)
def searchConfig(key, section = None):
config = None
if section:
try:
config = CONFIG.get(section, key).strip()
except:
pass
if config == None:
try:
config = CONFIG.get('global', key).strip()
except:
return None
if config.startswith('[') and config.endswith(']'):
return [v.strip() for v in config[1:-1].split(',') if v]
if config == 'True':
return True
if config == 'False':
return False
return config
def getKeyDefinition(key, source, section = None):
csv_telemetry_source = key.split('#')
file = None
truncate = None
op = None
qty = None
if len(csv_telemetry_source) == 2:
file = csv_telemetry_source[0]
key = csv_telemetry_source[1]
math = parseForMath(key)
#can't truncate math op's; so either or
if math:
key, op, qty = math[0]
else:
key, truncate = parseForTruncation(key)[0]
if truncate:
start, end = truncate[1:-1].split(':')
start = int(start) if start else None
end = int(end) if end else None
else:
start = None
end = None
def truncateValues(values, start = None, end = None):
if type(values) == list:
return [str(val)[start:end] for val in values]
else:
return str(values)[start:end]
def fetchTelemOrFileData(file, key):
if file.startswith('/') and hasattr(CVP, 'cvprac'):
#this is super hacked need a telemetry Data Model parser. cvp-connector has one but in js
try:
found = CVP.cvprac.get('/api/v1/rest/' + searchSource('sn', source, '').upper() + file)
found = found['notifications'][0]['updates'][key]['value']
if type(found) == dict:
__keys = found.keys()
if 'Value' in __keys:
found = found['Value']
elif 'value' in __keys:
found = found['value']
_type, val = found.items()[0]
return val
else:
return found
except:
LOGGER.log("-failed to properly fetch/decode telemetry data")
return None
else:
global SUPPLEMENT_FILES
try:
return SUPPLEMENT_FILES[file][key]
except KeyError as e:
pass
try:
with xlrd.open_workbook(file+'.xls') as f:
sheet = f.sheet_by_index(0)
sheet.cell_value(0,0)
SUPPLEMENT_FILES[file] = {}
for col in range(sheet.ncols):
col = sheet.col_values(col)
col = [int(v) if type(v) == float else v for v in col]
SUPPLEMENT_FILES[file][col[0]] = col[1:]
return SUPPLEMENT_FILES[file][key]
except:
return None
if file:
toReturn = fetchTelemOrFileData(file, key)
elif key.isdigit():
toReturn = key
else:
toReturn = searchSource(key, source) or searchConfig(key, section)
if toReturn == 'ERROR' or not toReturn:
toReturn = None
if math:
return (toReturn, op, qty)
elif truncate:
return truncateValues(toReturn, start, end)
else:
return toReturn
def parseForRequiredKeys(template):
return re.findall('{(.*?)}', template)
def parseForIterables(template):
return re.findall('\[[\s\S]*?\](?!else)', template)
def parseForSections(template):
return re.findall('(@[\s\S]*?@)({.*?})*', template)
def parseForTruncation(key):
return re.findall('([\w]+)(\([-+]?\d*:[-+]?\d*\))?', key)
def parseForMath(key):
return re.findall('(\w+)([+\-*]+)(\d+)?', key)
#builds a tuple of values followed by a comparator lambda
#used to check if tests pass while supporting section injections from the global variable space
def buildConditionTest(keys):
condition_list = []
_keys = keys.split('&')
for key in _keys:
key = re.split('([^a-z0-9A-Z_]+)', key)
if len(key) > 1:
condition = key[1]
if condition == '=':
condition_fn = lambda key, value, source = None, section = None : value == getKeyDefinition(key, source, section)
else:
condition_fn = lambda key, value, source = None, section = None : value != getKeyDefinition(key, source, section)
condition_list.append( ((key[0], key[2]), condition_fn) )
else:
condition_list.append( ((key[0],), lambda key, source = None, section = None : bool(getKeyDefinition(key, source, section))) )
return condition_list
def buildValueDict(source, template, injectSection = None):
valueDict = {}
valueDict['error'] = []
keys = parseForRequiredKeys(template)
for key in keys:
#check if dict already has defined
if valueDict.get(key, None):
continue
defined = getKeyDefinition(key, source, injectSection)
if not defined:
valueDict['error'].append(key)
else:
valueDict[key] = defined
return valueDict
def getBySerial(sn):
return searchSource(sn.lower(), DEVICES)
def getByHostname(hostname):
return searchSource(hostname.lower(), HOST_TO_DEVICE)
def buildGlobalData(injectSection = None):
#INIT CONFIG
loadConfig()
global DEBUG
DEBUG = searchConfig('debug', injectSection)
#INIT TEMPLATES
loadTemplates(injectSection)
#INIT FABRIC CSV
loadDevices(injectSection)
def loadConfig():
global CONFIG
CONFIG = {}
CONFIG = ConfigParser()
CONFIG.read('global.conf')
def loadTemplates(injectSection = None):
global TEMPLATES
TEMPLATES = {}
parser = ConfigParser()
parser.read('templates.conf')
for sectionName in parser.sections():
TEMPLATES[sectionName] = Configlet(sectionName, dict(parser.items(sectionName)), injectSection)
def loadDevices(injectSection = None):
global DEVICES
global SPINES
global ASSIGN_TO
global COMPILE_FOR
global HOST_TO_DEVICE
DEVICES = {}
SPINES = []
ASSIGN_TO = []
COMPILE_FOR = []
HOST_TO_DEVICE = {}
mode = searchConfig('mode', injectSection)
_temp_vars = {}
if mode == 'day2':
spines = searchConfig('spines', injectSection)
leafs = searchConfig('leafs', injectSection)
compile_for = searchConfig('compile_for', injectSection)
assign_to = searchConfig('assign_to', injectSection)
follow_child_containers = searchConfig('follow_child_containers', injectSection)
CVP.populate()
spines = CVP.fetchDevices(spines, follow_child_containers) if spines else []
leafs = CVP.fetchDevices(leafs, follow_child_containers) if leafs else []
compile_for = CVP.fetchDevices(compile_for, follow_child_containers) if compile_for else []
assign_to = CVP.fetchDevices(assign_to, follow_child_containers) if assign_to else []
switch_vars = searchConfig('switch_vars', injectSection)
if switch_vars:
with xlrd.open_workbook(switch_vars) as f:
sheet = f.sheet_by_index(0)
sheet.cell_value(0,0)
headers = [val.lower() for val in sheet.row_values(0)]
#row[0] is the serial
#passing a dict to the switch to preserve csv headers
for row in range(1, sheet.nrows):
sn_index = headers.index("sn")
row = sheet.row_values(row)
sn = row[sn_index].lower()
_temp_vars[sn] = dict(zip(headers,row))
#build the master list and label the implicit role for day2, since we don't know what's what
for sn, cvp_device in CVP.devices.items():
implicitRole = None
if cvp_device in leafs:
implicitRole = 'leaf'
elif cvp_device in spines:
implicitRole = 'spine'
_temp_vars_device = searchSource(sn, _temp_vars, {})
sn = sn.lower()
hostname = (searchSource('hostname', _temp_vars_device, None) or cvp_device['hostname']).lower()
DEVICES[sn] = Switch(_temp_vars_device, cvp_device, injectSection, implicitRole)
HOST_TO_DEVICE[hostname] = DEVICES[sn]
if implicitRole == 'spine':
SPINES.append(DEVICES[sn])
if cvp_device in compile_for:
COMPILE_FOR.append(DEVICES[sn])
if cvp_device in assign_to:
ASSIGN_TO.append(DEVICES[sn])
elif mode == 'day1':
with xlrd.open_workbook(searchConfig('device_source', injectSection) or "fabric_parameters.xls") as f:
sheet = f.sheet_by_index(0)
sheet.cell_value(0,0)