Skip to content

Commit dc05d3e

Browse files
author
Krystian Panek
authored
Merge pull request #2 from Cognifide/host-internal-ip
Host internal IP always auto determined
2 parents b1c7ac1 + ab1af94 commit dc05d3e

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

src/main/kotlin/com/cognifide/gradle/environment/docker/Runtime.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface Runtime {
1313

1414
val safeVolumes: Boolean
1515

16-
val hostInternalIp: String?
16+
val hostInternalIp: String
1717

1818
fun determinePath(path: String): String
1919

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
package com.cognifide.gradle.environment.docker.runtime
22

33
import com.cognifide.gradle.environment.EnvironmentExtension
4+
import com.cognifide.gradle.environment.docker.DockerProcess
45
import com.cognifide.gradle.environment.docker.Runtime
56

67
abstract class Base(protected val environment: EnvironmentExtension) : Runtime {
78

89
protected val logger = environment.project.logger
910

1011
override fun toString(): String = name.toLowerCase()
12+
13+
@Suppress("SpreadOperator", "TooGenericExceptionCaught")
14+
protected fun detectHostInternalIp(): String? = try {
15+
DockerProcess.execString {
16+
val args = listOf("run", "alpine", "/bin/ash", "-c", "ip -4 route list match 0/0 | cut -d ' ' -f 3")
17+
withArgs(*args.toTypedArray())
18+
}.takeIf { it.isNotBlank() }
19+
} catch (e: Exception) {
20+
logger.debug("Cannot detect Docker host internal IP. Cause: ${e.message}", e)
21+
null
22+
}
1123
}

src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Desktop.kt

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,22 @@ package com.cognifide.gradle.environment.docker.runtime
22

33
import com.cognifide.gradle.common.utils.Formats
44
import com.cognifide.gradle.environment.EnvironmentExtension
5-
import com.cognifide.gradle.environment.docker.DockerProcess
65
import org.gradle.internal.os.OperatingSystem
76

87
class Desktop(environment: EnvironmentExtension) : Base(environment) {
98

109
override val name: String get() = NAME
1110

12-
override val hostIp: String get() = environment.prop.string("environment.docker.desktop.hostIp") ?: "127.0.0.1"
11+
override val hostIp: String get() = environment.prop.string("environment.docker.desktop.hostIp")
12+
?: "127.0.0.1"
13+
14+
override val hostInternalIp: String get() = environment.prop.string("environment.docker.desktop.hostInternalIp")
15+
?: detectHostInternalIp() ?: "172.17.0.1"
1316

1417
override val safeVolumes: Boolean get() = !OperatingSystem.current().isWindows
1518

1619
override fun determinePath(path: String) = Formats.normalizePath(path)
1720

18-
override val hostInternalIp: String?
19-
get() = when {
20-
OperatingSystem.current().isWindows || OperatingSystem.current().isMacOsX -> null
21-
else -> detectHostInternalIp() ?: environment.prop.string("environment.docker.desktop.hostInternalIp") ?: "172.17.0.1"
22-
}
23-
24-
@Suppress("SpreadOperator", "TooGenericExceptionCaught")
25-
private fun detectHostInternalIp(): String? = try {
26-
DockerProcess.execString {
27-
val args = listOf("run", "alpine", "/bin/ash", "-c", "ip -4 route list match 0/0 | cut -d ' ' -f 3")
28-
withArgs(*args.toTypedArray())
29-
}.takeIf { it.isNotBlank() }
30-
} catch (e: Exception) {
31-
logger.debug("Cannot detect Docker host internal IP. Cause: ${e.message}", e)
32-
null
33-
}
34-
3521
companion object {
3622
const val NAME = "desktop"
3723
}

src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Toolbox.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ class Toolbox(environment: EnvironmentExtension) : Base(environment) {
88

99
override val name: String get() = NAME
1010

11-
override val hostIp: String get() = detectHostIp() ?: environment.prop.string("environment.docker.toolbox.hostIp") ?: "192.168.99.100"
11+
override val hostIp: String get() = environment.prop.string("environment.docker.toolbox.hostIp")
12+
?: detectHostIp() ?: "192.168.99.100"
13+
14+
override val hostInternalIp: String get() = environment.prop.string("environment.docker.toolbox.hostInternalIp")
15+
?: detectHostInternalIp() ?: "10.0.2.2"
1216

1317
@Suppress("TooGenericExceptionCaught")
1418
fun detectHostIp(): String? = try {
@@ -21,9 +25,6 @@ class Toolbox(environment: EnvironmentExtension) : Base(environment) {
2125

2226
override val safeVolumes: Boolean = true
2327

24-
override val hostInternalIp: String?
25-
get() = environment.prop.string("environment.docker.toolbox.hostInternalIp") ?: "10.0.2.2"
26-
2728
var cygpathPath = environment.prop.string("environment.cygpath.path")
2829
?: "C:\\Program Files\\Git\\usr\\bin\\cygpath.exe"
2930

0 commit comments

Comments
 (0)