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