def perform_startup_ip_check(emitter: StdoutEmitter, ursula: Ursula, force: bool = False) -> None: """ Used on ursula startup to determine if the external IP address is consistent with the configuration's values. """ try: external_ip = determine_external_ip_address( network=ursula.domain, known_nodes=ursula.known_nodes) except UnknownIPAddress: message = 'Cannot automatically determine external IP address' emitter.message(message) return # TODO: crash, or not to crash... that is the question rest_host = ursula.rest_interface.host try: validate_worker_ip(worker_ip=rest_host) except InvalidWorkerIP: message = f'{rest_host} is not a valid or permitted worker IP address. Set the correct external IP then try again\n' \ f'automatic configuration -> nucypher ursula config ip-address\n' \ f'manual configuration -> nucypher ursula config --rest-host <IP ADDRESS>' emitter.message(message) return ip_mismatch = external_ip != rest_host if ip_mismatch and not force: error = f'\nX External IP address ({external_ip}) does not match configuration ({ursula.rest_interface.host}).\n' hint = f"Run 'nucypher ursula config ip-address' to reconfigure the IP address then try " \ f"again or use --no-ip-checkup to bypass this check (not recommended).\n" emitter.message(error, color='red') emitter.message(hint, color='yellow') raise click.Abort() else: emitter.message('✓ External IP matches configuration', 'green')
def collect_worker_ip_address(emitter: StdoutEmitter, network: str, force: bool = False) -> str: # From environment variable # TODO: remove this environment variable? ip = os.environ.get(NUCYPHER_ENVVAR_WORKER_IP_ADDRESS) if ip: message = f'Using IP address ({ip}) from {NUCYPHER_ENVVAR_WORKER_IP_ADDRESS} environment variable' emitter.message(message, verbosity=2) return ip # From node swarm try: message = f'Detecting external IP address automatically' emitter.message(message, verbosity=2) ip = determine_external_ip_address(network=network) except UnknownIPAddress: if force: raise emitter.message( 'Cannot automatically determine external IP address - input required' ) # Confirmation if not force: if not click.confirm(CONFIRM_URSULA_IPV4_ADDRESS.format(rest_host=ip)): ip = click.prompt(COLLECT_URSULA_IPV4_ADDRESS, type=WORKER_IP) validate_worker_ip(worker_ip=ip) return ip
def convert(self, value, param, ctx): _ip = super().convert(value, param, ctx) try: validate_worker_ip(worker_ip=_ip) except InvalidWorkerIP as e: self.fail(str(e)) return value
def collect_worker_ip_address(emitter: StdoutEmitter, network: str, force: bool = False) -> str: # From node swarm try: message = f'Detecting external IP address automatically' emitter.message(message, verbosity=2) ip = determine_external_ip_address(network=network) except UnknownIPAddress: if force: raise emitter.message('Cannot automatically determine external IP address - input required') ip = click.prompt(COLLECT_URSULA_IPV4_ADDRESS, type=WORKER_IP) # Confirmation if not force: if not click.confirm(CONFIRM_URSULA_IPV4_ADDRESS.format(rest_host=ip)): ip = click.prompt(COLLECT_URSULA_IPV4_ADDRESS, type=WORKER_IP) validate_worker_ip(worker_ip=ip) return ip