SHARE
TWEET

Untitled

a guest Dec 31st, 2018 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. case class Server(
  2.     id: Option[String],
  3.     realmId: String,
  4.     groupId: String,
  5.     name: String,
  6.     ip: String,
  7.     tags: Option[List[String]],
  8.     comments: Option[String],
  9.     state: Option[String] = None,
  10.     auth_method: String = "password",
  11.     auth_user: String = "root",
  12.     auth_password: Option[String] = None,
  13.     auth_privateKey: Option[String]
  14. )
  15.  
  16. val server = Server(
  17.     id = Some("2389345923894"),
  18.     realmId = "testrealm",
  19.     groupId = "testgroup",
  20.     ip ="127.0.0.1:2211",
  21.     name = "test2211",
  22.     comments = Some("Test ::2211"),
  23.     tags = Some(List( "test")),
  24.     auth_method = "password",
  25.     auth_user = "root",
  26.     auth_password = Some("root"),
  27.     state = None
  28. )
  29.  
  30.  
  31.  
  32. object ServersTerminalHelper {
  33.  
  34.     private val rootPath = System.getProperty("user.dir")
  35.     val pemPath = (s:Server) => s"${rootPath}/${s.name}.exported.pem"
  36.  
  37.     def savePem(s: Server): Option[Server] =
  38.         if (s.auth_method == "pem" && s.auth_privateKey.isDefined) {
  39.             import sys.process._
  40.             val pw = new java.io.PrintWriter(new java.io.File(pemPath(s)))
  41.             pw.write(s.auth_privateKey.get.replace("\\n", "\n"))
  42.             pw.close
  43.  
  44.             println("PEM saved " + pemPath(s))
  45.             (s"chmod 0600 ${pemPath(s)}"!)
  46.             Some(s)
  47.         } else {
  48.             None
  49.         }
  50.    
  51.     def alias(s: Server): Option[String] = {
  52.         if (s.auth_method == "pem") {
  53.            
  54.             val sshOpts = "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "
  55.             Some(s"alias ssh-${s.name}='ssh ${sshOpts} -i ${pemPath(s)} ${s.auth_user}@${s.ip}'")
  56.         } else {
  57.             None
  58.         }
  59.     }
  60.  
  61.     def saveAll(list: List[Server]) = {
  62.         println(list.flatMap(savePem).flatMap(alias).mkString("\n"))
  63.     }
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top