Updated code to match new rules
[integration/test.git] / tools / clustering / cluster-monitor / timed_isolation.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 for a specified duration.
8 The tool's first integer argument corresponds to the number of a controller
9 in a json file's ordered list of controllers. This is the controller to
10 be isolated. The second argument is the duration of isolation in
11 seconds.
12
13 A file named 'cluster.json' containing a list of the IP addresses and
14 credentials of the controllers is required. It resides in the same
15 directory as monitor.py.
16
17 The file should look like this:
18
19     {
20         "cluster": {
21             "controllers": [
22                 "172.17.10.93",
23                 "172.17.10.94",
24                 "172.17.10.95"
25             ],
26             "user": "username",
27             "pass": "password"
28         }
29     }
30
31 Usage:python timed_isolation.py [controller to be isolated]  [duration of isolation in seconds]
32 """
33
34 import sys
35 import time
36
37
38 def import_utility_modules():
39     global UtilLibrary, json
40     import sys
41     sys.path.append('../../../csit/libraries')
42     import UtilLibrary
43     import json
44
45
46 import_utility_modules()
47
48 try:
49     with open('cluster.json') as cluster_file:
50         data = json.load(cluster_file)
51 except:
52     print str(sys.exc_info())
53     print 'unable to open the file cluster.json'
54     exit(1)
55 try:
56     controllers = data["cluster"]["controllers"]
57     user_name = data["cluster"]["user"]
58     user_pass = data["cluster"]["pass"]
59 except:
60     print str(sys.exc_info())
61     print 'Error reading the file cluster.json'
62     exit(1)
63 try:
64     isolate = int(sys.argv[1])
65     duration = int(sys.argv[2])
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(controllers, user_name, user_pass, isolate)
73
74 print 'Pausing for ' + str(duration) + ' seconds...'
75 time.sleep(duration)
76
77 print UtilLibrary.flush_iptables(controllers, user_name, user_pass)