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