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