def reboot(self, wait=60, timeout=1200, interval=30): """ Method called by user to reboot the Host :param wait: *OPTIONAL* Time to sleep before reconnecting, Default value is 60 :param timeout: *OPTIONAL* Time by which device need to reboot. Default is 1200 seconds :param interval: *OPTIONAL* Interval at which reconnect need to be attempted after reboot is performed. Default is 30 seconds :return: True if device reboot is successful. In all other cases Exception is raised """ try: res = self.execute(command="shutdown -r", pattern='', no_response=1) if res == -1: raise TobyException("Error sending reboot command to Device", host_obj=self) self.log(message='Sent the reboot command to %s' % self.host) if not check_socket( host=self.host, negative=1, socket_type=self.connect_mode): raise TobyException( 'Device %s did not go down after executing a reboot: FAIL' % self.host, host_obj=self) self.log(message='Device %s has gone down. ' 'Waiting %s seconds before checking %s socket.' % (self.host, wait, self.connect_mode)) self.handle.close() time.sleep(wait) if not check_socket(host=self.host, socket_type=self.connect_mode, timeout=timeout, interval=interval): raise TobyException( 'Device %s did not come back online. Reboot FAIL' % self.host, host_obj=self) time.sleep(10) self.reconnect() except: raise TobyException("Reboot Failed", host_obj=self) return True
def reconnect(self, timeout=30, interval=10, force=True): """ Method called by user to reconnect to Host :param timeout: *OPTIONAL* Time by which device need to reconnect. Default is 30 seconds :param interval: *OPTIONAL* Interval at which reconnect need to be attempted. Default is 10 seconds :return: True if device reconnection is successful. In all other cases Exception is raised """ if force is True: try: oldprompt = self.prompt self.prompt = '\$' if 'proxy_host' not in self._kwargs and 'proxy_hosts' not in self._kwargs: self.log(level='INFO', message="Now checking %s's %s server.." % (self.host, self.connect_mode)) if check_socket(host=self.host, socket_type=self.connect_mode, timeout=timeout, interval=interval): self.log( level='INFO', message='Successfully created %s socket to %s' % (self.connect_mode, self.host)) else: raise TobyException( 'Failed to create %s socket to %s' % (self.host, self.connect_mode), host_obj=self) if self.connect_mode == 'ssh': if self.handle.client.get_transport().isAlive(): self.handle.client.close() self.handle = _connect_unix(self._kwargs) self.set_prompt(oldprompt) except: raise TobyException("Error reconnecting to Device", host_obj=self) return True else: try: if self.handle.client.get_transport().isAlive(): self.log(level='INFO', message='Unix channel is alive') return True except: pass
def reconnect(self, timeout=30, interval=10): """ Method called by user to reconnect to Host :param timeout: *OPTIONAL* Time by which device need to reconnect. Default is 30 seconds :param interval: *OPTIONAL* Interval at which reconnect need to be attempted. Default is 10 seconds :return: True if device reconnection is successful. In all other cases Exception is raised """ try: self.log(level='INFO', message="Now checking %s's %s server.." % (self.host, self.connect_mode)) if check_socket(host=self.host, socket_type=self.connect_mode, timeout=timeout, interval=interval): self.log(level='DEBUG', message='Successfully created %s socket to %s' % (self.connect_mode, self.host)) else: raise TobyException('Failed to create %s socket to %s' % (self.host, self.connect_mode), host_obj=self) if self.connect_mode == 'ssh': if self.handle.client.get_transport().isAlive(): self.handle.client.close() self.handle = SshConn(host=self.host, user=self.user, password=self.password, port=22, initialize_command='cd') else: self.handle = TelnetConn(host=self.host, user=self.user, password=self.password) if self.port == 23: self.prompt = 'Toby-%s-%s' % (os.getpid(), self.host) self.prompt += '%' self.set_shell_prompt(prompt=self.prompt) else: self.prompt = '>\s?' except: raise TobyException("Error reconnecting to Device", host_obj=self) return True
def test_check_socket_invalid(self, patch1): self.assertFalse(check_socket(host='dummy'))
def test_check_socket_exception_neg(self, patch1, patch2, patch3): self.assertTrue(check_socket(host='dummy', port=22, negative=1))
def test_check_socket_exception(self, patch1, patch2, patch3): self.assertFalse(check_socket(host='dummy', socket_type='telnet'))
def test_check_socket_neg(self, patch1, patch2, patch3): self.assertFalse( check_socket(host='dummy', socket_type='ssh', negative=1))
def test_check_socket(self, patch1, patch2): self.assertTrue(check_socket(host='dummy', socket_type='telnet'))