Step 2: Move test folder to root
[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 sys.path.append('../../../csit/libraries')
36 import UtilLibrary
37 import json
38 import time
39
40 try:
41     with open('cluster.json') as cluster_file:
42         data = json.load(cluster_file)
43 except:
44     print str(sys.exc_info())
45     print 'unable to open the file cluster.json'
46     exit(1)
47 try:
48     controllers = data["cluster"]["controllers"]
49     user_name = data["cluster"]["user"]
50     user_pass = data["cluster"]["pass"]
51 except:
52     print str(sys.exc_info())
53     print 'Error reading the file cluster.json'
54     exit(1)
55 try:
56     isolate = int(sys.argv[1])
57     duration = int(sys.argv[2])
58 except:
59     print 'You must specify the number (e.g. 1, 2, 3) of the controller to isolate.'
60     exit(1)
61
62 print 'Isolating controller ' + str(isolate)
63
64 print UtilLibrary.isolate_controller(controllers, user_name, user_pass, isolate)
65
66 print 'Pausing for ' + str(duration) + ' seconds...'
67 time.sleep(duration)
68
69 print UtilLibrary.flush_iptables(controllers, user_name, user_pass)