Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.edu.buaa.scs.model

/**
*
* @param ticket 访问凭证
* @param host 服务器主机地址
*/
data class TicketResponse(
/* 访问凭证 */
val ticket: kotlin.String? = null,
/* 服务器主机地址 */
val host: kotlin.String? = null
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ package cn.edu.buaa.scs.controller.models

/**
*
* @param role
* @param content
* @param ticket 访问凭证
* @param host 服务器主机地址
*/
data class ChatCompletionRequestMessagesInner(
val role: kotlin.String,
val content: kotlin.String
data class TicketResponse(
/* 访问凭证 */
val ticket: kotlin.String? = null,
/* 服务器主机地址 */
val host: kotlin.String? = null
)

5 changes: 5 additions & 0 deletions cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/route/Vm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ fun Route.vmRoute() {
}
}

post("/ticket") {
val vmId = call.getVmIdFromPath()
val ticketResponse = call.vm.getWebTicket(vmId)
call.respond(ticketResponse)
}
}

route("/template") {
Expand Down
4 changes: 4 additions & 0 deletions cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/service/Vm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class VmService(val call: ApplicationCall) : IService {
return sfClient.getHosts().getOrThrow()
}

suspend fun getWebTicket(uuid: String): TicketResponse {
return vmClient.getWebTicket(uuid).getOrThrow()
}

fun getPersonalVms(): List<VirtualMachineCrd> {
val vmApplyList =
mysql.vmApplyList.filter { ((it.studentId eq call.userId()) or (it.teacherId eq call.userId())) and (it.experimentId eq 0) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import cn.edu.buaa.scs.vm.sangfor.SangforClient
import cn.edu.buaa.scs.vm.vcenter.VCenterClient
import io.ktor.server.application.*

lateinit var vmClient: IVMClient
lateinit var vmClient: VCenterClient
lateinit var sfClient: SangforClient

@Suppress("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cn.edu.buaa.scs.vm.vcenter
import cn.edu.buaa.scs.config.globalConfig
import cn.edu.buaa.scs.error.NotFoundException
import cn.edu.buaa.scs.model.Host
import cn.edu.buaa.scs.model.TicketResponse
import cn.edu.buaa.scs.model.VirtualMachine
import cn.edu.buaa.scs.model.virtualMachines
import cn.edu.buaa.scs.storage.mysql
Expand Down Expand Up @@ -56,6 +57,10 @@ object VCenterClient : IVMClient {
client.get<VirtualMachine>("/vm/$uuid").getOrThrow()
}

suspend fun getWebTicket(uuid: String): Result<TicketResponse> = runCatching {
client.post<TicketResponse>("/vm/$uuid/ticket").getOrThrow()
}

override suspend fun getVMByName(name: String, applyId: String): Result<VirtualMachine> = runCatching {
mysql.virtualMachines.find { (it.name eq name) and (it.applyId eq applyId) }?: throw vmNotFound(name)
}
Expand Down
30 changes: 30 additions & 0 deletions openapi/cloudapi_v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,27 @@ paths:
in: query
name: sync
description: 表示该请求是否同步返回,默认为false,即默认异步。client需要在后续查询具体的执行情况
'/vm/{vmId}/ticket':
parameters:
- schema:
type: string
name: vmId
in: path
required: true
description: vm uuid
post:
summary: 创建 Web 访问凭证
tags:
- 虚拟机
description: 创建虚拟机的 Web 访问凭证,返回 ticket 和 host。
operationId: post-vm-vmId-ticket
responses:
'200':
description: 成功创建访问凭证
content:
application/json:
schema:
$ref: '#/components/schemas/TicketResponse'
/vms:
get:
summary: get Virtual Machine list
Expand Down Expand Up @@ -4977,6 +4998,15 @@ components:
type: integer
loadCustomFeedbacks:
type: boolean
TicketResponse:
type: object
properties:
ticket:
type: string
description: 访问凭证
host:
type: string
description: 服务器主机地址
securitySchemes:
Authorization:
type: apiKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ fun Application.vcenterRouting() {
VCenterWrapper.convertVMToTemplate(call.getVmUuid()).getOrThrow()
call.respond("OK")
}

post("/ticket") {
val ticketResponse = VCenterWrapper.getWebTicket(call.getVmUuid())
call.respond(ticketResponse)
}
}

route("/health") {
Expand Down
20 changes: 18 additions & 2 deletions vcenter/src/main/kotlin/cn/edu/buaa/scs/vcenter/VCenterWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cn.edu.buaa.scs.config.globalConfig
import cn.edu.buaa.scs.model.VirtualMachine
import cn.edu.buaa.scs.model.VirtualMachineExtraInfo
import cn.edu.buaa.scs.model.applyExtraInfo
import cn.edu.buaa.scs.model.TicketResponse
import cn.edu.buaa.scs.utils.jsonMapper
import cn.edu.buaa.scs.utils.logger
import cn.edu.buaa.scs.vm.ConfigVmOptions
Expand Down Expand Up @@ -69,11 +70,16 @@ object VCenterWrapper {
connectionPool[channelNum] = vcenterConnect()
launch {
for (taskFunc in taskChannel) {
val connection = connectionPool[channelNum] ?: vcenterConnect()
var connection = connectionPool[channelNum] ?: vcenterConnect()
try {
taskFunc(connection)
} catch (e: Throwable) {
logger("vm-worker-$channelNum")().error { e.stackTraceToString() }
try {
connection = vcenterConnect()
taskFunc(connection)
} catch (e: Throwable) {
logger("vm-worker-$channelNum")().error { e.stackTraceToString() }
}
} finally {
connectionPool[channelNum] = connection
}
Expand Down Expand Up @@ -360,6 +366,16 @@ object VCenterWrapper {
}.getOrThrow()
}

fun getWebTicket(uuid: String): TicketResponse {
val connection = vcenterConnect()
val vmRef = connection.getVmRefByUuid(uuid)
val vmTicket = connection.vimPort.acquireTicket(vmRef, "webmks")
return TicketResponse(
ticket = vmTicket.ticket,
host = vmTicket.host
)
}

suspend fun convertVMToTemplate(uuid: String): Result<Unit> {
return baseSyncTask { connection ->
val vmRef = connection.getVmRefByUuid(uuid)
Expand Down
Loading