Skip to content

Commit

Permalink
feat :: 큰일났다~~
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunSu1768 committed Jul 8, 2024
1 parent 8cc1646 commit f74b8fa
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import xquare.app.xquareinfra.domain.auth.adapter.dto.request.SignupRequest
import xquare.app.xquareinfra.domain.auth.application.port.`in`.LoginUseCase
import xquare.app.xquareinfra.domain.auth.application.port.`in`.SignupUseCase

@RequestMapping("/auth")
@RequestMapping("/v1/auth")
@RestController
class AuthWebAdapter(
private val loginUseCase: LoginUseCase,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package xquare.app.xquareinfra.domain.container.adapter

import org.springframework.web.bind.annotation.*
import xquare.app.xquareinfra.domain.container.adapter.dto.request.SetContainerConfigRequest
import xquare.app.xquareinfra.domain.container.application.port.`in`.SetContainerConfigUseCase
import java.util.*

@RequestMapping("/v2/container")
@RestController
class V2ContainerWebAdapter(
private val setContainerConfigUseCase: SetContainerConfigUseCase
) {
@PutMapping("/config")
fun setContainerConfig(
@RequestParam(name = "deployId", required = true)
deployId: UUID,
@RequestBody
setContainerConfigRequest: SetContainerConfigRequest
) = setContainerConfigUseCase.setContainerConfig(deployId, setContainerConfigRequest)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package xquare.app.xquareinfra.domain.container.adapter.dto.request

data class SetContainerConfigRequest(
val stag: ContainerConfigDetails?,
val prod: ContainerConfigDetails?
)

data class ContainerConfigDetails(
val branch: String,
val containerPort: Int
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package xquare.app.xquareinfra.domain.container.application.port.`in`

import xquare.app.xquareinfra.domain.container.adapter.dto.request.SetContainerConfigRequest
import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment
import java.util.UUID

interface SetContainerConfigUseCase {
fun setContainerConfig(
deployId: UUID,
setContainerConfigRequest: SetContainerConfigRequest
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package xquare.app.xquareinfra.domain.container.application.service

import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import xquare.app.xquareinfra.domain.container.adapter.dto.request.ContainerConfigDetails
import xquare.app.xquareinfra.domain.container.adapter.dto.request.SetContainerConfigRequest
import xquare.app.xquareinfra.domain.container.application.port.`in`.SetContainerConfigUseCase
import xquare.app.xquareinfra.domain.container.application.port.out.FindContainerPort
import xquare.app.xquareinfra.domain.container.application.port.out.SaveContainerPort
import xquare.app.xquareinfra.domain.container.domain.Container
import xquare.app.xquareinfra.domain.container.domain.ContainerEnvironment
import xquare.app.xquareinfra.domain.deploy.application.port.out.FindDeployPort
import xquare.app.xquareinfra.domain.deploy.domain.Deploy
import xquare.app.xquareinfra.infrastructure.exception.BusinessLogicException
import xquare.app.xquareinfra.infrastructure.feign.client.github.GithubClient
import xquare.app.xquareinfra.infrastructure.feign.client.github.dto.request.DispatchEventRequest
import xquare.app.xquareinfra.infrastructure.global.env.github.GithubProperties
import java.time.LocalDateTime
import java.util.*

@Transactional
@Service
class SetContainerConfigService(
private val findDeployPort: FindDeployPort,
private val findContainerPort: FindContainerPort,
private val githubClient: GithubClient,
private val githubProperties: GithubProperties,
private val saveContainerPort: SaveContainerPort

) : SetContainerConfigUseCase{
override fun setContainerConfig(deployId: UUID, setContainerConfigRequest: SetContainerConfigRequest) {
val deploy = findDeployPort.findById(deployId) ?: throw BusinessLogicException.DEPLOY_NOT_FOUND

setContainerConfigRequest.stag?.let {
processContainerConfig(deploy, it, ContainerEnvironment.stag)
}

setContainerConfigRequest.prod?.let {
processContainerConfig(deploy, it, ContainerEnvironment.prod)
}
}

private fun processContainerConfig(deploy: Deploy, config: ContainerConfigDetails, environment: ContainerEnvironment) {
var container = findContainerPort.findByDeployAndEnvironment(deploy, environment)
var containerId: UUID? = null
if (container != null) {
containerId = container.id
}

container = saveContainerPort.save(
Container(
id = containerId,
deploy = deploy,
containerEnvironment = environment,
lastDeploy = LocalDateTime.now(),
subDomain = null,
environmentVariable = container?.environmentVariable ?: mapOf(),
githubBranch = config.branch,
containerPort = config.containerPort
)
)

githubClient.dispatchWorkflow(
authorization = "Bearer ${githubProperties.token}",
accept = "application/vnd.github.v3+json",
request = DispatchEventRequest(
event_type = "write-values",
client_payload = mapOf(
"club" to deploy.team.teamNameEn,
"name" to deploy.deployName,
"organization" to deploy.organization,
"repository" to deploy.repository,
"branch" to container.githubBranch!!,
"environment" to container.containerEnvironment.name,
"containerPort" to container.containerPort!!
)
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class Container(
containerEnvironment: ContainerEnvironment,
lastDeploy: LocalDateTime,
subDomain: String?,
environmentVariable: Map<String, String>
environmentVariable: Map<String, String>,
githubBranch: String? = null,
containerPort: Int? = null
) : BaseUUIDEntity(id) {
@OneToOne
@JoinColumn(name = "deploy_id")
Expand All @@ -33,6 +35,12 @@ class Container(
var subDomain: String? = subDomain
protected set

@Column(name = "github_branch", nullable = true)
var githubBranch: String? = githubBranch

@Column(name = "container_port", nullable = true)
var containerPort: Int? = containerPort

@ElementCollection
@MapKeyColumn(name="variable_key", length=100)
@Column(name = "environment_variable", columnDefinition = "TEXT")
Expand All @@ -42,4 +50,12 @@ class Container(
fun updateEnvironmentVariable(environmentVariable: Map<String, String>) {
this.environmentVariable = environmentVariable
}

fun updateGithubBranch(githubBranch: String) {
this.githubBranch = githubBranch
}

fun updateContainerPort(containerPort: Int) {
this.containerPort = containerPort
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package xquare.app.xquareinfra.infrastructure.feign.client.github

import org.springframework.cloud.openfeign.FeignClient
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestHeader
import xquare.app.xquareinfra.infrastructure.feign.client.github.dto.request.DispatchEventRequest


@FeignClient(name = "github", url = "\${github.url}")
interface GithubClient {
@PostMapping("/repos/team-xquare/xquare-gitops-repo/dispatches")
fun dispatchWorkflow(
@RequestHeader("Authorization") authorization: String?,
@RequestHeader("Accept") accept: String?,
@RequestBody request: DispatchEventRequest?
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package xquare.app.xquareinfra.infrastructure.feign.client.github.dto.request

data class DispatchEventRequest(
val event_type: String,
val client_payload: Map<String, Any>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package xquare.app.xquareinfra.infrastructure.global.env.github

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConstructorBinding

@ConstructorBinding
@ConfigurationProperties(value = "github")
data class GithubProperties(
val token: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class RequestPermitConfig : SecurityConfigurerAdapter<DefaultSecurityFilterChain
override fun configure(builder: HttpSecurity) {
builder.authorizeRequests().run {
requestMatchers(CorsUtils::isCorsRequest).permitAll()
.antMatchers("/team").permitAll()
.antMatchers("/auth/**").permitAll()
.antMatchers("/deploy/**/approve").permitAll()
.antMatchers(("/container/sync")).permitAll()
.antMatchers("/logs").permitAll()
.antMatchers("/v1/team").permitAll()
.antMatchers("/v1/auth/**").permitAll()
.antMatchers("/v1/deploy/**/approve").permitAll()
.antMatchers(("/v1/container/sync")).permitAll()
.antMatchers("/v1/logs").permitAll()
anyRequest().authenticated()
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ redis:
host: ${REDIS_HOST:localhost}
port: ${REDIS_PORT:6379}

github:
token: ${GITHUB_TOKEN}
url: https://api.github.com
logging:
level:
root: info
Expand Down

0 comments on commit f74b8fa

Please sign in to comment.