Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit a06eca4

Browse files
authored
Merge pull request #475 from jumpstarter-dev/backport-474-to-release-0.6
2 parents bffd12f + c2bacaf commit a06eca4

6 files changed

Lines changed: 30 additions & 41 deletions

File tree

packages/jumpstarter-cli-admin/jumpstarter_cli_admin/install.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import asyncclick as click
44
from jumpstarter_cli_common.opt import opt_context, opt_kubeconfig
55
from jumpstarter_cli_common.version import get_client_version
6-
from jumpstarter_kubernetes import get_ip_address, helm_installed, install_helm_chart
6+
from jumpstarter_kubernetes import helm_installed, install_helm_chart
7+
8+
from jumpstarter.common.ipaddr import get_ip_address
79

810

911
def get_chart_version() -> str:

packages/jumpstarter-driver-http/jumpstarter_driver_http/driver.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from aiohttp import web
77
from jumpstarter_driver_opendal.driver import Opendal
88

9+
from jumpstarter.common.ipaddr import get_ip_address
910
from jumpstarter.driver import Driver, export
1011

1112

@@ -41,18 +42,7 @@ def __post_init__(self):
4142
]
4243
)
4344
if self.host is None:
44-
self.host = self.get_default_ip()
45-
46-
def get_default_ip(self):
47-
try:
48-
import socket
49-
50-
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
51-
s.connect(("8.8.8.8", 80))
52-
return s.getsockname()[0]
53-
except Exception:
54-
self.logger.warning("Could not determine default IP address, falling back to 0.0.0.0")
55-
return "0.0.0.0"
45+
self.host = get_ip_address(logger=self.logger)
5646

5747
@classmethod
5848
def client(cls) -> str:

packages/jumpstarter-driver-tftp/jumpstarter_driver_tftp/driver.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import os
3-
import socket
43
import threading
54
from dataclasses import dataclass, field
65
from typing import Optional
@@ -9,6 +8,7 @@
98

109
from jumpstarter_driver_tftp.server import TftpServer
1110

11+
from jumpstarter.common.ipaddr import get_ip_address
1212
from jumpstarter.driver import Driver, export
1313

1414

@@ -55,17 +55,7 @@ def __post_init__(self):
5555
self.storage = self.children["storage"]
5656

5757
if self.host == "":
58-
self.host = self.get_default_ip()
59-
60-
def get_default_ip(self):
61-
"""Get the IP address of the default route interface"""
62-
try:
63-
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
64-
s.connect(("8.8.8.8", 80))
65-
return s.getsockname()[0]
66-
except Exception:
67-
self.logger.warning("Could not determine default IP address, falling back to 0.0.0.0")
68-
return "0.0.0.0"
58+
self.host = get_ip_address(logger=self.logger)
6959

7060
@classmethod
7161
def client(cls) -> str:

packages/jumpstarter-kubernetes/jumpstarter_kubernetes/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
V1Alpha1ExporterList,
77
V1Alpha1ExporterStatus,
88
)
9-
from .install import get_ip_address, helm_installed, install_helm_chart
9+
from .install import helm_installed, install_helm_chart
1010
from .leases import (
1111
LeasesV1Alpha1Api,
1212
V1Alpha1Lease,
@@ -34,7 +34,6 @@
3434
"V1Alpha1LeaseSelector",
3535
"V1Alpha1LeaseSpec",
3636
"V1Alpha1List",
37-
"get_ip_address",
3837
"helm_installed",
3938
"install_helm_chart",
4039
]

packages/jumpstarter-kubernetes/jumpstarter_kubernetes/install.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
import asyncio
22
import shutil
3-
import socket
43
from typing import Literal, Optional
54

65

7-
def get_ip_address() -> 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 a bogus address, do it the hard way
13-
if not address or address.startswith("127."):
14-
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
15-
s.connect(("1.1.1.1", 0))
16-
address = s.getsockname()[0]
17-
return address
18-
19-
206
def helm_installed(name: str) -> bool:
217
return shutil.which(name) is not None
228

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import logging
2+
import socket
3+
from ipaddress import ip_address
4+
5+
6+
def get_ip_address(logger: logging.Logger | None = None) -> str:
7+
"""Get the IP address of the host machine"""
8+
# Try to get the IP address using the hostname
9+
hostname = socket.gethostname()
10+
address = socket.gethostbyname(hostname)
11+
# If it returns nothing or a loopbadck address, do it the hard way
12+
if not address or ip_address(address).is_loopback:
13+
try:
14+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
15+
s.connect(("192.175.48.1", 53)) # AS112
16+
return s.getsockname()[0]
17+
except Exception:
18+
if logger:
19+
logger.warning("Could not determine default IP address, falling back to 0.0.0.0")
20+
return "0.0.0.0"
21+
22+
return address

0 commit comments

Comments
 (0)