From df55a79cfd19fd86611a091ec5bd26f0db2c57e6 Mon Sep 17 00:00:00 2001 From: DevDavido <997605+DevDavido@users.noreply.github.com> Date: Thu, 11 Apr 2024 18:43:56 +0200 Subject: [PATCH 1/3] fix: Use less verbose log level during extension configuration --- .../gradle/icm/docker/extension/IntershopDockerExtension.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/intershop/gradle/icm/docker/extension/IntershopDockerExtension.kt b/src/main/kotlin/com/intershop/gradle/icm/docker/extension/IntershopDockerExtension.kt index a4e3a5e..c6d1521 100644 --- a/src/main/kotlin/com/intershop/gradle/icm/docker/extension/IntershopDockerExtension.kt +++ b/src/main/kotlin/com/intershop/gradle/icm/docker/extension/IntershopDockerExtension.kt @@ -136,7 +136,7 @@ open class IntershopDockerExtension @Inject constructor(val project: Project, val addPrefixTrim = trimString(addPrefix) if(addPrefixTrim!= "") { if(addPrefix != addPrefixTrim) { - project.logger.quiet("Additional containerprefix {} is used.", addPrefixTrim) + project.logger.info("Additional container prefix {} is used.", addPrefixTrim) } containerPrefix.append(addPrefixTrim) containerPrefix.append("-") @@ -146,12 +146,12 @@ open class IntershopDockerExtension @Inject constructor(val project: Project, if(prefixConfig != "") { val prefixConfigTrim = trimString(prefixConfig) if(prefixConfig != prefixConfigTrim) { - project.logger.quiet("Configured prefix {} is used for all containers.", 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() From 9f4628e99cdc72c34c0bd411a344e69cec074bd1 Mon Sep 17 00:00:00 2001 From: DevDavido <997605+DevDavido@users.noreply.github.com> Date: Thu, 11 Apr 2024 18:45:24 +0200 Subject: [PATCH 2/3] refactor: Use Kotlin stdlib to check for empty string --- .../icm/docker/extension/IntershopDockerExtension.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/intershop/gradle/icm/docker/extension/IntershopDockerExtension.kt b/src/main/kotlin/com/intershop/gradle/icm/docker/extension/IntershopDockerExtension.kt index c6d1521..9130614 100644 --- a/src/main/kotlin/com/intershop/gradle/icm/docker/extension/IntershopDockerExtension.kt +++ b/src/main/kotlin/com/intershop/gradle/icm/docker/extension/IntershopDockerExtension.kt @@ -134,8 +134,8 @@ 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) { + if (addPrefixTrim.isNotEmpty()) { + if (addPrefix != addPrefixTrim) { project.logger.info("Additional container prefix {} is used.", addPrefixTrim) } containerPrefix.append(addPrefixTrim) @@ -143,9 +143,9 @@ open class IntershopDockerExtension @Inject constructor(val project: Project, } } val prefixConfig = containerProjectPrefix.getOrElse("") - if(prefixConfig != "") { + if (prefixConfig.isNotEmpty()) { val prefixConfigTrim = trimString(prefixConfig) - if(prefixConfig != prefixConfigTrim) { + if (prefixConfig != prefixConfigTrim) { project.logger.info("Configured prefix {} is used for all containers.", prefixConfigTrim) } containerPrefix.append(prefixConfigTrim) From e20c7a2306188a5acc84df90dd4be48b91423dac Mon Sep 17 00:00:00 2001 From: DevDavido <997605+DevDavido@users.noreply.github.com> Date: Thu, 11 Apr 2024 18:49:18 +0200 Subject: [PATCH 3/3] fix: Exception on Linux during IP detection --- .../gradle/icm/docker/utils/IPFinder.kt | 65 +++++++------------ 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/src/main/kotlin/com/intershop/gradle/icm/docker/utils/IPFinder.kt b/src/main/kotlin/com/intershop/gradle/icm/docker/utils/IPFinder.kt index 68e9215..9901044 100644 --- a/src/main/kotlin/com/intershop/gradle/icm/docker/utils/IPFinder.kt +++ b/src/main/kotlin/com/intershop/gradle/icm/docker/utils/IPFinder.kt @@ -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 { return try { val sysIPHostName: Pair @@ -34,54 +34,39 @@ object IPFinder { Pair(ip.hostAddress, ip.canonicalHostName) } else -> { - val nets = NetworkInterface.getNetworkInterfaces() - var pair: Pair = 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 } }