From 3e235ed641a5234dfae80bd85ac3f428feb2ffd3 Mon Sep 17 00:00:00 2001 From: Niklas Haiden Date: Thu, 4 Jan 2024 22:27:37 +0100 Subject: [PATCH 1/3] begin rabbitmq and keycloak journey --- build.gradle | 7 +++++++ compose.yaml | 5 +++++ .../ariesbackend/model/DockerService.kt | 3 ++- .../model/dockerservice/WebsiteService.kt | 19 +++++++++++++++++++ .../ariesbackend/model/types/DatabaseType.kt | 3 ++- .../ariesbackend/web/MachineController.kt | 1 - src/main/resources/application.properties | 6 +++++- 7 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/tech/niklas/ariesbackend/model/dockerservice/WebsiteService.kt diff --git a/build.gradle b/build.gradle index d0137ca..de9b1f7 100644 --- a/build.gradle +++ b/build.gradle @@ -33,7 +33,14 @@ dependencies { implementation 'me.paulschwarz:spring-dotenv:4.0.0' implementation 'org.springframework.boot:spring-boot-starter-amqp' implementation 'org.springframework.boot:spring-boot-starter-quartz' + implementation 'org.springframework.boot:spring-boot-starter-actuator' + implementation 'org.springframework.boot:spring-boot-configuration-processor' + implementation 'com.github.dasniko:testcontainers-keycloak:3.2.0' testImplementation 'org.springframework.amqp:spring-rabbit-test' + testImplementation 'org.testcontainers:junit-jupiter' + testImplementation 'org.springframework.boot:spring-boot-testcontainers' + testImplementation 'org.testcontainers:rabbitmq' + testImplementation 'org.testcontainers:postgresql' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' developmentOnly 'org.springframework.boot:spring-boot-docker-compose' diff --git a/compose.yaml b/compose.yaml index b6ac624..1362948 100644 --- a/compose.yaml +++ b/compose.yaml @@ -17,5 +17,10 @@ services: PGADMIN_DEFAULT_PASSWORD: Niklas12 volumes: - pgadmin-data:/var/lib/pgadmin + rabbitmq: + image: rabbitmq:management + ports: + - "5672:5672" + - "15672:15672" volumes: pgadmin-data: \ No newline at end of file diff --git a/src/main/kotlin/tech/niklas/ariesbackend/model/DockerService.kt b/src/main/kotlin/tech/niklas/ariesbackend/model/DockerService.kt index 8b21289..e31f129 100644 --- a/src/main/kotlin/tech/niklas/ariesbackend/model/DockerService.kt +++ b/src/main/kotlin/tech/niklas/ariesbackend/model/DockerService.kt @@ -26,8 +26,9 @@ open class DockerService protected constructor( @NotNull @ManyToOne @JoinColumn(name = "machineID") - var serviceMachine: DockerMachine + var serviceMachine: DockerMachine, + @OneToOne ) { override fun equals(other: Any?): Boolean { if (this === other) return true diff --git a/src/main/kotlin/tech/niklas/ariesbackend/model/dockerservice/WebsiteService.kt b/src/main/kotlin/tech/niklas/ariesbackend/model/dockerservice/WebsiteService.kt new file mode 100644 index 0000000..68d6c11 --- /dev/null +++ b/src/main/kotlin/tech/niklas/ariesbackend/model/dockerservice/WebsiteService.kt @@ -0,0 +1,19 @@ +package tech.niklas.ariesbackend.model.dockerservice + +import jakarta.persistence.DiscriminatorValue +import jakarta.persistence.Entity +import tech.niklas.ariesbackend.model.DockerMachine +import tech.niklas.ariesbackend.model.DockerService +import tech.niklas.ariesbackend.model.types.DatabaseType +import tech.niklas.ariesbackend.model.types.ServiceType + +@Entity +@DiscriminatorValue("website-service") +class WebsiteService( + serviceID: String?, + serviceName: String, + serviceType: ServiceType, + serviceMachine: DockerMachine, + serviceUrl: String +) + : DockerService(serviceID, serviceName, serviceType, serviceMachine) diff --git a/src/main/kotlin/tech/niklas/ariesbackend/model/types/DatabaseType.kt b/src/main/kotlin/tech/niklas/ariesbackend/model/types/DatabaseType.kt index 870a71d..7c30e6b 100644 --- a/src/main/kotlin/tech/niklas/ariesbackend/model/types/DatabaseType.kt +++ b/src/main/kotlin/tech/niklas/ariesbackend/model/types/DatabaseType.kt @@ -2,5 +2,6 @@ package tech.niklas.ariesbackend.model.types enum class DatabaseType(val databaseType: String) { POSTGRESQL("postgres"), - MARIADB("mariadb") + MARIADB("mariadb"), + MONGODB("mongodb") } \ No newline at end of file diff --git a/src/main/kotlin/tech/niklas/ariesbackend/web/MachineController.kt b/src/main/kotlin/tech/niklas/ariesbackend/web/MachineController.kt index c161943..fa79a0b 100644 --- a/src/main/kotlin/tech/niklas/ariesbackend/web/MachineController.kt +++ b/src/main/kotlin/tech/niklas/ariesbackend/web/MachineController.kt @@ -36,7 +36,6 @@ class MachineController(@Autowired private val dockerMachineService: DockerMachi @PatchMapping("/update/{id}") fun updateMachine(@PathVariable @NotBlank id: String, @RequestBody dockerMachine: DockerMachine): DockerMachine { - return dockerMachineService.updateMachine(id, dockerMachine) } } \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 1d3157d..f607d8a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -8,4 +8,8 @@ spring.security.oauth2.client.registration.keycloak.client-id=${KC_CLIENT_ID} spring.security.oauth2.client.registration.keycloak.client-secret=${KC_CLIENT_SECRET} spring.security.oauth2.client.registration.keycloak.scope[3]=profile,email spring.devtools.livereload.enabled=true -server.port=8081 +spring.rabbitmq.host=localhost +spring.rabbitmq.port=5672 +spring.rabbitmq.username=guest +spring.rabbitmq.password=guest +server.port=8080 From 61b3c40c449c388823adcfc77f3c024582e0fd83 Mon Sep 17 00:00:00 2001 From: Niklas Haiden Date: Sat, 20 Jan 2024 23:18:57 +0100 Subject: [PATCH 2/3] rabbitmq working test queue --- .devcontainer/devcontainer.json | 3 +- build.gradle | 2 +- compose.yaml | 2 +- .../ariesbackend/model/DockerService.kt | 2 +- .../ariesbackend/queue/RabbitMQConfig.kt | 43 +++++++++++++++++++ .../ariesbackend/queue/RabbitMQListener.kt | 12 ++++++ .../ariesbackend/web/RabbitController.kt | 21 +++++++++ 7 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQConfig.kt create mode 100644 src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQListener.kt create mode 100644 src/main/kotlin/tech/niklas/ariesbackend/web/RabbitController.kt diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 937cac8..77d8107 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -3,11 +3,12 @@ { "name": "Aries-Backend DevContainer File for quick dev env setup", // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile - "image": "mcr.microsoft.com/devcontainers/java:0-17", + "image": "mcr.microsoft.com/devcontainers/java:0-21", "features": { "ghcr.io/devcontainers/features/java:1": { "version": "none", "installGradle": "true", + "gradleVersion": "8.3", }, "ghcr.io/devcontainers/features/docker-in-docker:2": { "dockerDashComposeVersion": "v2", diff --git a/build.gradle b/build.gradle index de9b1f7..deda596 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - id 'org.springframework.boot' version '3.1.0' + id 'org.springframework.boot' version '3.2.1' id 'io.spring.dependency-management' version '1.1.0' id 'org.jetbrains.kotlin.jvm' version '1.8.21' id 'org.jetbrains.kotlin.plugin.spring' version '1.8.21' diff --git a/compose.yaml b/compose.yaml index 1362948..fa11ef8 100644 --- a/compose.yaml +++ b/compose.yaml @@ -6,7 +6,7 @@ services: - 'POSTGRES_PASSWORD=secret' - 'POSTGRES_USER=myuser' ports: - - '5433:5432' + - '5432:5432' pgadmin: image: dpage/pgadmin4 restart: always diff --git a/src/main/kotlin/tech/niklas/ariesbackend/model/DockerService.kt b/src/main/kotlin/tech/niklas/ariesbackend/model/DockerService.kt index e31f129..33e917d 100644 --- a/src/main/kotlin/tech/niklas/ariesbackend/model/DockerService.kt +++ b/src/main/kotlin/tech/niklas/ariesbackend/model/DockerService.kt @@ -28,7 +28,7 @@ open class DockerService protected constructor( @JoinColumn(name = "machineID") var serviceMachine: DockerMachine, - @OneToOne + ) { override fun equals(other: Any?): Boolean { if (this === other) return true diff --git a/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQConfig.kt b/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQConfig.kt new file mode 100644 index 0000000..6a669ee --- /dev/null +++ b/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQConfig.kt @@ -0,0 +1,43 @@ +package tech.niklas.ariesbackend.queue + +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory +import org.springframework.amqp.rabbit.connection.ConnectionFactory +import org.springframework.amqp.rabbit.core.RabbitTemplate +import org.springframework.beans.factory.annotation.Value +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +@Configuration +class RabbitMQConfig { + @Value("\${spring.rabbitmq.host}") + private lateinit var host: String + + @Value("\${spring.rabbitmq.port}") + private var port: Int = 0 + + @Value("\${spring.rabbitmq.username}") + private lateinit var username: String + + @Value("\${spring.rabbitmq.password}") + private lateinit var password: String + + @Bean + fun connectionFactory(): ConnectionFactory { + val connectionFactory = CachingConnectionFactory() + connectionFactory.setHost(host) + connectionFactory.setPort(port) + connectionFactory.username = username + connectionFactory.setPassword(password) + return connectionFactory + } + + @Bean + fun rabbitTemplate(connectionFactory: ConnectionFactory?): RabbitTemplate? { + return connectionFactory?.let { RabbitTemplate(it) } + } + + @Bean + fun dockerDeploy(): org.springframework.amqp.core.Queue { + return org.springframework.amqp.core.Queue("docker.deploy", true) + } +} \ No newline at end of file diff --git a/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQListener.kt b/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQListener.kt new file mode 100644 index 0000000..ee3cce7 --- /dev/null +++ b/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQListener.kt @@ -0,0 +1,12 @@ +package tech.niklas.ariesbackend.queue + +import org.springframework.amqp.rabbit.annotation.RabbitListener +import org.springframework.stereotype.Component + +@Component +class RabbitMQListener { + @RabbitListener(queues = arrayOf("docker.deploy")) + fun processMessage(message: String) { + println("Received message: $message") + } +} \ No newline at end of file diff --git a/src/main/kotlin/tech/niklas/ariesbackend/web/RabbitController.kt b/src/main/kotlin/tech/niklas/ariesbackend/web/RabbitController.kt new file mode 100644 index 0000000..afcfc35 --- /dev/null +++ b/src/main/kotlin/tech/niklas/ariesbackend/web/RabbitController.kt @@ -0,0 +1,21 @@ +package tech.niklas.ariesbackend.web + +import org.springframework.amqp.core.Queue +import org.springframework.amqp.rabbit.core.RabbitTemplate +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +@RequestMapping("/rabbit") +class RabbitController(@Autowired private val rabbitTemplate: RabbitTemplate, + @Autowired private val queue: Queue) { + + @GetMapping("/test") + fun sendMessage(): String { + val message: String = "Hello Rabbit!" + rabbitTemplate.convertAndSend(queue.name, message) + return "Message sent successfully!" + } +} \ No newline at end of file From 826dff3186534f32d34ee076625ad4a7086828c8 Mon Sep 17 00:00:00 2001 From: Niklas Haiden Date: Tue, 19 Mar 2024 11:03:47 +0100 Subject: [PATCH 3/3] working tests and actions update --- .github/workflows/dev.yml | 8 ++++---- build.gradle | 6 ++++-- compose.yaml | 13 +++++++++++++ docker-compose-example.yml | 7 ++++++- .../tech/niklas/ariesbackend/model/DockerAgent.kt | 2 +- .../niklas/ariesbackend/queue/RabbitMQConfig.kt | 15 +++++++++++++-- .../niklas/ariesbackend/queue/RabbitMQListener.kt | 4 ---- .../niklas/ariesbackend/web/RabbitController.kt | 6 +++++- 8 files changed, 46 insertions(+), 15 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index a91b090..337cab1 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -26,9 +26,9 @@ jobs: - name: Login to Docker Hub to push image uses: docker/login-action@v2 with: - registry: cr.nkls.pw - username: ${{ secrets.NEXUS_USERNAME }} - password: ${{ secrets.NEXUS_PASSWORD }} + registry: oci.draco.sh + username: ${{ secrets.OCI_USERNAME }} + password: ${{ secrets.OCI_PASSWORD }} - name: Build Docker Image and push uses: docker/build-push-action@v3 @@ -36,4 +36,4 @@ jobs: context: . platforms: linux/amd64,linux/arm64/v8 push: true - tags: cr.nkls.pw/aries-backend:latest-dev + tags: oci.draco.sh/aries/aries-backend:latest-dev diff --git a/build.gradle b/build.gradle index deda596..fa7f304 100644 --- a/build.gradle +++ b/build.gradle @@ -6,10 +6,11 @@ plugins { id 'org.jetbrains.kotlin.jvm' version '1.8.21' id 'org.jetbrains.kotlin.plugin.spring' version '1.8.21' id 'org.jetbrains.kotlin.plugin.jpa' version '1.8.21' + id 'org.jetbrains.kotlin.plugin.serialization' version '1.9.21' } group = 'tech.niklas' -version = '0.0.1-alpha' +version = '0.0.10-alpha' sourceCompatibility = '17' configurations { @@ -35,9 +36,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-quartz' implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-configuration-processor' - implementation 'com.github.dasniko:testcontainers-keycloak:3.2.0' + testImplementation 'com.github.dasniko:testcontainers-keycloak:3.2.0' testImplementation 'org.springframework.amqp:spring-rabbit-test' testImplementation 'org.testcontainers:junit-jupiter' + testImplementation 'org.testcontainers:rabbitmq' testImplementation 'org.springframework.boot:spring-boot-testcontainers' testImplementation 'org.testcontainers:rabbitmq' testImplementation 'org.testcontainers:postgresql' diff --git a/compose.yaml b/compose.yaml index fa11ef8..9ea782b 100644 --- a/compose.yaml +++ b/compose.yaml @@ -22,5 +22,18 @@ services: ports: - "5672:5672" - "15672:15672" + + kafka: + image: 'bitnami/kafka:latest' + ports: + - '9092:9092' + environment: + - KAFKA_CFG_NODE_ID=0 + - KAFKA_CFG_PROCESS_ROLES=controller,broker + - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093 + - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT + - KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=0@kafka:9093 + - KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER + volumes: pgadmin-data: \ No newline at end of file diff --git a/docker-compose-example.yml b/docker-compose-example.yml index 4a042d9..559dccc 100644 --- a/docker-compose-example.yml +++ b/docker-compose-example.yml @@ -1,4 +1,3 @@ - version: '2' services: @@ -25,5 +24,11 @@ services: ports: - '8089:8080' + rabbitmq: + image: rabbitmq:management + ports: + - "5672:5672" + - "15672:15672" + volumes: pgdata: \ No newline at end of file diff --git a/src/main/kotlin/tech/niklas/ariesbackend/model/DockerAgent.kt b/src/main/kotlin/tech/niklas/ariesbackend/model/DockerAgent.kt index ad9abb7..fd0e0bf 100644 --- a/src/main/kotlin/tech/niklas/ariesbackend/model/DockerAgent.kt +++ b/src/main/kotlin/tech/niklas/ariesbackend/model/DockerAgent.kt @@ -4,7 +4,7 @@ import jakarta.persistence.* import jakarta.validation.constraints.NotBlank import jakarta.validation.constraints.NotNull import org.hibernate.Hibernate - +import java.io.Serial @Entity @Table(name = "dockeragents") data class DockerAgent( diff --git a/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQConfig.kt b/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQConfig.kt index 6a669ee..6e1cf22 100644 --- a/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQConfig.kt +++ b/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQConfig.kt @@ -1,11 +1,15 @@ package tech.niklas.ariesbackend.queue +import org.springframework.amqp.core.Queue import org.springframework.amqp.rabbit.connection.CachingConnectionFactory import org.springframework.amqp.rabbit.connection.ConnectionFactory import org.springframework.amqp.rabbit.core.RabbitTemplate +import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter +import org.springframework.amqp.support.converter.MessageConverter import org.springframework.beans.factory.annotation.Value import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Primary @Configuration class RabbitMQConfig { @@ -31,13 +35,20 @@ class RabbitMQConfig { return connectionFactory } + @Bean + fun jsonMessageConverter(): MessageConverter { + return Jackson2JsonMessageConverter() + } + @Bean fun rabbitTemplate(connectionFactory: ConnectionFactory?): RabbitTemplate? { return connectionFactory?.let { RabbitTemplate(it) } } @Bean - fun dockerDeploy(): org.springframework.amqp.core.Queue { - return org.springframework.amqp.core.Queue("docker.deploy", true) + fun dockerDeploy(): Queue { + return Queue("docker.deploy", true) } + + } \ No newline at end of file diff --git a/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQListener.kt b/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQListener.kt index ee3cce7..83b122d 100644 --- a/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQListener.kt +++ b/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQListener.kt @@ -5,8 +5,4 @@ import org.springframework.stereotype.Component @Component class RabbitMQListener { - @RabbitListener(queues = arrayOf("docker.deploy")) - fun processMessage(message: String) { - println("Received message: $message") - } } \ No newline at end of file diff --git a/src/main/kotlin/tech/niklas/ariesbackend/web/RabbitController.kt b/src/main/kotlin/tech/niklas/ariesbackend/web/RabbitController.kt index afcfc35..9ee5ab9 100644 --- a/src/main/kotlin/tech/niklas/ariesbackend/web/RabbitController.kt +++ b/src/main/kotlin/tech/niklas/ariesbackend/web/RabbitController.kt @@ -1,11 +1,13 @@ package tech.niklas.ariesbackend.web +import com.fasterxml.jackson.databind.ObjectMapper import org.springframework.amqp.core.Queue import org.springframework.amqp.rabbit.core.RabbitTemplate import org.springframework.beans.factory.annotation.Autowired import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController +import tech.niklas.ariesbackend.model.DockerAgent @RestController @RequestMapping("/rabbit") @@ -15,7 +17,9 @@ class RabbitController(@Autowired private val rabbitTemplate: RabbitTemplate, @GetMapping("/test") fun sendMessage(): String { val message: String = "Hello Rabbit!" - rabbitTemplate.convertAndSend(queue.name, message) + val objectMapper: ObjectMapper = ObjectMapper() + val dockerAgent: DockerAgent = DockerAgent("xxx", "machine1", "secret123", "secret.aries.dev") + rabbitTemplate.convertAndSend(queue.name, objectMapper.writeValueAsString(dockerAgent)) return "Message sent successfully!" } } \ No newline at end of file