Skip to content
/ titan Public
forked from titan-data/titan

Commit

Permalink
implement status command (#65)
Browse files Browse the repository at this point in the history
Fixes #56
  • Loading branch information
Derek Smart authored and mcred committed Oct 29, 2019
1 parent c389aac commit 681014a
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 43 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.2
0.3.3
68 changes: 52 additions & 16 deletions config/reflect-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.EngineParameters",
"name":"io.titandata.remote.engine.EngineParameters",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -27,7 +27,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.EngineParameters[]",
"name":"io.titandata.remote.engine.EngineParameters[]",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -36,7 +36,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.EngineRemote",
"name":"io.titandata.remote.engine.EngineRemote",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -45,7 +45,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.EngineRemote[]",
"name":"io.titandata.remote.engine.EngineRemote[]",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -72,7 +72,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.NopParameters",
"name":"io.titandata.remote.nop.NopParameters",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -81,7 +81,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.NopParameters[]",
"name":"io.titandata.remote.nop.NopParameters[]",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -90,7 +90,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.NopRemote",
"name":"io.titandata.remote.nop.NopRemote",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -99,7 +99,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.NopRemote[]",
"name":"io.titandata.remote.nop.NopRemote[]",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand Down Expand Up @@ -245,7 +245,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.S3Parameters",
"name":"io.titandata.remote.s3.S3Parameters",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -254,7 +254,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.S3Parameters[]",
"name":"io.titandata.remote.s3.S3Parameters[]",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -263,7 +263,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.S3Remote",
"name":"io.titandata.remote.s3.S3Remote",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -272,7 +272,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.S3Remote[]",
"name":"io.titandata.remote.s3.S3Remote[]",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -281,7 +281,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.SshParameters",
"name":"io.titandata.remote.s3web.S3WebParameters",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -290,7 +290,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.SshParameters[]",
"name":"io.titandata.remote.s3web.S3WebParameters[]",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -299,7 +299,7 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.SshRemote",
"name":"io.titandata.remote.s3web.S3WebRemote",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand All @@ -308,7 +308,43 @@
"allPublicClasses":true
},
{
"name":"io.titandata.models.SshRemote[]",
"name":"io.titandata.remote.s3web.S3WebRemote[]",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true,
"allDeclaredClasses":true,
"allPublicClasses":true
},
{
"name":"io.titandata.remote.ssh.SshParameters",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true,
"allDeclaredClasses":true,
"allPublicClasses":true
},
{
"name":"io.titandata.remote.ssh.SshParameters[]",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true,
"allDeclaredClasses":true,
"allPublicClasses":true
},
{
"name":"io.titandata.remote.ssh.SshRemote",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
"allPublicConstructors":true,
"allDeclaredClasses":true,
"allPublicClasses":true
},
{
"name":"io.titandata.remote.ssh.SshRemote[]",
"allDeclaredFields":true,
"allPublicMethods":true,
"allDeclaredConstructors":true,
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<artifactId>titan</artifactId>
<name>Titan CLI</name>
<description></description>
<version>0.3.2</version>
<version>0.3.3</version>

<properties>
<java.version>1.8</java.version>
Expand Down Expand Up @@ -123,7 +123,7 @@
<dependency>
<groupId>io.titandata</groupId>
<artifactId>titan-client</artifactId>
<version>0.4.4</version>
<version>0.4.5</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/titandata/titan/providers/Local.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data class Container (
)

class Local: Provider {
private val titanServerVersion = "0.4.4"
private val titanServerVersion = "0.4.5"
private val dockerRegistryUrl = "titandata"

private val httpHandler = HttpHandler()
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/io/titandata/titan/providers/local/Commit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ class Commit (
"email" to email,
"message" to message!!,
"container" to repoMetadata["container"]!!,
"repoTags" to repoMetadata["repoTags"]!!,
"image" to repoMetadata["image"]!!,
"tag" to repoMetadata["tag"]!!,
"digest" to repoMetadata["digest"]!!,
"runtime" to repoMetadata["runtime"]!!
)
val commit = Commit(uuid, metadata)
val response = commitsApi.createCommit(container, commit)
val hash = response.id
println("Commit $hash")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package io.titandata.titan.providers.local
import io.titandata.client.apis.RemotesApi
import io.titandata.client.infrastructure.ClientException
import io.titandata.serialization.RemoteUtil
import io.titandata.models.NopRemote
import io.titandata.remote.nop.NopRemote
import io.titandata.models.Remote

class RemoteLog (
Expand Down
35 changes: 24 additions & 11 deletions src/main/kotlin/io/titandata/titan/providers/local/Run.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package io.titandata.titan.providers.local

import com.google.gson.JsonObject
import io.titandata.client.apis.RepositoriesApi
import io.titandata.titan.clients.Docker
import io.titandata.titan.clients.Docker.Companion.fetchName
Expand All @@ -28,14 +27,20 @@ class Run (
if(containerName.contains("/")) {
exit("Container name cannot contain a slash",1)
}
val image = arguments.last()
val imageArray = arguments.last().split(":")
val image = imageArray[0]
val tag = if (imageArray[1].isNullOrEmpty()) {
"latest"
} else {
imageArray[1]
}

var imageInfo: JSONObject? = null
try {
imageInfo = docker.inspectImage(image)
imageInfo = docker.inspectImage("$image:$tag")
} catch (e: CommandException) {
docker.pull(image)
imageInfo = docker.inspectImage(image)
docker.pull("$image:$tag")
imageInfo = docker.inspectImage("$image:$tag")
}

if (imageInfo == null) {
Expand Down Expand Up @@ -65,21 +70,29 @@ class Run (
argumentEdit.removeAt((argumentEdit.indexOf("--name") + 1))
argumentEdit.removeAt(argumentEdit.indexOf("--name"))
}
if (argumentEdit.contains(image)) {
argumentEdit.removeAt(argumentEdit.indexOf(image))
if (argumentEdit.contains("$image:$tag")) {
argumentEdit.removeAt(argumentEdit.indexOf("$image:$tag"))
}
argList.add("--name")
argList.add(containerName)
argList.addAll(argumentEdit)
val imageSHA = imageInfo.getJSONObject("Config").getString("Image")
val repoDigest = imageInfo.optJSONArray("RepoDigests")[0] as String
val dockerRunCommand = if(repoDigest.isNullOrEmpty()) {
"$image:$tag"
} else {
repoDigest
}
val metadata = mapOf(
"container" to imageSHA,
"repoTags" to imageInfo.getJSONArray("RepoTags")[0],
"container" to dockerRunCommand,
"image" to image,
"tag" to tag,
"digest" to repoDigest,
"runtime" to argList.toString()
)

val updateRepo = Repository(containerName, metadata)
repositoriesApi.updateRepository(containerName, updateRepo)
docker.run(imageSHA, "", argList)
docker.run(dockerRunCommand, "", argList)
println("Running controlled container $containerName")
}
}
36 changes: 27 additions & 9 deletions src/main/kotlin/io/titandata/titan/providers/local/Status.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,40 @@
package io.titandata.titan.providers.local

import io.titandata.titan.providers.Container
import io.titandata.client.apis.RepositoriesApi
import java.text.DecimalFormat
import kotlin.math.log10

class Status (
private val getContainersStatus: () -> List<Container>
private val getContainersStatus: () -> List<Container>,
private val repositoriesApi: RepositoriesApi = RepositoriesApi()
){
private val n = System.lineSeparator()

/**
* https://stackoverflow.com/a/5599842
*/
private fun readableFileSize(size: Long): String {
if (size <= 0) return "0"
val units = arrayOf("Bi", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB")
val digitGroups = (log10(size.toDouble()) / log10(1024.0)).toInt()
return DecimalFormat("#,##0.#").format(size / Math.pow(1024.0, digitGroups.toDouble())) + " " + units[digitGroups]
}

fun status(container: String) {
val status = repositoriesApi.getRepositoryStatus(container)
for(cont in getContainersStatus()) {
if(container == cont.name) {
println("Status: ${cont.status}")
System.out.printf("%15s %s${n}", "Status: ", cont.status)
}
}
println("*** SAMPLE STATUS ***")
println("Size: 5.4GiB")
println("Current Head: d3f4c1")
println("Volumes:")
println(" 1.23GiB /var/lib/postgres/data")
println("Operations: None")
println("*** SAMPLE STATUS ***")
System.out.printf("%15s %s${n}", "Logical Size: ", readableFileSize(status.logicalSize))
System.out.printf("%15s %s${n}", "Actual Size: ", readableFileSize(status.actualSize))
System.out.printf("%15s %s${n}", "Last Commit: ", status.lastCommit)
println()
System.out.printf("%-30s %-10s %s${n}", "Volume", "Logical", "Actual")
for (volume in status.volumeStatus) {
System.out.printf("%-30s %-10s %s${n}", volume.properties["path"], readableFileSize(volume.logicalSize), readableFileSize(volume.actualSize))
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.2
0.3.3

0 comments on commit 681014a

Please sign in to comment.