blob: 3c715eff88ef469558f8912fe3b0267250437eb5 [file] [log] [blame]
koder aka kdanilovdda86d32015-03-16 11:20:04 +02001import time
2import json
3import os.path
koder aka kdanilov2c473092015-03-29 17:12:13 +03004import logging
koder aka kdanilovdda86d32015-03-16 11:20:04 +02005
koder aka kdanilovdda86d32015-03-16 11:20:04 +02006from concurrent.futures import ThreadPoolExecutor, wait
7
koder aka kdanilove06762a2015-03-22 23:32:09 +02008from disk_perf_test_tool.ssh_utils import connect, copy_paths
9
koder aka kdanilov2c473092015-03-29 17:12:13 +030010logger = logging.getLogger('io-perf-tool')
11
koder aka kdanilovdda86d32015-03-16 11:20:04 +020012
13def wait_all_ok(futures):
14 return all(future.result() for future in futures)
15
16
koder aka kdanilov2c473092015-03-29 17:12:13 +030017def deploy_and_start_sensors(monitor_uri, config,
18 remote_path='/tmp/sensors',
19 connected_config=None):
koder aka kdanilovdda86d32015-03-16 11:20:04 +020020 paths = {os.path.dirname(__file__): remote_path}
21 with ThreadPoolExecutor(max_workers=32) as executor:
22 futures = []
23
koder aka kdanilov2c473092015-03-29 17:12:13 +030024 if connected_config is not None:
25 assert config is None
26 node_iter = connected_config
27 else:
28 node_iter = config.items()
29
30 for uri_or_conn, config in node_iter:
koder aka kdanilovdda86d32015-03-16 11:20:04 +020031 futures.append(executor.submit(deploy_and_start_sensor,
koder aka kdanilov2c473092015-03-29 17:12:13 +030032 paths, uri_or_conn,
33 monitor_uri,
koder aka kdanilovdda86d32015-03-16 11:20:04 +020034 config, remote_path))
35
36 if not wait_all_ok(futures):
37 raise RuntimeError("Sensor deployment fails on some nodes")
38
39
koder aka kdanilov2c473092015-03-29 17:12:13 +030040def deploy_and_start_sensor(paths, uri_or_conn, monitor_uri, config,
41 remote_path):
koder aka kdanilovdda86d32015-03-16 11:20:04 +020042 try:
koder aka kdanilov2c473092015-03-29 17:12:13 +030043 if isinstance(uri_or_conn, basestring):
44 conn = connect(uri_or_conn)
45 else:
46 conn = uri_or_conn
47
koder aka kdanilovdda86d32015-03-16 11:20:04 +020048 copy_paths(conn, paths)
49 sftp = conn.open_sftp()
50
51 config_remote_path = os.path.join(remote_path, "conf.json")
52 main_remote_path = os.path.join(remote_path, "main.py")
53
54 with sftp.open(config_remote_path, "w") as fd:
55 fd.write(json.dumps(config))
56
57 cmd_templ = "python {0} -d start -u {1} {2}"
58 cmd = cmd_templ.format(main_remote_path,
59 monitor_uri,
60 config_remote_path)
61 conn.exec_command(cmd)
62 sftp.close()
koder aka kdanilov2c473092015-03-29 17:12:13 +030063
64 if isinstance(uri_or_conn, basestring):
65 conn.close()
koder aka kdanilove4ade1a2015-03-16 20:44:16 +020066 except:
koder aka kdanilov2c473092015-03-29 17:12:13 +030067 logger.exception("During deploing sensors in {0}".format(uri_or_conn))
koder aka kdanilovdda86d32015-03-16 11:20:04 +020068 return False
69 return True
70
71
koder aka kdanilov2c473092015-03-29 17:12:13 +030072def stop_and_remove_sensor(uri_or_conn, remote_path):
73 if isinstance(uri_or_conn, basestring):
74 conn = connect(uri_or_conn)
75 else:
76 conn = uri_or_conn
77
koder aka kdanilovdda86d32015-03-16 11:20:04 +020078 main_remote_path = os.path.join(remote_path, "main.py")
79
80 cmd_templ = "python {0} -d stop"
81 conn.exec_command(cmd_templ.format(main_remote_path))
koder aka kdanilove4ade1a2015-03-16 20:44:16 +020082
83 # some magic
koder aka kdanilovdda86d32015-03-16 11:20:04 +020084 time.sleep(0.3)
koder aka kdanilovdda86d32015-03-16 11:20:04 +020085
86 conn.exec_command("rm -rf {0}".format(remote_path))
87
koder aka kdanilov2c473092015-03-29 17:12:13 +030088 if isinstance(uri_or_conn, basestring):
89 conn.close()
koder aka kdanilovdda86d32015-03-16 11:20:04 +020090
koder aka kdanilovda45e882015-04-06 02:24:42 +030091 logger.debug("Sensors stopped and removed")
92
koder aka kdanilovdda86d32015-03-16 11:20:04 +020093
koder aka kdanilov2c473092015-03-29 17:12:13 +030094def stop_and_remove_sensors(config, remote_path='/tmp/sensors',
95 connected_config=None):
koder aka kdanilovdda86d32015-03-16 11:20:04 +020096 with ThreadPoolExecutor(max_workers=32) as executor:
97 futures = []
98
koder aka kdanilov2c473092015-03-29 17:12:13 +030099 if connected_config is not None:
100 assert config is None
101 conf_iter = connected_config
102 else:
103 conf_iter = config.items()
104
105 for uri_or_conn, config in conf_iter:
koder aka kdanilovdda86d32015-03-16 11:20:04 +0200106 futures.append(executor.submit(stop_and_remove_sensor,
koder aka kdanilov2c473092015-03-29 17:12:13 +0300107 uri_or_conn, remote_path))
koder aka kdanilovdda86d32015-03-16 11:20:04 +0200108
109 wait(futures)