diff --git a/src/main/kotlin/com/cognifide/gradle/environment/docker/Runtime.kt b/src/main/kotlin/com/cognifide/gradle/environment/docker/Runtime.kt index c2c3211..ef1cd4b 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/Runtime.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/Runtime.kt @@ -13,7 +13,7 @@ interface Runtime { val safeVolumes: Boolean - val hostInternalIp: String? + val hostInternalIp: String fun determinePath(path: String): String diff --git a/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Base.kt b/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Base.kt index 00881cd..8a52a36 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Base.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Base.kt @@ -1,6 +1,7 @@ package com.cognifide.gradle.environment.docker.runtime import com.cognifide.gradle.environment.EnvironmentExtension +import com.cognifide.gradle.environment.docker.DockerProcess import com.cognifide.gradle.environment.docker.Runtime abstract class Base(protected val environment: EnvironmentExtension) : Runtime { @@ -8,4 +9,15 @@ abstract class Base(protected val environment: EnvironmentExtension) : Runtime { protected val logger = environment.project.logger override fun toString(): String = name.toLowerCase() + + @Suppress("SpreadOperator", "TooGenericExceptionCaught") + protected fun detectHostInternalIp(): String? = try { + DockerProcess.execString { + val args = listOf("run", "alpine", "/bin/ash", "-c", "ip -4 route list match 0/0 | cut -d ' ' -f 3") + withArgs(*args.toTypedArray()) + }.takeIf { it.isNotBlank() } + } catch (e: Exception) { + logger.debug("Cannot detect Docker host internal IP. Cause: ${e.message}", e) + null + } } diff --git a/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Desktop.kt b/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Desktop.kt index fbf1174..6ea9ae2 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Desktop.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Desktop.kt @@ -2,36 +2,22 @@ package com.cognifide.gradle.environment.docker.runtime import com.cognifide.gradle.common.utils.Formats import com.cognifide.gradle.environment.EnvironmentExtension -import com.cognifide.gradle.environment.docker.DockerProcess import org.gradle.internal.os.OperatingSystem class Desktop(environment: EnvironmentExtension) : Base(environment) { override val name: String get() = NAME - override val hostIp: String get() = environment.prop.string("environment.docker.desktop.hostIp") ?: "127.0.0.1" + override val hostIp: String get() = environment.prop.string("environment.docker.desktop.hostIp") + ?: "127.0.0.1" + + override val hostInternalIp: String get() = environment.prop.string("environment.docker.desktop.hostInternalIp") + ?: detectHostInternalIp() ?: "172.17.0.1" override val safeVolumes: Boolean get() = !OperatingSystem.current().isWindows override fun determinePath(path: String) = Formats.normalizePath(path) - override val hostInternalIp: String? - get() = when { - OperatingSystem.current().isWindows || OperatingSystem.current().isMacOsX -> null - else -> detectHostInternalIp() ?: environment.prop.string("environment.docker.desktop.hostInternalIp") ?: "172.17.0.1" - } - - @Suppress("SpreadOperator", "TooGenericExceptionCaught") - private fun detectHostInternalIp(): String? = try { - DockerProcess.execString { - val args = listOf("run", "alpine", "/bin/ash", "-c", "ip -4 route list match 0/0 | cut -d ' ' -f 3") - withArgs(*args.toTypedArray()) - }.takeIf { it.isNotBlank() } - } catch (e: Exception) { - logger.debug("Cannot detect Docker host internal IP. Cause: ${e.message}", e) - null - } - companion object { const val NAME = "desktop" } diff --git a/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Toolbox.kt b/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Toolbox.kt index bc08865..ef2b0b7 100644 --- a/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Toolbox.kt +++ b/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Toolbox.kt @@ -8,7 +8,11 @@ class Toolbox(environment: EnvironmentExtension) : Base(environment) { override val name: String get() = NAME - override val hostIp: String get() = detectHostIp() ?: environment.prop.string("environment.docker.toolbox.hostIp") ?: "192.168.99.100" + override val hostIp: String get() = environment.prop.string("environment.docker.toolbox.hostIp") + ?: detectHostIp() ?: "192.168.99.100" + + override val hostInternalIp: String get() = environment.prop.string("environment.docker.toolbox.hostInternalIp") + ?: detectHostInternalIp() ?: "10.0.2.2" @Suppress("TooGenericExceptionCaught") fun detectHostIp(): String? = try { @@ -21,9 +25,6 @@ class Toolbox(environment: EnvironmentExtension) : Base(environment) { override val safeVolumes: Boolean = true - override val hostInternalIp: String? - get() = environment.prop.string("environment.docker.toolbox.hostInternalIp") ?: "10.0.2.2" - var cygpathPath = environment.prop.string("environment.cygpath.path") ?: "C:\\Program Files\\Git\\usr\\bin\\cygpath.exe"