-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kotlin DSL support #317
Comments
Can anyone give an example of how to use this plugin from .kts at least without DSL? |
There are two points about making this plugin work with the Kotlin DSL, or making it usable from another plugin implemented in Java or Kotlin:
|
At the moment i created few extension that makes a little easier to use the plugin: fun Service.runSessions(action: RunHandler.() -> Unit) =
run(delegateClosureOf(action))
fun RunHandler.session(vararg remotes: Remote, action: SessionHandler.() -> Unit) =
session(*remotes, delegateClosureOf(action))
fun SessionHandler.put(from: Any, into: Any) =
put(hashMapOf("from" to from, "into" to into)) So that i can do something like: val raspiLocal = remotes.create("raspi-local") {
val raspiPassword: String by project
host = "192.168.1.101"
user = "pi"
password = raspiPassword
}
ssh.runSessions {
session(raspiLocal) {
put(installDist.destinationDir, "~/workspace/dragalia-telegram-bot")
execute("sudo service dragalia-telegram-bot restart")
}
} It works like a charm! https://github.com/lamba92/dragalia-telegram-bot/blob/master/build.gradle.kts |
Without extensions:
|
An alternative to a whacky Kotlin implementation could be that one isolates the functionality of the SSH plugin into a separate custom task, written in Groovy, and then register / wire that task from the Kotlin build script side. This works best when the custom task resides in plugins {
groovy
`kotlin-dsl`
}
...
tasks.named<GroovyCompile>("compileGroovy") {
classpath = sourceSets.main.get().compileClasspath
}
tasks.named<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>("compileKotlin") {
classpath += files(conventionOf(sourceSets.main.get()).getPlugin(GroovySourceSet::class.java).groovy.classesDirectory)
} (Reference: https://docs.gradle.org/6.1-rc-1/release-notes.html#compilation-order) |
I just came across https://github.com/steklopod/gradle-ssh-plugin |
This references this plugin, so no, it's not an alternative. |
I write this code, and it works successfully. //Deploy Function
tasks.create(name = "deployBootJar") {
group = "deploy"
dependsOn(":bootJar")
val artifactId = project.name
val version = project.version
val port = 8206
val serverAppPath = "/docker/fs/apps"
val host = "host"
val user = "user"
val password = "password"
val myServer = Remote(mapOf<String, String>("host" to host,
"user" to user,
"password" to password))
doLast {
ssh.run(delegateClosureOf<RunHandler> {
session(myServer, delegateClosureOf<SessionHandler> {
execute("mkdir -p ${serverAppPath}/${artifactId}")
execute("""
cat>${serverAppPath}/${artifactId}/docker-compose.yml<<EOF
version: '3.9'
services:
$artifactId:
image: jarboot17:GA
container_name: $artifactId
restart: always
volumes:
- ${serverAppPath}/${artifactId}/${artifactId}-${version}.jar:/app.jar
ports:
- ${port}:${port}
EOF
""".trimIndent())
put(hashMapOf(
"from" to File("${projectDir}/build/libs/${artifactId}-${version}.jar"),
"into" to "${serverAppPath}/${artifactId}/${artifactId}-${version}.jar"))
execute("cd ${serverAppPath}/${artifactId};docker-compose up -d $artifactId")
})
})
}
} |
How would I configure the ssh settings ? nothing seems to work oO if have tried:
and
|
Had the same issue for trying to set knownHosts for GlobalSettings through closure (/delegateOf). Instead I just added knownHosts property to the Remote(mapOf()) function and it works like a charm. |
Here is the code that worked for me. With a bit better formatting. tasks.create("deploy") {
val myServer = Remote(
mutableMapOf<String, Any>(
"host" to "192.168.59.7",
"user" to "shalva",
"password" to "yourPassword",
"knownHosts" to AllowAnyHosts.instance
)
)
doLast {
ssh.run(delegateClosureOf<RunHandler> {
session(
myServer,
delegateClosureOf<SessionHandler> {
put(
hashMapOf(
"from" to "${project.rootDir}/build/bin/linuxArm64/debugExecutable/untitled.kexe",
"into" to "/home/shalva/" // Dont use ~, it does not work
)
)
})
})
}
} |
Currently, the configuration is done via Groovy delegates. This maps badly to Kotlin.
Is there any way to configure the plugin via the Kotlin DSL? Of course, a direct support of the Kotlin DSL would be a dream :-)
The text was updated successfully, but these errors were encountered: