com.google.inject.name.Named Scala Examples

The following examples show how to use com.google.inject.name.Named. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: SilhouetteModule.scala    From crm-seed   with Apache License 2.0 5 votes vote down vote up
package com.dataengi.crm.identities.modules

import com.dataengi.crm.configurations.{CompaniesConfiguration, RolesConfiguration, RootConfiguration, SilhouetteConfiguration}
import com.dataengi.crm.identities.actions.{ActionsProvider, ActionsProviderImplementation}
import com.google.inject.name.Named
import com.google.inject.{Provides, Singleton}
import com.mohiva.play.silhouette.api.actions.SecuredErrorHandler
import com.mohiva.play.silhouette.api.crypto.{AuthenticatorEncoder, Crypter, CrypterAuthenticatorEncoder}
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.services._
import com.mohiva.play.silhouette.api.util._
import com.mohiva.play.silhouette.api.{Environment, EventBus, Silhouette, SilhouetteProvider}
import com.mohiva.play.silhouette.crypto.{JcaCrypter, JcaCrypterSettings}
import com.mohiva.play.silhouette.impl.authenticators._
import com.mohiva.play.silhouette.impl.providers._
import com.mohiva.play.silhouette.impl.services._
import net.ceedubs.ficus.readers.EnumerationReader._
import com.mohiva.play.silhouette.impl.util._
import com.mohiva.play.silhouette.password.BCryptPasswordHasher
import com.mohiva.play.silhouette.persistence.daos.DelegableAuthInfoDAO
import com.mohiva.play.silhouette.persistence.repositories.DelegableAuthInfoRepository
import net.ceedubs.ficus.Ficus._
import net.ceedubs.ficus.readers.ArbitraryTypeReader._
import net.codingwell.scalaguice.ScalaModule
import play.api.Configuration
import play.api.libs.ws.WSClient
import com.dataengi.crm.identities.daos._
import com.dataengi.crm.identities.repositories._
import com.dataengi.crm.identities.services._
import com.dataengi.crm.identities.slick.initiation.InitiateTables
import com.dataengi.crm.identities.utils.auth.DefaultEnv

import scala.concurrent.ExecutionContext

class SilhouetteModule extends ScalaModule {

  def configure() = {
    // Initiation ORDER make sense
    bind[InitiateTables].asEagerSingleton()
    bind[IdentitiesInitiation].asEagerSingleton()

    // Silhouette
    bind[SecuredErrorHandler].to[CustomSecuredErrorHandler]
    bind[Silhouette[DefaultEnv]].to[SilhouetteProvider[DefaultEnv]]
    bind[CacheLayer].to[PlayCacheLayer]
    bind[PasswordHasher].toInstance(new BCryptPasswordHasher)
    bind[FingerprintGenerator].toInstance(new DefaultFingerprintGenerator(false))
    bind[EventBus].toInstance(EventBus())
    bind[Clock].toInstance(Clock())

    // Replace this with the bindings to your concrete DAOs
    bind[PasswordInfoDAO].to[PasswordInfoDAOSlickImplementation]
    bind[DelegableAuthInfoDAO[PasswordInfo]].to[PasswordInfoDAO]

    // Repository
    bind[RolesRepository].to[RolesRepositoryImplementation]
    bind[CompaniesRepository].to[CompaniesRepositoryImplementation]
    bind[InvitesRepository].to[InvitesRepositoryImplementation]
    bind[JWTAuthenticatorRepository].to[JWTAuthenticatorCacheRepositoryImplementation]
    bind[UsersRepository].to[UsersRepositoryImplementation]
    bind[RecoverPasswordInfoRepository].to[RecoverPasswordInfoRepositoryImplementation]

    // DAOs
    bind[CompaniesDAO].to[CompaniesSlickDAOImplementation]
    bind[RolesDAO].to[RolesSlickDAOImplementation]
    bind[InvitesDAO].to[InvitesSlickDAOImplementation]
    bind[JWTAuthenticatorDAO].to[JWTAuthenticatorSlickDAOImplementation]
    bind[UsersDAO].to[UsersSlickDAOImplementation]
    bind[RecoverPasswordInfoDAO].to[RecoverPasswordInfoSlickDAOImplementation]

    // Services
    bind[UsersService].to[UsersServiceImplementation]
    bind[CompaniesService].to[CompaniesServiceImplementation]
    bind[RolesService].to[RolesServiceImplementation]
    bind[InvitesService].to[InvitesServiceImplementation]
    bind[AuthenticationService].to[AuthenticationServiceImplementation]
    bind[UsersManagementService].to[UsersManagementServiceImplementation]

    // Actions
    bind[ActionsProvider].to[ActionsProviderImplementation]

    // Configuration
    bind[RolesConfiguration].asEagerSingleton()
    bind[CompaniesConfiguration].asEagerSingleton()
    bind[RootConfiguration].asEagerSingleton()
    bind[SilhouetteConfiguration].asEagerSingleton()

  }

  
  @Provides
  def provideCredentialsProvider(authInfoRepository: AuthInfoRepository,
                                 passwordHasherRegistry: PasswordHasherRegistry,
                                 executionContext: ExecutionContext): CredentialsProvider = {

    new CredentialsProvider(authInfoRepository, passwordHasherRegistry)(executionContext)
  }

} 
Example 2
Source File: HomeController.scala    From Aton   with GNU General Public License v3.0 5 votes vote down vote up
package controllers

import akka.actor.{ActorRef, ActorSystem}
import com.google.inject.name.Named
import com.google.inject.{Inject, Singleton}
import dao.{DatabaseInitializer, LaboratoryDAO, UserDAO}
import model.{Role, User}
import play.api.{Environment, Logger}
import play.api.i18n.MessagesApi
import services.{LaboratoryService, UserService}
import views.html._

import scala.concurrent.{Await, ExecutionContext}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._


@Singleton
class HomeController @Inject()(databaseInitializer: DatabaseInitializer, laboratoryService : LaboratoryService, @Named("computerChecker") computerChecker: ActorRef, actorSystem: ActorSystem)(implicit userService: UserService, executionContext: ExecutionContext, environment: Environment) extends ControllerWithNoAuthRequired {
  val logger = Logger("HomeController")

  play.Logger.debug("Configuring Computer Checker...")
  actorSystem.scheduler.schedule(0.microseconds,5.minutes, computerChecker,"Execute")
  play.Logger.debug("Computer Checker configured.")

  logger.debug("Initializing database")
  Await.result(databaseInitializer.initialize(), 2.seconds)
  logger.debug("Database initialized")

  def home = AsyncStack { implicit request =>
    play.Logger.debug("Logged user: " + loggedIn)
    implicit val (username: Option[String], isAdmin: Boolean) = loggedIn match {
      case Some(User(usernameString, password, Some(name), role)) => (Some(name), role == Role.Administrator)
      case Some(User(usernameString, password, None, role)) => (Some(usernameString), role == Role.Administrator)
      case _ => (None, false)
    }
    logger.debug("PeticiĆ³n de listar todos los laboratorios con el siguiente request recibida " + request)
    logger.debug("User: " + username + ", is admin: " + isAdmin)
    laboratoryService.listAll.map { _ =>
      Ok(index("Aton"))
    }
  }

  def about = StackAction { implicit request =>
    implicit val (username: Option[String], isAdmin: Boolean) = loggedIn match {
      case Some(User(usernameString, password, Some(name), role)) => (Some(name), role == Role.Administrator)
      case Some(User(usernameString, password, None, role)) => (Some(usernameString), role == Role.Administrator)
      case _ => (None, false)
    }

    Ok//(index(messagesApi("about"),views.html.about()))
  }
} 
Example 3
Source File: PassChainExecutor.scala    From apalache   with Apache License 2.0 5 votes vote down vote up
package at.forsyte.apalache.infra.passes

import com.google.inject.Inject
import com.google.inject.name.Named
import com.typesafe.scalalogging.LazyLogging



class PassChainExecutor @Inject()(val options: WriteablePassOptions,
                                  @Named("InitialPass") val initialPass: Pass)
  extends LazyLogging {

  def run(): Option[Pass] = {
    def exec(seqNo: Int, passToRun: Pass): Option[Pass] = {
      logger.info("PASS #%d: %s".format(seqNo, passToRun.name))
      val result = passToRun.execute()
      val outcome = if (result) "[OK]" else "[FAIL]"
      logger.debug("PASS #%d: %s %s".format(seqNo, passToRun.name, outcome))
      if (!result) {
        None // return the negative result
      } else {
        val nextPass = passToRun.next()
        if (nextPass.isDefined) {
          exec(1 + seqNo, nextPass.get) // call the next pass in line
        } else {
          Some(passToRun) // finished
        }
      }
    }

    exec(0, initialPass)
  }
} 
Example 4
Source File: FileManagerModule.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.modules

import com.amazonaws.auth.{ AWSStaticCredentialsProvider, BasicAWSCredentials }
import com.amazonaws.services.s3.{ AmazonS3, AmazonS3ClientBuilder }
import com.google.inject.name.Named
import com.google.inject.{ AbstractModule, Provides }
import net.codingwell.scalaguice.ScalaModule
import org.hatdex.hat.api.service.{ AwsS3Configuration, FileManager, FileManagerS3 }
import play.api.Configuration
import play.api.libs.concurrent.AkkaGuiceSupport

class FileManagerModule extends AbstractModule with ScalaModule with AkkaGuiceSupport {

  override def configure() = {
    bind[FileManager].to[FileManagerS3]
    ()
  }

  @Provides
  def provideCookieAuthenticatorService(configuration: Configuration): AwsS3Configuration = {
    import AwsS3Configuration.configLoader
    configuration.get[AwsS3Configuration]("storage.s3Configuration")
  }

  @Provides @Named("s3client-file-manager")
  def provides3Client(configuration: AwsS3Configuration): AmazonS3 = {
    val awsCreds: BasicAWSCredentials = new BasicAWSCredentials(configuration.accessKeyId, configuration.secretKey)
    AmazonS3ClientBuilder.standard()
      .withRegion(configuration.region)
      .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
      .build()
  }

} 
Example 5
Source File: FileManager.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.api.service

import javax.inject.Inject

import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.model.{ GeneratePresignedUrlRequest, SSEAlgorithm }
import com.google.inject.name.Named
import com.typesafe.config.Config
import org.hatdex.hat.resourceManagement.HatServer
import play.api.{ ConfigLoader, Logger }

import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration

trait FileManager {
  def getUploadUrl(filename: String, maybeContentType: Option[String])(implicit hatServer: HatServer): Future[String]
  def getContentUrl(filename: String)(implicit hatServer: HatServer): Future[String]
  def getFileSize(fileName: String)(implicit hatServer: HatServer): Future[Long]
  def deleteContents(filename: String)(implicit hatServer: HatServer): Future[Unit]
}

case class AwsS3Configuration(
    bucketName: String,
    accessKeyId: String,
    secretKey: String,
    region: String,
    signedUrlExpiry: FiniteDuration)

object AwsS3Configuration {
  implicit val configLoader: ConfigLoader[AwsS3Configuration] = new ConfigLoader[AwsS3Configuration] {
    def load(rootConfig: Config, path: String): AwsS3Configuration = {
      val config = rootConfig.getConfig(path)
      AwsS3Configuration(
        bucketName = config.getString("bucketName"),
        accessKeyId = config.getString("accessKeyId"),
        secretKey = config.getString("secretKey"),
        region = config.getString("region"),
        signedUrlExpiry = ConfigLoader.finiteDurationLoader.load(config, "signedUrlExpiry"))
    }
  }
}

class FileManagerS3 @Inject() (
    awsS3Configuration: AwsS3Configuration,
    @Named("s3client-file-manager") s3client: AmazonS3)(implicit ec: RemoteExecutionContext) extends FileManager {

  private val logger = Logger(this.getClass)
  private val bucketName = awsS3Configuration.bucketName

  def getUploadUrl(fileName: String, maybeContentType: Option[String])(implicit hatServer: HatServer): Future[String] = {
    val expiration = org.joda.time.DateTime.now().plus(awsS3Configuration.signedUrlExpiry.toMillis)

    val generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, s"${hatServer.domain}/$fileName")
      .withMethod(com.amazonaws.HttpMethod.PUT)
      .withExpiration(expiration.toDate)
      .withSSEAlgorithm(SSEAlgorithm.AES256)

    // TODO: to be replaced with mandatory validated content MIME type in API v2.7
    val generatePresignedUrlRequestWithContent = maybeContentType.map { contentType =>
      generatePresignedUrlRequest.withContentType(contentType)
    }.getOrElse(generatePresignedUrlRequest)

    val url = Future(s3client.generatePresignedUrl(generatePresignedUrlRequestWithContent))
    url.map(_.toString)
  }

  def getContentUrl(fileName: String)(implicit hatServer: HatServer): Future[String] = {
    val expiration = org.joda.time.DateTime.now().plus(awsS3Configuration.signedUrlExpiry.toMillis)

    val generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, s"${hatServer.domain}/$fileName")
      .withMethod(com.amazonaws.HttpMethod.GET)
      .withExpiration(expiration.toDate)

    val url = Future(s3client.generatePresignedUrl(generatePresignedUrlRequest))
    url.map(_.toString)
  }

  def getFileSize(fileName: String)(implicit hatServer: HatServer): Future[Long] = {
    logger.debug(s"Getting file size for $bucketName ${hatServer.domain}/$fileName")
    Future(s3client.getObjectMetadata(bucketName, s"${hatServer.domain}/$fileName"))
      .map { metadata => Option(metadata.getContentLength).getOrElse(0L) }
      .recover { case _ => 0L }
  }

  def deleteContents(fileName: String)(implicit hatServer: HatServer): Future[Unit] = {
    Future(s3client.deleteObject(bucketName, s"${hatServer.domain}/$fileName"))
  }
} 
Example 6
Source File: PresetService.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowmanager


import scala.concurrent.{ExecutionContext, Future}

import com.google.inject.Inject
import com.google.inject.name.Named

import ai.deepsense.commons.auth.{Authorizator, AuthorizatorProvider}
import ai.deepsense.commons.auth.usercontext.UserContext
import ai.deepsense.commons.models.ClusterDetails
import ai.deepsense.models.workflows.Workflow
import ai.deepsense.models.workflows.Workflow._
import ai.deepsense.workflowmanager.exceptions.{WorkflowNotFoundException, WorkflowOwnerMismatchException}
import ai.deepsense.workflowmanager.model.WorkflowPreset
import ai.deepsense.workflowmanager.storage.WorkflowStorage
import ai.deepsense.workflowmanager.storage.impl.PresetsDao


class PresetService @Inject()(presetStore: PresetsDao,
                              workflowStorage: WorkflowStorage,
                              authorizatorProvider: AuthorizatorProvider,
                              @Named("roles.workflows.update") roleUpdate: String)
                             (implicit ec: ExecutionContext) {



  def listPresets(): Future[Seq[ClusterDetails]] = {
    presetStore.getPresets
  }

  def createPreset(clusterConfig: ClusterDetails): Future[Long] = {
    presetStore.savePreset(clusterConfig)
  }

  def getPreset(presetId: Long): Future[Option[ClusterDetails]] = {
    presetStore.getPreset(presetId)
  }

  def updatePreset(presetId: Long, clusterConfig: ClusterDetails): Future[Long] = {
    presetStore.updatePreset(presetId, clusterConfig).map(_ => presetId)
  }

  def removePreset(presetId: Long): Future[Unit] = {
    presetStore.removePreset(presetId)
  }

  def saveWorkflowsPreset(userContextFuture: Future[UserContext], workflowId: Workflow.Id,
                          workflowPreset: WorkflowPreset): Future[Unit] = {
    val authorizator: Authorizator = authorizatorProvider.forContext(userContextFuture)
    authorizator.withRole(roleUpdate) {
      userContext => {
        workflowStorage.get(workflowId).flatMap {
          case Some(w) =>
              if (w.ownerId != userContext.user.id) {
                throw new WorkflowOwnerMismatchException(workflowId)
              }
              presetStore.saveWorkflowsPreset(workflowPreset: WorkflowPreset)
          case None => throw new WorkflowNotFoundException(workflowId)
        }
      }

    }
  }

  def getWorkflowsPreset(workflowId: Id): Future[Option[ClusterDetails]] = {
    presetStore.getWorkflowsPreset(workflowId)
  }
} 
Example 7
Source File: NotebookDaoImpl.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.workflowmanager.storage.impl

import java.util.UUID

import scala.concurrent.{ExecutionContext, Future}

import com.google.inject.Inject
import com.google.inject.name.Named
import slick.driver.JdbcDriver
import slick.lifted.{Index, PrimaryKey, ProvenShape}

import ai.deepsense.graph.Node
import ai.deepsense.models.workflows.Workflow.Id
import ai.deepsense.workflowmanager.storage.NotebookStorage

class NotebookDaoImpl @Inject()(
    @Named("workflowmanager") db: JdbcDriver#API#Database,
    @Named("workflowmanager") driver: JdbcDriver)
    (implicit ec: ExecutionContext)
  extends NotebookStorage {

  import driver.api._

  override def get(workflowId: Id, nodeId: Node.Id): Future[Option[String]] = {
    db.run(
      notebooks.filter(n => n.workflowId === workflowId.value && n.nodeId === nodeId.value).result
    ).map {
      case Seq() => None
      case Seq((_, _, notebook)) => Some(notebook)
    }
  }

  override def save(workflowId: Id, nodeId: Node.Id, notebook: String): Future[Unit] =
    db.run(notebooks.insertOrUpdate((workflowId.value, nodeId.value, notebook))).map(_ => ())

  override def getAll(workflowId: Id): Future[Map[Node.Id, String]] = {
    db.run(notebooks.filter(_.workflowId === workflowId.value).result).map(
      _.map {
        case (_, nodeId, notebook) => Node.Id.fromUuid(nodeId) -> notebook
      }.toMap)
  }

  val WorkflowId = "workflow_id"
  val NodeId = "node_id"
  val Notebook = "notebook"

  private class Notebooks(tag: Tag)
    extends Table[(UUID, UUID, String)](tag, "NOTEBOOKS") {

    def workflowId: Rep[UUID] = column[UUID](WorkflowId)
    def nodeId: Rep[UUID] = column[UUID](NodeId)
    def notebook: Rep[String] = column[String](Notebook)

    def pk: PrimaryKey = primaryKey("pk_notebooks", (workflowId, nodeId))
    def index: Index = index("idx_notebooks_workflow_id", workflowId, unique = false)

    def * : ProvenShape[(UUID, UUID, String)] =
      (workflowId, nodeId, notebook)
  }

  private val notebooks = TableQuery[Notebooks]

  private[impl] def create(): Future[Unit] = db.run(notebooks.schema.create)
  private[impl] def drop(): Future[Unit] = db.run(notebooks.schema.drop)
} 
Example 8
Source File: MqModule.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.sessionmanager.mq

import java.util.concurrent.TimeoutException

import akka.actor.{ActorRef, ActorSystem}
import com.google.inject.name.Named
import com.google.inject.{AbstractModule, Provides, Singleton}
import com.rabbitmq.client.ConnectionFactory
import com.thenewmotion.akka.rabbitmq.ConnectionActor

import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal

import ai.deepsense.commons.utils.Logging
import ai.deepsense.sessionmanager.service.executor.SessionExecutorClients
import ai.deepsense.sparkutils.AkkaUtils
import ai.deepsense.workflowexecutor.communication.mq.MQCommunication
import ai.deepsense.workflowexecutor.communication.mq.json.Global.{GlobalMQDeserializer, GlobalMQSerializer}
import ai.deepsense.workflowexecutor.rabbitmq.MQCommunicationFactory

class MqModule extends AbstractModule with Logging {
  override def configure(): Unit = {}

  @Provides
  @Singleton
  def communicationFactory(
      actorSystem: ActorSystem,
      @Named("MQConnectionActor") connection: ActorRef): MQCommunicationFactory = {
    MQCommunicationFactory(actorSystem, connection, GlobalMQSerializer, GlobalMQDeserializer)
  }

  @Provides
  @Singleton
  @Named("MQConnectionActor")
  def createConnection(
      system: ActorSystem,
      @Named("queue.host") host: String,
      @Named("queue.port") port: Int,
      @Named("queue.user") user: String,
      @Named("queue.pass") pass: String): ActorRef = {
    val factory = new ConnectionFactory()
    factory.setHost(host)
    factory.setPort(port)
    factory.setUsername(user)
    factory.setPassword(pass)
    system.actorOf(
      ConnectionActor.props(factory),
      MQCommunication.mqActorSystemName)
  }

  @Provides
  @Singleton
  def createSessionExecutorClients(
      communicationFactory: MQCommunicationFactory): SessionExecutorClients = {
    new SessionExecutorClients(communicationFactory)
  }

  @Provides
  @Singleton
  @Named("SessionService.HeartbeatSubscribed")
  def heartbeatSubscriber(
      system: ActorSystem,
      communicationFactory: MQCommunicationFactory,
      @Named("SessionService.Actor") sessionServiceActor: ActorRef,
      @Named("queue.heartbeat.subscription.timeout") timeout: Long): Future[Unit] = {
    import ai.deepsense.sessionmanager.mq.MQCommunicationFactoryEnrichments._
    implicit val ec: ExecutionContext = system.dispatcher
    val subscribed = communicationFactory
      .registerBroadcastSubscriber("seahorse_heartbeats_all", sessionServiceActor)

    val subscribedWithTimeout = Future.firstCompletedOf(List(subscribed, Future {
      Thread.sleep(timeout)
      throw new TimeoutException
    }))

    subscribedWithTimeout.onFailure {
      case NonFatal(e) =>
        logger.error(s"Haven't subscribed to Heartbeats after '$timeout' millis." +
          " Shutting down!")
        AkkaUtils.terminate(system)
    }

    subscribedWithTimeout.map(_.data)
  }
} 
Example 9
Source File: StateInferencer.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.sessionmanager.service.sessionspawner

import java.time.Instant

import com.google.inject.Inject
import com.google.inject.name.Named
import org.apache.spark.launcher.SparkAppHandle

import ai.deepsense.sessionmanager.service.Status
import ai.deepsense.sessionmanager.service.Status.Status

trait StateInferencer {
  def handleHeartbeat(currentTime: Instant): StateInferencer
  def statusForApi(currentTime: Instant, sparkState: SparkAppHandle.State): Status
}

// Factory is needed because of config and guice
class StateInferencerFactory @Inject()(
    @Named("session-manager.executor-first-heartbeat-timeout") val firstHeartbeatTimeout: Int,
    @Named("session-manager.heartbeat-maximum-delay") val heartbeatMaximumDelay: Int
) {

  def newInferencer(startTime: Instant): StateInferencer =
    StateInferencerForClientMode(startTime, None)

  private case class StateInferencerForClientMode(
      startTime: Instant, lastHeartbeat: Option[Instant]
  ) extends StateInferencer {

    def handleHeartbeat(currentTime: Instant): StateInferencer = this.copy(
      lastHeartbeat = Some(currentTime)
    )

    def statusForApi(currentTime: Instant, sparkState: SparkAppHandle.State): Status = {
      val executorIsNotRunning = sparkState.isFinal

      if(executorIsNotRunning) {
        Status.Error
      } else {
        lastHeartbeat match {
          case None => statusBeforeFirstHeartbeat(currentTime)
          case Some(lastHeartbeatTime) => statusAfterFirstHeartbeat(currentTime, lastHeartbeatTime)
        }
      }
    }

    private def statusAfterFirstHeartbeat(currentTime: Instant, lastHeartbeatTime: Instant) = {
      val secondsFromLastHeartbeat = secondsBetween(currentTime, lastHeartbeatTime)
      if (secondsFromLastHeartbeat < heartbeatMaximumDelay) {
        Status.Running
      } else {
        Status.Error
      }
    }

    private def statusBeforeFirstHeartbeat(currentTime: Instant) = {
      val secondsFromStart = secondsBetween(currentTime, startTime)
      if (secondsFromStart < firstHeartbeatTimeout) {
        Status.Creating
      } else {
        Status.Error
      }
    }

    private def secondsBetween(a: Instant, b: Instant) =
      java.time.Duration.between(a, b).abs().getSeconds.toInt

  }

} 
Example 10
Source File: OutputInterceptorFactory.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.sessionmanager.service.sessionspawner.sparklauncher.outputintercepting

import java.io.File
import java.text.SimpleDateFormat
import java.util.logging._
import java.util.{Calendar, UUID}

import com.google.inject.Inject
import com.google.inject.name.Named
import org.apache.spark.launcher.SparkLauncher

import ai.deepsense.commons.models.ClusterDetails



case class OutputInterceptorHandle private [outputintercepting] (
    private val logger: Logger,
    private val childProcLoggerName: String,
    private val loggerFileHandler: FileHandler ) {

  def attachTo(sparkLauncher: SparkLauncher): Unit = {
    sparkLauncher.setConf(
      "spark.launcher.childProcLoggerName", childProcLoggerName
    )
  }

  def writeOutput(text: String): Unit = {
    logger.info(text)
  }

  def close(): Unit = {
    loggerFileHandler.close()
  }

}

class OutputInterceptorFactory @Inject()(
  @Named("session-executor.spark-applications-logs-dir") val executorsLogDirectory: String
) {

  def prepareInterceptorWritingToFiles(clusterDetails: ClusterDetails): OutputInterceptorHandle = {
    new File(executorsLogDirectory).mkdirs()

    val childProcLoggerName = s"WE-app-${UUID.randomUUID()}"
    val logger = Logger.getLogger(childProcLoggerName)

    val fileName = {
      val time = Calendar.getInstance().getTime()
      // Colons are not allowed in Windows filenames
      val format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss")
      val formattedTime = format.format(time)
      val illegalFileNameCharactersRegExp = "[^a-zA-Z0-9.-]"
      s"$formattedTime-${clusterDetails.name.replaceAll(illegalFileNameCharactersRegExp, "_")}.log"
    }
    val fileHandler = new FileHandler(s"$executorsLogDirectory/$fileName")
    fileHandler.setFormatter(new SimpleFormaterWithoutOutputRedirectorNoise)
    logger.addHandler(fileHandler)
    sys.addShutdownHook {
      fileHandler.close()
    }
    OutputInterceptorHandle(logger, childProcLoggerName, fileHandler)
  }

  class SimpleFormaterWithoutOutputRedirectorNoise extends Formatter {

    val simpleFormatter = new SimpleFormatter

    override def format(logRecord: LogRecord): String = {
      val formatted = simpleFormatter.format(logRecord)
      val redirectorNoise = "org.apache.spark.launcher.OutputRedirector redirect\nINFO: "
      val beginningOfRedirectorNoise = formatted.indexOf(redirectorNoise)

      val endOfRedirectorNoise = if (beginningOfRedirectorNoise > 0) {
        beginningOfRedirectorNoise + redirectorNoise.length
      } else {
        0
      }

      formatted.substring(endOfRedirectorNoise)
    }
  }

} 
Example 11
Source File: SparkLauncherConfig.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.sessionmanager.service.sessionspawner.sparklauncher

import java.io.File
import java.net.{URI, URL}

import com.google.inject.Inject
import com.google.inject.name.Named

class SparkLauncherConfig @Inject()(
  @Named("session-executor.parameters.class-name") val className: String,
  @Named("session-executor.parameters.application-jar-path") val weJarPath: String,
  @Named("session-executor.parameters.deps-zip-path") val weDepsPath: String,
  @Named("session-executor.parameters.spark-resources-jars-dir") val sparkResourcesJarsDir: String,
  @Named("session-executor.parameters.spark-home-path") val sparkHome: String,
  @Named("session-executor.parameters.queue.port") val queuePort: Int,
  @Named("session-executor.parameters.queue.host") val queueHost: String,
  @Named("session-executor.parameters.queue.user") val queueUser: String,
  @Named("session-executor.parameters.queue.pass") val queuePass: String,
  @Named("session-executor.parameters.workflow-manager.scheme") val wmScheme: String,
  @Named("session-executor.parameters.workflow-manager.address") val wmAddress: String,
  @Named("session-executor.parameters.workflow-manager.username") val wmUsername: String,
  @Named("session-executor.parameters.workflow-manager.password") val wmPassword: String,
  @Named("session-executor.parameters.mail-server.smtp.host") val mailServerHost: String,
  @Named("session-executor.parameters.mail-server.smtp.port") val mailServerPort: Int,
  @Named("session-executor.parameters.mail-server.user") val mailServerUser: String,
  @Named("session-executor.parameters.mail-server.password") val mailServerPassword: String,
  @Named("session-executor.parameters.mail-server.sender") val mailServerSender: String,
  @Named("session-executor.parameters.notebook-server.address") val notebookServerAddress: String,
  @Named("session-executor.parameters.datasource-server.address") val datasourceManagerServerAddress: String,
  @Named("session-executor.parameters.temp-dir") val tempDir: String,
  @Named("session-executor.parameters.python-driver-binary") val pythonDriverBinary: String,
  @Named("session-executor.parameters.python-executor-binary") val pythonExecutorBinary: String
) {

  def weDepsFileName: String = {
    new File(new URI(weDepsPath).getPath).getName
  }

} 
Example 12
Source File: SessionService.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.sessionmanager.service

import java.util.concurrent.TimeUnit

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
import scalaz.ListT._
import scalaz.OptionT
import scalaz.OptionT._
import scalaz.std.scalaFuture._

import akka.actor.ActorRef
import akka.pattern.ask
import akka.util.Timeout
import com.google.inject.Inject
import com.google.inject.name.Named

import ai.deepsense.commons.models.{ClusterDetails, Id}
import ai.deepsense.commons.utils.Logging
import ai.deepsense.sparkutils.ScalaUtils
import ai.deepsense.sessionmanager.rest.responses.NodeStatusesResponse
import ai.deepsense.sessionmanager.service.SessionService.FutureOpt
import ai.deepsense.sessionmanager.service.actors.SessionServiceActor
import ai.deepsense.sessionmanager.service.sessionspawner.{ExecutorSession, SessionConfig}

class SessionService @Inject() (
  @Named("SessionService.Actor") private val serviceActor: ActorRef,
  @Named("session-service.timeout") private val timeoutMillis: Int,
  @Named("predefined-users.admin.id") private val adminUserId: String
)(implicit ec: ExecutionContext) extends Logging {

  private implicit val implicitTimeout = Timeout(timeoutMillis, TimeUnit.MILLISECONDS)

  def getSession(callerId: String, workflowId: Id): FutureOpt[Session] = {
    logger.info(s"Getting session '$workflowId'")

    getSessionImpl(callerId, workflowId)
      .map { _.sessionForApi() }
  }

  def createSession(
      sessionConfig: SessionConfig,
      clusterConfig: ClusterDetails): Future[Session] = {
    logger.info(s"Creating session with config $sessionConfig")
    (serviceActor ? SessionServiceActor.CreateRequest(sessionConfig, clusterConfig)).mapTo[Session]
  }

  def listSessions(callerId: String): Future[List[Session]] = {
    logger.info(s"Listing sessions")
    listT((serviceActor ? SessionServiceActor.ListRequest()).mapTo[List[ExecutorSession]])
      .filter(session => isAuthorized(callerId = callerId, ownerId = session.sessionConfig.userId))
      .map { _.sessionForApi() }
      .run
  }

  def killSession(callerId: String, workflowId: Id): FutureOpt[Unit] =
    getSessionImpl(callerId, workflowId)
      .map { _ =>
        logger.info(s"Killing session '$workflowId'")
        (serviceActor ? SessionServiceActor.KillRequest(workflowId)).mapTo[Unit]
      }

  def launchSession(callerId: String, workflowId: Id): FutureOpt[Unit] =
    getSessionImpl(callerId, workflowId)
      .map { _ =>
        logger.info(s"Launching nodes in session '$workflowId'")
        (serviceActor ? SessionServiceActor.LaunchRequest(workflowId))
          .mapTo[Try[Unit]]
          .flatMap(ScalaUtils.futureFromTry)
      }

  def nodeStatusesQuery(callerId: String, workflowId: Id): FutureOpt[NodeStatusesResponse] =
    getSessionImpl(callerId, workflowId)
      .flatMapF { _ =>
        logger.info(s"Asking for node statuses for session $workflowId")
        (serviceActor ? SessionServiceActor.NodeStatusesRequest(workflowId))
          .mapTo[Try[NodeStatusesResponse]]
          .flatMap(ScalaUtils.futureFromTry)
      }

  private def isAuthorized(callerId: String, ownerId: String): Boolean =
    callerId == ownerId || callerId == adminUserId

  private def getSessionImpl(callerId: String, workflowId: Id): FutureOpt[ExecutorSession] =
    optionT((serviceActor ? SessionServiceActor.GetRequest(workflowId))
      .mapTo[Option[ExecutorSession]])
      .map { es =>
          if (isAuthorized(callerId = callerId, ownerId = es.sessionConfig.userId)) {
            es
          } else {
            throw UnauthorizedOperationException(
              s"user $callerId accessing session '$workflowId' owned by ${es.sessionConfig.userId}")
          }
      }
}

object SessionService {
  type FutureOpt[A] = OptionT[Future, A]
} 
Example 13
Source File: ServiceModule.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.sessionmanager.service

import akka.actor.{ActorRef, ActorSystem}
import com.google.inject.name.Named
import com.google.inject.{AbstractModule, Provides, Singleton}

import ai.deepsense.commons.akka.GuiceAkkaExtension
import ai.deepsense.sessionmanager.service.actors.SessionServiceActor
import ai.deepsense.sessionmanager.service.sessionspawner.SessionSpawner
import ai.deepsense.sessionmanager.service.sessionspawner.sparklauncher.SparkLauncherSessionSpawner

class ServiceModule extends AbstractModule {
  override def configure(): Unit = {
    bind(classOf[SessionSpawner]).to(classOf[SparkLauncherSessionSpawner])
  }

  @Provides
  @Singleton
  @Named("SessionService.Actor")
  def sessionServiceActor(system: ActorSystem): ActorRef = {
    system.actorOf(GuiceAkkaExtension(system).props[SessionServiceActor], "SessionService.Actor")
  }
} 
Example 14
Source File: KeystoneApiModule.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.commons.jclouds

import java.util.Properties

import com.google.inject.name.Named
import com.google.inject.{AbstractModule, Provides, Singleton}
import org.jclouds.Constants.{PROPERTY_CONNECTION_TIMEOUT, PROPERTY_SO_TIMEOUT}
import org.jclouds.ContextBuilder
import org.jclouds.openstack.keystone.v2_0.KeystoneApi

class KeystoneApiModule extends AbstractModule {
  override def configure(): Unit = {
    // Configuration not needed - everything is done by the methods annotated with "Provides".
  }

  @Provides
  @Singleton
  def provideKeystoneApi(
      @Named("auth-service.endpoint") endpoint: String,
      @Named("auth-service.identity") identity: String,
      @Named("auth-service.password") password: String,
      @Named("auth-service.timeout.connection") connectionTimeout: Long,
      @Named("auth-service.timeout.socket") socketTimeout: Long): KeystoneApi = {
    val overrides = new Properties()
    overrides.setProperty(PROPERTY_CONNECTION_TIMEOUT, connectionTimeout.toString)
    overrides.setProperty(PROPERTY_SO_TIMEOUT, socketTimeout.toString)
    ContextBuilder.newBuilder("openstack-keystone")
      .endpoint(endpoint)
      .credentials(identity, password)
      .overrides(overrides)
      .buildApi(classOf[KeystoneApi])
  }
} 
Example 15
Source File: RestModule.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.commons.rest

import scala.concurrent.Await
import scala.concurrent.duration._

import akka.actor.{ActorRef, ActorSystem}
import akka.pattern.ask
import akka.util.Timeout
import com.google.inject.name.Named
import com.google.inject.{AbstractModule, Provides, Singleton}

import ai.deepsense.commons.akka.GuiceAkkaExtension


class RestModule extends AbstractModule {
  override def configure(): Unit = {
    bind(classOf[RestServer])
  }

  @Provides
  @Singleton
  @Named("ApiRouterActorRef")
  def provideApiRouterActorRef(
      @Named("server.startup.timeout") startupTimeout: Long,
      system: ActorSystem): ActorRef = {
    val supervisor =
      system.actorOf(GuiceAkkaExtension(system).props[RestServiceSupervisor], "RestSupervisor")
    val restServiceActorProps = GuiceAkkaExtension(system).props[RestServiceActor]
    implicit val timeout: Timeout = startupTimeout.seconds
    val actorRef = supervisor.ask((restServiceActorProps, "RestServiceActor")).mapTo[ActorRef]
    Await.result(actorRef, timeout.duration)
  }
} 
Example 16
Source File: VersionApi.scala    From seahorse   with Apache License 2.0 5 votes vote down vote up
package ai.deepsense.commons.rest

import com.google.inject.Inject
import com.google.inject.name.Named
import spray.routing.Route

import ai.deepsense.commons.auth.usercontext.TokenTranslator
import ai.deepsense.commons.buildinfo.BuildInfo

class VersionApi @Inject() (
    @Named("componentName") val componentName: String,
    val tokenTranslator: TokenTranslator)
  extends RestApi with RestComponent {

  override def route: Route = {
    handleRejections(rejectionHandler) {
      handleExceptions(exceptionHandler) {
        path("version") {
          get {
            complete(s"name: $componentName, version: ${BuildInfo.version}, " +
              s"scalaVersion: ${BuildInfo.scalaVersion}, sbtVersion: ${BuildInfo.sbtVersion}, " +
              s"gitCommitId: ${BuildInfo.gitCommitId}")
          }
        }
      }
    }
  }
}