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 ###############################################################################
15 from ssh_client import SSHClient
16 from dha_adapters.libvirt_adapter import LibvirtAdapter
21 delete = common.delete
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 LOCAL_PLUGIN_FOLDER = '/opt/opnfv'
30 class InstallFuelMaster(object):
32 def __init__(self, dea_file, dha_file, fuel_ip, fuel_username,
33 fuel_password, fuel_node_id, iso_file, work_dir,
35 self.dea_file = dea_file
36 self.dha = LibvirtAdapter(dha_file)
37 self.fuel_ip = fuel_ip
38 self.fuel_username = fuel_username
39 self.fuel_password = fuel_password
40 self.fuel_node_id = fuel_node_id
41 self.iso_file = iso_file
42 self.iso_dir = os.path.dirname(self.iso_file)
43 self.work_dir = work_dir
44 self.fuel_plugins_dir = fuel_plugins_dir
45 self.file_dir = os.path.dirname(os.path.realpath(__file__))
46 self.ssh = SSHClient(self.fuel_ip, self.fuel_username,
50 log('Start Fuel Installation')
52 self.dha.node_power_off(self.fuel_node_id)
55 self.dha.node_zero_mbr(self.fuel_node_id)
57 self.dha.node_set_boot_order(self.fuel_node_id, ['disk', 'iso'])
60 self.proceed_with_installation()
61 except Exception as e:
62 self.post_install_cleanup()
65 def proceed_with_installation(self):
67 self.dha.node_eject_iso(self.fuel_node_id)
69 log('Insert ISO %s' % self.iso_file)
70 self.dha.node_insert_iso(self.fuel_node_id, self.iso_file)
72 self.dha.node_power_on(self.fuel_node_id)
74 log('Waiting for Fuel master to accept SSH')
75 self.wait_for_node_up()
77 log('Wait until Fuel menu is up')
78 fuel_menu_pid = self.wait_until_fuel_menu_up()
80 log('Inject our own astute.yaml settings')
81 self.inject_own_astute_yaml()
83 log('Let the Fuel deployment continue')
84 log('Found FUEL menu as PID %s, now killing it' % fuel_menu_pid)
85 self.ssh_exec_cmd('kill %s' % fuel_menu_pid, False)
87 log('Wait until installation complete')
88 self.wait_until_installation_completed()
90 log('Waiting for one minute for Fuel to stabilize')
93 self.delete_deprecated_fuel_client_config_from_fuel_6_1()
95 self.collect_plugin_files()
97 self.install_plugins()
99 self.post_install_cleanup()
101 log('Fuel Master installed successfully !')
103 def collect_plugin_files(self):
105 s.exec_cmd('mkdir %s' % PLUGINS_DIR)
106 if self.fuel_plugins_dir:
107 for f in glob.glob('%s/*.rpm' % self.fuel_plugins_dir):
108 s.scp_put(f, PLUGINS_DIR)
110 s.exec_cmd('cp %s/*.rpm %s' % (LOCAL_PLUGIN_FOLDER,
113 def install_plugins(self):
114 log('Installing Fuel Plugins')
116 r = s.exec_cmd('find %s -type f -name \'*.rpm\'' % PLUGINS_DIR)
117 for f in r.splitlines():
118 log('Found plugin %s, installing ...' % f)
119 r, e = s.exec_cmd('fuel plugins --install %s' % f, False)
120 if e and 'does not update installed package' not in r:
121 raise Exception('Installation of Fuel Plugin %s '
122 'failed: %s' % (f, e))
124 def wait_for_node_up(self):
128 for i in range(WAIT_LOOP):
133 except Exception as e:
134 log('Trying to SSH into Fuel VM %s ... sleeping %s seconds'
135 % (self.fuel_ip, SLEEP_TIME))
136 time.sleep(SLEEP_TIME)
141 raise Exception('Could not SSH into Fuel VM %s' % self.fuel_ip)
143 def wait_until_fuel_menu_up(self):
150 for i in range(WAIT_LOOP):
151 ret = self.ssh.exec_cmd(CMD)
152 fuel_menu_pid = self.get_fuel_menu_pid(ret, SEARCH)
153 if not fuel_menu_pid:
154 time.sleep(SLEEP_TIME)
157 if not fuel_menu_pid:
158 raise Exception('Could not find the Fuel Menu Process ID')
161 def get_fuel_menu_pid(self, printout, search):
162 for line in printout.splitlines():
163 if line.endswith(search):
164 return clean(line)[1]
166 def ssh_exec_cmd(self, cmd, check=True):
168 ret = self.ssh.exec_cmd(cmd, check=check)
171 def inject_own_astute_yaml(self):
173 s.exec_cmd('rm -rf %s' % self.work_dir, False)
174 s.exec_cmd('mkdir %s' % self.work_dir)
175 s.scp_put(self.dea_file, self.work_dir)
176 s.scp_put('%s/common.py' % self.file_dir, self.work_dir)
177 s.scp_put('%s/dea.py' % self.file_dir, self.work_dir)
178 s.scp_put('%s/transplant_fuel_settings.py'
179 % self.file_dir, self.work_dir)
180 log('Modifying Fuel astute')
181 s.run('python %s/%s %s/%s'
182 % (self.work_dir, TRANSPLANT_FUEL_SETTINGS,
183 self.work_dir, os.path.basename(self.dea_file)))
185 def wait_until_installation_completed(self):
188 CMD = 'ps -ef | grep %s | grep -v grep' % BOOTSTRAP_ADMIN
190 install_completed = False
192 for i in range(WAIT_LOOP):
193 ret = self.ssh.exec_cmd(CMD)
195 install_completed = True
198 time.sleep(SLEEP_TIME)
200 if not install_completed:
201 raise Exception('Fuel installation did not complete')
203 def post_install_cleanup(self):
204 log('Eject ISO file %s' % self.iso_file)
205 self.dha.node_eject_iso(self.fuel_node_id)
206 log('Remove ISO directory %s' % self.iso_dir)
209 def delete_deprecated_fuel_client_config_from_fuel_6_1(self):
211 response, error = s.exec_cmd('fuel -v', False)
213 'DEPRECATION WARNING' in error and
215 FUEL_CLIENT_CONFIG in error):
216 log('Delete deprecated fuel client config %s' % FUEL_CLIENT_CONFIG)
218 s.exec_cmd('rm %s' % FUEL_CLIENT_CONFIG, False)