All Downloads are FREE. Search and download functionalities are using the official Maven repository.

codacy.test.docker.EvolutionsDocker.scala Maven / Gradle / Ivy

The newest version!
package codacy.test
package docker

import java.sql.DriverManager

import com.typesafe.config.ConfigFactory

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}

import com.whisk.docker.{
  ContainerLink,
  DockerCommandExecutor,
  DockerContainer,
  DockerContainerManager,
  DockerReadyChecker
}

trait EvolutionsDocker extends Databases with WithHost {
  self: PostgresDocker =>

  val evolutionsImage: String

  lazy val config = ConfigFactory.load()

  // import scala.collection.JavaConverters._
  //   config
  //     .getConfigList("evolutionsDatabases")
  //     .asScala
  //     .map(c => {
  //       DB(c.getString("name"), c.getString("env"), c.getString("config"), c.getBoolean("create"))
  //     })
  //     .toSeq

  def postgresLink() = ContainerLink(postgresContainer, postgresContainerName)

  val evolutionsCommand = "/codacy/bin/run-evolutions-main"

  lazy val evolutionsContainer: DockerContainer = {

    val envVars = databases.map(_.envVar(postgresContainerName, defaultPostgresPort)) ++ Seq(
      "PLAY_CONFIG_FILE=application.conf",
      "PLAY_LOG_FILE=application-logger.xml"
    )

    DockerContainer(evolutionsImage)
      .withLinks(postgresLink())
      .withEnv(envVars: _*)
      .withEntrypoint("sh", "-c", s"$evolutionsCommand && echo EVOLUTIONS_DONE")
      // .withLogLineReceiver(
      //   com.whisk.docker.LogLineReceiver(true, (str) => {
      //     println("[RAGNAROS] "+str)
      //   })
      // )
      .withReadyChecker(DockerReadyChecker.LogLineContains("EVOLUTIONS_DONE").looped(90, 1.seconds))
  }

  /* here be dragons touch at your own risk */
  def performEvolutions()(implicit dockerExecutor: DockerCommandExecutor, executionContext: ExecutionContext) = {
    // We need this for loading the driver class
    Class.forName("org.postgresql.Driver")

    val url =
      s"jdbc:postgresql://${hostIp}:$postgresPort/?loggerLevel=OFF"
    val conn = DriverManager.getConnection(url, postgresUser, postgresPassword)

    val statement = conn.createStatement()
    val commands = databases.map(_.statement).flatten

    commands.foreach(statement.executeUpdate)

    conn.close

    // run evolutions
    val evolutionsManager =
      new DockerContainerManager(Seq(evolutionsContainer), dockerExecutor)

    for {
      _ <- evolutionsManager.pullImages()
      _ <- evolutionsManager.initReadyAll(90.seconds)
      _ <- Future.traverse(evolutionsManager.states)(_.remove(force = true, removeVolumes = true))
    } yield true
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy