7ace713d6b7999bdeaa58011906214264bf6a40a
[genesis.git] / fuel / deploy / install_fuel_master.py
1 ###############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # szilard.cserey@ericsson.com
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
11 import common
12 import time
13 import os
14 import glob
15 from ssh_client import SSHClient
16 from dha_adapters.libvirt_adapter import LibvirtAdapter
17
18 log = common.log
19 err = common.err
20 clean = common.clean
21 delete = common.delete
22
23 TRANSPLANT_FUEL_SETTINGS = 'transplant_fuel_settings.py'
24 BOOTSTRAP_ADMIN = '/usr/local/sbin/bootstrap_admin_node'
25 FUEL_CLIENT_CONFIG = '/etc/fuel/client/config.yaml'
26 PLUGINS_DIR = '~/plugins'
27
28
29 class InstallFuelMaster(object):
30
31     def __init__(self, dea_file, dha_file, fuel_ip, fuel_username,
32                  fuel_password, fuel_node_id, iso_file, work_dir,
33                  fuel_plugins_dir):
34         self.dea_file = dea_file
35         self.dha = LibvirtAdapter(dha_file)
36         self.fuel_ip = fuel_ip
37         self.fuel_username = fuel_username
38         self.fuel_password = fuel_password
39         self.fuel_node_id = fuel_node_id
40         self.iso_file = iso_file
41         self.iso_dir = os.path.dirname(self.iso_file)
42         self.work_dir = work_dir
43         self.fuel_plugins_dir = fuel_plugins_dir
44         self.file_dir = os.path.dirname(os.path.realpath(__file__))
45         self.ssh = SSHClient(self.fuel_ip, self.fuel_username,
46                              self.fuel_password)
47
48     def install(self):
49         log('Start Fuel Installation')
50
51         self.dha.node_power_off(self.fuel_node_id)
52
53         log('Zero the MBR')
54         self.dha.node_zero_mbr(self.fuel_node_id)
55
56         self.dha.node_set_boot_order(self.fuel_node_id, ['disk', 'iso'])
57
58         try:
59             self.proceed_with_installation()
60         except Exception as e:
61             self.post_install_cleanup()
62             err(e)
63
64     def proceed_with_installation(self):
65         log('Eject ISO')
66         self.dha.node_eject_iso(self.fuel_node_id)
67
68         log('Insert ISO %s' % self.iso_file)
69         self.dha.node_insert_iso(self.fuel_node_id, self.iso_file)
70
71         self.dha.node_power_on(self.fuel_node_id)
72
73         log('Waiting for Fuel master to accept SSH')
74         self.wait_for_node_up()
75
76         log('Wait until Fuel menu is up')
77         fuel_menu_pid = self.wait_until_fuel_menu_up()
78
79         log('Inject our own astute.yaml settings')
80         self.inject_own_astute_yaml()
81
82         log('Let the Fuel deployment continue')
83         log('Found FUEL menu as PID %s, now killing it' % fuel_menu_pid)
84         self.ssh_exec_cmd('kill %s' % fuel_menu_pid, False)
85
86         log('Wait until installation complete')
87         self.wait_until_installation_completed()
88
89         log('Waiting for one minute for Fuel to stabilize')
90         time.sleep(60)
91
92         self.delete_deprecated_fuel_client_config_from_fuel_6_1()
93
94         self.upload_plugin_files()
95
96         self.install_plugins()
97
98         self.post_install_cleanup()
99
100         log('Fuel Master installed successfully !')
101
102     def upload_plugin_files(self):
103         with self.ssh as s:
104             s.exec_cmd('mkdir %s' % PLUGINS_DIR)
105             if self.fuel_plugins_dir:
106                 for f in glob.glob('%s/*.rpm' % self.fuel_plugins_dir):
107                     s.scp_put(f, PLUGINS_DIR)
108
109     def install_plugins(self):
110         log('Installing Fuel Plugins')
111         with self.ssh as s:
112             r = s.exec_cmd('find %s -type f -name \'*.rpm\'' % PLUGINS_DIR)
113             for f in r.splitlines():
114                 log('Found plugin %s, installing ...' % f)
115                 r, e = s.exec_cmd('fuel plugins --install %s' % f, False)
116                 if e and 'does not update installed package' not in r:
117                     raise('Installation of Fuel Plugin %s failed' % f)
118
119     def wait_for_node_up(self):
120         WAIT_LOOP = 60
121         SLEEP_TIME = 10
122         success = False
123         for i in range(WAIT_LOOP):
124             try:
125                 self.ssh.open()
126                 success = True
127                 break
128             except Exception as e:
129                 log('Trying to SSH into Fuel VM %s ... sleeping %s seconds'
130                     % (self.fuel_ip, SLEEP_TIME))
131                 time.sleep(SLEEP_TIME)
132             finally:
133                 self.ssh.close()
134
135         if not success:
136             raise('Could not SSH into Fuel VM %s' % self.fuel_ip)
137
138     def wait_until_fuel_menu_up(self):
139         WAIT_LOOP = 60
140         SLEEP_TIME = 10
141         CMD = 'ps -ef'
142         SEARCH = 'fuelmenu'
143         fuel_menu_pid = None
144         with self.ssh:
145             for i in range(WAIT_LOOP):
146                 ret = self.ssh.exec_cmd(CMD)
147                 fuel_menu_pid = self.get_fuel_menu_pid(ret, SEARCH)
148                 if not fuel_menu_pid:
149                     time.sleep(SLEEP_TIME)
150                 else:
151                     break
152         if not fuel_menu_pid:
153             raise('Could not find the Fuel Menu Process ID')
154         return fuel_menu_pid
155
156     def get_fuel_menu_pid(self, printout, search):
157         for line in printout.splitlines():
158             if line.endswith(search):
159                 return clean(line)[1]
160
161     def ssh_exec_cmd(self, cmd, check=True):
162         with self.ssh:
163             ret = self.ssh.exec_cmd(cmd, check=check)
164         return ret
165
166     def inject_own_astute_yaml(self):
167         with self.ssh as s:
168             s.exec_cmd('rm -rf %s' % self.work_dir, False)
169             s.exec_cmd('mkdir %s' % self.work_dir)
170             s.scp_put(self.dea_file, self.work_dir)
171             s.scp_put('%s/common.py' % self.file_dir, self.work_dir)
172             s.scp_put('%s/dea.py' % self.file_dir, self.work_dir)
173             s.scp_put('%s/transplant_fuel_settings.py'
174                       % self.file_dir, self.work_dir)
175             log('Modifying Fuel astute')
176             s.run('python %s/%s %s/%s'
177                   % (self.work_dir, TRANSPLANT_FUEL_SETTINGS,
178                      self.work_dir, os.path.basename(self.dea_file)))
179
180     def wait_until_installation_completed(self):
181         WAIT_LOOP = 360
182         SLEEP_TIME = 10
183         CMD = 'ps -ef | grep %s | grep -v grep' % BOOTSTRAP_ADMIN
184
185         install_completed = False
186         with self.ssh:
187             for i in range(WAIT_LOOP):
188                 ret = self.ssh.exec_cmd(CMD)
189                 if not ret:
190                     install_completed = True
191                     break
192                 else:
193                     time.sleep(SLEEP_TIME)
194
195         if not install_completed:
196             raise('Fuel installation did not complete')
197
198     def post_install_cleanup(self):
199         log('Eject ISO file %s' % self.iso_file)
200         self.dha.node_eject_iso(self.fuel_node_id)
201         log('Remove ISO directory %s' % self.iso_dir)
202         delete(self.iso_dir)
203
204     def delete_deprecated_fuel_client_config_from_fuel_6_1(self):
205         with self.ssh as s:
206             response, error = s.exec_cmd('fuel -v', False)
207         if (error and
208             'DEPRECATION WARNING' in error and
209             '6.1.0' in error and
210             FUEL_CLIENT_CONFIG in error):
211             log('Delete deprecated fuel client config %s' % FUEL_CLIENT_CONFIG)
212             with self.ssh as s:
213                 s.exec_cmd('rm %s' % FUEL_CLIENT_CONFIG, False)