Updates docs for SR1 with final revision
[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 LOCAL_PLUGIN_FOLDER = '/opt/opnfv'
28
29
30 class InstallFuelMaster(object):
31
32     def __init__(self, dea_file, dha_file, fuel_ip, fuel_username,
33                  fuel_password, fuel_node_id, iso_file, work_dir,
34                  fuel_plugins_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,
47                              self.fuel_password)
48
49     def install(self):
50         log('Start Fuel Installation')
51
52         self.dha.node_power_off(self.fuel_node_id)
53
54         log('Zero the MBR')
55         self.dha.node_zero_mbr(self.fuel_node_id)
56
57         self.dha.node_set_boot_order(self.fuel_node_id, ['disk', 'iso'])
58
59         try:
60             self.proceed_with_installation()
61         except Exception as e:
62             self.post_install_cleanup()
63             err(e)
64
65     def proceed_with_installation(self):
66         log('Eject ISO')
67         self.dha.node_eject_iso(self.fuel_node_id)
68
69         log('Insert ISO %s' % self.iso_file)
70         self.dha.node_insert_iso(self.fuel_node_id, self.iso_file)
71
72         self.dha.node_power_on(self.fuel_node_id)
73
74         log('Waiting for Fuel master to accept SSH')
75         self.wait_for_node_up()
76
77         log('Wait until Fuel menu is up')
78         fuel_menu_pid = self.wait_until_fuel_menu_up()
79
80         log('Inject our own astute.yaml settings')
81         self.inject_own_astute_yaml()
82
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)
86
87         log('Wait until installation complete')
88         self.wait_until_installation_completed()
89
90         log('Waiting for one minute for Fuel to stabilize')
91         time.sleep(60)
92
93         self.delete_deprecated_fuel_client_config_from_fuel_6_1()
94
95         self.collect_plugin_files()
96
97         self.install_plugins()
98
99         self.post_install_cleanup()
100
101         log('Fuel Master installed successfully !')
102
103     def collect_plugin_files(self):
104         with self.ssh as s:
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)
109             else:
110                 s.exec_cmd('cp %s/*.rpm %s' % (LOCAL_PLUGIN_FOLDER,
111                                                PLUGINS_DIR))
112
113     def install_plugins(self):
114         log('Installing Fuel Plugins')
115         with self.ssh as s:
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))
123
124     def wait_for_node_up(self):
125         WAIT_LOOP = 60
126         SLEEP_TIME = 10
127         success = False
128         for i in range(WAIT_LOOP):
129             try:
130                 self.ssh.open()
131                 success = True
132                 break
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)
137             finally:
138                 self.ssh.close()
139
140         if not success:
141             raise Exception('Could not SSH into Fuel VM %s' % self.fuel_ip)
142
143     def wait_until_fuel_menu_up(self):
144         WAIT_LOOP = 60
145         SLEEP_TIME = 10
146         CMD = 'ps -ef'
147         SEARCH = 'fuelmenu'
148         fuel_menu_pid = None
149         with self.ssh:
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)
155                 else:
156                     break
157         if not fuel_menu_pid:
158             raise Exception('Could not find the Fuel Menu Process ID')
159         return fuel_menu_pid
160
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]
165
166     def ssh_exec_cmd(self, cmd, check=True):
167         with self.ssh:
168             ret = self.ssh.exec_cmd(cmd, check=check)
169         return ret
170
171     def inject_own_astute_yaml(self):
172         with self.ssh as s:
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)))
184
185     def wait_until_installation_completed(self):
186         WAIT_LOOP = 360
187         SLEEP_TIME = 10
188         CMD = 'ps -ef | grep %s | grep -v grep' % BOOTSTRAP_ADMIN
189
190         install_completed = False
191         with self.ssh:
192             for i in range(WAIT_LOOP):
193                 ret = self.ssh.exec_cmd(CMD)
194                 if not ret:
195                     install_completed = True
196                     break
197                 else:
198                     time.sleep(SLEEP_TIME)
199
200         if not install_completed:
201             raise Exception('Fuel installation did not complete')
202
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)
207         delete(self.iso_dir)
208
209     def delete_deprecated_fuel_client_config_from_fuel_6_1(self):
210         with self.ssh as s:
211             response, error = s.exec_cmd('fuel -v', False)
212         if (error and
213             'DEPRECATION WARNING' in error and
214             '6.1.0' in error and
215             FUEL_CLIENT_CONFIG in error):
216             log('Delete deprecated fuel client config %s' % FUEL_CLIENT_CONFIG)
217             with self.ssh as s:
218                 s.exec_cmd('rm %s' % FUEL_CLIENT_CONFIG, False)