Adapt monitor 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                 print("Installing {} in: {}".format(INSTALlATION_SCRIPT, y))
69                 pwd = idkey = ''
70                 if (y['role'].lower() == 'controller') or (
71                         y['role'].lower() == 'compute'):
72                     ip = str(y['ip'])
73                     user = str(y['user'])
74                     if 'password' in y.keys():
75                         pwd = str(y['password'])
76                     if 'key_filename' in y.keys():
77                         idkey = str(y['key_filename'])
78
79                     if pwd:
80                         ssh_d = ssh.SSH(user, host=ip, password=pwd)
81                     elif idkey:
82                         idkey = "/tmp/id_rsa"
83                         ssh_d = ssh.SSH(user, host=ip, key_filename=idkey)
84                     status, stdout, stderr = ssh_d.execute(
85                         "cd /etc && if [ ! -d " + CONFIG_DIR +
86                         " ]; then sudo mkdir " + CONFIG_DIR + "; fi"
87                     )
88                     if status:
89                         print Exception(
90                             "Command: \"mkdir {}\"".format(CONFIG_DIR) +
91                             " failed."
92                         )
93                         logger.info(stdout.splitlines())
94                     if args.CLIENT_CONFIG:
95                         with open(args.CLIENT_CONFIG) as stdin_file:
96                             ssh_d.run("sudo sh -c \"cat > " + CONFIG_FILE +
97                                       "\"",
98                                       stdin=stdin_file)
99                     with open(args.INSTALlATION_SCRIPT) as stdin_file:
100                         ssh_d.run("sudo sh -c \"cat > " + INSTALlATION_SCRIPT +
101                                   "\"",
102                                   stdin=stdin_file)
103
104                     for u in os.uname():
105                         if 'ubuntu' in u.lower():
106                             NODE_OS = 'ubuntu'
107                             break
108                     if NODE_OS == 'ubuntu':
109                         status, stdout, stderr = ssh_d.execute(
110                             "sudo apt-get install -y docker.io"
111                         )
112                     else:
113                         status, stdout, stderr = ssh_d.execute(
114                             "sudo service docker start"
115                         )
116                     if status:
117                         raise Exception(
118                             "Command for installing docker failed.")
119                         logger.info(stdout.splitlines())
120
121                     ssh_d.run(
122                         "cd /etc/{}/ && bash ./install.sh".format(CONFIG_DIR),
123                         raise_on_error=False
124                     )
125
126
127 if __name__ == '__main__':
128     main()