Merge "Fixes external network bridge and adds quota limits"
[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         log('Zero the MBR')
36         self.dha.node_zero_mbr(self.fuel_node_id)
37
38         self.dha.node_set_boot_order(self.fuel_node_id, ['disk', 'iso'])
39
40         self.proceed_with_installation()
41
42     def proceed_with_installation(self):
43         log('Eject ISO')
44         self.dha.node_eject_iso(self.fuel_node_id)
45
46         log('Insert ISO %s' % self.iso_file)
47         self.dha.node_insert_iso(self.fuel_node_id, self.iso_file)
48
49         self.dha.node_power_on(self.fuel_node_id)
50
51         log('Waiting for Fuel master to accept SSH')
52         self.wait_for_node_up()
53
54         log('Wait until Fuel menu is up')
55         fuel_menu_pid = self.wait_until_fuel_menu_up()
56
57         log('Inject our own astute.yaml settings')
58         self.inject_own_astute_yaml()
59
60         log('Let the Fuel deployment continue')
61         log('Found FUEL menu as PID %s, now killing it' % fuel_menu_pid)
62         self.ssh_exec_cmd('kill %s' % fuel_menu_pid, False)
63
64         log('Wait until installation complete')
65         self.wait_until_installation_completed()
66
67         log('Waiting for one minute for Fuel to stabilize')
68         time.sleep(60)
69
70         log('Eject ISO')
71         self.dha.node_eject_iso(self.fuel_node_id)
72
73         log('Fuel Master installed successfully !')
74
75     def wait_for_node_up(self):
76         WAIT_LOOP = 60
77         SLEEP_TIME = 10
78         success = False
79         for i in range(WAIT_LOOP):
80             try:
81                 self.ssh.open()
82                 success = True
83                 break
84             except Exception as e:
85                 log('Trying to SSH into Fuel VM %s ... sleeping %s seconds'
86                     % (self.fuel_ip, SLEEP_TIME))
87                 time.sleep(SLEEP_TIME)
88             finally:
89                 self.ssh.close()
90
91         if not success:
92            err('Could not SSH into Fuel VM %s' % self.fuel_ip)
93
94     def wait_until_fuel_menu_up(self):
95         WAIT_LOOP = 60
96         SLEEP_TIME = 10
97         CMD = 'ps -ef'
98         SEARCH = 'fuelmenu'
99         fuel_menu_pid = None
100         with self.ssh:
101             for i in range(WAIT_LOOP):
102                 ret = self.ssh.exec_cmd(CMD)
103                 fuel_menu_pid = self.get_fuel_menu_pid(ret, SEARCH)
104                 if not fuel_menu_pid:
105                     time.sleep(SLEEP_TIME)
106                 else:
107                     break
108         if not fuel_menu_pid:
109             err('Could not find the Fuel Menu Process ID')
110         return fuel_menu_pid
111
112     def get_fuel_menu_pid(self, printout, search):
113         fuel_menu_pid = None
114         for line in printout.splitlines():
115             if search in line:
116                 fuel_menu_pid = clean(line)[1]
117                 break
118         return fuel_menu_pid
119
120     def ssh_exec_cmd(self, cmd, check=True):
121         with self.ssh:
122             ret = self.ssh.exec_cmd(cmd, check=check)
123         return ret
124
125     def inject_own_astute_yaml(self):
126         dest ='~/%s/' % self.work_dir
127
128         with self.ssh as s:
129             s.exec_cmd('rm -rf %s' % self.work_dir, check=False)
130             s.exec_cmd('mkdir ~/%s' % self.work_dir)
131             s.scp_put(self.dea_file, dest)
132             s.scp_put('%s/common.py' % self.file_dir, dest)
133             s.scp_put('%s/dea.py' % self.file_dir, dest)
134             s.scp_put('%s/transplant_fuel_settings.py' % self.file_dir, dest)
135             log('Modifying Fuel astute')
136             s.run('python ~/%s/%s ~/%s/%s'
137                   % (self.work_dir, TRANSPLANT_FUEL_SETTINGS,
138                      self.work_dir, os.path.basename(self.dea_file)))
139
140     def wait_until_installation_completed(self):
141         WAIT_LOOP = 320
142         SLEEP_TIME = 10
143         CMD = 'ps -ef | grep %s | grep -v grep' % BOOTSTRAP_ADMIN
144
145         install_completed = False
146         with self.ssh:
147             for i in range(WAIT_LOOP):
148                 ret = self.ssh.exec_cmd(CMD)
149                 if not ret:
150                     install_completed = True
151                     break
152                 else:
153                     time.sleep(SLEEP_TIME)
154
155         if not install_completed:
156             err('Fuel installation did not complete')