Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
13 changes: 8 additions & 5 deletions sbt-plugin/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import scala.sys.process._

val snunitVersion = Def.setting {
val snunitDir = baseDirectory.value / ".."
val versionString =
Process(
Seq("./mill", "--disable-ticker", "show", s"snunit-plugins-shared[${Versions.scala212}].publishVersion"),
cwd = snunitDir
).!!
Process(
Seq("./mill", "--disable-ticker", s"snunit-plugins-shared[${Versions.scala212}].publishLocal"),
cwd = snunitDir
).!!
val versionString = Process(
Seq("./mill", "--disable-ticker", "show", s"snunit-plugins-shared[${Versions.scala212}].publishVersion"),
cwd = snunitDir
).!!
val JString(version) = Parser.parseFromString(versionString).get
version
}
Expand Down
13 changes: 11 additions & 2 deletions sbt-plugin/src/main/scala/SNUnitPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ object SNUnitPlugin extends AutoPlugin {
object autoImport {
val snunitPort = settingKey[Int]("Port where the SNUnit app runs")
val snunitCurlCommand = settingKey[Seq[String]]("curl command to use")
val snunitAppName = settingKey[String]("app name in NGINX Unit config")
val snunitConfig = taskKey[String]("NGINX Unit configuration")
val snunitVersion: String = snunit.plugin.internal.BuildInfo.snunitVersion

val deployToNGINXUnit = taskKey[Unit]("Deploy app to NGINX Unit")
Expand All @@ -34,10 +36,17 @@ object SNUnitPlugin extends AutoPlugin {
},
snunitCurlCommand := Seq("curl"),
snunitPort := 8080,
deployToNGINXUnit := {
snunitAppName := name.value,
snunitConfig := {
val appName = snunitAppName.value
val port = snunitPort.value
SNUnitPluginShared.defaultConfig(appName, port)
},
deployToNGINXUnit := {
val config = snunitConfig.value
val executable = (Compile / nativeLink).value
shared.value.deployToNGINXUnit(executable.toString, port)
val appName = snunitAppName.value
shared.value.deployToNGINXUnit(appName = appName, config = config, executable = executable.toString)
}
)
}
44 changes: 44 additions & 0 deletions sbt-plugin/src/sbt-test/snunit-plugin/custom-config/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
enablePlugins(ScalaNativePlugin)
enablePlugins(SNUnitPlugin)

scalaVersion := "3.2.0"
snunitConfig := """{
"listeners": {
"*:8086": {
"pass": "routes"
}
},
"routes": [
{
"action": {
"pass": "applications/my_app"
}
}
],
"applications": {
"my_app": {
"processes": {
"max": 10,
"spare": 2,
"idle_timeout": 1
},
"type": "external",
"executable": "$SNUNIT_BINARY",
"limits": {
"timeout": 1,
"requests": 1000
}
}
}
}"""
snunitCurlCommand := Seq("sudo", "curl")
snunitAppName := "my_app"
libraryDependencies += "com.github.lolgab" %%% "snunit" % snunitVersion

lazy val doCallToServer = taskKey[Unit]("test that server works")
doCallToServer := {
import sys.process._
val port = snunitPort.value
val response = s"curl -sL http://127.0.0.1:8086".!!
require(response == "Hello world\n")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.7.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sys.props.get("plugin.version") match {
case Some(x) => addSbtPlugin("com.github.lolgab" % "sbt-snunit" % x)
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import snunit._

object HelloWorld {
def main(args: Array[String]): Unit = {
SyncServerBuilder
.build(_.send(StatusCode.OK, "Hello world", Seq()))
.listen()
}
}
2 changes: 2 additions & 0 deletions sbt-plugin/src/sbt-test/snunit-plugin/custom-config/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> deployToNGINXUnit
> doCallToServer
43 changes: 25 additions & 18 deletions snunit-plugins-shared/src/SNUnitPluginShared.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,39 +76,46 @@ private[plugin] class SNUnitPluginShared(buildTool: BuildTool, logger: Logger, s

private def doCurl(control: File, command: String*) =
(snunitCurlCommand ++ Seq("-sL", "--unix-socket", control.toString) ++ command).!!
def deployToNGINXUnit(executable: String, port: Int): Unit = {
def deployToNGINXUnit(appName: String, config: String, executable: String): Unit = {
require(isUnitInstalled(), "unitd is not installed")
val control = unitControlSocket()
require(controlSocketExists(control), "control socket doesn't exist")
require(isUnitRunning(control), "unitd is not available")
val config = s"""{
"applications": {
"app": {
"type": "external",
"executable": "$executable"
}
},
"listeners": {
"*:$port": {
"pass": "applications/app"
}
}
}"""
val configToApply = config.replaceAllLiterally(SNUnitPluginShared.binaryPlaceholder, executable)
println(configToApply)
val result = doCurl(
control,
"-X",
"PUT",
"-d",
config,
configToApply,
"http://localhost/config"
)
logger.info(result)
restartApp(control)
restartApp(control, appName = appName)
}

private def restartApp(control: File) = {
val result = doCurl(control, "http://localhost/control/applications/app/restart")
private def restartApp(control: File, appName: String) = {
val result = doCurl(control, s"http://localhost/control/applications/$appName/restart")

logger.info(result)
}
}
private[plugin] object SNUnitPluginShared {
private final val binaryPlaceholder = "$SNUNIT_BINARY"
def defaultConfig(appName: String, port: Int): String = {
s"""{
"listeners": {
"*:$port": {
"pass": "applications/$appName"
}
},
"applications": {
"$appName": {
"type": "external",
"executable": "$binaryPlaceholder"
}
}
}"""
}
}
5 changes: 5 additions & 0 deletions snunit-plugins-shared/src/UnitConfiguration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package snunit.plugin

case class Route(

)