[skip ci] add options to container start
This commit is contained in:
parent
dd96949ed3
commit
6a4badbd82
1 changed files with 13 additions and 10 deletions
|
@ -16,15 +16,16 @@ fun UbuntuProv.provideContainerPlatform(
|
||||||
containerName: String,
|
containerName: String,
|
||||||
imageName: String = "ubuntu",
|
imageName: String = "ubuntu",
|
||||||
startMode: ContainerStartMode = ContainerStartMode.USE_RUNNING_ELSE_CREATE,
|
startMode: ContainerStartMode = ContainerStartMode.USE_RUNNING_ELSE_CREATE,
|
||||||
sudo: Boolean = true
|
sudo: Boolean = true,
|
||||||
|
options: String = ""
|
||||||
): ProvResult = requireLast {
|
): ProvResult = requireLast {
|
||||||
val dockerCmd = if (sudo) "sudo docker " else "docker "
|
val dockerCmd = dockerCommand(sudo)
|
||||||
|
|
||||||
if (startMode == ContainerStartMode.CREATE_NEW_KILL_EXISTING) {
|
if (startMode == ContainerStartMode.CREATE_NEW_KILL_EXISTING) {
|
||||||
exitAndRmContainer(containerName)
|
exitAndRmContainer(containerName)
|
||||||
}
|
}
|
||||||
if ((startMode == ContainerStartMode.CREATE_NEW_KILL_EXISTING) || (startMode == ContainerStartMode.CREATE_NEW_FAIL_IF_EXISTING)) {
|
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")
|
throw RuntimeException("could not start docker")
|
||||||
}
|
}
|
||||||
} else if (startMode == ContainerStartMode.USE_RUNNING_ELSE_CREATE) {
|
} 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 either container not found or container found but not running
|
||||||
if (!runCheckResult.success || "false\n" == runCheckResult.out) {
|
if (!runCheckResult.success || "false\n" == runCheckResult.out) {
|
||||||
cmdNoEval(dockerCmd + "rm -f $containerName")
|
cmdNoEval(dockerCmd + "rm -f $containerName")
|
||||||
cmd(dockerCmd + "run -dit --name=$containerName $imageName")
|
cmd(dockerCmd + "run -dit $options --name=$containerName $imageName")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ProvResult(containerRuns(containerName, sudo))
|
ProvResult(containerRuns(containerName, sudo))
|
||||||
|
@ -41,7 +42,7 @@ fun UbuntuProv.provideContainerPlatform(
|
||||||
|
|
||||||
|
|
||||||
fun UbuntuProv.containerRunsPlatform(containerName: String, sudo: Boolean = true): Boolean {
|
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
|
return cmdNoEval(dockerCmd + "inspect -f '{{.State.Running}}' $containerName").out?.equals("true\n") ?: false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,19 +52,19 @@ fun UbuntuProv.runContainerPlatform(
|
||||||
imageName: String = "ubuntu",
|
imageName: String = "ubuntu",
|
||||||
sudo: Boolean = true
|
sudo: Boolean = true
|
||||||
) = def {
|
) = def {
|
||||||
val dockerCmd = if (sudo) "sudo docker " else "docker "
|
val dockerCmd = dockerCommand(sudo)
|
||||||
cmd(dockerCmd + "run -dit --name=$containerName $imageName")
|
cmd(dockerCmd + "run -dit --name=$containerName $imageName")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun UbuntuProv.containerExecPlatform(containerName: String, cmd: String, sudo: Boolean = true) = def {
|
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")
|
cmd(dockerCmd + "exec $containerName $cmd")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun UbuntuProv.dockerProvideImagePlatform(image: DockerImage, skipIfExisting: Boolean, sudo: Boolean): ProvResult {
|
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())) {
|
if (skipIfExisting && dockerImageExists(image.imageName())) {
|
||||||
return ProvResult(true)
|
return ProvResult(true)
|
||||||
|
@ -82,7 +83,7 @@ fun UbuntuProv.dockerProvideImagePlatform(image: DockerImage, skipIfExisting: Bo
|
||||||
|
|
||||||
|
|
||||||
fun UbuntuProv.dockerImageExistsPlatform(imageName: String, sudo: Boolean): Boolean {
|
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 != "")
|
return (cmdNoEval(dockerCmd + "images $imageName -q").out != "")
|
||||||
}
|
}
|
||||||
|
@ -92,10 +93,12 @@ fun UbuntuProv.exitAndRmContainerPlatform(
|
||||||
containerName: String,
|
containerName: String,
|
||||||
sudo: Boolean
|
sudo: Boolean
|
||||||
) = requireAll {
|
) = requireAll {
|
||||||
val dockerCmd = if (sudo) "sudo docker " else "docker "
|
val dockerCmd = dockerCommand(sudo)
|
||||||
|
|
||||||
if (containerRuns(containerName)) {
|
if (containerRuns(containerName)) {
|
||||||
cmd(dockerCmd + "stop $containerName")
|
cmd(dockerCmd + "stop $containerName")
|
||||||
}
|
}
|
||||||
cmd(dockerCmd + "rm $containerName")
|
cmd(dockerCmd + "rm $containerName")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun dockerCommand(sudo: Boolean) = if (sudo) "sudo docker " else "docker "
|
||||||
|
|
Loading…
Reference in a new issue