@@ -38,16 +38,16 @@ def get_hostname(self):
3838 except Exception as e :
3939 print ("Failed to extract hostname" )
4040 print ("Error: " + str (e ))
41- sys . exit ( 1 )
41+ return ""
4242 else :
4343 print ("Failed to fetch hostname configuration" )
44- sys . exit ( 1 )
44+ return ""
4545
4646
4747 def configure_ospf (self ) {
4848
4949 ospf_config = """!
50- router ospf ztp-bash
50+ router ospf ztp-python
5151 area 0
5252 interface Loopback0
5353 !
@@ -59,12 +59,33 @@ def configure_ospf(self) {
5959 !
6060 end
6161 """
62-
62+
63+ try :
64+ response = self .xrapply_string (cmd = ospf_config )
65+
66+ if response ["status" ] == "success" :
67+ print ("OSPF configuration successfully applied" )
68+ print (response ["output" ])
69+ return True
70+ else :
71+ print ("Failed to apply OSPF configuration" )
72+ print (response ["output" ])
73+ return False
74+ except Exception as e :
75+ print ("Failed to apply OSPF configuration" )
76+ print ("Error : " + str (e ))
77+ return False
6378 }
6479
6580
6681 def configure_bgp (asn = None , hostname = None ) {
67-
82+
83+ hostname = self .get_hostname ()
84+
85+ if hostname == "" :
86+ print ("Require hostname to determine bgp config, aborting" )
87+ return False
88+
6889 bgp_config = """ !
6990 router bgp {local_asn}
7091 address-family ipv4 unicast
@@ -80,17 +101,77 @@ def configure_bgp(asn=None, hostname=None) {
80101 neighbor_ip = ParameterMap [hostname ][bgp_neighbor_ip ],
81102 remote_asn = ParameterMap [hostname ][remote_asn ])
82103
104+ with open ("/tmp/bgp_config" , 'w' ) as fd :
105+ fd .write (bgp_config )
106+
107+ try :
108+ response = self .xrapply (filename = "/tmp/bgp_config" )
109+ os .remove ("/tmp/bgp_config" )
110+
111+ if response ["status" ] == "success" :
112+ print ("BGP configuration successfully applied" )
113+ print (response ["output" ])
114+ return True
115+ else :
116+ print ("Failed to apply BGP configuration" )
117+ print (response ["output" ])
118+ return False
119+ except Exception as e :
120+ print ("Failed to apply BGP configuration" )
121+ print ("Error : " + str (e ))
122+ os .remove ("/tmp/bgp_config" )
123+ return False
83124 }
84125
85126
86- def configure_loopback () {
127+ def configure_loopback (self ) {
87128
88129 hostname = self .get_hostname ()
130+
131+ if hostname == "" :
132+ print ("Require hostname to determine bgp config, aborting" )
133+ return False
134+
89135 loopback0_config = """!
90136 interface Loopback0
91- ipv4 address ${HostMap[${hostname}] }
137+ ipv4 address {loopback0_ip }
92138 !
93139 end
94- """
95-
140+ """ .format (loopback0_ip = ParameterMap [hostname ][loopback0_ip ])
141+
142+ try :
143+ response = self .xrapply_string (cmd = loopback0_config )
144+
145+ if response ["status" ] == "success" :
146+ print ("Loopback0 configuration successfully applied" )
147+ print (response ["output" ])
148+ return True
149+ else :
150+ print ("Failed to apply Loopback0 configuration" )
151+ print (response ["output" ])
152+ return False
153+ except Exception as e :
154+ print ("Failed to apply Loopback0 configuration" )
155+ print ("Error : " + str (e ))
156+ return False
157+
96158 }
159+
160+
161+ if __name__ == "__main__" :
162+
163+ config_obj = ZtpChildClass ()
164+
165+ if not config_obj .configure_loopback ():
166+ print ("Couldn't apply loopback0 configuration, aborting" )
167+ sys .exit (1 )
168+
169+ if not config_obj .configure_ospf ():
170+ print ("Couldn't apply ospf configuration, aborting" )
171+ sys .exit (1 )
172+
173+ if not config_obj .configure_bgp ():
174+ print ("Couldn't apply bgp configuration, aborting" )
175+ sys .exit (1 )
176+
177+ sys .exit (0 )
0 commit comments