implement first draft for email reporting

master
Josha von Gizycki 3 weeks ago
parent 7b1d7c5dc9
commit 2be1af365d

@ -8,6 +8,7 @@
<version>3.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>alfred</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
@ -41,6 +42,7 @@
</properties>
<dependencies>
<!-- Scaffolding -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
@ -49,6 +51,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
@ -62,6 +68,7 @@
<artifactId>kotlin-stdlib</artifactId>
</dependency>
<!-- Dev time -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>

@ -1,6 +1,7 @@
package alfred.web.core
import alfred.web.core.build.BuildConfig
import alfred.web.core.build.BuildContext
import alfred.web.core.build.LogFile
import org.springframework.stereotype.Service
@ -17,6 +18,9 @@ class Processes {
environment()["ALFRED_REV"] = rev
}
fun builder(ctx: BuildContext) =
builder(ctx.config, ctx.logFile, ctx.rev)
fun startThread(block: () -> Unit) {
Thread {
block()

@ -64,6 +64,7 @@ private fun Properties.toBuildConfig(env: Map<String, String>) =
script = getProperty("script"),
gitRepo = getProperty("git.repo.url"),
gitCloneTimeout = getProperty("git.close.timeout")?.toLong() ?: 30L,
mailReportTo = getProperty("mail.report.to"),
env = env
)
@ -81,6 +82,7 @@ data class BuildConfig(
val gitRepo: String?,
val gitCloneTimeout: Long,
val workspace: String,
val mailReportTo: String?,
@field:JsonIgnore
val env: Map<String, String>
)

@ -0,0 +1,7 @@
package alfred.web.core.event
import alfred.web.core.build.BuildContext
data class BuildFinished(
val ctx: BuildContext
)

@ -0,0 +1,36 @@
package alfred.web.core.event.listener
import alfred.web.core.event.BuildFinished
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.event.EventListener
import org.springframework.mail.MailSender
import org.springframework.mail.SimpleMailMessage
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Component
@Component
class SendMailOnFinish(
@Value("\${ALFRED_MAIL_FROM}")
val sender: String,
val mailSender: MailSender
) {
@EventListener
@Async
fun onBuildFinished(event: BuildFinished) {
if (event.ctx.config.mailReportTo === null) {
return
}
val ctx = event.ctx
val msg = SimpleMailMessage().apply {
from = sender
subject = "Build ${ctx.buildId} finished"
setTo(event.ctx.config.mailReportTo)
text = ctx.logFile.backingFile.readLines().joinToString("\n")
}
mailSender.send(msg)
}
}

@ -9,8 +9,10 @@ import alfred.web.core.build.Builds
import alfred.web.core.build.ProcessInfo
import alfred.web.core.build.Workspace
import alfred.web.core.build.Workspaces
import alfred.web.core.event.BuildFinished
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service
import java.util.concurrent.TimeUnit
import kotlin.collections.forEach
@ -20,8 +22,9 @@ import kotlin.jvm.java
class GitRunner(
val builds: Builds,
val handles: Handles,
val processes: Processes,
val workspaces: Workspaces,
val processes: Processes
val eventPublisher: ApplicationEventPublisher
) {
val scriptsDir = ".alfred"
@ -51,6 +54,8 @@ class GitRunner(
execScripts(ctx, wsPath)
logFile.footer()
eventPublisher.publishEvent(BuildFinished(ctx))
}
}

@ -3,22 +3,20 @@ package alfred.web.core.runner
import alfred.web.core.Handle
import alfred.web.core.Handles
import alfred.web.core.Processes
import alfred.web.core.build.BuildContext
import alfred.web.core.build.BuildId
import alfred.web.core.build.Builds
import alfred.web.core.build.ProcessInfo
import alfred.web.core.build.Workspaces
import alfred.web.core.build.*
import alfred.web.core.event.BuildFinished
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service
import kotlin.jvm.java
@Service
class ScriptRunner(
val builds: Builds,
val handles: Handles,
val processes: Processes,
val workspaces: Workspaces
val workspaces: Workspaces,
val eventPublisher: ApplicationEventPublisher
) {
fun run(buildId: BuildId, rev: String?): ProcessInfo {
@ -35,25 +33,34 @@ class ScriptRunner(
buildId = buildId
)
val process = runScript(ctx)
val pid = process.pid()
logger.info("pid for build $buildId is: $pid")
return ProcessInfo(pid = pid, logFile = logFile)
}
private fun runScript(
ctx: BuildContext
): Process {
val wsDir = workspaces.prepare(ctx)
logFile.header(buildId, rev, wsDir)
ctx.logFile.header(ctx.buildId, ctx.rev, wsDir)
val process = processes.builder(config, logFile, rev)
.command(config.script)
val process = processes.builder(ctx)
.command(ctx.config.script)
.directory(wsDir.toFile())
.start()
handles.add(Handle(process.toHandle(), buildId))
handles.add(Handle(process.toHandle(), ctx.buildId))
process.onExit().whenComplete { _, _ ->
logFile.footer()
ctx.logFile.footer()
workspaces.cleanUp(wsDir)
}
val pid = process.pid()
logger.info("pid for build $buildId is: $pid")
eventPublisher.publishEvent(BuildFinished(ctx))
}
return ProcessInfo(pid = pid, logFile = logFile)
return process
}
val logger: Logger = LoggerFactory.getLogger(this::class.java)

@ -5,4 +5,12 @@ ALFRED_HOME=/tmp/alfred
ALFRED_PORT=8080
server.port=${ALFRED_PORT}
spring.application.name=Alfred - RCE AAS
spring.application.name=Alfred - RCE AAS
spring.mail.host=${ALFRED_MAIL_HOST}
spring.mail.port=${ALFRED_MAIL_PORT}
spring.mail.username=${ALFRED_MAIL_USERNAME}
spring.mail.password=${ALFRED_MAIL_PASSWORD}
spring.mail.properties.mail.smtp.auth=${ALFRED_MAIL_SMTP_AUTH : true}
spring.mail.properties.mail.smtp.starttls.enable=${ALFRED_MAIL_STARTTLS : true}
ALFRED_MAIL_FROM=alfred@your.service

@ -38,6 +38,7 @@ class BuildsTest {
assertEquals("https://something.nowhere", cfg.gitRepo)
assertEquals(10, cfg.gitCloneTimeout)
assertEquals(1, cfg.env.count())
assertEquals("merri@shire", cfg.mailReportTo)
assertEquals("env1-value", cfg.env["ENV1"])
}

@ -7,6 +7,8 @@ script=echo Breakfast
git.repo.url=https://something.nowhere
git.close.timeout=10
ignored=true
mail.report.to=merri@shire
ENV1=env1-value
ignored=true

Loading…
Cancel
Save