Esempio n. 1
0
    def connect_to_server(self, config, retry_limit=4, current_retry_count=1):
        hostname = config.hostname
        elevation_password = config.elevation_pass
        ssh_key_password = config.ssh_key_password
        user = config.ssh_user
        password = config.ssh_user_password
        ssh_key = config.ssh_key
        try:
            # We should be creating the connect_kwargs before hand and adding the key if it was provided
            server_connection = Connection(host=hostname,
                                           user=user,
                                           connect_kwargs=dict(
                                               key_filename=ssh_key,
                                               password=password,
                                               passphrase=ssh_key_password))
            # Yes, we are saving the elevation password to an object and passing it around. Fight me
            server_connection.sudopass = elevation_password
            # Checking to make sure we can actually get connected to the server.
            if current_retry_count == 1:
                print(f'Attempting to connect to {hostname}')
            else:
                print(
                    f'Attempting to connect to {hostname}. Attempt #{current_retry_count}'
                )
            server_connection.run('cat /dev/null')
            # Checking to make sure we have sudo privileges on the server
            print(f'Obtaining sudo privileges')
            server_connection.sudo(
                'cat /dev/null',
                hide=not self.VERBOSE,
                watchers=[SUDOPASS_LAMBDA(server_connection.sudopass)])
            print(f'Establishing OS Type')
            server_connection.distro = DistroAbstractionLayer(
                server_connection)

            return server_connection
        except socket.gaierror:
            print(f'Unable to reach host "{hostname}"')
            sys.exit(1)
        except ssh_exception.PasswordRequiredException:
            if current_retry_count >= retry_limit:
                print(
                    f'Failed to connect to host "{hostname}" due to some issue other password'
                )
                sys.exit(1)

            config.ssh_key_pass = None
            return connect_to_server(config,
                                     current_retry_count=current_retry_count +
                                     1)
        except ssh_exception.AuthenticationException as exception:
            error = exception.args[0]
            if error == 'No authentication methods available':
                raise invoke_exceptions.AuthFailure()
            if current_retry_count >= retry_limit:
                print(
                    f'Failed to connect to host "{hostname}" due to incorrect ssh key password"'
                )
                sys.exit(1)

            print('Missing User Password')
            config.ssh_user_password = getpass(
                f"Please Enter {config.ssh_user}'s password: "******"{hostname}" due to incorrect ssh key password"'
                )
                sys.exit(1)

            if config.ssh_user_password:
                print('Invalid SSH User Password')
            else:
                print('No User SSH Password Provided')
            config.ssh_user_password = getpass(
                f"Please {config.ssh_user}'s SSH Password: "******"{hostname}" due to bad sudo password'
                )
                sys.exit(1)

            config.elevation_pass = getpass(
                'Incorrect sudo password. Please re-enter sudo password: ')
            return connect_to_server(config,
                                     current_retry_count=current_retry_count +
                                     1)