blob: 73e7902cfe53ea4e60389cfcf4340caa9cdd32b0 [file] [log] [blame]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03001import time
2import json
3import os.path
4import logging
5
6from concurrent.futures import ThreadPoolExecutor, wait
7
8from wally.ssh_utils import copy_paths, run_over_ssh
9
koder aka kdanilov168f6092015-04-19 02:33:38 +030010logger = logging.getLogger('wally.sensors')
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030011
12
13def wait_all_ok(futures):
14 return all(future.result() for future in futures)
15
16
koder aka kdanilov168f6092015-04-19 02:33:38 +030017def deploy_and_start_sensors(sensor_configs,
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030018 remote_path='/tmp/sensors/sensors'):
19
20 paths = {os.path.dirname(__file__): remote_path}
21 with ThreadPoolExecutor(max_workers=32) as executor:
22 futures = []
23
24 for node_sensor_config in sensor_configs:
25 futures.append(executor.submit(deploy_and_start_sensor,
26 paths,
27 node_sensor_config,
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030028 remote_path))
29
30 if not wait_all_ok(futures):
31 raise RuntimeError("Sensor deployment fails on some nodes")
32
33
koder aka kdanilov168f6092015-04-19 02:33:38 +030034def deploy_and_start_sensor(paths, node_sensor_config, remote_path):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030035 try:
36 copy_paths(node_sensor_config.conn, paths)
37 sftp = node_sensor_config.conn.open_sftp()
38
39 config_remote_path = os.path.join(remote_path, "conf.json")
40
koder aka kdanilov168f6092015-04-19 02:33:38 +030041 sensors_config = node_sensor_config.sensors.copy()
42 sensors_config['source_id'] = node_sensor_config.source_id
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030043 with sftp.open(config_remote_path, "w") as fd:
koder aka kdanilov168f6092015-04-19 02:33:38 +030044 fd.write(json.dumps(sensors_config))
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030045
46 cmd_templ = 'env PYTHONPATH="{0}" python -m ' + \
47 "sensors.main -d start -u {1} {2}"
48
49 cmd = cmd_templ.format(os.path.dirname(remote_path),
koder aka kdanilov168f6092015-04-19 02:33:38 +030050 node_sensor_config.monitor_url,
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030051 config_remote_path)
52
53 run_over_ssh(node_sensor_config.conn, cmd,
54 node=node_sensor_config.url)
55 sftp.close()
56
57 except:
koder aka kdanilov168f6092015-04-19 02:33:38 +030058 msg = "During deploing sensors on {0}".format(node_sensor_config.url)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030059 logger.exception(msg)
60 return False
61 return True
62
63
64def stop_and_remove_sensor(conn, url, remote_path):
65 cmd = 'env PYTHONPATH="{0}" python -m sensors.main -d stop'
66
67 run_over_ssh(conn, cmd.format(remote_path), node=url)
68
69 # some magic
70 time.sleep(0.3)
71
koder aka kdanilov168f6092015-04-19 02:33:38 +030072 # logger.warning("Sensors don't removed")
73 run_over_ssh(conn, "rm -rf {0}".format(remote_path), node=url)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030074
75
76def stop_and_remove_sensors(configs, remote_path='/tmp/sensors'):
77 with ThreadPoolExecutor(max_workers=32) as executor:
78 futures = []
79
80 for node_sensor_config in configs:
81 futures.append(executor.submit(stop_and_remove_sensor,
82 node_sensor_config.conn,
83 node_sensor_config.url,
84 remote_path))
85
86 wait(futures)
koder aka kdanilov168f6092015-04-19 02:33:38 +030087 logger.debug("Sensors stopped and removed")