Skip to content

Commit

Permalink
Merge pull request #2 from Cognifide/host-internal-ip
Browse files Browse the repository at this point in the history
Host internal IP always auto determined
  • Loading branch information
Krystian Panek authored Mar 11, 2020
2 parents b1c7ac1 + ab1af94 commit dc05d3e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface Runtime {

val safeVolumes: Boolean

val hostInternalIp: String?
val hostInternalIp: String

fun determinePath(path: String): String

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
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 {

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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"

Expand Down

0 comments on commit dc05d3e

Please sign in to comment.