Add support for Ampere Falcon HW
[ta/remote-installer.git] / src / remoteinstaller / installer / bmc_management / hw17.py
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from .bmctools import BMC
16 import logging
17 import time
18
19 class BMCException(Exception):
20     pass
21
22 class HW17(BMC):
23     def __init__(self, host, user, passwd, priv_level='ADMINISTRATOR', log_path=None):
24         super(HW17, self).__init__(host, user, passwd, priv_level, log_path)
25
26     def attach_virtual_cd(self, nfs_host, nfs_mount, boot_iso_filename):
27         for _ in range(2):
28             self._setup_bmc_nfs_service(nfs_host, nfs_mount, boot_iso_filename)
29             success = self._wait_for_bmc_nfs_service(90, 'mounted')
30             if success:
31                 return True
32             else:
33                 logging.debug('BMC NFS server did not start yet')
34                 self.reset()
35
36         raise BMCException('NFS service setup failed')
37
38     def _detach_virtual_media(self):
39         logging.debug('Detach virtual media')
40
41         comp_code = self._run_ipmitool_raw_command('0x3c 0x00')
42         if comp_code[0] == '80':
43             raise BMCException('BMC NFS service reset failed, cannot get configuration')
44         elif comp_code[0] == '81':
45             raise BMCException('BMC NFS service reset failed, cannot set configuration')
46         else:
47             BMCException('BMC NFS service reset failed (rc={})'.format(comp_code))
48
49     def _set_boot_from_virtual_media(self):
50         logging.debug('Set boot from cd (%s), and boot after that', self._host)
51         self._run_ipmitool_command('chassis bootdev floppy options=persistent')
52
53     def _get_bmc_nfs_service_status(self):
54         logging.debug('Get BMC NFS service status')
55
56         status_code = self._run_ipmitool_raw_command('0x3c 0x03')
57         if status_code[0] == '00':
58             status = 'mounted'
59         elif status_code[0] == '64':
60             status = 'mounting'
61         elif status_code[0] == 'ff':
62             status = 'dismounted'
63         elif status_code[0] == '20':
64             status = 'nfserror'
65         else:
66             raise BMCException('Could not get BMC NFS service status (rc={})'.format(status_code))
67
68         logging.debug('Returned status: %s', status)
69         return status
70
71     def _set_bmc_nfs_configuration(self, nfs_host, mount_path, image_name):
72         logging.debug('Set BMC NFS configuration')
73
74         nfs_host_hex = self._convert_to_hex(nfs_host)
75         mount_path_hex = self._convert_to_hex(mount_path)
76         image_name_hex = self._convert_to_hex(image_name)
77
78         logging.debug('Set the IP address of the BMC NFS service (%s)', self._host)
79         comp_code = self._run_ipmitool_raw_command('0x3c 0x01 0x00 {} 0x00'.format(nfs_host_hex))
80         if comp_code[0] != '':
81             raise BMCException('Failed to set BMC NFS service IP address (rc={})'.format(comp_code))
82
83         logging.debug('Set the path of the BMC NFS service (%s)', mount_path)
84         comp_code = self._run_ipmitool_raw_command('0x3c 0x01 0x01 {} 0x00'.format(mount_path_hex))
85         if comp_code[0] != '':
86             raise BMCException('Failed to set BMC NFS service path (rc={})'.format(comp_code))
87
88         logging.debug('Set the ISO image name of the BMC NFS service (%s)', image_name)
89         comp_code = self._run_ipmitool_raw_command('0x3c 0x01 0x02 {} 0x00'.format(image_name_hex))
90         if comp_code[0] != '':
91             raise BMCException('Failed to set BMC NFS service iso image name (rc={})'.format(comp_code))
92
93     def _setup_bmc_nfs_service(self, nfs_host, mount_path, image_name):
94         logging.debug('Setup BMC NFS service')
95
96         self._detach_virtual_media()
97         self._set_bmc_nfs_configuration(nfs_host, mount_path, image_name)
98
99         logging.debug('Start the BMC NFS service')
100         comp_code = self._run_ipmitool_raw_command('0x3c 0x02 0x01')
101         if comp_code[0] != '':
102             raise BMCException('Failed to start the BMC NFS service (rc={})'.format(comp_code))
103