Skip to content

Commit

Permalink
update janalyse, README fix, fill name in ssh options, sshDir and ssh…
Browse files Browse the repository at this point in the history
…KeyFile changes
  • Loading branch information
shmishleniy committed Feb 13, 2017
1 parent 360ec1e commit 3461e63
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ Add resolver to `project/plugins.sbt`:
resolvers += "JAnalyse Repository" at "http://www.janalyse.fr/repository/"
```

Add import to your project build file

```sbt
import deployssh.DeploySSH._
```

Enable plugin in your project.
For example in your `build.sbt`

Expand All @@ -57,14 +63,19 @@ You can use `.conf` files or set configs directly in project settings.

Allowed config fields:

* `name` - your server name. **Should be unique** in all loaded configs. (Duplication will be overriden)
* `name` - your server name. **Should be unique** in all loaded configs. (Duplication will be overridden)
* `host` - ip adress or hostname of the server
* `user` - ssh username. If missing or empty will be used your current user (`user.name`)
* `password`- ssh password. If missing or empty will be used ssh key
* `passphrase`- passphrase for ssh key. Remove or leave empty for ssh key without passphrase
* `port` - ssh port. If missing or empty will be used `22`
* `sshDir` - directory with you ssh keys. This directory should contain `id_rsa` or `id_dsa`. By default `user.name/.ssh` directory. This field is not allowed to be empty in `.conf` file. You should remove this field from config in `.conf` file to use default value.
* `sshKeyFile` - private key that will be used for ssh connection. By default will be used `id_rsa` or `id_dsa`. This field is not allowed to be empty in `.conf` file. You should remove this field from config in `.conf` file to use default value.
* `sshDir` - directory with you ssh keys.
This directory should contain `identity`, `id_dsa`, `id_ecdsa`, `id_ed25519` or `id_rsa` (the first matched file in the folder will be used for auth).
By default `user.name/.ssh` directory. This field is not allowed to be empty in `.conf` file.
You should remove this field from config in `.conf` file to use default value.
* `sshKeyFile` - add additional private key file name that will be used for ssh connection.
This file name will be added to head of the default list [`identity`, `id_dsa`, `id_ecdsa`, `id_ed25519`, `id_rsa`].
This field is not allowed to be empty in `.conf` file. You should remove this field from config in `.conf` file to use default value.

**`name` and `host` fields are mandatory**

Expand Down Expand Up @@ -95,7 +106,7 @@ servers = [
host = "169.254.0.2"
user = "ssh_test"
sshDir = "/tmp/.sshKeys"
sshKeyFile = "id_a12
sshKeyFile = "id_a12" #custom private key file name
}
]
```
Expand Down Expand Up @@ -124,7 +135,7 @@ lazy val myProject = project.enablePlugins(DeploySSH).settings(
)
)

val mySettings = Seq(
lazy val mySettings = Seq(
ServerConfig("server_5", "169.254.0.2")
)
```
Expand Down Expand Up @@ -159,9 +170,11 @@ or

Use `deploySshExecBefore` and `deploySshExecAfter` to execute any bash commands before and after deploy.

Any exeption in `deploySshExecBefore` and `deploySshExecAfter` will abort deploy for all servers.
Any exception in `deploySshExecBefore` and `deploySshExecAfter` will abort deploy for all servers.

To skip deploy only for current server you should wrap exception to `SkipDeployException`.

To skip deploy only for curent server you should wrap exeption to `SkipDeployException`.
For example stop and update and run your app, copy with scp needed application.conf depends on server name:

``` sbt
lazy val myProject = project.enablePlugins(DeploySSH).settings(
Expand All @@ -181,6 +194,9 @@ lazy val myProject = project.enablePlugins(DeploySSH).settings(
),
deploySshExecAfter ++= Seq(
(ssh: SSH) => {
ssh.scp { scp =>
scp.send(file(s"./src/main/resources/application-${ssh.options.name.get}.conf"), s"/home/app/application.conf")))
}
ssh.execOnce("nohup ./myApp/run & echo $! > ~/pid")
ssh.execOnce("touch pid")
val pid = ssh.execOnceAndTrim("cat pid")
Expand Down
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sbtPlugin := true

name := "sbt-deploy-ssh"
organization := "com.github.shmishleniy"
version := org.eclipse.jgit.api.Git.open(file(".")).describe().call()
version := "0.1.3"

publishMavenStyle := false
bintrayPublishSettings
Expand All @@ -15,7 +15,8 @@ bintrayOrganization in bintray := None
resolvers += "JAnalyse Repository" at "http://www.janalyse.fr/repository/"
libraryDependencies ++= Seq(
"com.typesafe" % "config" % "1.2.1",
"fr.janalyse" %% "janalyse-ssh" % "0.9.19"
"fr.janalyse" %% "janalyse-ssh" % "0.9.19",
"org.scalaz" %% "scalaz-core" % "7.2.8"
)

scalacOptions in Compile ++= Seq("-encoding","UTF-8","-target:jvm-1.7","-deprecation","-feature")
Expand Down
15 changes: 11 additions & 4 deletions src/main/scala/DeploySSH.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,23 @@ object DeploySSH extends AutoPlugin {
execBefore: Seq[(SSH) => Any],
execAfter: Seq[(SSH) => Any],
log: Logger): Unit = {
import java.io.File.{separator=>`/`}
val sshKey = serverConfig.sshDir.getOrElse(Properties.userHome+`/`+".ssh")+`/`+serverConfig.sshKeyFile
import java.io.File.separator
import java.nio.file.Paths

val sshDir = serverConfig.sshDir.map(Paths.get(_)).getOrElse(Paths.get(Properties.userHome + separator + ".ssh"))
val keyFilenames = (serverConfig.sshKeyFile.toList ++ SSHOptions.defaultPrivKeyFilenames).distinct
val identities = keyFilenames.map(f => sshDir.resolve(f)).map(p => SSHIdentity(p.toString))

implicit val ssh = SSH(
SSHOptions(serverConfig.host,
SSHOptions(
serverConfig.host,
serverConfig.user getOrElse Properties.userName,
serverConfig.password,
serverConfig.passphrase,
Some(serverConfig.name),
port = serverConfig.port.getOrElse(22),
identities = SSHIdentity(sshKey)::Nil)
identities = identities
)
)
val sftp = ssh.newSftp
log.info("Exec before deploy")
Expand Down

0 comments on commit 3461e63

Please sign in to comment.