[skip ci] add options to container start

This commit is contained in:
az 2021-11-29 14:53:20 +01:00
parent dd96949ed3
commit 6a4badbd82

View file

@ -16,15 +16,16 @@ fun UbuntuProv.provideContainerPlatform(
containerName: String,
imageName: String = "ubuntu",
startMode: ContainerStartMode = ContainerStartMode.USE_RUNNING_ELSE_CREATE,
sudo: Boolean = true
sudo: Boolean = true,
options: String = ""
): ProvResult = requireLast {
val dockerCmd = if (sudo) "sudo docker " else "docker "
val dockerCmd = dockerCommand(sudo)
if (startMode == ContainerStartMode.CREATE_NEW_KILL_EXISTING) {
exitAndRmContainer(containerName)
}
if ((startMode == ContainerStartMode.CREATE_NEW_KILL_EXISTING) || (startMode == ContainerStartMode.CREATE_NEW_FAIL_IF_EXISTING)) {
if (!cmd(dockerCmd + "run -dit --name=$containerName $imageName").success) {
if (!cmd(dockerCmd + "run -dit $options --name=$containerName $imageName").success) {
throw RuntimeException("could not start docker")
}
} else if (startMode == ContainerStartMode.USE_RUNNING_ELSE_CREATE) {
@ -33,7 +34,7 @@ fun UbuntuProv.provideContainerPlatform(
// if either container not found or container found but not running
if (!runCheckResult.success || "false\n" == runCheckResult.out) {
cmdNoEval(dockerCmd + "rm -f $containerName")
cmd(dockerCmd + "run -dit --name=$containerName $imageName")
cmd(dockerCmd + "run -dit $options --name=$containerName $imageName")
}
}
ProvResult(containerRuns(containerName, sudo))
@ -41,7 +42,7 @@ fun UbuntuProv.provideContainerPlatform(
fun UbuntuProv.containerRunsPlatform(containerName: String, sudo: Boolean = true): Boolean {
val dockerCmd = if (sudo) "sudo docker " else "docker "
val dockerCmd = dockerCommand(sudo)
return cmdNoEval(dockerCmd + "inspect -f '{{.State.Running}}' $containerName").out?.equals("true\n") ?: false
}
@ -51,19 +52,19 @@ fun UbuntuProv.runContainerPlatform(
imageName: String = "ubuntu",
sudo: Boolean = true
) = def {
val dockerCmd = if (sudo) "sudo docker " else "docker "
val dockerCmd = dockerCommand(sudo)
cmd(dockerCmd + "run -dit --name=$containerName $imageName")
}
fun UbuntuProv.containerExecPlatform(containerName: String, cmd: String, sudo: Boolean = true) = def {
val dockerCmd = if (sudo) "sudo docker " else "docker "
val dockerCmd = dockerCommand(sudo)
cmd(dockerCmd + "exec $containerName $cmd")
}
fun UbuntuProv.dockerProvideImagePlatform(image: DockerImage, skipIfExisting: Boolean, sudo: Boolean): ProvResult {
val dockerCmd = if (sudo) "sudo docker " else "docker "
val dockerCmd = dockerCommand(sudo)
if (skipIfExisting && dockerImageExists(image.imageName())) {
return ProvResult(true)
@ -82,7 +83,7 @@ fun UbuntuProv.dockerProvideImagePlatform(image: DockerImage, skipIfExisting: Bo
fun UbuntuProv.dockerImageExistsPlatform(imageName: String, sudo: Boolean): Boolean {
val dockerCmd = if (sudo) "sudo docker " else "docker "
val dockerCmd = dockerCommand(sudo)
return (cmdNoEval(dockerCmd + "images $imageName -q").out != "")
}
@ -92,10 +93,12 @@ fun UbuntuProv.exitAndRmContainerPlatform(
containerName: String,
sudo: Boolean
) = requireAll {
val dockerCmd = if (sudo) "sudo docker " else "docker "
val dockerCmd = dockerCommand(sudo)
if (containerRuns(containerName)) {
cmd(dockerCmd + "stop $containerName")
}
cmd(dockerCmd + "rm $containerName")
}
private fun dockerCommand(sudo: Boolean) = if (sudo) "sudo docker " else "docker "