add openstack-export support
[bottlenecks.git] / monitor / dispatch / automate_collectd_client.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Tech and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import logging
11 import yaml
12 import utils.infra_setup.passwordless_SSH.ssh as ssh
13
14 logger = logging.getLogger(__name__)
15 collectd_client_install_sh =\
16     "/home/opnfv/bottlenecks/monitor/dispatch/install_collectd_client.sh"
17 collectd_client_install_conf =\
18     "/home/opnfv/bottlenecks/monitor/config/collectd_client.conf"
19
20 with open('/tmp/pod.yaml') as f:
21     dataMap = yaml.safe_load(f)
22     for x in dataMap:
23         for y in dataMap[x]:
24             if (y['role'] == 'Controller') or (y['role'] == 'Compute'):
25                 ip = str(y['ip'])
26                 user = str(y['user'])
27                 pwd = str(y['password'])
28                 ssh_d = ssh.SSH(user, host=ip, password=pwd)
29                 status, stdout, stderr = ssh_d.execute(
30                     "cd /etc && mkdir collectd_config"
31                 )
32                 if status:
33                     print Exception(
34                         "Command: \"mkdir collectd_config\" failed.")
35                     logger.info(stdout.splitlines())
36                 with open(collectd_client_install_sh) as stdin_file:
37                     ssh_d.run("cat > /etc/collectd_config/install.sh",
38                               stdin=stdin_file)
39                 with open(collectd_client_install_conf) as stdin_file:
40                     ssh_d.run(
41                         "cat > /etc/collectd_config/collectd_client.conf",
42                         stdin=stdin_file
43                     )
44                 status, stdout, stderr = ssh_d.execute(
45                     "sudo apt-get install -y docker.io"
46                 )
47                 if status:
48                     raise Exception("Command for installing docker failed.")
49                     logger.info(stdout.splitlines())
50                 ssh_d.run("cd /etc/collectd_config/ && bash ./install.sh")