-
Notifications
You must be signed in to change notification settings - Fork 0
/
ns_conf.py
106 lines (84 loc) · 4.02 KB
/
ns_conf.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
#!/usr/bin/env python
import os, sys, logging
from optparse import Opti
# we have config in our home directory, so add that to python path and import it
sys.path.append(os.getenv("HOME"))
import ns_config as config
import NSLib
def main():
# define cli parser configuration and options
cli_parser = OptionParser("Usage: %prog enable|disable [OPTIONS]")
cli_parser.add_option("-l", dest="list_config", action="store_true", help="List available configurations")
cli_parser.add_option("-s", dest="loadbalancer_conf", default="default", help="Select a loadbalancer")
cli_parser.add_option("--vserver", dest="vserver", default="", help="Name of a virtual server")
cli_parser.add_option("--service", dest="service", default="", help="Service name")
cli_parser.add_option("--group", dest="group", default="", help="Service group name")
(OPTS, ARGS) = cli_parser.parse_args()
# list configuration
if OPTS.list_config:
list_configurations()
# must specify one argument and it must be recognised
if not (len(ARGS) == 1 and ARGS[0].lower() in ('enable', 'disable')):
print "ERROR: You must specify only one argument: 'enable' or 'disable'"
cli_parser.print_help()
sys.exit(-1)
action = ARGS[0].lower()
# must specify at least one (empty parameters will get handled automatically)
if not (OPTS.vserver or OPTS.service or OPTS.group):
print "ERROR: You must specify at least one of the following 'vserver', 'service' or 'group'"
cli_parser.print_help()
sys.exit(-1)
# see which lb configuration we're told to use
lb_config = OPTS.loadbalancer_conf
if OPTS.loadbalancer_conf == 'default':
lb_config = config.netscalers['default']
# initialise SOAP connection object (performs login automatically)
ns_c = NSLib.NSConfigApi(hostname=config.netscalers[lb_config]['NS_ADDR'],
username=config.netscalers[lb_config]['USERNAME'],
password=config.netscalers[lb_config]['PASSWORD'])
# need to build a list of services that we enable or disable
# source is three options: vserver, service and group
# start with empty list and add service from each option
services = []
# services from vserver: query loadbalancer and retrieve list of services
if OPTS.vserver:
# we accept comma separated lists as well
for vs in OPTS.vserver.split(','):
services += ns_c.get_services_list(vs)
# services from service: very simple, just add them
if OPTS.service:
services += OPTS.service.split(',')
# services from group: add group list to them
if OPTS.group:
for gr in OPTS.group.split(','):
services += config.netscalers[lb_config]['groups'][gr]
print '*** the following services will be %sD:' % action.upper()
for s in services:
print '- %s' % s
confirm = raw_input('Do you want to proceed? (yes|no) ')
if confirm.upper() != 'YES':
print 'Terminated...'
sys.exit(-1)
ns_c.set_service_state(action, services, verbose=True)
def list_configurations():
for c in config.netscalers:
if c != 'default':
print '-' * 30
print c
if c == config.netscalers['default']:
print "(default)"
print ""
print " ADDRESS: %s" % config.netscalers[c]['NS_ADDR']
print "USERNAME: %s" % config.netscalers[c]['USERNAME']
print ""
if 'vserver_ignore_list' in config.netscalers[c]:
print 'Virtual servers that will be ignored when checking for errors:'
for vs in config.netscalers[c]['vserver_ignore_list']:
print " %s" % vs
if 'groups' in config.netscalers[c]:
print 'Following service groups are defined:'
for (grp_name, list) in config.netscalers[c]['groups'].iteritems():
print ' %s: %s' % (grp_name, repr(list))
sys.exit(0)
if __name__ == '__main__':
main()