a6537214eed447d2522f66e54c627fea43e33a54
[ta/remote-installer.git] / src / remoteinstaller / installer / catfile.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 import sys
16 import argparse
17 import logging
18 import pexpect
19
20 class CatFileException(Exception):
21     pass
22
23 class CatFile(object):
24     def __init__(self, bmc_host, bmc_user, bmc_password, login_user, login_password):
25         self._host = bmc_host
26         self._user = bmc_user
27         self._password = bmc_password
28         self._sol = None
29
30         self._login_user = login_user
31         self._login_password = login_password
32
33     def _open_console(self, log):
34         logging.info('Open SOL console')
35
36         logging.debug('deactivate sol')
37         expect_session = pexpect.spawn('ipmitool -I lanplus -H {} -U {} -P {} sol deactivate'.format(self._host, self._user, self._password))
38         expect_session.expect(pexpect.EOF)
39
40         logging.debug('activate sol, output will go to %s', log)
41         self._sol = pexpect.spawn('ipmitool -I lanplus -H {} -U {} -P {} sol activate'.format(self._host, self._user, self._password), timeout=None)
42         logfile = open(log, 'wb')
43         self._sol.logfile_read = logfile
44
45     def _close_console(self):
46         logging.info('Close SOL console')
47
48         if self._sol:
49             logging.debug('Logout from host')
50             self._sol.sendline('logout\r\n')
51             self._sol.sendline()
52             self._sol.expect('login:', timeout=10)
53             self._sol.terminate()
54
55         logging.debug('deactivate sol')
56         session = pexpect.spawn('ipmitool -I lanplus -H {} -U {} -P {} sol deactivate'.format(self._host, self._user, self._password))
57         session.expect(pexpect.EOF)
58
59     def _expect_cmd_prompt(self):
60         self._sol.expect('# ', timeout=10)
61
62     def _login(self):
63         logging.info('Login to host')
64
65         try:
66             self._sol.sendline()
67
68             self._expect_cmd_prompt()
69             logging.debug('Command prompt found')
70
71             return
72         except pexpect.exceptions.TIMEOUT as e:
73             pass
74
75         try:
76             self._sol.sendline()
77
78             self._sol.expect('login:', timeout=10)
79             logging.debug('Login prompt found')
80
81             self._sol.sendline(self._login_user)
82
83             self._sol.expect('Password:', timeout=10)
84             logging.debug('Password prompt found')
85
86             self._sol.sendline(self._login_password)
87
88             self._sol.sendline()
89             self._expect_cmd_prompt()
90             logging.debug('Command prompt found')
91         except pexpect.exceptions.TIMEOUT as e:
92             logging.debug(e)
93             raise
94
95     def _cat_log(self, path, timeout=120):
96         logging.debug('Catting %s', path)
97
98         self._sol.sendline('cat {}; echo CONSOLE_CAT_DONE\r\n'.format(path))
99         self._sol.expect('CONSOLE_CAT_DONE', timeout=timeout)
100         logging.debug('Catting done')
101
102         self._expect_cmd_prompt()
103
104     def cat(self, path, log_file, timeout=None):
105         try:
106             self._open_console(log_file)
107             self._login()
108             self._cat_log(path, timeout)
109             self._close_console()
110         except Exception as ex:
111             logging.warn('Cat file failed: %s', str(ex))
112             raise CatFileException(str(ex))
113
114 def main():
115     parser = argparse.ArgumentParser()
116     parser.add_argument('-H', '--bmc_host', required=True, help='BMC host')
117     parser.add_argument('-U', '--bmc_user', required=True, help='BMC user')
118     parser.add_argument('-P', '--bmc_password', required=True, help='BMC user password')
119     parser.add_argument('-u', '--user', required=True, help='Login user')
120     parser.add_argument('-p', '--password', required=True, help='Login user password')
121     parser.add_argument('-f', '--file', required=True, help='File path to cat')
122     parser.add_argument('-o', '--output_file', required=True, help='Output file name of the log')
123     parser.add_argument('-t', '--timeout', required=False, help='Timeout for catting the file')
124
125     args = parser.parse_args()
126
127     logging.basicConfig(level=logging.DEBUG)
128
129     cat_file = CatFile(args.bmc_host, args.bmc_user, args.bmc_password, args.user, args.password)
130     cat_file.cat(args.file, args.output_file, args.password)
131
132 if __name__ == "__main__":
133     sys.exit(main())