def get_connection_dialog(self):
     con = self.connection
     connection_statement_list = authentication_statement_list + \
         pre_connection_statement_list
     custom_auth_stmt = custom_auth_statements(
                          self.connection.settings.LOGIN_PROMPT,
                          self.connection.settings.PASSWORD_PROMPT)
     if custom_auth_stmt:
         connection_statement_list = custom_auth_stmt + connection_statement_list
     return con.connect_reply + Dialog(connection_statement_list)
 def get_connection_dialog(self):
     """ creates and returns a Dialog to handle all device prompts
         appearing during initial connection to the device.
         See statements.py for connnection statement lists
     """
     con = self.connection
     custom_auth_stmt = custom_auth_statements(patterns.login_prompt,
                                               patterns.password)
     return con.connect_reply + \
                 Dialog(custom_auth_stmt + connection_statement_list
                     if custom_auth_stmt else connection_statement_list)
 def get_connection_dialog(self):
     """ creates and returns a Dialog to handle all device prompts
         appearing during initial connection to the device.
         See statements.py for connnection statement lists
     """
     con = self.connection
     custom_auth_stmt = custom_auth_statements(
                          self.connection.settings.LOGIN_PROMPT,
                          self.connection.settings.PASSWORD_PROMPT)
     return con.connect_reply \
                 + Dialog(custom_auth_stmt + connection_statement_list
                      if custom_auth_stmt else connection_statement_list)
    def call_service(self,
                     reload_command=None,
                     reply=Dialog([]),
                     timeout=None,
                     image_to_boot=None,
                     return_output=False,
                     *args,
                     **kwargs):

        self.result = False
        reload_cmd = reload_command or self.reload_command
        timeout = timeout or self.timeout
        conn = self.connection.active

        # update all subconnection context with image_to_boot
        if image_to_boot:
            for subconn in self.connection:
                subconn.context.image_to_boot = image_to_boot

        reload_dialog = self.dialog
        if reply:
            reload_dialog = reply + reload_dialog

        custom_auth_stmt = custom_auth_statements(
            conn.settings.LOGIN_PROMPT, conn.settings.PASSWORD_PROMPT)
        if custom_auth_stmt:
            reload_dialog += Dialog(custom_auth_stmt)

        reload_dialog += Dialog([switch_prompt])

        conn.log.info('Processing on active rp %s-%s' %
                      (conn.hostname, conn.alias))
        conn.sendline(reload_cmd)
        try:
            reload_cmd_output = reload_dialog.process(
                conn.spawn,
                timeout=timeout,
                prompt_recovery=self.prompt_recovery,
                context=conn.context)
        except Exception as e:
            raise SubCommandFailure('Error during reload', e) from e

        if 'state' in conn.context and conn.context.state == 'rommon':
            # If manual boot enabled wait for all peers to come to boot state.
            sleep(self.connection.settings.STACK_ROMMON_SLEEP)

            conn.context.pop('state')
            try:
                # send boot command for each subconnection
                for subconn in self.connection.subconnections:
                    utils.send_boot_cmd(subconn, timeout, self.prompt_recovery,
                                        reply)

                # process boot up for each subconnection
                for subconn in self.connection.subconnections:
                    self.connection.log.info('Processing on rp '
                                             '%s-%s' %
                                             (conn.hostname, subconn.alias))
                    utils.boot_process(subconn, timeout, self.prompt_recovery,
                                       reload_dialog)

            except Exception as e:
                self.connection.log.error(e)
                raise SubCommandFailure('Reload failed.', e) from e
        else:
            try:
                # bring device to enable mode
                conn.state_machine.go_to('any',
                                         conn.spawn,
                                         timeout=timeout,
                                         prompt_recovery=self.prompt_recovery,
                                         context=conn.context)
                conn.state_machine.go_to('enable',
                                         conn.spawn,
                                         timeout=timeout,
                                         prompt_recovery=self.prompt_recovery,
                                         context=conn.context)
            except Exception as e:
                raise SubCommandFailure(
                    'Failed to bring device to disable mode.', e) from e

        # check active and standby rp is ready
        self.connection.log.info('Wait for Standby RP to be ready.')

        interval = self.connection.settings.RELOAD_POSTCHECK_INTERVAL
        if utils.is_active_standby_ready(conn,
                                         timeout=timeout,
                                         interval=interval):
            self.connection.log.info('Active and Standby RPs are ready.')
        else:
            self.connection.log.info(
                'Timeout in %s secs. '
                'Standby RP is not in Ready state. Reload failed' % timeout)
            self.result = False
            return

        self.connection.log.info('Sleeping for %s secs.' % \
                self.connection.settings.STACK_POST_RELOAD_SLEEP)
        sleep(self.connection.settings.STACK_POST_RELOAD_SLEEP)

        self.connection.log.info('Disconnecting and reconnecting')
        self.connection.disconnect()
        self.connection.connect()

        self.connection.log.info("+++ Reload Completed Successfully +++")
        self.result = True

        if return_output:
            Result = namedtuple('Result', ['result', 'output'])
            self.result = Result(
                self.result,
                reload_cmd_output.match_output.replace(reload_cmd, '', 1))
Esempio n. 5
0
    def call_service(self,
                     reload_command=None,
                     reply=Dialog([]),
                     timeout=None,
                     return_output=False,
                     *args,
                     **kwargs):

        self.result = False
        reload_cmd = reload_command or self.reload_command
        timeout = timeout or self.timeout
        conn = self.connection.active

        reload_dialog = self.dialog
        if reply:
            reload_dialog = reply + reload_dialog

        custom_auth_stmt = custom_auth_statements(
            conn.settings.LOGIN_PROMPT, conn.settings.PASSWORD_PROMPT)
        if custom_auth_stmt:
            reload_dialog += Dialog(custom_auth_stmt)

        self.connection.log.info('Processing on rp %s-%s' %
                                 (conn.hostname, conn.alias))
        conn.sendline(reload_cmd)
        try:
            reload_output = reload_dialog.process(
                conn.spawn,
                timeout=timeout,
                prompt_recovery=self.prompt_recovery,
                context=conn.context)
        except Exception as e:
            raise SubCommandFailure('Error during reload', e) from e

        try:
            # check other rp if they reach to stable state
            for subconn in self.connection.subconnections:
                if subconn.alias != conn.alias:
                    self.connection.log.info('Processing on rp %s-%s' %
                                             (conn.hostname, subconn.alias))
                    subconn.spawn.sendline()
                    reload_peer_output = reload_dialog.process(
                        subconn.spawn,
                        timeout=timeout,
                        prompt_recovery=self.prompt_recovery,
                        context=subconn.context)
        except Exception as e:
            raise SubCommandFailure('Reload failed.', e) from e

        self.connection.log.info('Sleeping for %s secs.' % \
                self.connection.settings.QUAD_RELOAD_SLEEP)
        sleep(self.connection.settings.QUAD_RELOAD_SLEEP)

        self.connection.log.info('Disconnecting and reconnecting')
        self.connection.disconnect()
        self.connection.connect()

        self.connection.log.info("+++ Reload Completed Successfully +++")
        self.result = True

        if return_output:
            Result = namedtuple('Result', ['result', 'output'])
            self.result = Result(
                self.result,
                reload_output.match_output.replace(reload_cmd, '', 1))
Esempio n. 6
0
    def call_service(self,
                     command=None,
                     reply=Dialog([]),
                     timeout=None,
                     sync_standby=True,
                     *args,
                     **kwargs):

        self.result = False
        switchover_cmd = command or self.command
        timeout = timeout or self.timeout
        conn = self.connection.active

        dialog = self.dialog

        if reply:
            dialog = reply + self.dialog
        custom_auth_stmt = custom_auth_statements(
            conn.settings.LOGIN_PROMPT, conn.settings.PASSWORD_PROMPT)
        if custom_auth_stmt:
            dialog += Dialog(custom_auth_stmt)

        self.connection.log.info('Processing on original Global Active rp '
                                 '%s-%s' % (conn.hostname, conn.alias))
        conn.sendline(switchover_cmd)
        try:
            active_output = dialog.process(
                conn.spawn,
                timeout=timeout,
                prompt_recovery=self.prompt_recovery,
                context=conn.context)
        except Exception as e:
            raise SubCommandFailure('Error during switchover ', e) from e

        # check if active rp changed to rpr state and update state machine
        if 'state' in conn.context and conn.context.state == 'rpr':
            conn.state_machine.detect_state(conn.spawn)
            conn.context.pop('state')

        self.connection.log.info(
            'Processing on new Global Active rp '
            '%s-%s' % (conn.hostname, self.connection.standby.alias))

        if utils.is_active_ready(self.connection.standby):
            self.connection.log.info('Standby RP changed to active role')
            #  Reassign roles for each rp
            #  standby -> active
            #  active ics -> standby
            #  standby ics -> active ics
            #  active -> standby ics
            self.reassign_roles(conn)
        else:
            raise SubCommandFailure(
                'Failed to bring standby rp to active role')

        if not sync_standby:
            self.connection.log.info(
                "Standby state check disabled on user request")
            self.connection.log.info('Switchover successful')
            self.result = True
        else:
            new_active = self.connection.active
            new_standby = self.connection.standby
            self.connection.log.info(
                'Waiting for new standby RP to be STANDBY HOT')

            start_time = time()
            while (time() - start_time) < timeout:
                if utils.is_peer_standby_hot(new_active):
                    self.connection.log.info(
                        'Standby RP is in STANDBY HOT state.')
                    break
                else:
                    self.connection.log.info('Sleeping for %s secs.' % \
                        self.connection.settings.QUAD_SWITCHOVER_SLEEP)
                    sleep(self.connection.settings.QUAD_SWITCHOVER_SLEEP)
            else:
                self.connection.log.info(
                    'Timeout in %s secs. '
                    'Standby RP is not in STANDBY HOT state. Switchover failed'
                    % timeout)
                self.result = False
                return

            new_active.execute('show module')

            self.connection.log.info('Processing on new Global Standby rp '
                                     '%s-%s' %
                                     (conn.hostname, new_standby.alias))
            new_standby.spawn.sendline()
            try:
                new_standby.state_machine.go_to('any',
                                                new_standby.spawn,
                                                context=new_standby.context)
                new_standby.state_machine.detect_state(new_standby.spawn)
                new_standby.enable()
            except Exception as e:
                raise SubCommandFailure(
                    'Error while bringing standby rp '
                    'to enable state', e) from e

            self.connection.log.info('Switchover sucessful')
            self.result = True