acacf379d07cce13015a0f2ffa227e039df0bc74
[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 Exception('Installation of Fuel Plugin %s '
118                                     'failed: %s' % (f, e))
119
120     def wait_for_node_up(self):
121         WAIT_LOOP = 60
122         SLEEP_TIME = 10
123         success = False
124         for i in range(WAIT_LOOP):
125             try:
126                 self.ssh.open()
127                 success = True
128                 break
129             except Exception as e:
130                 log('Trying to SSH into Fuel VM %s ... sleeping %s seconds'
131                     % (self.fuel_ip, SLEEP_TIME))
132                 time.sleep(SLEEP_TIME)
133             finally:
134                 self.ssh.close()
135
136         if not success:
137             raise Exception('Could not SSH into Fuel VM %s' % self.fuel_ip)
138
139     def wait_until_fuel_menu_up(self):
140         WAIT_LOOP = 60
141         SLEEP_TIME = 10
142         CMD = 'ps -ef'
143         SEARCH = 'fuelmenu'
144         fuel_menu_pid = None
145         with self.ssh:
146             for i in range(WAIT_LOOP):
147                 ret = self.ssh.exec_cmd(CMD)
148                 fuel_menu_pid = self.get_fuel_menu_pid(ret, SEARCH)
149                 if not fuel_menu_pid:
150                     time.sleep(SLEEP_TIME)
151                 else:
152                     break
153         if not fuel_menu_pid:
154             raise Exception('Could not find the Fuel Menu Process ID')
155         return fuel_menu_pid
156
157     def get_fuel_menu_pid(self, printout, search):
158         for line in printout.splitlines():
159             if line.endswith(search):
160                 return clean(line)[1]
161
162     def ssh_exec_cmd(self, cmd, check=True):
163         with self.ssh:
164             ret = self.ssh.exec_cmd(cmd, check=check)
165         return ret
166
167     def inject_own_astute_yaml(self):
168         with self.ssh as s:
169             s.exec_cmd('rm -rf %s' % self.work_dir, False)
170             s.exec_cmd('mkdir %s' % self.work_dir)
171             s.scp_put(self.dea_file, self.work_dir)
172             s.scp_put('%s/common.py' % self.file_dir, self.work_dir)
173             s.scp_put('%s/dea.py' % self.file_dir, self.work_dir)
174             s.scp_put('%s/transplant_fuel_settings.py'
175                       % self.file_dir, self.work_dir)
176             log('Modifying Fuel astute')
177             s.run('python %s/%s %s/%s'
178                   % (self.work_dir, TRANSPLANT_FUEL_SETTINGS,
179                      self.work_dir, os.path.basename(self.dea_file)))
180
181     def wait_until_installation_completed(self):
182         WAIT_LOOP = 360
183         SLEEP_TIME = 10
184         CMD = 'ps -ef | grep %s | grep -v grep' % BOOTSTRAP_ADMIN
185
186         install_completed = False
187         with self.ssh:
188             for i in range(WAIT_LOOP):
189                 ret = self.ssh.exec_cmd(CMD)
190                 if not ret:
191                     install_completed = True
192                     break
193                 else:
194                     time.sleep(SLEEP_TIME)
195
196         if not install_completed:
197             raise Exception('Fuel installation did not complete')
198
199     def post_install_cleanup(self):
200         log('Eject ISO file %s' % self.iso_file)
201         self.dha.node_eject_iso(self.fuel_node_id)
202         log('Remove ISO directory %s' % self.iso_dir)
203         delete(self.iso_dir)
204
205     def delete_deprecated_fuel_client_config_from_fuel_6_1(self):
206         with self.ssh as s:
207             response, error = s.exec_cmd('fuel -v', False)
208         if (error and
209             'DEPRECATION WARNING' in error and
210             '6.1.0' in error and
211             FUEL_CLIENT_CONFIG in error):
212             log('Delete deprecated fuel client config %s' % FUEL_CLIENT_CONFIG)
213             with self.ssh as s:
214                 s.exec_cmd('rm %s' % FUEL_CLIENT_CONFIG, False)