blob: b2dc3f1e3ffd581012991117471947ccca53ab9d [file] [log] [blame]
koder aka kdanilovdda86d32015-03-16 11:20:04 +02001import time
2import json
3import os.path
4
koder aka kdanilovdda86d32015-03-16 11:20:04 +02005from concurrent.futures import ThreadPoolExecutor, wait
6
koder aka kdanilove06762a2015-03-22 23:32:09 +02007from disk_perf_test_tool.ssh_utils import connect, copy_paths
8
koder aka kdanilovdda86d32015-03-16 11:20:04 +02009
10def wait_all_ok(futures):
11 return all(future.result() for future in futures)
12
13
14def deploy_and_start_sensors(monitor_uri, config, remote_path='/tmp/sensors'):
15 paths = {os.path.dirname(__file__): remote_path}
16 with ThreadPoolExecutor(max_workers=32) as executor:
17 futures = []
18
19 for uri, config in config.items():
20 futures.append(executor.submit(deploy_and_start_sensor,
21 paths, uri, monitor_uri,
22 config, remote_path))
23
24 if not wait_all_ok(futures):
25 raise RuntimeError("Sensor deployment fails on some nodes")
26
27
28def deploy_and_start_sensor(paths, uri, monitor_uri, config, remote_path):
29 try:
30 conn = connect(uri)
31 copy_paths(conn, paths)
32 sftp = conn.open_sftp()
33
34 config_remote_path = os.path.join(remote_path, "conf.json")
35 main_remote_path = os.path.join(remote_path, "main.py")
36
37 with sftp.open(config_remote_path, "w") as fd:
38 fd.write(json.dumps(config))
39
40 cmd_templ = "python {0} -d start -u {1} {2}"
41 cmd = cmd_templ.format(main_remote_path,
42 monitor_uri,
43 config_remote_path)
koder aka kdanilove06762a2015-03-22 23:32:09 +020044 print "Executing", cmd
koder aka kdanilovdda86d32015-03-16 11:20:04 +020045 conn.exec_command(cmd)
46 sftp.close()
47 conn.close()
koder aka kdanilove4ade1a2015-03-16 20:44:16 +020048 except:
koder aka kdanilovdda86d32015-03-16 11:20:04 +020049 return False
50 return True
51
52
53def stop_and_remove_sensor(uri, remote_path):
54 conn = connect(uri)
55 main_remote_path = os.path.join(remote_path, "main.py")
56
57 cmd_templ = "python {0} -d stop"
58 conn.exec_command(cmd_templ.format(main_remote_path))
koder aka kdanilove4ade1a2015-03-16 20:44:16 +020059
60 # some magic
koder aka kdanilovdda86d32015-03-16 11:20:04 +020061 time.sleep(0.3)
koder aka kdanilovdda86d32015-03-16 11:20:04 +020062
63 conn.exec_command("rm -rf {0}".format(remote_path))
64
65 conn.close()
66
67
68def stop_and_remove_sensors(config, remote_path='/tmp/sensors'):
69 with ThreadPoolExecutor(max_workers=32) as executor:
70 futures = []
71
72 for uri, config in config.items():
73 futures.append(executor.submit(stop_and_remove_sensor,
74 uri, remote_path))
75
76 wait(futures)