Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case class Server(
- id: Option[String],
- realmId: String,
- groupId: String,
- name: String,
- ip: String,
- tags: Option[List[String]],
- comments: Option[String],
- state: Option[String] = None,
- auth_method: String = "password",
- auth_user: String = "root",
- auth_password: Option[String] = None,
- auth_privateKey: Option[String]
- )
- val server = Server(
- id = Some("2389345923894"),
- realmId = "testrealm",
- groupId = "testgroup",
- ip ="127.0.0.1:2211",
- name = "test2211",
- comments = Some("Test ::2211"),
- tags = Some(List( "test")),
- auth_method = "password",
- auth_user = "root",
- auth_password = Some("root"),
- state = None
- )
- object ServersTerminalHelper {
- private val rootPath = System.getProperty("user.dir")
- val pemPath = (s:Server) => s"${rootPath}/${s.name}.exported.pem"
- def savePem(s: Server): Option[Server] =
- if (s.auth_method == "pem" && s.auth_privateKey.isDefined) {
- import sys.process._
- val pw = new java.io.PrintWriter(new java.io.File(pemPath(s)))
- pw.write(s.auth_privateKey.get.replace("\\n", "\n"))
- pw.close
- println("PEM saved " + pemPath(s))
- (s"chmod 0600 ${pemPath(s)}"!)
- Some(s)
- } else {
- None
- }
- def alias(s: Server): Option[String] = {
- if (s.auth_method == "pem") {
- val sshOpts = "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "
- Some(s"alias ssh-${s.name}='ssh ${sshOpts} -i ${pemPath(s)} ${s.auth_user}@${s.ip}'")
- } else {
- None
- }
- }
- def saveAll(list: List[Server]) = {
- println(list.flatMap(savePem).flatMap(alias).mkString("\n"))
- }
- }
Add Comment
Please, Sign In to add comment