Skip to content
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

Open
Skyr opened this issue Nov 12, 2018 · 12 comments
Open

Kotlin DSL support #317

Skyr opened this issue Nov 12, 2018 · 12 comments

Comments

@Skyr
Copy link

Skyr commented Nov 12, 2018

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 :-)

@tanmatra
Copy link

Can anyone give an example of how to use this plugin from .kts at least without DSL?

@calvertdw
Copy link

calvertdw commented Feb 1, 2019

@eskatos
Copy link

eskatos commented Apr 19, 2019

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:

  • Closure taking methods should be overloaded with type-safe counterparts like Action<T>
  • DSL types shouldn't use Groovy traits as they confuse Java and Kotlin compilers and simply prevent using this plugin, e.g. Remote

See gradle/kotlin-dsl-samples#763

@lamba92
Copy link

lamba92 commented Oct 29, 2019

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

@Eng-Fouad
Copy link

Eng-Fouad commented May 6, 2020

Without extensions:

tasks.create(name = "deploy-war") {
    
    group = "deploy"

    val myServer = remotes.create("my-server") {
        host = "10.10.10.10"
        user = "root"
        password = "root"
    }
    
    /* or
    val myServer = org.hidetake.groovy.ssh.core.Remote(
            mapOf<String, String>("host" to "10.10.10.10",
                                  "user" to "root",
                                  "password" to "root"))
    */
    
    doLast {
        ssh.run(delegateClosureOf<org.hidetake.groovy.ssh.core.RunHandler> {
            session(myServer, delegateClosureOf<org.hidetake.groovy.ssh.session.SessionHandler> {
                put(hashMapOf("from" to tasks.getByName<War>("war").archiveFile.get().asFile, "into" to "/path/to/directory"))
            })
        })
    }
}

@realdadfish
Copy link

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 buildSrc and when Gradle 6.1 or later is used, so the Kotlin buildscript compilation can be made dependent on the output of the Groovy compiler, like so:

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)

@gaara87
Copy link

gaara87 commented Jan 1, 2021

I just came across https://github.com/steklopod/gradle-ssh-plugin

@realdadfish
Copy link

I just came across https://github.com/steklopod/gradle-ssh-plugin

This references this plugin, so no, it's not an alternative.

@natsufumij
Copy link

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")
            })
        })
    }
}

@aminabromand
Copy link

How would I configure the ssh settings ?

nothing seems to work oO
I want to change the known hosts file, but it will always user the default settings knownHosts: new File("${System.properties['user.home']}/.ssh/known_hosts"),

if have tried:

tasks.create("deploy") {

        val myServer = Remote(
                mapOf<String, String>(
                        "host" to "192.168.1.1",
                        "user" to "username",
                        "password" to "password"))


        doLast {

            ssh.run(delegateClosureOf<RunHandler> {
                settings(
                        delegateClosureOf<PerServiceSettings>{
                            mapOf(
                                    "knownHosts" to AllowAnyHosts.instance,

                                    )
                        }
                )
                session(
                        myServer,
                        delegateClosureOf<SessionHandler> {
                    put(
                            hashMapOf(
                                    "from" to "${project.rootDir}/deploy",
                                    "into" to "/home/username/"))
                })
            })
        }

    }

and

tasks.create("deploy") {

        val myServer = Remote(
                mapOf<String, String>(
                        "host" to "192.168.1.1",
                        "user" to "username",
                        "password" to "password"))
ssh.settings (
                    delegateClosureOf<GlobalSettings>{
                        mapOf(
                                "knownHosts" to AllowAnyHosts.instance,

                        )
                    }
            )


        doLast {

            ssh.run(delegateClosureOf<RunHandler> {

                session(
                        myServer,
                        delegateClosureOf<SessionHandler> {
                    put(
                            hashMapOf(
                                    "from" to "${project.rootDir}/deploy",
                                    "into" to "/home/username/"))
                })
            })
        }

    }

@hmmlopez
Copy link

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.

@shalva97
Copy link

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
                        )
                    )
                })
        })
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests