environment

master
Josha von Gizycki 5 years ago committed by Josha von Gizycki
parent 0dbc69782c
commit bd09041826

@ -1,6 +1,8 @@
package de.joshavg package de.joshavg
import org.eclipse.microprofile.config.inject.ConfigProperty import org.eclipse.microprofile.config.inject.ConfigProperty
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File import java.io.File
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Paths import java.nio.file.Paths
@ -12,24 +14,38 @@ import javax.enterprise.context.ApplicationScoped
data class BuildConfig(val exec: String, data class BuildConfig(val exec: String,
val user: String, val user: String,
val dir: String, val dir: String,
val key: String? = null) { val key: String,
fun asPublic() = copy(key = null) val env: Map<String, String>) {
fun asPublic() = copy(key = "")
} }
@ApplicationScoped @ApplicationScoped
class Builds(@ConfigProperty(name = "ALFRED_HOME") class Builds(@ConfigProperty(name = "ALFRED_HOME")
val home: String) { private val home: String) {
private val logger: Logger = LoggerFactory.getLogger(this::class.java)
init {
logger.info("ALFRED_HOME is $home")
}
fun buildConfig(build: String): BuildConfig { fun buildConfig(build: String): BuildConfig {
val path = Paths.get(home, "builds", "$build.properties") val path = Paths.get(home, "builds", "$build.properties")
val props = Properties() val props = Properties()
props.load(path.toFile().inputStream()) props.load(path.toFile().inputStream())
val env = props
.entries
.filter { it.key.toString() == it.key.toString().toUpperCase() }
.map { Pair(it.key.toString(), it.value.toString()) }
.toMap()
return BuildConfig( return BuildConfig(
exec = props.getProperty("exec"), exec = props.getProperty("exec"),
user = props.getProperty("user"), user = props.getProperty("user"),
dir = props.getProperty("dir"), dir = props.getProperty("dir"),
key = props.getProperty("key")) key = props.getProperty("key") ?: "",
env = env)
} }
fun createLogFile(build: String): File { fun createLogFile(build: String): File {

@ -52,12 +52,17 @@ class BuildsEndpoint(
logger.info("preparing process for build $build with config ${config.asPublic()}") logger.info("preparing process for build $build with config ${config.asPublic()}")
logger.info("log file: $logFile") logger.info("log file: $logFile")
val builder = ProcessBuilder(config.exec) val pid = ProcessBuilder(config.exec)
.directory(File(config.dir)) .directory(File(config.dir))
.redirectOutput(logFile) .redirectOutput(logFile)
.redirectError(logFile) .redirectError(logFile)
val process = builder.start() .apply {
val pid = process.toHandle().pid() environment().putAll(config.env)
environment()["ALFRED_LOG_FILE"] = logFile.absolutePath
}
.start()
.toHandle()
.pid()
logger.info("pid for build $build is: $pid") logger.info("pid for build $build is: $pid")
}.start() }.start()

@ -1,14 +0,0 @@
package de.joshavg
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
@Path("/hello")
class ExampleResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
fun hello() = "hello"
}

@ -10,7 +10,7 @@ class Security(@Inject
fun requireKey(build: String, key: String?, block: (BuildConfig) -> Any): Response { fun requireKey(build: String, key: String?, block: (BuildConfig) -> Any): Response {
val buildConfig = builds.buildConfig(build) val buildConfig = builds.buildConfig(build)
if (buildConfig.key != key) { if (buildConfig.key != "" && buildConfig.key != key) {
return Response.status(Response.Status.UNAUTHORIZED).build() return Response.status(Response.Status.UNAUTHORIZED).build()
} }

Loading…
Cancel
Save