fixing pep8 problems for test verify tox job
[integration/test.git] / tools / clustering / cluster-monitor / isolate.py
1 #!/usr/bin/python
2 """
3 Controller Isolation Tool
4 Author: Phillip Shea
5 Updated: 2015-May-07
6
7 This tool isolates an indicated controller from the cluster. The tool takes
8 a single integer argument corresponding to the number of a controller
9 in a json file's ordered list of controllers. This is the controller to
10 be isolated.
11
12 A file named 'cluster.json' containing a list of the IP addresses, ports and
13 credentials of the controllers is required. It resides in the same
14 directory as monitor.py.
15
16 The file should look like this:
17
18     {
19         "cluster": {
20             "controllers": [
21                 {"ip": "172.17.10.93", "port": "8181"},
22                 {"ip": "172.17.10.93", "port": "8181"},
23                 {"ip": "172.17.10.93", "port": "8181"}
24             ],
25             "user": "username",
26             "pass": "password",
27         }
28     }
29
30 Usage:python isolate.py [controller to be isolated]
31 """
32
33 import sys
34
35
36 def import_utility_modules():
37     global UtilLibrary, json
38     import sys
39     sys.path.append('../../../csit/libraries')
40     import UtilLibrary
41     import json
42
43
44 import_utility_modules()
45
46 try:
47     with open('cluster.json') as cluster_file:
48         data = json.load(cluster_file)
49 except:
50     print str(sys.exc_info())
51     print "unable to open the file cluster.json"
52     exit(1)
53 try:
54     cluster_list = data["cluster"]["controllers"]
55     cluster_ips = []
56     for controller in cluster_list:
57         cluster_ips.append(controller["ip"])
58     user_name = data["cluster"]["user"]
59     user_pass = data["cluster"]["pass"]
60 except:
61     print str(sys.exc_info())
62     print 'Error reading the file cluster.json'
63     exit(1)
64 try:
65     isolate = int(sys.argv[1])
66 except:
67     print 'You must specify the number (e.g. 1, 2, 3) of the controller to isolate.'
68     exit(1)
69
70 print "isolating controller " + str(isolate)
71
72 print UtilLibrary.isolate_controller(cluster_ips, user_name, user_pass, isolate)