Adapt monitoring install to apex
[bottlenecks.git] / monitor / dispatch / install_clients.py
1 ##############################################################################
2 # Copyright (c) 2018 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 Currently supported installers are Apex, Compass...
11 Supported monitoring tools are Cadvisor, Collectd, Barometer...
12 Carefully, do not change the path or name of the configuration files which
13 are hard coded for docker volume mapping.
14 '''
15 import os
16 import logging
17 import yaml
18 import utils.infra_setup.passwordless_SSH.ssh as ssh
19 import argparse
20
21 logger = logging.getLogger(__name__)
22
23 parser = argparse.ArgumentParser(description='Monitoring Clients Dispatcher')
24 parser.add_argument("-i", "--INSTALLER_TYPE",
25                     help="The installer for the system under monitoring")
26 # Barometer config and installation files
27 # /home/opnfv/bottlenecks/monitor/dispatch/install_barometer_client.sh
28 # /home/opnfv/bottlenecks/monitor/config/barometer_client.conf
29 # Cadvisor installation file
30 # /home/opnfv/bottlenecks/monitor/dispatch/install_cadvisor_client.sh
31 # Collectd config and installation files
32 # /home/opnfv/bottlenecks/monitor/dispatch/install_collectd_client.sh
33 # /home/opnfv/bottlenecks/monitor/config/collectd_client.conf
34 parser.add_argument("-s", "--INSTALlATION_SCRIPT",
35                     help="The path of the script to install monitoring script")
36 parser.add_argument("-c", "--CLIENT_CONFIG", default="",
37                     help="The path of the config of monitoring client")
38 parser.add_argument("-p", "--POD_DISCRIPTOR", default="/tmp/pod.yaml",
39                     help="The path of pod discrition file")
40 args = parser.parse_args()
41
42 INSTALLERS = ['apex', 'compass']
43 if args.INSTALLER_TYPE not in INSTALLERS:
44     raise Exception("The installer is not supported.")
45 if not args.INSTALlATION_SCRIPT:
46     raise Exception("Must specify the client installation script path!")
47
48 if "barometer" in args.INSTALlATION_SCRIPT.lower():
49     CONFIG_FILE = "/etc/barometer_config/barometer_client.conf"
50     CONFIG_DIR = "barometer_config"
51     INSTALlATION_SCRIPT = "/etc/barometer_config/install.sh"
52 elif "collectd" in args.INSTALlATION_SCRIPT.lower():
53     CONFIG_FILE = "/etc/collectd_config/collectd_client.conf"
54     CONFIG_DIR = "collectd_config"
55     INSTALlATION_SCRIPT = "/etc/collectd_config/install.sh"
56 elif "cadvisor" in args.INSTALlATION_SCRIPT.lower():
57     CONFIG_DIR = "cadvisor_config"
58     INSTALlATION_SCRIPT = "/etc/cadvisor_config/install.sh"
59 else:
60     raise Exception("The monitor client is not supported")
61
62
63 def main():
64     with open(args.POD_DISCRIPTOR) as f:
65         dataMap = yaml.safe_load(f)
66         for x in dataMap:
67             for y in dataMap[x]:
68                 if (y['role'].lower() == 'controller') or (
69                         y['role'].lower() == 'compute'):
70                     ip = str(y['ip'])
71                     user = str(y['user'])
72                     pwd = str(y['password'])
73                     idkey = str(y['key_filename'])
74
75                     if pwd:
76                         ssh_d = ssh.SSH(user, host=ip, password=pwd)
77                     elif idkey:
78                         idkey = "/tmp/id_rsa"
79                         ssh_d = ssh.SSH(user, host=ip, key_filename=idkey)
80                     status, stdout, stderr = ssh_d.execute(
81                         "cd /etc && mkdir " + CONFIG_DIR
82                     )
83                     if status:
84                         print Exception(
85                             "Command: \"mkdir {}\".format(CONFIG_DIR) failed.")
86                         logger.info(stdout.splitlines())
87                     if args.CLIENT_CONFIG:
88                         with open(args.CLIENT_CONFIG) as stdin_file:
89                             ssh_d.run("cat > " + CONFIG_FILE,
90                                       stdin=stdin_file)
91                     with open(args.INSTALlATION_SCRIPT) as stdin_file:
92                         ssh_d.run("cat > " + INSTALlATION_SCRIPT,
93                                   stdin=stdin_file)
94
95                     for u in os.uname():
96                         if 'ubuntu' in u.lower():
97                             NODE_OS = 'ubuntu'
98                             break
99                     if NODE_OS == 'ubuntu':
100                         status, stdout, stderr = ssh_d.execute(
101                             "sudo apt-get install -y docker.io"
102                         )
103                     else:
104                         status, stdout, stderr = ssh_d.execute(
105                             "sudo yum install -y docker-io"
106                         )
107                     if status:
108                         raise Exception(
109                             "Command for installing docker failed.")
110                         logger.info(stdout.splitlines())
111
112                     ssh_d.run(
113                         "cd /etc/{}/ && bash ./install.sh".format(CONFIG_DIR)
114                     )
115
116
117 if __name__ == '__main__':
118     main()