24 lines
770 B
Python
24 lines
770 B
Python
import socket
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def get_ip_address():
|
|
"""
|
|
Get the primary IP address of the host.
|
|
This is used by Airflow workers to advertise their IP for log serving,
|
|
ensuring the webserver can reach them in a multi-host environment.
|
|
"""
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
try:
|
|
# This doesn't even have to be reachable
|
|
s.connect(('10.255.255.255', 1))
|
|
ip_address = s.getsockname()[0]
|
|
logger.info(f"Determined host IP address as: {ip_address}")
|
|
except Exception as e:
|
|
logger.warning(f"Could not determine IP address, falling back to 127.0.0.1. Error: {e}")
|
|
ip_address = '127.0.0.1'
|
|
finally:
|
|
s.close()
|
|
return ip_address
|