Skip to content

Commit 1f5b25d

Browse files
committed
!sbt Use multiJvmPlugin from pekko.
1 parent 9c5323c commit 1f5b25d

File tree

5 files changed

+753
-5
lines changed

5 files changed

+753
-5
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import ValidatePullRequest._
1111
import net.bzzt.reproduciblebuilds.ReproducibleBuildsPlugin.reproducibleBuildsCheckResolver
1212
import PekkoDependency._
1313
import Dependencies.{ h2specExe, h2specName }
14-
import com.typesafe.sbt.SbtMultiJvm.MultiJvmKeys.MultiJvm
14+
import MultiJvmPlugin.MultiJvmKeys.MultiJvm
1515
import java.nio.file.Files
1616
import java.nio.file.attribute.{ PosixFileAttributeView, PosixFilePermission }
1717

project/Jvm.scala

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* license agreements; and to You under the Apache License, version 2.0:
4+
*
5+
* https://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* This file is part of the Apache Pekko project, which was derived from Akka.
8+
*/
9+
10+
/*
11+
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
12+
*/
13+
14+
import java.io.File
15+
import java.lang.{ ProcessBuilder => JProcessBuilder }
16+
17+
import sbt._
18+
import scala.sys.process.Process
19+
20+
object Jvm {
21+
def startJvm(
22+
javaBin: File,
23+
jvmOptions: Seq[String],
24+
runOptions: Seq[String],
25+
logger: Logger,
26+
connectInput: Boolean) = {
27+
forkJava(javaBin, jvmOptions ++ runOptions, logger, connectInput)
28+
}
29+
30+
def forkJava(javaBin: File, options: Seq[String], logger: Logger, connectInput: Boolean) = {
31+
val java = javaBin.toString
32+
val command = (java :: options.toList).toArray
33+
val builder = new JProcessBuilder(command: _*)
34+
Process(builder).run(logger, connectInput)
35+
}
36+
37+
/**
38+
* check if the current operating system is some OS
39+
*/
40+
def isOS(os: String) =
41+
try {
42+
System.getProperty("os.name").toUpperCase.startsWith(os.toUpperCase)
43+
} catch {
44+
case _: Throwable => false
45+
}
46+
47+
/**
48+
* convert to proper path for the operating system
49+
*/
50+
def osPath(path: String) = if (isOS("WINDOWS")) Process(Seq("cygpath", path)).lineStream.mkString else path
51+
52+
def getPodName(hostAndUser: String, sbtLogger: Logger): String = {
53+
val command: Array[String] =
54+
Array("kubectl", "get", "pods", "-l", s"host=$hostAndUser", "--no-headers", "-o", "name")
55+
val builder = new JProcessBuilder(command: _*)
56+
sbtLogger.debug("Jvm.getPodName about to run " + command.mkString(" "))
57+
val podName = Process(builder).!!
58+
sbtLogger.debug("Jvm.getPodName podName is " + podName)
59+
podName.stripPrefix("pod/").stripSuffix("\n")
60+
}
61+
62+
def syncJar(jarName: String, hostAndUser: String, remoteDir: String, sbtLogger: Logger): Process = {
63+
val podName = getPodName(hostAndUser, sbtLogger)
64+
val command: Array[String] =
65+
Array("kubectl", "exec", podName, "--", "/bin/bash", "-c", s"rm -rf $remoteDir && mkdir -p $remoteDir")
66+
val builder = new JProcessBuilder(command: _*)
67+
sbtLogger.debug("Jvm.syncJar about to run " + command.mkString(" "))
68+
val process = Process(builder).run(sbtLogger, false)
69+
if (process.exitValue() == 0) {
70+
val command: Array[String] = Array("kubectl", "cp", osPath(jarName), podName + ":" + remoteDir + "/")
71+
val builder = new JProcessBuilder(command: _*)
72+
sbtLogger.debug("Jvm.syncJar about to run " + command.mkString(" "))
73+
Process(builder).run(sbtLogger, false)
74+
} else {
75+
process
76+
}
77+
}
78+
79+
def forkRemoteJava(
80+
java: String,
81+
jvmOptions: Seq[String],
82+
appOptions: Seq[String],
83+
jarName: String,
84+
hostAndUser: String,
85+
remoteDir: String,
86+
logger: Logger,
87+
connectInput: Boolean,
88+
sbtLogger: Logger): Process = {
89+
val podName = getPodName(hostAndUser, sbtLogger)
90+
sbtLogger.debug("About to use java " + java)
91+
val shortJarName = new File(jarName).getName
92+
val javaCommand = List(List(java), jvmOptions, List("-cp", shortJarName), appOptions).flatten
93+
val command = Array(
94+
"kubectl",
95+
"exec",
96+
podName,
97+
"--",
98+
"/bin/bash",
99+
"-c",
100+
("cd " :: (remoteDir :: (" ; " :: javaCommand))).mkString(" "))
101+
sbtLogger.debug("Jvm.forkRemoteJava about to run " + command.mkString(" "))
102+
val builder = new JProcessBuilder(command: _*)
103+
Process(builder).run(logger, connectInput)
104+
}
105+
}
106+
107+
class JvmBasicLogger(name: String) extends BasicLogger {
108+
def jvm(message: String) = "[%s] %s".format(name, message)
109+
110+
def log(level: Level.Value, message: => String) = System.out.synchronized {
111+
System.out.println(jvm(message))
112+
}
113+
114+
def trace(t: => Throwable) = System.out.synchronized {
115+
val traceLevel = getTrace
116+
if (traceLevel >= 0) System.out.print(StackTrace.trimmed(t, traceLevel))
117+
}
118+
119+
def success(message: => String) = log(Level.Info, message)
120+
def control(event: ControlEvent.Value, message: => String) = log(Level.Info, message)
121+
122+
def logAll(events: Seq[LogEvent]) = System.out.synchronized { events.foreach(log) }
123+
}
124+
125+
final class JvmLogger(name: String) extends JvmBasicLogger(name)

project/MultiNode.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
1212
*/
1313

14-
import com.typesafe.sbt.SbtMultiJvm
15-
import com.typesafe.sbt.SbtMultiJvm.MultiJvmKeys._
14+
import MultiJvmPlugin.MultiJvmKeys.multiJvmCreateLogger
15+
import MultiJvmPlugin.MultiJvmKeys._
1616
import sbt._
1717
import sbt.Keys._
1818

@@ -57,7 +57,7 @@ object MultiNode extends AutoPlugin {
5757
}
5858

5959
private val multiJvmSettings =
60-
SbtMultiJvm.multiJvmSettings ++
60+
MultiJvmPlugin.multiJvmSettings ++
6161
inConfig(MultiJvm)(org.scalafmt.sbt.ScalafmtPlugin.scalafmtConfigSettings) ++
6262
inConfig(MultiJvm)(Seq(
6363
MultiJvm / jvmOptions := defaultMultiJvmOptions,

0 commit comments

Comments
 (0)