Commit 8421ed76 authored by David's avatar David

Things still work

parent 7dc73f47
package is.kow.backupcoordinator
// Expect the pre/post to be bash scripts
case class HostBackup(
hostname: String,
repoName: String,
......@@ -9,3 +10,18 @@ case class HostBackup(
keepDaily: Int = 30,
keepMonthly: Int = 3
)
case class BackupConfiguration(
networkMountPath: String,
localMountLocation: String = "/mnt/auto-backup" //TODO: make this semi-random so it is reliable?
)
/**
* Wrap up a command, potentially including a rollback command
* @param command
* @param rollback
*/
case class CommandWrapper(
command: String,
rollback: Option[String] = None
)
\ No newline at end of file
......@@ -28,11 +28,16 @@ object Main extends App {
val smallerHostBackups = List(
HostBackup("scm.dark.kow.is",
"gitrepos",
List("/home/gitbucket/.gitbucket"))
"gitrepos",
List("/home/gitbucket/.gitbucket")
)
)
val hostBackupActor = system.actorOf(HostBackupActor.props(smallerHostBackups.head))
val backupConfig = BackupConfiguration(
networkMountPath = "10.10.220.92:/mnt/OldTank/backupTesting"
)
val hostBackupActor = system.actorOf(HostBackupActor.props(smallerHostBackups.head, backupConfig))
}
package is.kow.backupcoordinator.actors
import akka.actor.{Actor, ActorLogging, ActorRef, Props, ReceiveTimeout, Terminated}
import is.kow.backupcoordinator.HostBackup
import is.kow.backupcoordinator.{BackupConfiguration, CommandWrapper, HostBackup}
import is.kow.backupcoordinator.actors.HostBackupActor.EstablishSSHSession
import is.kow.backupcoordinator.actors.SSHCommandActor.{ExecuteCommand, ExecutionCompleted}
import is.kow.backupcoordinator.actors.SSHConnectionActor.Connect
import net.schmizz.sshj.SSHClient
object HostBackupActor {
def props(hostBackup: HostBackup): Props = Props(new HostBackupActor(hostBackup))
def props(hostBackup: HostBackup, backupConfiguration: BackupConfiguration): Props = Props(new HostBackupActor(hostBackup, backupConfiguration))
case object EstablishSSHSession
......@@ -16,7 +16,7 @@ object HostBackupActor {
}
class HostBackupActor(hostBackup: HostBackup, statusActor: Option[ActorRef] = None) extends Actor with ActorLogging {
class HostBackupActor(hostBackup: HostBackup, backupConfiguration: BackupConfiguration, statusActor: Option[ActorRef] = None) extends Actor with ActorLogging {
import scala.concurrent.duration._
......@@ -77,7 +77,7 @@ class HostBackupActor(hostBackup: HostBackup, statusActor: Option[ActorRef] = No
val mountActor = context.actorOf(SSHCommandActor.props(client))
context.watch(mountActor)
mountActor ! ExecuteCommand("mount 10.10.220.92:/mnt/OldTank/backupTesting /mnt/auto-backup")
mountActor ! ExecuteCommand(s"mount ${backupConfiguration.networkMountPath} ${backupConfiguration.localMountLocation}")
context.become(awaitingMountComplete(client, mountActor))
context.setReceiveTimeout(5.seconds)
case ReceiveTimeout =>
......
......@@ -2,10 +2,9 @@ package is.kow.backupcoordinator.actors
import java.util.concurrent.{Executors, TimeUnit}
import akka.actor.{Actor, ActorLogging, PoisonPill, Props}
import is.kow.backupcoordinator.actors.SSHCommandActor.{ExecuteCommand, ExecutionCompleted}
import akka.actor.{Actor, ActorLogging, Props}
import is.kow.backupcoordinator.actors.SSHCommandActor.{ExecuteCommand, ExecutionCompleted, ExecutionFailedException}
import net.schmizz.sshj.SSHClient
import net.schmizz.sshj.connection.channel.direct.Session
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor, Future}
import scala.io.Source
......@@ -17,13 +16,18 @@ object SSHCommandActor {
sealed trait SSHCommandActorMessages
case class ExecuteCommand(command: String) extends SSHCommandActorMessages
case class ExecuteCommand(
command: String,
environment: Map[String, String] = Map.empty[String, String]
) extends SSHCommandActorMessages
case class ExecutionCompleted(command: ExecuteCommand, stdOut: String, stdErr: String, exitValue: Integer) extends SSHCommandActorMessages
case class ExecutionFailedException(command: ExecuteCommand, stdOut: String, stdErr: String, exitValue: Integer) extends Exception
}
class SSHCommandActor(ssh: SSHClient) extends Actor with ActorLogging{
class SSHCommandActor(ssh: SSHClient) extends Actor with ActorLogging {
private val session = ssh.startSession()
......@@ -40,6 +44,7 @@ class SSHCommandActor(ssh: SSHClient) extends Actor with ActorLogging{
case e: ExecuteCommand =>
//This is really the only thing we're going to work on
log.debug(s"Starting to execute command || ${e.command} ||")
//TODO: also include environment
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
......@@ -53,6 +58,9 @@ class SSHCommandActor(ssh: SSHClient) extends Actor with ActorLogging{
val exitStatus = command.getExitStatus
//join on it, and then also get the exit status
log.debug("Command Execution completed!") //Has to be last thing!
if (exitStatus != 0) {
throw new ExecutionFailedException(e, stdOut, stdErr, exitStatus)
}
ExecutionCompleted(e, stdOut, stdErr, exitStatus)
}.onComplete {
case Success(ec) =>
......
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