def init_remote(self, ip, port=22, custom_creds=None): """ Initialise connection to remote :param ip: IP of host :type ip: str :param port: port for SSH :type port: int :param custom_creds: custom creds :type custom_creds: dict """ logger.debug('SSH_MANAGER: Create new connection for ' '{ip}:{port}'.format(ip=ip, port=port)) keys = self._get_keys() if ip != self.admin_ip else [] if ip == self.admin_ip: ssh_client = SSHClient( host=ip, port=port, auth=SSHAuth( username=self.admin_login, password=self.__admin_password, keys=keys) ) ssh_client.sudo_mode = SSH_FUEL_CREDENTIALS['sudo'] elif custom_creds: ssh_client = SSHClient( host=ip, port=port, auth=SSHAuth(**custom_creds)) else: try: ssh_client = SSHClient( host=ip, port=port, auth=SSHAuth( username=self.slave_login, password=self.__slave_password, keys=keys) ) except SSHException: ssh_client = SSHClient( host=ip, port=port, auth=SSHAuth( username=self.slave_fallback_login, password=self.__slave_password, keys=keys) ) ssh_client.sudo_mode = SSH_SLAVE_CREDENTIALS['sudo'] self.connections[(ip, port)] = ssh_client logger.debug('SSH_MANAGER: New connection for ' '{ip}:{port} is created'.format(ip=ip, port=port))
def get_remote(self, ip, port=22): """ Function returns remote SSH connection to node by ip address :param ip: IP of host :type ip: str :param port: port for SSH :type port: int :rtype: SSHClient """ if (ip, port) not in self.connections: logger.debug('SSH_MANAGER:Create new connection for ' '{ip}:{port}'.format(ip=ip, port=port)) keys = self._get_keys() if ip != self.admin_ip else [] if ip == self.admin_ip: ssh_client = SSHClient( host=ip, port=port, username=self.admin_login, password=self.__admin_password, private_keys=keys ) ssh_client.sudo_mode = SSH_FUEL_CREDENTIALS['sudo'] else: try: ssh_client = SSHClient( host=ip, port=port, username=self.slave_login, password=self.__slave_password, private_keys=keys ) except AuthenticationException: ssh_client = SSHClient( host=ip, port=port, username=self.slave_fallback_login, password=self.__slave_password, private_keys=keys ) ssh_client.sudo_mode = SSH_SLAVE_CREDENTIALS['sudo'] self.connections[(ip, port)] = ssh_client logger.debug('SSH_MANAGER:Return existed connection for ' '{ip}:{port}'.format(ip=ip, port=port)) logger.debug('SSH_MANAGER: Connections {0}'.format(self.connections)) return self._connect(self.connections[(ip, port)])
def get_remote(self, ip, port=22): """ Function returns remote SSH connection to node by ip address :param ip: IP of host :type ip: str :param port: port for SSH :type port: int :rtype: SSHClient """ if (ip, port) not in self.connections: logger.debug('SSH_MANAGER: Create new connection for ' '{ip}:{port}'.format(ip=ip, port=port)) keys = self._get_keys() if ip != self.admin_ip else [] if ip == self.admin_ip: ssh_client = SSHClient(host=ip, port=port, username=self.admin_login, password=self.__admin_password, private_keys=keys) ssh_client.sudo_mode = SSH_FUEL_CREDENTIALS['sudo'] else: try: ssh_client = SSHClient(host=ip, port=port, username=self.slave_login, password=self.__slave_password, private_keys=keys) except AuthenticationException: ssh_client = SSHClient(host=ip, port=port, username=self.slave_fallback_login, password=self.__slave_password, private_keys=keys) ssh_client.sudo_mode = SSH_SLAVE_CREDENTIALS['sudo'] self.connections[(ip, port)] = ssh_client logger.debug('SSH_MANAGER: Return existed connection for ' '{ip}:{port}'.format(ip=ip, port=port)) logger.debug('SSH_MANAGER: Connections {0}'.format(self.connections)) return self._connect(self.connections[(ip, port)])