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

TRY: Vm management #4

Merged
merged 4 commits into from
Apr 13, 2022
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
11 changes: 6 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
val ktor_version: String by project
val kotlin_version: String by project

plugins {
Expand All @@ -22,9 +21,15 @@ repositories {
}

dependencies {
val ktor_version = "1.6.8"
// ktor server
implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-server-netty:$ktor_version")

// ktor client
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("io.ktor:ktor-client-cio:$ktor_version")

// coroutine
val coroutine_version = "1.6.0"
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version")
Expand All @@ -39,11 +44,7 @@ dependencies {
implementation("io.ktor:ktor-auth:$ktor_version")

// serialization
val jackson_version = "2.13.0"
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0")
implementation("io.ktor:ktor-jackson:$ktor_version")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:$jackson_version")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version")

// Redis
implementation("io.lettuce:lettuce-core:6.1.5.RELEASE")
Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
ktor_version=1.6.5
kotlin_version=1.5.31
kotlin.code.style=official
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
package cn.edu.buaa.scs.controller.plugins

import cn.edu.buaa.scs.utils.InstantSerializer
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.jackson.*
import org.ktorm.jackson.KtormModule
import java.time.Instant

fun Application.configureContentNegotiation() {
install(ContentNegotiation) {
jackson {
registerModule(KtormModule())
registerModule(JavaTimeModule().apply {
addSerializer(
Instant::class.java,
InstantSerializer()
)
})
registerModule(Jdk8Module())
}
}
}
12 changes: 0 additions & 12 deletions src/main/kotlin/cn/edu/buaa/scs/utils/Time.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package cn.edu.buaa.scs.utils

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

const val defaultDateTimeFormatter = "yyyy/MM/dd HH:mm:ss"
Expand All @@ -27,12 +23,4 @@ fun Long.formatDateTime(formatString: String = defaultDateTimeFormatter): String
object TimeUtil {
fun currentDateTime(): String =
System.currentTimeMillis().formatDateTime()
}

class InstantSerializer : StdSerializer<Instant>(Instant::class.java) {
override fun serialize(value: Instant?, gen: JsonGenerator?, provider: SerializerProvider?) {
val formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")
.withZone(ZoneOffset.ofHours(8))
gen?.writeString(value?.let { formatter.format(it) })
}
}
4 changes: 4 additions & 0 deletions src/main/kotlin/cn/edu/buaa/scs/vm/IVmClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cn.edu.buaa.scs.vm

interface IVmClient {
}
77 changes: 77 additions & 0 deletions src/main/kotlin/cn/edu/buaa/scs/vm/OldVmClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cn.edu.buaa.scs.vm

import cn.edu.buaa.scs.utils.getConfigString
import io.ktor.application.*
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.features.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.http.content.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.ByteArrayInputStream
import java.net.URLDecoder
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory

object OldVmClient : IVmClient {
internal lateinit var svcPath: String

private val client: HttpClient by lazy {
HttpClient(CIO) {
engine {
maxConnectionsCount = 1000
endpoint {
maxConnectionsPerRoute = 100
pipelineMaxSize = 20
keepAliveTime = 5000
connectTimeout = 5000
connectAttempts = 5
}

}
defaultRequest {
header("SOAPAction", "\"\"")
}
}
}

private suspend fun <T> baseAction(req: String, respTag: String, convert: (String) -> T): T {
val resp = client.post<String>(svcPath) {
body = TextContent(req, contentType = ContentType.Text.Xml)
}
val builder: DocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
val doc = withContext(Dispatchers.IO) {
ByteArrayInputStream(resp.toByteArray(Charsets.UTF_8)).use {
builder.parse(it)
}
}
val node = doc.getElementsByTagName(respTag).item(0)
return convert(URLDecoder.decode(node.textContent, Charsets.UTF_8))
}

suspend fun getVmInfo(name: String): String {
return baseAction(
"""
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:vcm="http://vm.manager.buaa.edu.cn/VCManager">
<soapenv:Header/>
<soapenv:Body>
<vcm:GetVMInfo>
<name>$name</name>
</vcm:GetVMInfo>
</soapenv:Body>
</soapenv:Envelope>
""".trimIndent(),
"GetVMInfoReturn"
) {
it
}
}
}

@Suppress("unused")
fun Application.oldVmClientModule() {
val endpoint = getConfigString("oldVmManager.endpoint")
OldVmClient.svcPath = endpoint
}
17 changes: 17 additions & 0 deletions src/test/kotlin/cn/edu/buaa/scs/vm/OldVmClientTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.edu.buaa.scs.vm

import cn.edu.buaa.scs.testEnv
import io.ktor.server.testing.*
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test

class OldVmClientTest {
@Test
fun testGetVmInfo() {
withApplication(testEnv) {
runBlocking {
println(OldVmClient.getVmInfo("debian11-ks-2"))
}
}
}
}