Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ktlint #111

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,20 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.gantsign.maven</groupId>
<artifactId>ktlint-maven-plugin</artifactId>
<version>1.2.3</version>
<executions>
<execution>
<id>format-and-check</id>
<goals>
<goal>format</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
30 changes: 25 additions & 5 deletions src/main/kotlin/io/titandata/titan/Cli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,43 @@

package io.titandata.titan

import io.titandata.titan.commands.*
import io.titandata.titan.providers.ProviderFactory
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.options.versionOption
import io.titandata.client.infrastructure.ApiClient
import io.titandata.titan.commands.abortModule
import io.titandata.titan.commands.checkoutModule
import io.titandata.titan.commands.cloneModule
import io.titandata.titan.commands.commitModule
import io.titandata.titan.commands.cpModule
import io.titandata.titan.commands.deleteModule
import io.titandata.titan.commands.installModule
import io.titandata.titan.commands.listModule
import io.titandata.titan.commands.logModule
import io.titandata.titan.commands.migrateModule
import io.titandata.titan.commands.pullModule
import io.titandata.titan.commands.pushModule
import io.titandata.titan.commands.remoteModule
import io.titandata.titan.commands.removeModule
import io.titandata.titan.commands.runModule
import io.titandata.titan.commands.startModule
import io.titandata.titan.commands.statusModule
import io.titandata.titan.commands.stopModule
import io.titandata.titan.commands.tagModule
import io.titandata.titan.commands.uninstallModule
import io.titandata.titan.commands.upgradeModule
import io.titandata.titan.exceptions.CommandException
import io.titandata.titan.providers.ProviderFactory
import kotlin.system.exitProcess
import okhttp3.logging.HttpLoggingInterceptor
import org.apache.log4j.BasicConfigurator
import org.kodein.di.Kodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.instance
import org.kodein.di.generic.setBinding
import kotlin.system.exitProcess

object Cli {
class Titan: CliktCommand(help = "Titan CLI") {
class Titan : CliktCommand(help = "Titan CLI") {

override fun run() {
val providerFactory = ProviderFactory()
Expand All @@ -46,7 +66,7 @@ object Cli {
ApiClient.builder.addInterceptor(logging)
}

BasicConfigurator.configure();
BasicConfigurator.configure()

val version = Cli::class.java.getResource("/VERSION").readText()
val kodein = Kodein {
Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/io/titandata/titan/Version.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

package io.titandata.titan

data class Version (
val major: Int = 0,
val minor: Int = 0,
val micro: Int = 0,
val preRelease: String? = null
data class Version(
val major: Int = 0,
val minor: Int = 0,
val micro: Int = 0,
val preRelease: String? = null
) {
companion object {
@JvmStatic
Expand Down Expand Up @@ -38,8 +38,8 @@ data class Version (
if (this.minor < to.minor) return -1
if (this.micro > to.micro) return 1
if (this.micro < to.micro) return -1
//TODO compare preRelease tag
// TODO compare preRelease tag
return 0
}
}
}
}
26 changes: 13 additions & 13 deletions src/main/kotlin/io/titandata/titan/clients/Docker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ package io.titandata.titan.clients
import io.titandata.titan.Version
import io.titandata.titan.Version.Companion.compare
import io.titandata.titan.utils.CommandExecutor
import kotlin.random.Random
import org.json.JSONArray
import org.json.JSONObject
import org.kohsuke.randname.RandomNameGenerator
import kotlin.random.Random

class Docker(private val executor: CommandExecutor, val identity: String = "titan") {

Expand All @@ -21,7 +21,7 @@ class Docker(private val executor: CommandExecutor, val identity: String = "tita
"--pid=host",
"--network=host",
"-d",
"--restart","always",
"--restart", "always",
"--name=$identity-launch",
"-v", "/var/lib:/var/lib",
"-v", "/run/docker:/run/docker",
Expand Down Expand Up @@ -51,7 +51,7 @@ class Docker(private val executor: CommandExecutor, val identity: String = "tita
}

fun titanLatestIsDownloaded(titanServerVersion: Version): Boolean {
val images= executor.exec(listOf("docker", "images", "titan", "--format", "\"{{.Tag}}\""))
val images = executor.exec(listOf("docker", "images", "titan", "--format", "\"{{.Tag}}\""))
val tags = images.split(System.lineSeparator())
for (item in tags) {
val tag = item.replace("\"", "")
Expand All @@ -73,7 +73,7 @@ class Docker(private val executor: CommandExecutor, val identity: String = "tita
return result.isNotEmpty()
}

fun titanLaunchIsAvailable():Boolean {
fun titanLaunchIsAvailable(): Boolean {
return containerIsRunning("$identity-launch")
}

Expand Down Expand Up @@ -128,7 +128,7 @@ class Docker(private val executor: CommandExecutor, val identity: String = "tita
return executor.exec(argList).trim()
}

fun rmStopped(container:String): String {
fun rmStopped(container: String): String {
val containerId = executor.exec(listOf("docker", "ps", "-f", "status=exited", "-f", "name=$container", "--format", "{{.ID}}")).trim()
return executor.exec(listOf("docker", "container", "rm", containerId))
}
Expand All @@ -147,21 +147,21 @@ class Docker(private val executor: CommandExecutor, val identity: String = "tita
}

fun inspectContainer(container: String): JSONObject? {
val results = executor.exec(listOf("docker", "inspect", "--type", "container", container))
val results = executor.exec(listOf("docker", "inspect", "--type", "container", container))
return JSONArray(results).optJSONObject(0)
}

fun fetchLogs(container: String) {
val lines = executor.exec(listOf("docker", "logs", container)).lines()
for (line in lines) {
if (!this.logs.containsKey(line) && !line.isNullOrEmpty()){
if (!this.logs.containsKey(line) && !line.isNullOrEmpty()) {
this.logs[line] = false
}
}
}

fun inspectImage(image: String): JSONObject? {
val results = executor.exec(listOf("docker", "inspect", "--type", "image", image))
val results = executor.exec(listOf("docker", "inspect", "--type", "image", image))
return JSONArray(results).optJSONObject(0)
}

Expand Down Expand Up @@ -200,16 +200,16 @@ class Docker(private val executor: CommandExecutor, val identity: String = "tita
val rawArguments = this.removePrefix("[").removeSuffix("]").toList(", ")
val returnArgs = mutableListOf<String>()
for (arg in rawArguments) {
if (arg != "--mount" && !arg.contains("type=volume")) {
returnArgs.add(arg)
}
if (arg != "--mount" && !arg.contains("type=volume")) {
returnArgs.add(arg)
}
}
return returnArgs
}

fun List<String>.fetchName(): String {
return when {
this.contains("--name") -> this[(this.indexOf("--name")+1)]
this.contains("--name") -> this[(this.indexOf("--name") + 1)]
else -> RandomNameGenerator(Random.nextInt()).next()
}
}
Expand All @@ -222,4 +222,4 @@ class Docker(private val executor: CommandExecutor, val identity: String = "tita
}
}
}
}
}
37 changes: 13 additions & 24 deletions src/main/kotlin/io/titandata/titan/clients/Kubernetes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,26 @@ import io.kubernetes.client.ApiException
import io.kubernetes.client.Configuration
import io.kubernetes.client.apis.AppsV1Api
import io.kubernetes.client.apis.CoreV1Api
import io.kubernetes.client.custom.Quantity
import io.kubernetes.client.custom.V1Patch
import io.kubernetes.client.models.V1ContainerBuilder
import io.kubernetes.client.models.V1ContainerPortBuilder
import io.kubernetes.client.models.V1EnvVarBuilder
import io.kubernetes.client.models.V1LabelSelectorBuilder
import io.kubernetes.client.models.V1ObjectMetaBuilder
import io.kubernetes.client.custom.V1Patch
import io.kubernetes.client.models.V1EnvVarBuilder
import io.kubernetes.client.models.V1PatchBuilder
import io.kubernetes.client.models.V1PersistentVolumeClaimBuilder
import io.kubernetes.client.models.V1PersistentVolumeClaimSpec
import io.kubernetes.client.models.V1PersistentVolumeClaimSpecBuilder
import io.kubernetes.client.models.V1PersistentVolumeClaimVolumeSource
import io.kubernetes.client.models.V1PersistentVolumeClaimVolumeSourceBuilder
import io.kubernetes.client.models.V1PodSpecBuilder
import io.kubernetes.client.models.V1PodTemplateBuilder
import io.kubernetes.client.models.V1PodTemplateSpecBuilder
import io.kubernetes.client.models.V1ResourceRequirementsBuilder
import io.kubernetes.client.models.V1ServiceBuilder
import io.kubernetes.client.models.V1ServicePortBuilder
import io.kubernetes.client.models.V1ServiceSpecBuilder
import io.kubernetes.client.models.V1StatefulSet
import io.kubernetes.client.models.V1StatefulSetBuilder
import io.kubernetes.client.models.V1StatefulSetSpecBuilder
import io.kubernetes.client.models.V1VolumeBuilder
import io.kubernetes.client.models.V1VolumeMountBuilder
import io.kubernetes.client.util.ClientBuilder
import io.kubernetes.client.util.Config
import io.titandata.models.Volume
import io.titandata.titan.Version
import io.titandata.titan.Version.Companion.compare
import io.titandata.titan.utils.CommandExecutor
import org.json.JSONArray
import org.json.JSONObject
import org.kohsuke.randname.RandomNameGenerator
import kotlin.random.Random

class Kubernetes() {
private val executor = CommandExecutor()
Expand All @@ -57,7 +42,7 @@ class Kubernetes() {
coreApi = CoreV1Api()
appsApi = AppsV1Api()

val jsonPatchClient = ClientBuilder.standard().setOverridePatchFormat(V1Patch.PATCH_FORMAT_JSON_PATCH).build();
val jsonPatchClient = ClientBuilder.standard().setOverridePatchFormat(V1Patch.PATCH_FORMAT_JSON_PATCH).build()
appsApiPatch = AppsV1Api(jsonPatchClient)
}

Expand All @@ -66,8 +51,13 @@ class Kubernetes() {
* the ports in the container. We then create a single replica stateful set with the given volumes (each with
* existing PVCs) mapped in.
*/
fun createStatefulSet(repoName: String, imageId: String, ports: List<Int>, volumes: List<Volume>,
environment: List<String>) {
fun createStatefulSet(
repoName: String,
imageId: String,
ports: List<Int>,
volumes: List<Volume>,
environment: List<String>
) {
val metadata = V1ObjectMetaBuilder()
.withName(repoName)
.withLabels(mapOf("titanRepository" to repoName))
Expand Down Expand Up @@ -96,7 +86,7 @@ class Kubernetes() {
.withName(repoName)
.withImage(imageId)
.withPorts(ports.map { V1ContainerPortBuilder().withContainerPort(it).withName("port-$it").build() })
.withVolumeMounts(volumes.map { V1VolumeMountBuilder().withName(it.name).withMountPath(it.properties["path"] as String).build()})
.withVolumeMounts(volumes.map { V1VolumeMountBuilder().withName(it.name).withMountPath(it.properties["path"] as String).build() })
.withEnv(environment.map { V1EnvVarBuilder().withName(it.substringBefore("=")).withValue(it.substringAfter("=")).build() })
.build())
.withVolumes(volumes.map { V1VolumeBuilder()
Expand Down Expand Up @@ -140,7 +130,7 @@ class Kubernetes() {
*
* We also return a pair, with the second element providing addtional context for the "failed" state
*/
fun getStatefulSetStatus(repoName: String) : Pair<String, String?> {
fun getStatefulSetStatus(repoName: String): Pair<String, String?> {
try {
var set = appsApi.readNamespacedStatefulSet(repoName, defaultNamespace, null, null, null)

Expand Down Expand Up @@ -226,7 +216,6 @@ class Kubernetes() {
}
}


/**
* Update the volumes within a given StatefulSet.
*/
Expand Down Expand Up @@ -262,4 +251,4 @@ class Kubernetes() {
val patch = "[{\"op\":\"replace\",\"path\":\"/spec/replicas\",\"value\":1}]"
appsApiPatch.patchNamespacedStatefulSet(repoName, defaultNamespace, V1Patch(patch), null, null, null, null)
}
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/io/titandata/titan/clients/VolumeDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.titandata.titan.utils.HttpHandler
import io.titandata.titan.utils.HttpHandler.Companion.asString
import org.json.JSONObject

class VolumeDriver(private val httpHandler: HttpHandler, private val volumeDriverUrl: String = "http://localhost:5000"){
class VolumeDriver(private val httpHandler: HttpHandler, private val volumeDriverUrl: String = "http://localhost:5000") {

fun list(): JSONObject {
val response = httpHandler.post("$volumeDriverUrl/VolumeDriver.List", emptyMap()).asString()
Expand All @@ -24,4 +24,4 @@ class VolumeDriver(private val httpHandler: HttpHandler, private val volumeDrive
val response = httpHandler.post("$volumeDriverUrl/VolumeDriver.Unmount", mapOf("Name" to volumeName)).asString()
return JSONObject(response)
}
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/io/titandata/titan/commands/Abort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

package io.titandata.titan.commands

import io.titandata.titan.Dependencies
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.clikt.parameters.arguments.argument
import io.titandata.titan.Dependencies
import org.kodein.di.Kodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.inSet
import org.kodein.di.generic.provider

class Abort : CliktCommand(help = "Abort current push or pull operation") {
private val dependencies: Dependencies by requireObject()
private val repository : String by argument()
private val repository: String by argument()
override fun run() {
val provider = dependencies.provider
provider.abort(repository)
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/io/titandata/titan/commands/Checkout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

package io.titandata.titan.commands

import io.titandata.titan.Dependencies
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.options.multiple
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import io.titandata.titan.Dependencies
import org.kodein.di.Kodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.inSet
Expand All @@ -20,7 +19,7 @@ class Checkout : CliktCommand(help = "Checkout a specific commit") {
private val dependencies: Dependencies by requireObject()
private val repository by argument()
private val commit by option("-c", "--commit", help = "Commit to checkout")
private val tags by option("-t", "--tag", help="Tag to filter latest commit, if commit is not specified").multiple()
private val tags by option("-t", "--tag", help = "Tag to filter latest commit, if commit is not specified").multiple()
override fun run() {
val provider = dependencies.provider
provider.checkout(repository, commit, tags)
Expand Down
Loading