Commit 402c9b95 authored by David's avatar David

Still remembering how to do things

parent 7a614c9c
......@@ -5,6 +5,7 @@ val akkaV = "2.5.14"
val scalaTestV = "3.0.5"
val akka = "com.typesafe.akka" %% "akka-actor" % akkaV
val akkaSlf4j = "com.typesafe.akka" %% "akka-slf4j" % akkaV
val akkaTestkit = "com.typesafe.akka" %% "akka-testkit" % akkaV % Test
val scalaTest = "org.scalactic" %% "scalactic" % scalaTestV
val scalacticScalaTest = "org.scalatest" %% "scalatest" % scalaTestV
......@@ -24,7 +25,7 @@ lazy val backupCoordinator = (project in file("."))
.settings(
name := "Backup Coordinator",
libraryDependencies ++= Seq(
akka, akkaTestkit, scalaTest, scalacticScalaTest, sshj, bouncycastle, jzlib,
akka, akkaSlf4j, akkaTestkit, scalaTest, scalacticScalaTest, sshj, bouncycastle, jzlib,
) ++ log4j
)
......
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
loglevel = "DEBUG"
logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"
}
\ No newline at end of file
......@@ -6,6 +6,7 @@
</Console>
</Appenders>
<Loggers>
<Logger level="debug" name="is.kow.backupcoordinator"/>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
......
package is.kow.backupcoordinator
import java.security.Security
import org.bouncycastle.jce.provider.BouncyCastleProvider
object Main extends App {
Security.addProvider(new BouncyCastleProvider)
//Set up an actor system to do the work
println("HELLO WORLD")
}
......@@ -2,17 +2,18 @@ package is.kow.backupcoordinator.actors
import java.util.concurrent.{Executors, TimeUnit}
import akka.actor.{Actor, Props}
import akka.actor.{Actor, ActorLogging, Props}
import is.kow.backupcoordinator.actors.SSHCommandActor.{ExecuteCommand, ExecutionCompleted}
import net.schmizz.sshj.SSHClient
import net.schmizz.sshj.connection.channel.direct.Session
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor, Future}
import scala.io.Source
import scala.util.{Failure, Success}
object SSHCommandActor {
def props(session: Session): Props = Props(new SSHCommandActor(session))
def props(ssh: SSHClient): Props = Props(new SSHCommandActor(ssh))
sealed trait SSHCommandActorMessages
......@@ -22,14 +23,23 @@ object SSHCommandActor {
}
class SSHCommandActor(session: Session) extends Actor {
class SSHCommandActor(ssh: SSHClient) extends Actor with ActorLogging{
val singleThread = Executors.newSingleThreadExecutor()
implicit val executionContext = ExecutionContext.fromExecutor(singleThread)
private val session = ssh.startSession()
private val singleThread = Executors.newSingleThreadExecutor()
private implicit val executionContext: ExecutionContextExecutor = ExecutionContext.fromExecutor(singleThread)
override def postStop(): Unit = {
log.debug(s"Stopping this session: $session")
session.close()
singleThread.shutdownNow()
}
override def receive: Receive = {
case e: ExecuteCommand =>
//This is really the only thing we're going to work on
log.debug(s"Starting to execute command ${e.command}")
val command = session.exec(e.command)
val replyTo = sender()
//This guy is fully synchronous and blocking so be sure to stick it into another thread
......
......@@ -2,20 +2,20 @@ package is.kow.backupcoordinator.experimentation
import java.nio.file.Paths
import com.typesafe.config.ConfigFactory
import com.typesafe.config.{Config, ConfigFactory}
import net.schmizz.sshj.SSHClient
import net.schmizz.sshj.connection.channel.direct.Session
trait EUtils {
val home = System.getProperty("user.home")
val home: String = System.getProperty("user.home")
val config = ConfigFactory.parseFile(Paths.get(home, ".ssh_config").toFile())
val config: Config = ConfigFactory.parseFile(Paths.get(home, ".ssh_config").toFile())
val password = config.getString("user.pass")
val username = config.getString("user.name")
val hostname = config.getString("host.name")
val hostKey = config.getString("host.fingerprint")
val password: String = config.getString("user.pass")
val username: String = config.getString("user.name")
val hostname: String = config.getString("host.name")
val hostKey: String = config.getString("host.fingerprint")
def establishConnection(context: SSHClient => Unit): Unit = {
val ssh = new SSHClient()
......
......@@ -11,6 +11,8 @@ import akka.util.Timeout
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.concurrent.duration._
object SSHActorExperiment extends App with EUtils {
val system = ActorSystem.create("SSHActorExperiment")
......@@ -26,15 +28,19 @@ object SSHActorExperiment extends App with EUtils {
establishConnection { ssh =>
commands.foreach { cmd =>
withSession(ssh) { session =>
val actor = system.actorOf(SSHCommandActor.props(session))
val actor = system.actorOf(SSHCommandActor.props(ssh))
val resultFuture = (actor ? ExecuteCommand(cmd)).mapTo[ExecutionCompleted]
val resultFuture = (actor ? ExecuteCommand(cmd)).mapTo[ExecutionCompleted]
val result = Await.result(resultFuture, Duration(10, TimeUnit.SECONDS))
val result = Await.result(resultFuture, 10.seconds)
println(s"COMMAND: ${cmd}\nOutput:\n${result.output}\nExitStatus: ${result.exitValue}\n\n")
}
println(s"COMMAND: ${cmd}\nOutput:\n${result.output}\nExitStatus: ${result.exitValue}\n\n")
}
}
val terminating = system.terminate()
println("Awaiting termination")
Await.ready(terminating, 10.seconds)
println("DEAD")
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment