Autodeploy inspired on Prototype #2
[genesis.git] / fuel / deploy / install_fuel_master.py
1 import common
2 import time
3 import os
4 from ssh_client import SSHClient
5 from dha_adapters.libvirt_adapter import LibvirtAdapter
6
7 log = common.log
8 err = common.err
9 clean = common.clean
10
11 TRANSPLANT_FUEL_SETTINGS = 'transplant_fuel_settings.py'
12 BOOTSTRAP_ADMIN = '/usr/local/sbin/bootstrap_admin_node'
13
14 class InstallFuelMaster(object):
15
16     def __init__(self, dea_file, dha_file, fuel_ip, fuel_username, fuel_password,
17                  fuel_node_id, iso_file, work_dir):
18         self.dea_file = dea_file
19         self.dha = LibvirtAdapter(dha_file)
20         self.fuel_ip = fuel_ip
21         self.fuel_username = fuel_username
22         self.fuel_password = fuel_password
23         self.fuel_node_id = fuel_node_id
24         self.iso_file = iso_file
25         self.work_dir = work_dir
26         self.file_dir = os.path.dirname(os.path.realpath(__file__))
27         self.ssh = SSHClient(self.fuel_ip, self.fuel_username,
28                              self.fuel_password)
29
30     def install(self):
31         log('Start Fuel Installation')
32
33         self.dha.node_power_off(self.fuel_node_id)
34
35         self.zero_mbr_set_boot_order()
36
37         self.proceed_with_installation()
38
39     def custom_install(self):
40         log('Start Custom Fuel Installation')
41
42         self.dha.node_power_off(self.fuel_node_id)
43
44         log('Zero the MBR')
45         self.dha.node_zero_mbr(self.fuel_node_id)
46
47         self.dha.node_set_boot_order(self.fuel_node_id, ['disk', 'iso'])
48
49         self.proceed_with_installation()
50
51     def proceed_with_installation(self):
52         log('Eject ISO')
53         self.dha.node_eject_iso(self.fuel_node_id)
54
55         log('Insert ISO %s' % self.iso_file)
56         self.dha.node_insert_iso(self.fuel_node_id, self.iso_file)
57
58         self.dha.node_power_on(self.fuel_node_id)
59
60         log('Waiting for Fuel master to accept SSH')
61         self.wait_for_node_up()
62
63         log('Wait until Fuel menu is up')
64         fuel_menu_pid = self.wait_until_fuel_menu_up()
65
66         log('Inject our own astute.yaml settings')
67         self.inject_own_astute_yaml()
68
69         log('Let the Fuel deployment continue')
70         log('Found FUEL menu as PID %s, now killing it' % fuel_menu_pid)
71         self.ssh_exec_cmd('kill %s' % fuel_menu_pid)
72
73         log('Wait until installation complete')
74         self.wait_until_installation_completed()
75
76         log('Waiting for one minute for Fuel to stabilize')
77         time.sleep(60)
78
79         log('Eject ISO')
80         self.dha.node_eject_iso(self.fuel_node_id)
81
82         log('Fuel Master installed successfully !')
83
84     def zero_mbr_set_boot_order(self):
85         if self.dha.node_can_zero_mbr(self.fuel_node_id):
86             log('Fuel Node %s capable of zeroing MBR so doing that...'
87                 % self.fuel_node_id)
88             self.dha.node_zero_mbr(self.fuel_node_id)
89             self.dha.node_set_boot_order(self.fuel_node_id, ['disk', 'iso'])
90         elif self.dha.node_can_set_boot_order_live(self.fuel_node_id):
91             log('Node %s can change ISO boot order live' % self.fuel_node_id)
92             self.dha.node_set_boot_order(self.fuel_node_id, ['iso', 'disk'])
93         else:
94             err('No way to install Fuel node')
95
96     def wait_for_node_up(self):
97         WAIT_LOOP = 60
98         SLEEP_TIME = 10
99         success = False
100         for i in range(WAIT_LOOP):
101             try:
102                 self.ssh.open()
103                 success = True
104                 break
105             except Exception as e:
106                 log('EXCEPTION [%s] received when SSH-ing into Fuel VM %s ... '
107                     'sleeping %s seconds' % (e, self.fuel_ip, SLEEP_TIME))
108                 time.sleep(SLEEP_TIME)
109             finally:
110                 self.ssh.close()
111
112         if not success:
113            err('Could not SSH into Fuel VM %s' % self.fuel_ip)
114
115     def wait_until_fuel_menu_up(self):
116         WAIT_LOOP = 60
117         SLEEP_TIME = 10
118         CMD = 'ps -ef'
119         SEARCH = 'fuelmenu'
120         fuel_menu_pid = None
121         with self.ssh:
122             for i in range(WAIT_LOOP):
123                 ret = self.ssh.exec_cmd(CMD)
124                 fuel_menu_pid = self.get_fuel_menu_pid(ret, SEARCH)
125                 if not fuel_menu_pid:
126                     time.sleep(SLEEP_TIME)
127                 else:
128                     break
129         if not fuel_menu_pid:
130             err('Could not find the Fuel Menu Process ID')
131         return fuel_menu_pid
132
133     def get_fuel_menu_pid(self, printout, search):
134         fuel_menu_pid = None
135         for line in printout.splitlines():
136             if search in line:
137                 fuel_menu_pid = clean(line)[1]
138                 break
139         return fuel_menu_pid
140
141     def ssh_exec_cmd(self, cmd):
142         with self.ssh:
143             ret = self.ssh.exec_cmd(cmd)
144         return ret
145
146     def inject_own_astute_yaml(self):
147         dest ='~/%s/' % self.work_dir
148
149         with self.ssh as s:
150             s.exec_cmd('rm -rf %s' % self.work_dir, check=False)
151             s.exec_cmd('mkdir ~/%s' % self.work_dir)
152             s.scp_put(self.dea_file, dest)
153             s.scp_put('%s/common.py' % self.file_dir, dest)
154             s.scp_put('%s/dea.py' % self.file_dir, dest)
155             s.scp_put('%s/transplant_fuel_settings.py' % self.file_dir, dest)
156             log('Modifying Fuel astute')
157             s.run('python ~/%s/%s ~/%s/%s'
158                   % (self.work_dir, TRANSPLANT_FUEL_SETTINGS,
159                      self.work_dir, os.path.basename(self.dea_file)))
160
161     def wait_until_installation_completed(self):
162         WAIT_LOOP = 180
163         SLEEP_TIME = 10
164         CMD = 'ps -ef | grep %s | grep -v grep' % BOOTSTRAP_ADMIN
165
166         install_completed = False
167         with self.ssh:
168             for i in range(WAIT_LOOP):
169                 ret = self.ssh.exec_cmd(CMD)
170                 if not ret:
171                     install_completed = True
172                     break
173                 else:
174                     time.sleep(SLEEP_TIME)
175
176         if not install_completed:
177             err('Fuel installation did not complete')