|
| 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) |
0 commit comments