|
1 | 1 | """ Stack based IOS-XE/cat9k/c9500X service implementations. """
|
| 2 | +import io |
| 3 | +import logging |
2 | 4 | from time import sleep
|
3 | 5 | from collections import namedtuple
|
4 | 6 | from datetime import timedelta
|
|
7 | 9 | from unicon.eal.dialogs import Dialog
|
8 | 10 | from unicon.core.errors import SubCommandFailure
|
9 | 11 | from unicon.bases.routers.services import BaseService
|
| 12 | +from unicon.logs import UniconStreamHandler, UNICON_LOG_FORMAT |
10 | 13 |
|
11 | 14 | from unicon.plugins.iosxe.stack.utils import StackUtils
|
12 | 15 | from unicon.plugins.generic.statements import custom_auth_statements
|
@@ -44,6 +47,7 @@ def __init__(self, connection, context, *args, **kwargs):
|
44 | 47 | self.end_state = 'enable'
|
45 | 48 | self.timeout = connection.settings.STACK_RELOAD_TIMEOUT
|
46 | 49 | self.reload_command = "redundancy reload shelf"
|
| 50 | + self.log_buffer = io.StringIO() |
47 | 51 | self.dialog = Dialog(stack_reload_stmt_list)
|
48 | 52 |
|
49 | 53 | def call_service(self,
|
@@ -82,6 +86,20 @@ def call_service(self,
|
82 | 86 | if not isinstance(append_error_pattern, list):
|
83 | 87 | raise ValueError('append_error_pattern should be a list')
|
84 | 88 | self.error_pattern += append_error_pattern
|
| 89 | + |
| 90 | + # Connecting to the log handler to capture the buffer output |
| 91 | + lb = UniconStreamHandler(self.log_buffer) |
| 92 | + lb.setFormatter(logging.Formatter(fmt=UNICON_LOG_FORMAT)) |
| 93 | + self.connection.log.addHandler(lb) |
| 94 | + |
| 95 | + # logging the output to subconnections |
| 96 | + for subcon in self.connection.subconnections: |
| 97 | + subcon.log.addHandler(lb) |
| 98 | + |
| 99 | + # Clear log buffer |
| 100 | + self.log_buffer.seek(0) |
| 101 | + self.log_buffer.truncate() |
| 102 | + |
85 | 103 | # update all subconnection context with image_to_boot
|
86 | 104 | if image_to_boot:
|
87 | 105 | for subconn in self.connection.subconnections:
|
@@ -243,12 +261,23 @@ def boot(con):
|
243 | 261 | self.connection.connection_provider.init_connection()
|
244 | 262 |
|
245 | 263 | self.connection.log.info("+++ Reload Completed Successfully +++")
|
| 264 | + |
| 265 | + # Read the log buffer |
| 266 | + self.log_buffer.seek(0) |
| 267 | + reload_output = self.log_buffer.read() |
| 268 | + # clear buffer |
| 269 | + self.log_buffer.truncate() |
| 270 | + |
| 271 | + # Remove the handler |
| 272 | + self.connection.log.removeHandler(lb) |
| 273 | + for subcon in self.connection.subconnections: |
| 274 | + subcon.log.removeHandler(lb) |
| 275 | + |
246 | 276 | self.result = True
|
247 | 277 |
|
248 | 278 | if return_output:
|
249 | 279 | Result = namedtuple('Result', ['result', 'output'])
|
250 |
| - self.result = Result(self.result, reload_cmd_output.match_output.replace(reload_cmd, '', 1)) |
251 |
| - |
| 280 | + self.result = Result(self.result, reload_output.replace(reload_cmd, '', 1)) |
252 | 281 |
|
253 | 282 | class SVLStackSwitchover(BaseService):
|
254 | 283 | """ Get Rp state
|
@@ -355,3 +384,4 @@ def call_service(self, command=None,
|
355 | 384 | else:
|
356 | 385 | self.connection.log.info('Switchover failed')
|
357 | 386 | self.result = False
|
| 387 | + |
0 commit comments