Skip to content

Instantly share code, notes, and snippets.

@joan38
Last active May 29, 2019 05:51
Show Gist options
  • Save joan38/5363ba43df4870e9f9896011d0e778a7 to your computer and use it in GitHub Desktop.
Save joan38/5363ba43df4870e9f9896011d0e778a7 to your computer and use it in GitHub Desktop.
lazy val orchestration = OrchestraProject("orchestration", file("orchestration"))
.settings(
name := "Orchestration",
organization := "com.drivetribe",
version ~= (_.replace('+', '-')),
ThisBuild / version / aggregate := false,
scalaVersion := "2.12.4",
scalacOptions ++= Seq(
"-deprecation",
"-feature",
"-Xlint:unsound-match",
"-Ywarn-inaccessible",
"-Ywarn-infer-any",
"-Ywarn-unused:imports",
"-Ywarn-unused:locals",
"-Ywarn-unused:patvars",
"-Ywarn-unused:privates",
"-Ypartial-unification",
"-Ywarn-dead-code"
),
ThisBuild / Compile / packageDoc / publishArtifact := false,
resolvers += Opts.resolver.sonatypeSnapshots,
libraryDependencies ++= Seq(
"com.beachape" %%% "enumeratum" % "1.5.13",
"com.amazonaws" % "aws-java-sdk" % "1.11.310",
"com.github.gilbertw1" %% "slack-scala-client" % "0.2.3",
"org.zeroturnaround" % "zt-zip" % "1.12"
) ++ monocle.value ++ orchestra.value
)
lazy val orchestrationJVM = orchestration.jvm
.settings(
scalaJSPipeline / devCommands ++= Seq("publishLocal", "console", "test"),
Docker / maintainer := "DriveTribe",
dockerRepository := Option("registry.drivetribe.com/tools"),
dockerUpdateLatest := true
)
lazy val orchestrationJS = orchestration.js
lazy val orchestra = Def.setting {
Seq(
"com.drivetribe" %%% "orchestra-github" % orchestraVersion,
"com.drivetribe" %%% "orchestra-cron" % orchestraVersion,
"com.drivetribe" %% "orchestra-lock" % orchestraVersion
)
}
lazy val monocle = Def.setting {
val monocleVersion = "1.5.0-cats"
Seq(
"com.github.julien-truffaut" %% "monocle-core" % monocleVersion,
"com.github.julien-truffaut" %% "monocle-macro" % monocleVersion
)
}
import scala.concurrent.Future
import org.eclipse.jgit.api.errors.RefNotFoundException
import com.drivetribe.orchestra.Dsl._
import com.drivetribe.orchestra.filesystem.{Directory, LocalFile}
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
import org.eclipse.jgit.api.Git
import com.drivetribe.orchestra.github.{GitRef, Repository}
object GitUtils {
val backendRepo = Repository("drivetribe/backend")
val frontendRepo = Repository("drivetribe/frontend")
val orchestrationRepo = Repository("drivetribe/orchestration")
def cloneOrchestration(ref: GitRef = GitRef("master"))(implicit workDir: Directory) =
clone(s"https://github.com/${orchestrationRepo.name}.git", LocalFile("orchestration"), ref)
def cloneInfrastructure(ref: GitRef = GitRef("master"))(implicit workDir: Directory) =
clone(s"https://github.com/drivetribe/infrastructure.git", LocalFile("infrastructure"), ref)
def cloneBackend(ref: GitRef = GitRef("master"))(implicit workDir: Directory) =
clone(s"https://github.com/${backendRepo.name}.git", LocalFile("backend"), ref)
def cloneWeb(ref: GitRef = GitRef("staging"))(implicit workDir: Directory) =
clone(s"https://github.com/${frontendRepo.name}.git", LocalFile("web"), ref)
def cloneStudio(ref: GitRef = GitRef("staging"))(implicit workDir: Directory) =
clone(s"https://github.com/drivetribe/studio.git", LocalFile("studio"), ref)
private def clone(uri: String, directory: LocalFile, ref: GitRef) = Future {
println(s"Cloning $uri and checking out ${ref.name}")
val git = Git
.cloneRepository()
.setURI(uri)
.setCredentialsProvider(
new UsernamePasswordCredentialsProvider("orchestra", sys.env("ORCHESTRA_GITHUB_TOKEN"))
)
.setNoCheckout(true)
.setDirectory(directory)
.call()
try git.checkout().setName(s"origin/${ref.name}").call()
catch {
case _: RefNotFoundException => git.checkout().setName(ref.name).call()
}
git.getRepository.getWorkTree.getAbsoluteFile
}
}
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import io.k8s.api.core.v1._
import com.drivetribe.orchestra.Dsl._
import com.drivetribe.orchestra.board.JobBoard
import com.drivetribe.orchestra.filesystem.Directory
import com.drivetribe.orchestra.github.GitRef
import com.drivetribe.orchestra.job.Job
import com.drivetribe.orchestra.model.JobId
import com.drivetribe.orchestra.parameter.Input
import com.drivetribe.orchestra.utils.Directories._
import com.drivetribe.orchestra.utils.Stages._
import com.drivetribe.orchestration.GitUtils
import com.drivetribe.orchestration.backend.{SbtCachedContainer, SbtContainer}
object Publish {
lazy val board =
JobBoard[GitRef => String](JobId("publishOrchestration"), "Publish Orchestration")(Input[GitRef]("Git ref"))
lazy val job = {
implicit val sbt = SbtCachedContainer
Job(board)(
PodSpec(Seq(sbt),
volumes =
Option(Seq(Volume("docker-sock", hostPath = Option(HostPathVolumeSource("/var/run/docker.sock"))))))
) { implicit workDir => gitRef =>
Await.result(
for {
orchestration <- GitUtils.cloneOrchestration(gitRef)
version <- dir(orchestration) { implicit workDir =>
for {
_ <- PullRequest.test()
version <- publish()
} yield version
}
} yield version,
1.hour
)
}
}
def publish()(implicit workDir: Directory, sbt: SbtContainer): Future[String] =
stage("Publish") {
for {
_ <- sbt("orchestrationJVM/docker:publish")
log <- sbt("-Dsbt.log.noformat=true orchestrationJVM/version")
regex = """(?s).*\[info\] (\d.*?)\s.*""".r
} yield
log match {
case regex(version) => version
case _ => throw new IllegalStateException("Can't get version")
}
}
}
import com.drivetribe.orchestra.filesystem.Directory
import com.drivetribe.orchestra.utils.Shells._
import io.k8s.api.core.v1.{Container, ResourceRequirements, VolumeMount}
import io.k8s.apimachinery.pkg.api.resource.Quantity
object SbtCachedContainer
extends Container(
name = "sbt",
image = Option("registry.drivetribe.com/tools/sbt:cached"),
tty = Option(true),
command = Option(Seq("cat")),
volumeMounts = Option(Seq(VolumeMount("docker-sock", mountPath = "/var/run/docker.sock"))),
resources = Option(
ResourceRequirements(
requests = Option(Map("cpu" -> Quantity("4000m"), "memory" -> Quantity("5.5G"))),
limits = Option(Map("cpu" -> Quantity("5000m"), "memory" -> Quantity("5.5G")))
)
)
)
with SbtContainer
object SbtCachedTestContainer
extends Container(
name = "sbt",
image = Option("registry.drivetribe.com/tools/sbt:cached"),
tty = Option(true),
command = Option(Seq("cat")),
volumeMounts = Option(Seq(VolumeMount("docker-sock", mountPath = "/var/run/docker.sock"))),
resources = Option(
ResourceRequirements(
requests = Option(Map("cpu" -> Quantity("4000m"), "memory" -> Quantity("8.5G"))),
limits = Option(Map("cpu" -> Quantity("5000m"), "memory" -> Quantity("8.5G")))
)
)
)
with SbtContainer
object SbtLatestContainer
extends Container(
name = "sbt",
image = Option("registry.drivetribe.com/tools/sbt"),
tty = Option(true),
command = Option(Seq("cat")),
volumeMounts = Option(Seq(VolumeMount("docker-sock", mountPath = "/var/run/docker.sock")))
)
with SbtContainer
trait SbtContainer extends Container {
def apply(command: String)(implicit workDir: Directory) = sh(s"sbt -mem 3072 $command", this)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment