Deploy.scala 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import sbt._
  2. import Keys._
  3. import sbtassembly.Plugin.AssemblyKeys._
  4. object Deploy {
  5. val deployPackTask = TaskKey[Unit]("deploy-pack")
  6. val deployPack = deployPackTask <<= (assembly, jarName in assembly, target, baseDirectory) map { (_, jarName, target, base) =>
  7. IO.delete(target / "deploy")
  8. IO.createDirectory(target / "deploy")
  9. IO.copyFile(target / jarName, target / "deploy" / jarName)
  10. if (base / "server.conf" exists)
  11. IO.copyFile(base / "server.conf", target / "deploy" / "server.conf")
  12. IO.write(target / "deploy" / "start.sh",
  13. """#!/bin/bash
  14. |DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
  15. |cd $DIR
  16. |java -Dfile.encoding=UTF8 -jar %s
  17. |""" format (jarName) stripMargin)
  18. target / "deploy" / "start.sh" setExecutable true
  19. }
  20. val deployHost = SettingKey[Option[String]]("deployHost", "default host, on which deployed files will be pushed")
  21. val deployDest = SettingKey[Option[String]]("deployDest", "default destination on that host")
  22. val deploySsh = InputKey[Unit]("deploy-ssh") <<= inputTask { (argTask: TaskKey[Seq[String]]) =>
  23. (argTask, deployPackTask, deployHost, deployDest, jarName in assembly, target) map { (args, _, deployHost, deployDest, jarName, target) =>
  24. val (host, dest) = if (args.size < 2) {
  25. (for {
  26. host <- deployHost
  27. dest <- deployDest
  28. } yield (host, dest)).getOrElse(sys.error("Destination was not provided on command line - and there was no default"))
  29. } else (args(0), args(1))
  30. val cmd = "rsync" +: "-avz" +: IO.listFiles(target / "deploy").map(_.toString) :+ (host+":"+dest)
  31. println("Copying files: " + cmd.mkString(" "))
  32. if (Process(cmd).! == 0) {
  33. val startCmd = List("ssh", host, "-x", "cd " + dest + "; (nohup ./start.sh > server.log 2>&1 &)")
  34. println("Starting process: " + startCmd.mkString(" "))
  35. Process(startCmd).!
  36. }
  37. }
  38. }
  39. lazy val deploySettings = Seq(deployPack, deployHost := None, deployDest := None, deploySsh)
  40. }