Coverage for /Users/ajo/work/jumpstarter/jumpstarter/packages/jumpstarter/jumpstarter/common/ipaddr.py: 42%

24 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-30 18:45 +0200

1import asyncio 

2import logging 

3import socket 

4from ipaddress import ip_address 

5 

6 

7def get_ip_address(logger: logging.Logger | None = None) -> str: 

8 """Get the IP address of the host machine""" 

9 # Try to get the IP address using the hostname 

10 hostname = socket.gethostname() 

11 address = socket.gethostbyname(hostname) 

12 # If it returns nothing or a loopbadck address, do it the hard way 

13 if not address or ip_address(address).is_loopback: 

14 try: 

15 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: 

16 s.connect(("192.175.48.1", 53)) # AS112 

17 return s.getsockname()[0] 

18 except Exception: 

19 if logger: 

20 logger.warning("Could not determine default IP address, falling back to 0.0.0.0") 

21 return "0.0.0.0" 

22 

23 return address 

24 

25 

26async def get_minikube_ip(): 

27 # Create the subprocess 

28 process = await asyncio.create_subprocess_exec( 

29 "minikube", "ip", stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE 

30 ) 

31 

32 # Wait for it to complete and get the output 

33 stdout, _ = await process.communicate() 

34 

35 # Decode and strip whitespace 

36 result = stdout.decode().strip() 

37 

38 # Optional: check if command was successful 

39 if process.returncode != 0: 

40 raise RuntimeError(stdout.decode()) 

41 

42 return result