-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigNS.py
397 lines (324 loc) · 14.1 KB
/
configNS.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
import os
import requests
import time
from nssrc.com.citrix.netscaler.nitro.service.nitro_service import nitro_service
from nssrc.com.citrix.netscaler.nitro.exception.nitro_exception import nitro_exception
from nssrc.com.citrix.netscaler.nitro.resource.config.ns.nsip import nsip
from nssrc.com.citrix.netscaler.nitro.resource.config.ns.nshostname import nshostname
from nssrc.com.citrix.netscaler.nitro.resource.config.dns.dnsnameserver import dnsnameserver
from nssrc.com.citrix.netscaler.nitro.resource.config.ns.nsconfig import nsconfig
from nssrc.com.citrix.netscaler.nitro.resource.config.system.systemfile import systemfile
from nssrc.com.citrix.netscaler.nitro.resource.config.basic.service import service
from nssrc.com.citrix.netscaler.nitro.resource.config.lb.lbvserver import lbvserver
from nssrc.com.citrix.netscaler.nitro.resource.config.lb.lbvserver_service_binding import lbvserver_service_binding
from nssrc.com.citrix.netscaler.nitro.resource.config.cs.csvserver import csvserver
from nssrc.com.citrix.netscaler.nitro.resource.config.ha.hanode import hanode
from nssrc.com.citrix.netscaler.nitro.resource.config.ssl.sslcertkey import sslcertkey
from nssrc.com.citrix.netscaler.nitro.resource.config.ssl.sslcertkey_sslvserver_binding import sslcertkey_sslvserver_binding
class netscaler:
"""This class reperesents a connection to a NetScaler using NITRO. Class
Methods below provide functionality to interact with the NetScaler."""
def __init__(self):
#initlization
self.ns_session = ""
self.nsip = os.environ['NSIP']
self.username = os.environ['USERNAME']
self.password = os.environ['PASSWORD']
self.hostname = os.environ['HOSTNAME']
self.vip1 = os.environ['VIP1']
self.vip2 = os.environ['VIP2']
self.vip3 = os.environ['VIP3']
self.vserver1 = os.environ['VSERVER']
self.vserver2 = os.environ['VSERVER1']
self.vserver3 = os.environ['VSERVER2']
self.service1 = os.environ['SERVICE1'] #HTTP
self.ip = os.environ['DOCKERHOST']
self.dns = os.environ['DNS']
def initConnection(self):
"""Create the NetScaler session using HTTP, passing in the credentials
to the NSIP"""
try:
self.ns_session = nitro_service(self.nsip,"HTTP")
self.ns_session.set_credential(self.username, self.password)
self.ns_session.timeout = 300
self.ns_session.login()
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
return
def savec(self):
"""Simple class used to save the config of the NS"""
try:
self.ns_session.save_config()
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
return
def closeConnection(self, savec=False):
"""Close the session. Can pass in if you wish to save the config or
not. Defaults to not saving the config"""
try:
if savec:
self.savec()
self.ns_session.logout()
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
return
def reboot(self, wait=False, savec=False):
"""Reboot the NetScaler. Can pass in if you wish to save the config or
not. Defaults to not saving the config. You can also choose to wait
in this function until the netscaler is back up using wait, doing so
will re-init the connection through nitro"""
try:
if savec:
self.savec()
self.ns_session.reboot(False)
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
if wait:
# if we want to wait lets keep checking the nitro status page for a
# 200 OK. If we get the 200, lets reinit the connection and cont.
while True:
time.sleep(15)
try:
r = requests.get("http://" + self.nsip +
"/nitro/v1/stat", timeout=15,
auth=(self.username,
self.password))
if r.status_code == 200:
# if were good to go, lets re-init that connection
self.initConnection()
break
except requests.exceptions.Timeout as e:
print "TO::Waiting on", self.cfg['config']['hostname'], "to reboot..."
except requests.exceptions.ConnectionError as e:
print "CE::Waiting on", self.cfg['config']['hostname'], "to reboot..."
return
def hostNameDnsTz(self):
"""Configure the HostName, DNS, and Time Zone for the NetScaler."""
# Begin by setting the hostname
try:
newNsHostname = nshostname()
newNsHostname.hostname = self.hostname
nshostname.update(self.ns_session, newNsHostname)
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
# Add DNS Entries, traverse the dns class variable and add the
# nameservers
try:
newDNS = dnsnameserver()
newDNS.ip = self.dns
dnsnameserver.add(self.ns_session, newDNS)
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
# Configure the NetScaler TimeZone
try:
nsconf = nsconfig()
nsconf.timezone = "GMT-04:00-EDT-America/New_York"
nsconfig.update(self.ns_session, nsconf)
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
return
def confFeatures(self):
"""Configure the features for the NetScaler"""
try:
self.ns_session.enable_features("lb")
self.ns_session.enable_features("ssl")
self.ns_session.enable_features("cs")
self.ns_session.enable_features("appfw")
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
return
def confModes(self):
"""Configure the modes for the NetScaler"""
try:
self.ns_session.enable_modes("usnip")
self.ns_session.enable_modes("l3")
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
return
def addServices(self):
"""Configure the services for the NetScaler"""
try:
#Setup the new service 1 HTTPS
newSVC = service()
newSVC.name = self.service1
newSVC.ip = self.ip
newSVC.port = "80"
newSVC.servicetype = "http"
#Add the new service1 and service
service.add(self.ns_session, newSVC)
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
return
def addLBVServers(self):
"""Configure the lbvservers for the NetScaler"""
#Setup a new lbvserver1
newLBVS = lbvserver()
newLBVS.name = self.vserver1
newLBVS.servicetype = "ssl"
newLBVS.ipv46 = self.vip1
newLBVS.port = "443"
newLBVS.persistencetype = "COOKIEINSERT"
newLBVS.lbmethod = "ROUNDROBIN"
try:
#Add the lbvs
response = lbvserver.add(self.ns_session, newLBVS)
if response.severity and response.severity == "WARNING":
print("\tWarning : " + response.message)
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
newSVCBinding = lbvserver_service_binding()
newSVCBinding.name = self.vserver1
newSVCBinding.servicename = self.service1
#newSVCBinding.weight = "1"
#Add the binding!
try:
lbvserver_service_binding.add(self.ns_session,
newSVCBinding)
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
#Setup a new lbvserver2
newLBVS = lbvserver()
newLBVS.name = self.vserver2
newLBVS.servicetype = "http"
newLBVS.ipv46 = self.vip2
newLBVS.port = "80"
newLBVS.persistencetype = "COOKIEINSERT"
newLBVS.lbmethod = "ROUNDROBIN"
try:
#Add the lbvs
response = lbvserver.add(self.ns_session, newLBVS)
if response.severity and response.severity == "WARNING":
print("\tWarning : " + response.message)
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
newSVCBinding = lbvserver_service_binding()
newSVCBinding.name = self.vserver2
newSVCBinding.servicename = self.service1
#newSVCBinding.weight = "1"
#Add the binding!
try:
lbvserver_service_binding.add(self.ns_session,
newSVCBinding)
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
#Setup a new lbvserver3
newLBVS = lbvserver()
newLBVS.name = self.vserver3
newLBVS.servicetype = "http"
newLBVS.ipv46 = self.vip3
newLBVS.port = "80"
newLBVS.persistencetype = "COOKIEINSERT"
newLBVS.lbmethod = "ROUNDROBIN"
try:
#Add the lbvs
response = lbvserver.add(self.ns_session, newLBVS)
if response.severity and response.severity == "WARNING":
print("\tWarning : " + response.message)
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
newSVCBinding = lbvserver_service_binding()
newSVCBinding.name = self.vserver3
newSVCBinding.servicename = self.service1
#newSVCBinding.weight = "1"
#Add the binding!
try:
lbvserver_service_binding.add(self.ns_session,
newSVCBinding)
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
return
def addcerts(self):
certs = { "512" : ["sha1", "sha224", "sha256"], "4096" : ["sha1", "sha224", "sha256", "sha384", "sha512"], "2048" : ["sha1", "sha224", "sha256", "sha384", "sha512"], "1024" : ["sha1", "sha224", "sha256", "sha384", "sha512"] }
for cert in certs :
for sha in certs[cert] :
cert_name = "root_rsa_" + cert + "_" + sha + ".pem"
print(cert_name)
key_name = "root_rsa_" + cert + "_" + sha + ".ky"
print(key_name)
newcert = sslcertkey()
newcert.certkey = cert + "_" + sha
newcert.cert = cert_name
newcert.key = key_name
try:
sslcertkey.add(self.ns_session,newcert)
except nitro_exception as e:
print("Exception::errorcode=" +
str(e.errorcode) + ",message=" + e.message)
except Exception as e:
print("Exception::message=" + str(e.args))
return
if __name__ == '__main__':
""" This is our main thread of execution, it starts all the work!"""
'''
# read in cnfig http://www.objgen.com/json/models/mdui
# fin = open("nsAutoCfg.json", "r")
# json_raw = fin.read()
# fin.close()
# jsn = json.loads(json_raw)
# Create some threads and netscalers
'''
ns = netscaler()
ns.initConnection()
# ns.defineIPs()
ns.hostNameDnsTz()
# ns.uploadLicense()
# ns.reboot(True, True)
ns.confFeatures()
ns.confModes()
# Lets add the certificates to the NS
ns.addcerts()
# Next lets add services and configure VServers
ns.addServices()
ns.addLBVServers()
# Were done here, lets save and close the connection
ns.savec()
ns.closeConnection()
print "All done preforming configuration"