-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from KenSuenobu/ticket-54
Ticket 54
- Loading branch information
Showing
3 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
scattersphere-tasks/src/main/scala/io/buildfactory/scattersphere/tasks/DockerTask.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* _____ __ __ __ | ||
* / ___/_________ _/ /_/ /____ ______________ / /_ ___ ________ | ||
* \__ \/ ___/ __ `/ __/ __/ _ \/ ___/ ___/ __ \/ __ \/ _ \/ ___/ _ \ | ||
* ___/ / /__/ /_/ / /_/ /_/ __/ / (__ ) /_/ / / / / __/ / / __/ | ||
* /____/\___/\__,_/\__/\__/\___/_/ /____/ .___/_/ /_/\___/_/ \___/ | ||
* /_/ | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.buildfactory.scattersphere.tasks | ||
|
||
import io.buildfactory.scattersphere.core.util.RunnableTask | ||
import io.buildfactory.scattersphere.core.util.logging.SimpleLogger | ||
|
||
import scala.sys.process.{Process, ProcessBuilder} | ||
|
||
/** [[RunnableTask]] that runs a docker process with a container name, and the given command, in --net host mode | ||
* ("--net host") in docker. These flags can be overridden, but it is not recommended that this be done unless you know | ||
* what you're doing! | ||
* | ||
* @param containerName name of the container to use | ||
* @param command the command to run from the referenced container | ||
*/ | ||
class DockerTask(containerName: String, command: String) extends RunnableTask with SimpleLogger { | ||
|
||
private var processBuilder: ProcessBuilder = null | ||
private var process: Process = null | ||
private var dockerFlags: String = "--net host" | ||
|
||
override def run(): Unit = { | ||
logger.debug(s"Running command ${command}") | ||
processBuilder = Process(s"docker run $dockerFlags $containerName $command") | ||
process = processBuilder.run | ||
} | ||
|
||
/** Retrieves the current docker flags. | ||
* | ||
* @return docker flags | ||
*/ | ||
def getDockerFlags(): String = dockerFlags | ||
|
||
/** Sets the docker flags to use - this will override the default "--net host" flags, so if you still need to use them, | ||
* make sure to include them in the flags statement. | ||
* | ||
* @param flags the flags to send to Docker. | ||
*/ | ||
def setDockerFlags(flags: String): Unit = dockerFlags = flags | ||
|
||
/** Returns the output (stdout) from the process as a stream of [[String]] data. | ||
* | ||
* @return stream of string objects | ||
*/ | ||
def getProcessOutput(): Stream[String] = processBuilder.lineStream | ||
|
||
override def onFinished(): Unit = { | ||
logger.trace(s"Command finished: ${command}") | ||
|
||
logger.info("Process is still running; terminating.") | ||
process.destroy() | ||
|
||
logger.debug(s"Command exit code: ${process.exitValue()}") | ||
} | ||
|
||
override def onException(t: Throwable): Unit = { | ||
logger.debug("Exception occurred during run", t) | ||
|
||
logger.debug("Process is still running; terminating.") | ||
process.destroy() | ||
} | ||
|
||
|
||
} |
84 changes: 84 additions & 0 deletions
84
scattersphere-tasks/src/test/scala/io/buildfactory/scattersphere/tasks/DockerTaskTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* _____ __ __ __ | ||
* / ___/_________ _/ /_/ /____ ______________ / /_ ___ ________ | ||
* \__ \/ ___/ __ `/ __/ __/ _ \/ ___/ ___/ __ \/ __ \/ _ \/ ___/ _ \ | ||
* ___/ / /__/ /_/ / /_/ /_/ __/ / (__ ) /_/ / / / / __/ / / __/ | ||
* /____/\___/\__,_/\__/\__/\___/_/ /____/ .___/_/ /_/\___/_/ \___/ | ||
* /_/ | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.buildfactory.scattersphere.tasks | ||
|
||
import io.buildfactory.scattersphere.core.util.execution.JobExecutor | ||
import io.buildfactory.scattersphere.core.util._ | ||
import io.buildfactory.scattersphere.core.util.{Job, JobBuilder, Task, TaskBuilder} | ||
import io.buildfactory.scattersphere.core.util.logging.SimpleLogger | ||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class DockerTaskTest extends FlatSpec with Matchers with SimpleLogger { | ||
|
||
"Docker task" should "be able to get ubuntu and run a command within its container" in { | ||
val dockerTask: DockerTask = new DockerTask("centos", "/bin/ls -al /etc/") | ||
|
||
dockerTask.getDockerFlags() shouldBe "--net host" | ||
|
||
val sTask1: Task = TaskBuilder("DockerTask1").withTask(dockerTask).build() | ||
val job: Job = JobBuilder().withTasks(sTask1).build() | ||
val jExec: JobExecutor = JobExecutor(job) | ||
|
||
jExec.queue().run() | ||
job.status shouldBe JobFinished | ||
|
||
val output: List[String] = dockerTask.getProcessOutput().toList | ||
|
||
assert(output.size > 1) | ||
assert(output(0).toLowerCase() contains "total") | ||
} | ||
|
||
it should "be able to run ps -axwww" in { | ||
val dockerTask: DockerTask = new DockerTask("centos", "/bin/ps -axwww") | ||
|
||
dockerTask.getDockerFlags() shouldBe "--net host" | ||
|
||
val sTask1: Task = TaskBuilder("DockerTaskPs").withTask(dockerTask).build() | ||
val job: Job = JobBuilder().withTasks(sTask1).build() | ||
val jExec: JobExecutor = JobExecutor(job) | ||
|
||
jExec.queue().run() | ||
job.status shouldBe JobFinished | ||
|
||
val output: List[String] = dockerTask.getProcessOutput().toList | ||
|
||
assert(output.size > 1) | ||
assert(output(1).toLowerCase() contains "ps -axwww") | ||
} | ||
|
||
it should "be able to run curl with --net host flags" in { | ||
val dockerTask: DockerTask = new DockerTask("centos", "curl http://www.google.com/") | ||
|
||
dockerTask.getDockerFlags() shouldBe "--net host" | ||
|
||
val sTask1: Task = TaskBuilder("centosTask3").withTask(dockerTask).build() | ||
val job: Job = JobBuilder().withTasks(sTask1).build() | ||
val jExec: JobExecutor = JobExecutor(job) | ||
|
||
jExec.queue().run() | ||
job.status shouldBe JobFinished | ||
|
||
val output: List[String] = dockerTask.getProcessOutput().toList | ||
|
||
assert(output.size > 1) | ||
} | ||
|
||
} |