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/.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 d0137ca..fa7f304 100644 --- a/build.gradle +++ b/build.gradle @@ -1,15 +1,16 @@ 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' 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 { @@ -33,7 +34,15 @@ 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' + 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' 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..9ea782b 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 @@ -17,5 +17,23 @@ services: PGADMIN_DEFAULT_PASSWORD: Niklas12 volumes: - pgadmin-data:/var/lib/pgadmin + rabbitmq: + image: rabbitmq:management + 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/model/DockerService.kt b/src/main/kotlin/tech/niklas/ariesbackend/model/DockerService.kt index 8b21289..33e917d 100644 --- a/src/main/kotlin/tech/niklas/ariesbackend/model/DockerService.kt +++ b/src/main/kotlin/tech/niklas/ariesbackend/model/DockerService.kt @@ -26,7 +26,8 @@ open class DockerService protected constructor( @NotNull @ManyToOne @JoinColumn(name = "machineID") - var serviceMachine: DockerMachine + var serviceMachine: DockerMachine, + ) { override fun equals(other: Any?): Boolean { 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/queue/RabbitMQConfig.kt b/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQConfig.kt new file mode 100644 index 0000000..6e1cf22 --- /dev/null +++ b/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQConfig.kt @@ -0,0 +1,54 @@ +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 { + @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 jsonMessageConverter(): MessageConverter { + return Jackson2JsonMessageConverter() + } + + @Bean + fun rabbitTemplate(connectionFactory: ConnectionFactory?): RabbitTemplate? { + return connectionFactory?.let { RabbitTemplate(it) } + } + + @Bean + 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 new file mode 100644 index 0000000..83b122d --- /dev/null +++ b/src/main/kotlin/tech/niklas/ariesbackend/queue/RabbitMQListener.kt @@ -0,0 +1,8 @@ +package tech.niklas.ariesbackend.queue + +import org.springframework.amqp.rabbit.annotation.RabbitListener +import org.springframework.stereotype.Component + +@Component +class RabbitMQListener { +} \ 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/kotlin/tech/niklas/ariesbackend/web/RabbitController.kt b/src/main/kotlin/tech/niklas/ariesbackend/web/RabbitController.kt new file mode 100644 index 0000000..9ee5ab9 --- /dev/null +++ b/src/main/kotlin/tech/niklas/ariesbackend/web/RabbitController.kt @@ -0,0 +1,25 @@ +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") +class RabbitController(@Autowired private val rabbitTemplate: RabbitTemplate, + @Autowired private val queue: Queue) { + + @GetMapping("/test") + fun sendMessage(): String { + val message: String = "Hello Rabbit!" + 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 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