Updated code to match new rules
[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 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                 "172.17.10.93",
22                 "172.17.10.94",
23                 "172.17.10.95"
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     user_name = data["cluster"]["user"]
56     user_pass = data["cluster"]["pass"]
57 except:
58     print str(sys.exc_info())
59     print 'Error reading the file cluster.json'
60     exit(1)
61 try:
62     isolate = int(sys.argv[1])
63 except:
64     print 'You must specify the number (e.g. 1, 2, 3) of the controller to isolate.'
65     exit(1)
66
67 print "isolating controller " + str(isolate)
68
69 print UtilLibrary.isolate_controller(cluster_list, user_name, user_pass, isolate)