diff --git a/build.gradle b/build.gradle index ce9001e..9a53818 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ plugins { } group 'de.difuture.ekut.pht' -version '0.9.0' +version '0.9.1' repositories { jcenter() @@ -106,7 +106,7 @@ publishing { model { tasks.generatePomFileForMavenPublication { - destination = file("$buildDir/libs/lib-0.9.0.pom") + destination = file("$buildDir/libs/lib-0.9.1.pom") } } diff --git a/src/main/kotlin/de/difuture/ekut/pht/lib/train/impl/docker/RunExecutor.kt b/src/main/kotlin/de/difuture/ekut/pht/lib/train/impl/docker/RunExecutor.kt index 409771c..7e93465 100644 --- a/src/main/kotlin/de/difuture/ekut/pht/lib/train/impl/docker/RunExecutor.kt +++ b/src/main/kotlin/de/difuture/ekut/pht/lib/train/impl/docker/RunExecutor.kt @@ -5,7 +5,7 @@ import de.difuture.ekut.pht.lib.train.api.RunResponse import de.difuture.ekut.pht.lib.train.api.StationRuntimeInfo import de.difuture.ekut.pht.lib.train.api.TrainCommand -object CheckRequirementsExecutor +object RunExecutor : DockerArrivalExecutor { override val command = TrainCommand.RUN diff --git a/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/DockerTrainStation.kt b/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/DockerTrainStation.kt deleted file mode 100644 index 2216fc4..0000000 --- a/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/DockerTrainStation.kt +++ /dev/null @@ -1,44 +0,0 @@ -package de.difuture.ekut.pht.lib.train.station - -//import de.difuture.ekut.pht.lib.train.impl.docker.RunAlgorithmExecutor - -//class DockerTrainStation( -// -// override val client: DockerRuntimeClient, -// override val stationInfo: StationRuntimeInfo -//) : TrainStation(client, stationInfo) { -// -// fun departWithAlgorithm(arrival: DockerRegistryTrainArrival): DockerRegistryTrainDeparture { -// -// val output = RunAlgorithmExecutor.execArrival(arrival, this.client, this.stationInfo) -// val response = output.response -// -// // The Output has an error if there either is an error or the response is null -// if (output.error != null || response == null) { -// -// throw TrainException.Output(output) -// } -// -// if (!response.success) { -// -// throw TrainException.RunAlgorithmFailed(output) -// } -// val nextTrainTag = response.nextTrainTag -// -// // Now the container needs to be commited to create the imageId for the -// val imageId = this.client.commitByRebase( -// output.containerOutput.containerId, -// response.exportFiles.map { Paths.get(it) }, -// response.dockerBaseImage, -// arrival.repoName, -// DockerTag.from(nextTrainTag.repr) -// ) -// // Return the train departure. The trainId is not going to change, the new TrainTag -// // is communicated via the nextTrainTag -// return DockerRegistryTrainDeparture( -// imageId, -// arrival.trainName, -// nextTrainTag, -// this.client) -// } -//} diff --git a/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/TrainStation.kt b/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/Platform.kt similarity index 94% rename from src/main/kotlin/de/difuture/ekut/pht/lib/train/station/TrainStation.kt rename to src/main/kotlin/de/difuture/ekut/pht/lib/train/station/Platform.kt index eaae4e6..01e0815 100644 --- a/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/TrainStation.kt +++ b/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/Platform.kt @@ -10,7 +10,7 @@ import de.difuture.ekut.pht.lib.train.api.TrainDeparture import de.difuture.ekut.pht.lib.train.api.DepartureExecutor /** - * A TrainStation is a client of the TrainAPI that consistently uses + * A Platform is a client of the TrainAPI that consistently uses * the provided [RuntimeClient]. Instances of this class hence serve as an entrypoint * for running commands on train arrivals or departures or to generate new departures from * arrivals @@ -19,7 +19,7 @@ import de.difuture.ekut.pht.lib.train.api.DepartureExecutor * @since 0.1.7 * */ -open class TrainStation( +open class Platform( protected open val client: T, protected open val stationInfo: StationRuntimeInfo diff --git a/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/SimpleDockerPlatform.kt b/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/SimpleDockerPlatform.kt new file mode 100644 index 0000000..3f34339 --- /dev/null +++ b/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/SimpleDockerPlatform.kt @@ -0,0 +1,59 @@ +package de.difuture.ekut.pht.lib.train.station + +import de.difuture.ekut.pht.lib.runtime.api.docker.DockerRuntimeClient +import de.difuture.ekut.pht.lib.train.api.DockerRebaseStrategy +import de.difuture.ekut.pht.lib.train.api.RunResponse +import de.difuture.ekut.pht.lib.train.api.StationRuntimeInfo +import de.difuture.ekut.pht.lib.train.impl.docker.DockerRegistryTrainArrival +import de.difuture.ekut.pht.lib.train.impl.docker.DockerRegistryTrainDeparture +import de.difuture.ekut.pht.lib.train.impl.docker.RunExecutor +import jdregistry.client.data.Tag as DockerTag +import java.nio.file.Paths + +class SimpleDockerPlatform( + + override val client: DockerRuntimeClient, + override val stationInfo: StationRuntimeInfo +) : Platform(client, stationInfo) { + + fun departWithAlgorithm(arrival: DockerRegistryTrainArrival): DockerRegistryTrainDeparture { + + val output = RunExecutor.execArrival(arrival, this.client, this.stationInfo) + val response = output.response + + // The Output has an error if there either is an error or the response is null + if (output.error != null || response == null) { + + throw TrainException.Output(output) + } + + if (response.state != RunResponse.AlgorithmExitState.SUCCESS) { + + throw TrainException.RunAlgorithmFailed(output) + } + val rebase = response.rebase + + // SimpleDockerPlatform only supports the DockerRebaseStrategy + if (rebase is DockerRebaseStrategy) { + + val nextTrainTag = rebase.next_train_tag + + // Now the container needs to be commited to create the imageId for the + val imageId = this.client.commitByRebase( + output.containerOutput.containerId, + rebase.export_files.map { Paths.get(it) }, + rebase.from, + arrival.repoName, + DockerTag.from(nextTrainTag.repr) + ) + // Return the train departure. The trainId is not going to change, the new TrainTag + // is communicated via the nextTrainTag + return DockerRegistryTrainDeparture( + imageId, + arrival.trainName, + nextTrainTag, + this.client) + } + throw TrainException.UnsupportedRebaseStrategy(rebase) + } +} diff --git a/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/TrainException.kt b/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/TrainException.kt index ad1a79b..0d87cc7 100644 --- a/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/TrainException.kt +++ b/src/main/kotlin/de/difuture/ekut/pht/lib/train/station/TrainException.kt @@ -1,15 +1,22 @@ package de.difuture.ekut.pht.lib.train.station -//sealed class TrainException(msg: String?) : Exception(msg) { -// -// /** -// * Exception to be thrown when the TrainOutput contains an error message -// */ -// data class Output(val output: TrainOutput<*>) : TrainException(output.error) -// -// /** -// * Exception to be thrown when the 'success' field of the RunAlgorithmResponse failed -// */ -// data class RunAlgorithmFailed(val output: TrainOutput) -// : TrainException(output.response?.message) -//} +import de.difuture.ekut.pht.lib.train.api.RebaseStrategy +import de.difuture.ekut.pht.lib.train.api.RunResponse +import de.difuture.ekut.pht.lib.train.api.TrainOutput + +sealed class TrainException(msg: String?) : Exception(msg) { + + /** + * Exception to be thrown when the TrainOutput contains an error message + */ + data class Output(val output: TrainOutput<*>) : TrainException(output.error) + + /** + * Exception to be thrown when the 'success' field of the RunAlgorithmResponse failed + */ + data class RunAlgorithmFailed(val output: TrainOutput) + : TrainException(output.response?.free_text_message) + + data class UnsupportedRebaseStrategy(val rebase: RebaseStrategy) + : TrainException("Unsupported Rebase Strategy: {}. Train cannot depart.".format(rebase.display)) +}