Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Exception on Linux during IP detection (#95390) #96

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,24 @@ open class IntershopDockerExtension @Inject constructor(val project: Project,
with(developmentConfig) {
val addPrefix = getConfigProperty(Configuration.ADDITIONAL_CONTAINER_PREFIX, "")
val addPrefixTrim = trimString(addPrefix)
if(addPrefixTrim!= "") {
if(addPrefix != addPrefixTrim) {
project.logger.quiet("Additional containerprefix {} is used.", addPrefixTrim)
if (addPrefixTrim.isNotEmpty()) {
if (addPrefix != addPrefixTrim) {
project.logger.info("Additional container prefix {} is used.", addPrefixTrim)
}
containerPrefix.append(addPrefixTrim)
containerPrefix.append("-")
}
}
val prefixConfig = containerProjectPrefix.getOrElse("")
if(prefixConfig != "") {
if (prefixConfig.isNotEmpty()) {
val prefixConfigTrim = trimString(prefixConfig)
if(prefixConfig != prefixConfigTrim) {
project.logger.quiet("Configured prefix {} is used for all containers.", prefixConfigTrim)
if (prefixConfig != prefixConfigTrim) {
project.logger.info("Configured prefix {} is used for all containers.", prefixConfigTrim)
}
containerPrefix.append(prefixConfigTrim)
} else {
val projectPrefix = trimString(project.name)
project.logger.quiet("Default project prefix {} is used for all containers.", projectPrefix)
project.logger.info("Default project prefix {} is used for all containers.", projectPrefix)
containerPrefix.append(projectPrefix)
}
containerPrefix.toString()
Expand Down
65 changes: 25 additions & 40 deletions src/main/kotlin/com/intershop/gradle/icm/docker/utils/IPFinder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import java.net.NetworkInterface

object IPFinder {

//Function to Find out IP Address
// Function to Find out IP Address
fun getSystemIP(): Pair<String?,String?> {
return try {
val sysIPHostName: Pair<String?,String?>
Expand All @@ -34,54 +34,39 @@ object IPFinder {
Pair(ip.hostAddress, ip.canonicalHostName)
}
else -> {
val nets = NetworkInterface.getNetworkInterfaces()
var pair: Pair<String?,String?> = Pair(null, null)
nets.toList().map { it.name }.sorted().forEach {
val ip = getSystemIP4Linux(it)
if(ip != null) {
pair = Pair(ip.hostAddress, ip.canonicalHostName)
return@forEach
}
val ip: InetAddress? = getSystemIP4Linux()
when(ip) {
is InetAddress -> Pair(ip.hostAddress, ip.canonicalHostName)
else -> Pair(null, null)
}
pair
}
}
sysIPHostName
} catch (E: Exception) {
System.err.println("System IP Exp : " + E.message)
Pair(null,null)
} catch (e: Exception) {
System.err.println("System IP Detection Exception: " + e.message)
Pair(null, null)
}
}

//For Linux OS
private fun getSystemIP4Linux(name: String): InetAddress? {
// For Linux OS
private fun getSystemIP4Linux(): InetAddress? {
return try {
var ip: InetAddress? = null
if(name.substring(0, 2) in listOf("en", "wl") || name.substring(0, 3) in listOf("eth", "usb")) {
val networkInterface = NetworkInterface.getByName(name)
val inetAddress = networkInterface.inetAddresses
var currentAddress = inetAddress.nextElement()

if (!inetAddress.hasMoreElements()) {
if (currentAddress is Inet4Address && !currentAddress.isLoopbackAddress()) {
ip = currentAddress
}
} else {
while (inetAddress.hasMoreElements()) {
currentAddress = inetAddress.nextElement()
if (currentAddress is Inet4Address && !currentAddress.isLoopbackAddress()) {
ip = currentAddress
break
}
}
NetworkInterface.getNetworkInterfaces().asSequence()
.filter { networkInterface ->
networkInterface.isUp && !networkInterface.isLoopback && !networkInterface.isVirtual
}.filter { networkInterface ->
networkInterface.name.substring(0, 2) in listOf("en", "wl") ||
networkInterface.name.substring(0, 3) in listOf("eth", "usb")
}.flatMap { networkInterface ->
networkInterface.inetAddresses.asSequence()
}.filter { inetAddress ->
!inetAddress.isLoopbackAddress && inetAddress is Inet4Address
}.elementAtOrElse(0) {
// Fallback to local host if sequence is empty
InetAddress.getLocalHost()
}
if (ip == null) {
ip = InetAddress.getLocalHost()
}
}
ip
} catch (E: Exception) {
System.err.println("System Linux IP Exp : " + E.message)
} catch (e: Exception) {
System.err.println("Linux System IP Detection Exception: " + e.message)
null
}
}
Expand Down
Loading