Skip to content

Commit

Permalink
fix: Exception on Linux during IP detection (#95390)
Browse files Browse the repository at this point in the history
  • Loading branch information
DevDavido authored Apr 15, 2024
2 parents 4abd8b1 + e20c7a2 commit b61caa4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 47 deletions.
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

0 comments on commit b61caa4

Please sign in to comment.