blob: a1091c2cc72fc2b74c2860d2c12d4d4835c7b667 [file] [log] [blame]
koder aka kdanilovdda86d32015-03-16 11:20:04 +02001import time
2import json
3import os.path
4
Yulia Portnova0e64ea22015-03-20 17:27:22 +02005from ssh_copy_directory import copy_paths
6from ssh_runner import connect
koder aka kdanilovdda86d32015-03-16 11:20:04 +02007
8from concurrent.futures import ThreadPoolExecutor, wait
9
10
11def wait_all_ok(futures):
12 return all(future.result() for future in futures)
13
14
15def deploy_and_start_sensors(monitor_uri, config, remote_path='/tmp/sensors'):
16 paths = {os.path.dirname(__file__): remote_path}
17 with ThreadPoolExecutor(max_workers=32) as executor:
18 futures = []
19
20 for uri, config in config.items():
21 futures.append(executor.submit(deploy_and_start_sensor,
22 paths, uri, monitor_uri,
23 config, remote_path))
24
25 if not wait_all_ok(futures):
26 raise RuntimeError("Sensor deployment fails on some nodes")
27
28
29def deploy_and_start_sensor(paths, uri, monitor_uri, config, remote_path):
30 try:
31 conn = connect(uri)
32 copy_paths(conn, paths)
33 sftp = conn.open_sftp()
34
35 config_remote_path = os.path.join(remote_path, "conf.json")
36 main_remote_path = os.path.join(remote_path, "main.py")
37
38 with sftp.open(config_remote_path, "w") as fd:
39 fd.write(json.dumps(config))
40
41 cmd_templ = "python {0} -d start -u {1} {2}"
42 cmd = cmd_templ.format(main_remote_path,
43 monitor_uri,
44 config_remote_path)
45 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)