diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Utils.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Utils.kt index 5a372f6..ded3d4c 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Utils.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Utils.kt @@ -134,15 +134,18 @@ fun remote(host: String, remoteUser: String, password: Secret? = null, platform: * Returns Prov instance which eexcutes its tasks in a local docker container with name containerName. * If a container with the given name is running already, it'll be reused if parameter useExistingContainer is set to true. * If a container is reused, it is not checked if it has the correct, specified image. + * Determines automatically if sudo is required if sudo is null, otherwise the specified sudo is used */ @Api // used by other libraries resp. KotlinScript fun docker( containerName: String = "provs_default", useExistingContainer: Boolean = true, imageName: String = "ubuntu", - sudo: Boolean = true + sudo: Boolean? = null ): Prov { + val sudoRequired = sudo ?: checkSudoRequiredForDocker() + local().provideContainer(containerName, imageName) return Prov.newInstance( @@ -153,7 +156,18 @@ fun docker( ContainerStartMode.USE_RUNNING_ELSE_CREATE else ContainerStartMode.CREATE_NEW_KILL_EXISTING, - sudo = sudo + sudo = sudoRequired ) ) } + + +fun checkSudoRequiredForDocker(): Boolean { + return if (local().chk("docker -v")) { + false + } else if (local().chk("sudo docker -v")) { + true + } else { + throw IllegalStateException("Docker could not be run.") + } +} \ No newline at end of file diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/extensions/server_software/k3s/domain/K3dKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/extensions/server_software/k3s/domain/K3dKtTest.kt index e0e5fcb..e0aafa8 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/extensions/server_software/k3s/domain/K3dKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/extensions/server_software/k3s/domain/K3dKtTest.kt @@ -24,7 +24,7 @@ internal class K3dKtTest { provideContainer( containerName, "yobasystems/alpine-docker:dind-amd64", - ContainerStartMode.CREATE_NEW_KILL_EXISTING, // for re-create a potentially existing container + // ContainerStartMode.CREATE_NEW_KILL_EXISTING, // for re-create a potentially existing container sudo = false, options = "--privileged" ) diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/ubuntu/utils/UtilsKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/ubuntu/utils/UtilsKtTest.kt index 5895295..0ace6af 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/ubuntu/utils/UtilsKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/ubuntu/utils/UtilsKtTest.kt @@ -4,6 +4,7 @@ import org.domaindrivenarchitecture.provs.core.Prov import org.domaindrivenarchitecture.provs.core.docker import org.domaindrivenarchitecture.provs.core.echoCommandForText import org.domaindrivenarchitecture.provs.test.tags.ContainerTest +import org.domaindrivenarchitecture.provs.test.tags.NonCi import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test