Skip to content

Commit

Permalink
deploy version in hebau
Browse files Browse the repository at this point in the history
  • Loading branch information
h56983577 committed Nov 29, 2023
1 parent 56078a6 commit ace1680
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ interface IProjectManager {
suspend fun changePassword(username: String, password: String): Result<Unit>
}

val managerList = listOf(
GitClient,
HarborClient,
BusinessKubeClient,
val managerList = listOf<IProjectManager>(
// GitClient,
// HarborClient,
// BusinessKubeClient,
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import cn.edu.buaa.scs.model.ProjectMember
import cn.edu.buaa.scs.project.IProjectManager
import cn.edu.buaa.scs.project.managerList
import cn.edu.buaa.scs.sdk.harbor.models.Artifact
import cn.edu.buaa.scs.storage.bugitDB
import cn.edu.buaa.scs.storage.file.FileManager
import cn.edu.buaa.scs.storage.mongo
import cn.edu.buaa.scs.storage.mysql
Expand Down Expand Up @@ -439,7 +438,8 @@ class ProjectService(val call: ApplicationCall) : IService, FileService.FileDeco
}

fun checkGitRepoNameExist(name: String): Boolean {
return bugitDB.gitRepoList.exists { it.lowerName eq name.lowercase() }
// return bugitDB.gitRepoList.exists { it.lowerName eq name.lowercase() }
return false
}

suspend fun createGitRepo(projectID: Long, req: PostProjectProjectIdReposRequest): Repository {
Expand Down
27 changes: 14 additions & 13 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 @@ -333,7 +333,7 @@ class VmService(val call: ApplicationCall) : IService {
}

suspend fun convertVMToTemplate(uuid: String, name: String): VirtualMachine {
var vm = mysql.virtualMachines.find { it.uuid.eq(uuid) } ?: throw NotFoundException("VM not found")
val vm = mysql.virtualMachines.find { it.uuid.eq(uuid) } ?: throw NotFoundException("VM not found")
call.user().assertWrite(vm)
// 检查是否已经关机
if (vm.powerState.value.lowercase() != "poweredoff") {
Expand All @@ -347,19 +347,20 @@ class VmService(val call: ApplicationCall) : IService {
throw BadRequestException("template name already exists")
}
// convert machine into template
vm = vmClient.convertVMToTemplate(uuid).getOrThrow()
// vm = vmClient.convertVMToTemplate(uuid).getOrThrow()
// config vm template
val (adminId, teacherId, studentId) = when {
call.user().isAdmin() -> Triple("default", "default", "default")
call.user().isTeacher() -> Triple("default", call.userId(), "default")
else -> Triple("default", "default", call.userId())
}
return vmClient.configVM(
vm.uuid,
adminId = adminId,
teacherId = teacherId,
studentId = studentId,
).getOrThrow()
// val (adminId, teacherId, studentId) = when {
// call.user().isAdmin() -> Triple("default", "default", "default")
// call.user().isTeacher() -> Triple("default", call.userId(), "default")
// else -> Triple("default", "default", call.userId())
// }
// return vmClient.configVM(
// vm.uuid,
// adminId = adminId,
// teacherId = teacherId,
// studentId = studentId,
// ).getOrThrow()
return vm
}
}

Expand Down
24 changes: 12 additions & 12 deletions cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/storage/MySQL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.ktorm.database.Database
import org.ktorm.logging.Logger

lateinit var mysql: Database
lateinit var bugitDB: Database
//lateinit var bugitDB: Database

@Suppress("unused")
fun Application.mysqlModule() {
Expand All @@ -30,17 +30,17 @@ fun Application.mysqlModule() {
)
logger("mainDB")().info { "main database connected" }

bugitDB = Database.connect(
dataSource = HikariDataSource(HikariConfig().apply {
jdbcUrl = getConfigString("db.bugit.connectionString")
username = getConfigString("db.bugit.username")
password = getConfigString("db.bugit.password")
addDataSourceProperty("useUnicode", "true")
addDataSourceProperty("characterEncoding", "utf8")
}),
logger = DBLogger(logger("bugitDB")().underlyingLogger)
)
logger("bugitDB")().info { "bugit database connected" }
// bugitDB = Database.connect(
// dataSource = HikariDataSource(HikariConfig().apply {
// jdbcUrl = getConfigString("db.bugit.connectionString")
// username = getConfigString("db.bugit.username")
// password = getConfigString("db.bugit.password")
// addDataSourceProperty("useUnicode", "true")
// addDataSourceProperty("characterEncoding", "utf8")
// }),
// logger = DBLogger(logger("bugitDB")().underlyingLogger)
// )
// logger("bugitDB")().info { "bugit database connected" }
}

internal class DBLogger(private val logger: org.slf4j.Logger) : Logger {
Expand Down
4 changes: 2 additions & 2 deletions cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/vm/VMModule.kt
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: IVMClient
lateinit var sfClient: SangforClient

@Suppress("unused")
Expand All @@ -15,7 +15,7 @@ fun Application.vmModule() {
username = getConfigString("vm.ssh.username"),
)

vmClient = VCenterClient
// vmClient = VCenterClient
sfClient = SangforClient
VMRoutine.run()
}
8 changes: 4 additions & 4 deletions cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/vm/VMRoutine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object VMRoutine : Routine {

private val updateVmCrd = Routine.alwaysDo("vm-worker-update-crd") {
val vmList = mutableListOf<VirtualMachine>()
vmList.addAll(vmClient.getAllVMs().getOrThrow())
// vmList.addAll(vmClient.getAllVMs().getOrThrow())
vmList.addAll(sfClient.getAllVMs().getOrThrow())
vmList.forEach { vmModel ->
val ns = vmModel.applyId.lowercase()
Expand All @@ -41,7 +41,7 @@ object VMRoutine : Routine {

private val updateVMsToDatabase = Routine.alwaysDo("vm-worker-update-db") {
val vmList = mutableListOf<VirtualMachine>()
vmList.addAll(vmClient.getAllVMs().getOrThrow())
// vmList.addAll(vmClient.getAllVMs().getOrThrow())
vmList.addAll(sfClient.getAllVMs().getOrThrow())
val existedVmUUIDList = mysql.virtualMachines.map { it.uuid }.toSet()
mysql.useTransaction {
Expand Down Expand Up @@ -120,8 +120,8 @@ object VMRoutine : Routine {
.toList()
.forEach {
val vm = it
vmClient.deleteVM(vm.uuid)
.onSuccess { vm.delete() }
// vmClient.deleteVM(vm.uuid)
// .onSuccess { vm.delete() }
}
}

Expand Down
21 changes: 11 additions & 10 deletions cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/vm/VMTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ class VMTask(taskData: TaskData) : Task(taskData) {

override suspend fun internalProcess(): Result<Unit> {
val content = jsonMapper.readValue<Content>(taskData.data)
return when (content.type) {
Type.Create -> {
val options = jsonMapper.readValue<CreateVmOptions>(content.data)
vmClient.createVM(options).map { }
}

Type.Delete -> {
vmClient.deleteVM(content.data)
}
}
// return when (content.type) {
// Type.Create -> {
// val options = jsonMapper.readValue<CreateVmOptions>(content.data)
// vmClient.createVM(options).map { }
// }
//
// Type.Delete -> {
// vmClient.deleteVM(content.data)
// }
// }
return Result.success(Unit)
}

}
116 changes: 58 additions & 58 deletions cloudapi-web/src/test/kotlin/cn/edu/buaa/scs/vm/VMClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,62 @@ import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test

class VMClientTest {
@Test
fun testGetAllVMs() {
withApplication(testEnv) {
runBlocking {
vmClient.getAllVMs().getOrNull()?.forEach { println(it) }
delay(100000L)
}
}
}

@Test
fun testPowerOnSync() {
withApplication(testEnv) {
runBlocking {
val result = vmClient.powerOnSync("420700fa-85cd-c8a8-1b95-de8c169613ed")
result.getOrThrow()
}
}
}

@Test
fun testPowerOffSync() {
withApplication(testEnv) {
runBlocking {
val result = vmClient.powerOffSync("420700fa-85cd-c8a8-1b95-de8c169613ed")
result.getOrThrow()
}
}
}

@Test
fun testCreateVm() {
withApplication(testEnv) {
runBlocking {
val result = vmClient.createVM(
CreateVmOptions(
name = "loheagn-test",
templateUuid = "4207e974-8edd-8555-abdc-a664fabf92a3",
applyId = "test-apply-id",
memory = 8192,
cpu = 8,
diskSize = 16106127360,
)
)
result.getOrThrow()
}
}
}

@Test
fun testDeleteVm() {
withApplication(testEnv) {
runBlocking {
val result = vmClient.deleteVM("4207002c-3175-941b-f1ae-3c91af32c627")
result.getOrThrow()
}
}
}
// @Test
// fun testGetAllVMs() {
// withApplication(testEnv) {
// runBlocking {
// vmClient.getAllVMs().getOrNull()?.forEach { println(it) }
// delay(100000L)
// }
// }
// }
//
// @Test
// fun testPowerOnSync() {
// withApplication(testEnv) {
// runBlocking {
// val result = vmClient.powerOnSync("420700fa-85cd-c8a8-1b95-de8c169613ed")
// result.getOrThrow()
// }
// }
// }
//
// @Test
// fun testPowerOffSync() {
// withApplication(testEnv) {
// runBlocking {
// val result = vmClient.powerOffSync("420700fa-85cd-c8a8-1b95-de8c169613ed")
// result.getOrThrow()
// }
// }
// }
//
// @Test
// fun testCreateVm() {
// withApplication(testEnv) {
// runBlocking {
// val result = vmClient.createVM(
// CreateVmOptions(
// name = "loheagn-test",
// templateUuid = "4207e974-8edd-8555-abdc-a664fabf92a3",
// applyId = "test-apply-id",
// memory = 8192,
// cpu = 8,
// diskSize = 16106127360,
// )
// )
// result.getOrThrow()
// }
// }
// }
//
// @Test
// fun testDeleteVm() {
// withApplication(testEnv) {
// runBlocking {
// val result = vmClient.deleteVM("4207002c-3175-941b-f1ae-3c91af32c627")
// result.getOrThrow()
// }
// }
// }
}

0 comments on commit ace1680

Please sign in to comment.