Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployment configurations #6

Merged
merged 23 commits into from
Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from 18 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
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import org.cqfn.save.buildutils.configureDetekt
import org.cqfn.save.buildutils.configureDiktat
import org.cqfn.save.buildutils.configureVersioning
import org.cqfn.save.buildutils.createDetektTask
import org.cqfn.save.buildutils.createDiktatTask
import org.cqfn.save.buildutils.createStackDeployTask
import org.cqfn.save.buildutils.installGitHooks

plugins {
Expand All @@ -18,6 +20,8 @@ allprojects {
configureDetekt()
}

createStackDeployTask()
configureVersioning()
createDiktatTask()
createDetektTask()
installGitHooks()
2 changes: 2 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ repositories {
}

dependencies {
implementation("org.springframework.boot:spring-boot-gradle-plugin:2.4.2")
implementation("org.cqfn.diktat:diktat-gradle-plugin:0.4.0")
implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.15.0")
implementation("org.ajoberstar.reckon:reckon-gradle:0.13.0")
}
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
object Versions {
const val jdk = "11" // jdk version that will be used as kotlin compiler target
const val BP_JVM_VERSION = "11" // jvm version for spring boot image build
const val kotlin = "1.4.21"
const val springBoot = "2.4.2"
const val springSecurity = "5.4.2"
Expand All @@ -9,4 +11,5 @@ object Versions {
const val dockerJavaApi = "3.2.7"
const val ktor = "1.5.1"
const val serialization = "1.0.1"
const val micrometer = "1.6.3"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.cqfn.save.buildutils

import org.gradle.api.Project
import org.gradle.api.tasks.Exec
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.withType
import org.springframework.boot.gradle.tasks.bundling.BootBuildImage

fun Project.createStackDeployTask() {
tasks.register<Exec>("startLocalDockerRegistry") {
enabled = false
description = "Start local docker registry for spring boot images. Disabled, see comment in deployDockerStack task."
commandLine("docker", "service", "create", "--name", "registry", "--publish", "published=6000,target=5000", "registry:2")
}

tasks.register<Exec>("deployDockerStack") {
dependsOn(subprojects.flatMap { it.tasks.withType<BootBuildImage>() })
doFirst {
copy {
description = "Copy configuration files from repo to actual locations"
from("save-deploy")
into("${System.getProperty("user.home")}/configs")
}
}
description = "Deploy to docker swarm. If swarm contains more than one node, some registry for built images is requried."
commandLine("docker", "stack", "deploy", "--compose-file", "docker-compose.yaml", "save")
// doLast {
// exec {
// description = "Stop local docker registry"
// commandLine("docker", "service", "rm", "registry")
// }
// }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.cqfn.save.buildutils

import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.named
import org.springframework.boot.gradle.dsl.SpringBootExtension
import org.springframework.boot.gradle.plugin.SpringBootPlugin
import org.springframework.boot.gradle.tasks.bundling.BootBuildImage

fun Project.configureSpringBoot() {
apply<SpringBootPlugin>()

dependencies {
add("implementation", "org.springframework.boot:spring-boot-starter-webflux:${Versions.springBoot}")
add("implementation", "org.springframework.boot:spring-boot-starter-actuator:${Versions.springBoot}")
add("implementation", "io.micrometer:micrometer-registry-prometheus:${Versions.micrometer}") // expose prometheus metrics in actuator
add("implementation", "org.springframework.security:spring-security-core:${Versions.springSecurity}")
add("testImplementation", "org.springframework.boot:spring-boot-starter-test:${Versions.springBoot}")
}

configure<SpringBootExtension> {
buildInfo() // configures `bootBuildInfo` task, which creates META-INF/build-info.properties file
}

tasks.named<BootBuildImage>("bootBuildImage") {
dependsOn(rootProject.tasks.getByName("startLocalDockerRegistry"))
// `host.docker.internal` for win 10?
imageName = "127.0.0.1:6000/${project.name}:${project.version}"
environment = mapOf("BP_JVM_VERSION" to Versions.BP_JVM_VERSION)
isPublish = false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.cqfn.save.buildutils

import org.ajoberstar.reckon.gradle.ReckonExtension
import org.ajoberstar.reckon.gradle.ReckonPlugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.configure

fun Project.configureVersioning() {
apply<ReckonPlugin>()

configure<ReckonExtension> {
scopeFromProp()
snapshotFromProp() // use -Preckon.stage=final to build a version without `SNAPSHOT` modifier
}
}
33 changes: 33 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# https://docs.docker.com/engine/swarm/stack-deploy/
# $ ./gradlew deployDockerStack
version: '3.9'

services:
orchestrator:
image: 127.0.0.1:6000/save-orchestrator:0.1.0-SNAPSHOT
ports:
- "5100:5100"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
labels:
- "prometheus-job=save-orchestrator"
backend:
image: 127.0.0.1:6000/save-backend:0.1.0-SNAPSHOT
ports:
- "5000:5000"
deploy:
labels:
- "prometheus-job=save-backend"
prometheus:
image: prom/prometheus
user: root # to access host's docker socket for service discovery, see https://groups.google.com/g/prometheus-users/c/EuEW0qRzXvg/m/0aqKh_ZABQAJ
ports:
- "9090:9090"
volumes:
- "~/configs/prometheus.yml:/etc/prometheus/prometheus.yml"
- "/var/run/docker.sock:/var/run/docker.sock"
grafana:
image: grafana/grafana
ports:
- "9100:3000"
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
group=org.cqfn.save
org.gradle.parallel=true
# these two properties are for using platform.* API in nativeMain: https://kotlinlang.org/docs/mpp-share-on-platforms.html#share-code-on-similar-platforms
kotlin.mpp.enableGranularSourceSetsMetadata=true
Expand Down
9 changes: 4 additions & 5 deletions save-backend/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import org.cqfn.save.buildutils.configureJacoco
import org.cqfn.save.buildutils.configureSpringBoot
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
}

configureSpringBoot()

tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = Versions.jdk
}
}

Expand All @@ -17,14 +20,10 @@ tasks.withType<Test> {

dependencies {
implementation(project(":save-common"))
implementation("org.springframework.boot:spring-boot-starter-webflux:${Versions.springBoot}")
implementation("org.springframework.boot:spring-boot-starter-actuator:${Versions.springBoot}")
implementation("org.springframework.security:spring-security-core:${Versions.springSecurity}")
implementation("org.liquibase:liquibase-core:${Versions.liquibase}")
implementation("org.hibernate:hibernate-core:${Versions.hibernate}")
implementation("org.slf4j:slf4j-api:${Versions.slf4j}")
implementation("ch.qos.logback:logback-core:${Versions.logback}")
testImplementation("org.springframework.boot:spring-boot-starter-test:${Versions.springBoot}")
}

configureJacoco()
3 changes: 2 additions & 1 deletion save-backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
server.port = 5000
server.port = 5000
management.endpoints.web.exposure.include=health,info,prometheus
8 changes: 7 additions & 1 deletion save-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ plugins {
}

kotlin {
jvm()
jvm {
compilations.all {
kotlinOptions {
jvmTarget = Versions.jdk
}
}
}
js(IR).browser()

// setup native compilation
Expand Down
27 changes: 27 additions & 0 deletions save-deploy/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
scrape_configs:
# Make Prometheus scrape itself for metrics.
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']

# Create a job for Docker Swarm containers.
- job_name: 'dockerswarm'
metrics_path: '/actuator/prometheus'
dockerswarm_sd_configs:
- host: unix:///var/run/docker.sock
role: tasks
relabel_configs:
# Only keep containers that should be running.
- source_labels: [__meta_dockerswarm_task_desired_state]
regex: running
action: keep
# Only keep containers that have a `prometheus-job` label.
- source_labels: [__meta_dockerswarm_service_label_prometheus_job]
regex: .+
action: keep
# Use the prometheus-job Swarm label as Prometheus job label.
- source_labels: [__meta_dockerswarm_service_label_prometheus_job]
target_label: job
# Adding docker_node label to the targets
- source_labels: [ __meta_dockerswarm_node_hostname ]
target_label: docker_node
9 changes: 4 additions & 5 deletions save-orchestrator/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import org.cqfn.save.buildutils.configureJacoco
import org.cqfn.save.buildutils.configureSpringBoot
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
}

configureSpringBoot()

tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = Versions.jdk
}
}

Expand All @@ -17,17 +20,13 @@ tasks.withType<Test> {

dependencies {
implementation(project(":save-common"))
implementation("org.springframework.boot:spring-boot-starter-webflux:${Versions.springBoot}")
implementation("org.springframework.boot:spring-boot-starter-actuator:${Versions.springBoot}")
implementation("org.springframework.security:spring-security-core:${Versions.springSecurity}")
implementation("org.liquibase:liquibase-core:${Versions.liquibase}")
implementation("org.hibernate:hibernate-core:${Versions.hibernate}")
implementation("org.slf4j:slf4j-api:${Versions.slf4j}")
implementation("ch.qos.logback:logback-core:${Versions.logback}")
implementation("com.github.docker-java:docker-java-core:${Versions.dockerJavaApi}")
implementation("com.github.docker-java:docker-java-transport-httpclient5:${Versions.dockerJavaApi}")
implementation("org.apache.commons:commons-compress:1.20")
testImplementation("org.springframework.boot:spring-boot-starter-test:${Versions.springBoot}")
}

configureJacoco()
3 changes: 2 additions & 1 deletion save-orchestrator/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
server.port = 5100
server.port = 5100
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a table with ports

management.endpoints.web.exposure.include=health,info,prometheus