diff --git a/pom.xml b/pom.xml
index b0cbac4..4344ab4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -8,6 +8,7 @@
3.5.4
+
alfred
web
0.0.1-SNAPSHOT
@@ -41,6 +42,7 @@
+
org.springframework.boot
spring-boot-starter-actuator
@@ -49,6 +51,10 @@
org.springframework.boot
spring-boot-starter-web
+
+ org.springframework.boot
+ spring-boot-starter-mail
+
com.fasterxml.jackson.module
jackson-module-kotlin
@@ -62,6 +68,7 @@
kotlin-stdlib
+
org.springframework.boot
spring-boot-devtools
diff --git a/src/main/kotlin/alfred/web/core/Processes.kt b/src/main/kotlin/alfred/web/core/Processes.kt
index 2f0b7ad..32fc48a 100644
--- a/src/main/kotlin/alfred/web/core/Processes.kt
+++ b/src/main/kotlin/alfred/web/core/Processes.kt
@@ -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()
diff --git a/src/main/kotlin/alfred/web/core/build/Builds.kt b/src/main/kotlin/alfred/web/core/build/Builds.kt
index 9daff05..429372b 100644
--- a/src/main/kotlin/alfred/web/core/build/Builds.kt
+++ b/src/main/kotlin/alfred/web/core/build/Builds.kt
@@ -64,6 +64,7 @@ private fun Properties.toBuildConfig(env: Map) =
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
)
diff --git a/src/main/kotlin/alfred/web/core/event/BuildFinished.kt b/src/main/kotlin/alfred/web/core/event/BuildFinished.kt
new file mode 100644
index 0000000..f7cf2f4
--- /dev/null
+++ b/src/main/kotlin/alfred/web/core/event/BuildFinished.kt
@@ -0,0 +1,7 @@
+package alfred.web.core.event
+
+import alfred.web.core.build.BuildContext
+
+data class BuildFinished(
+ val ctx: BuildContext
+)
diff --git a/src/main/kotlin/alfred/web/core/event/listener/SendMailOnFinish.kt b/src/main/kotlin/alfred/web/core/event/listener/SendMailOnFinish.kt
new file mode 100644
index 0000000..9add9e2
--- /dev/null
+++ b/src/main/kotlin/alfred/web/core/event/listener/SendMailOnFinish.kt
@@ -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)
+ }
+
+}
diff --git a/src/main/kotlin/alfred/web/core/runner/GitRunner.kt b/src/main/kotlin/alfred/web/core/runner/GitRunner.kt
index 88ffbeb..d0e4799 100644
--- a/src/main/kotlin/alfred/web/core/runner/GitRunner.kt
+++ b/src/main/kotlin/alfred/web/core/runner/GitRunner.kt
@@ -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))
}
}
diff --git a/src/main/kotlin/alfred/web/core/runner/ScriptRunner.kt b/src/main/kotlin/alfred/web/core/runner/ScriptRunner.kt
index af5f7f0..29ef24f 100644
--- a/src/main/kotlin/alfred/web/core/runner/ScriptRunner.kt
+++ b/src/main/kotlin/alfred/web/core/runner/ScriptRunner.kt
@@ -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)
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index e886900..ef539f0 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -5,4 +5,12 @@ ALFRED_HOME=/tmp/alfred
ALFRED_PORT=8080
server.port=${ALFRED_PORT}
-spring.application.name=Alfred - RCE AAS
\ No newline at end of file
+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
diff --git a/src/test/kotlin/alfred/web/core/build/BuildsTest.kt b/src/test/kotlin/alfred/web/core/build/BuildsTest.kt
index 15cef2f..44cd5e1 100644
--- a/src/test/kotlin/alfred/web/core/build/BuildsTest.kt
+++ b/src/test/kotlin/alfred/web/core/build/BuildsTest.kt
@@ -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"])
}
diff --git a/src/test/resources/home-1/builds/maximum.properties b/src/test/resources/home-1/builds/maximum.properties
index dd6df38..643b2f7 100644
--- a/src/test/resources/home-1/builds/maximum.properties
+++ b/src/test/resources/home-1/builds/maximum.properties
@@ -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