|
| 1 | +ThisBuild / scalaVersion := "2.12.8" |
| 2 | +ThisBuild / fork := true |
| 3 | +ThisBuild / cancelable in Global := true |
| 4 | +ThisBuild / scalacOptions in Compile ++= Vector( |
| 5 | + "-target:jvm-1.8", |
| 6 | + "-feature", |
| 7 | + "-unchecked", |
| 8 | + "-deprecation", |
| 9 | + "-language:_", |
| 10 | + "-encoding", "UTF-8", |
| 11 | + "-Xfatal-warnings", |
| 12 | + "-Ywarn-unused-import", |
| 13 | +) |
| 14 | + |
| 15 | +import deployssh.DeploySSH.{ServerConfig, ArtifactSSH} |
| 16 | +import fr.janalyse.ssh.SSH |
| 17 | + |
| 18 | +lazy val example = project.in(file(".")).settings( |
| 19 | + libraryDependencies += "com.typesafe" % "config" % "1.3.3", |
| 20 | + mainClass in (Compile, run) := Some("example.App"), |
| 21 | + deployConfigs ++= Seq( |
| 22 | + ServerConfig(name="server1", host="localhost", user=None), |
| 23 | + ), |
| 24 | + deployArtifacts ++= Seq( |
| 25 | + ArtifactSSH((packageBin in Universal).value, "/tmp/example") |
| 26 | + ), |
| 27 | + deploySshExecBefore ++= Seq( |
| 28 | + (ssh: SSH) => ssh.shell{ shell => |
| 29 | + shell.execute("cd /tmp/example") |
| 30 | + shell.execute("touch pid") |
| 31 | + val pid = shell.execute("cat pid") |
| 32 | + if (pid != "") { |
| 33 | + shell.execute(s"kill ${pid}; sleep 5; kill -9 ${pid}") |
| 34 | + } else () |
| 35 | + shell.execute("rm pid") |
| 36 | + } |
| 37 | + ), |
| 38 | + deploySshExecAfter ++= Seq( |
| 39 | + (ssh: SSH) => { |
| 40 | + ssh.scp { scp => |
| 41 | + scp.send(file(s"./deploy/${ssh.options.name.get}.conf"), "/tmp/example/app.conf") |
| 42 | + } |
| 43 | + ssh.shell{ shell => |
| 44 | + val name = (packageName in Universal).value |
| 45 | + val script = (executableScriptName in Universal).value |
| 46 | + shell.execute("cd /tmp/example") |
| 47 | + shell.execute(s"unzip -q -o ${name}.zip") |
| 48 | + shell.execute(s"rm ${name}.zip") |
| 49 | + shell.execute(s"nohup ./${name}/bin/${script} -Dconfig.file=/tmp/example/app.conf &") |
| 50 | + shell.execute("echo $! > pid") |
| 51 | + shell.execute("touch pid") |
| 52 | + val pid = shell.execute("cat pid") |
| 53 | + val (_, status) = shell.executeWithStatus("echo $?") |
| 54 | + if (status != 0 || pid == "") { |
| 55 | + throw new RuntimeException(s"status=${status}, pid=${pid}. please check package") |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + ), |
| 60 | +).enablePlugins(JavaAppPackaging, DeploySSH) |
0 commit comments