Is it possible with the Gradle Kotlin DSL to run a...
# gradle
n
Is it possible with the Gradle Kotlin DSL to run a process via SSH?
d
Yes, using an
Exec
task.
👍 1
n
Looking to create a custom Gradle task that copies a Linux X64 binary (after being created by Konan) to a Linux server, run the binary on the server, and print out everything from stdout via SSH.
d
You'll probably want two tasks, one to do the copy and one to do the exec.
n
Would be much easier to automate the process if Gradle had built-in SshExec, SshCopy, and SshMove tasks.
Being driven crazy by the multiple OpenSSH password dialogs that popup when running the custom task.
Adding a public key to the server fixed the popup dialogs problem.
Running the Kotlin Native binary remotely via a custom task combined with other custom tasks, which is tedious but it works nicely simple smile :
Copy code
// ...
kotlin {
    val username = properties["username"]
    val serverHost = properties["serverHost"]
    val serverDestDir = properties["serverDestDir"]
    // ...
    val compressLinuxX64ReleaseBinary by tasks.creating(Exec::class) {
        dependsOn("linkProm_manReleaseExecutableLinuxX64")
        val binaryDir = "$buildDir/bin/linuxX64/prom_manReleaseExecutable"
        val binaryFile = File("$binaryDir/prom_man.kexe")
        if (binaryFile.exists()) binaryFile.copyTo(target = File("$binaryDir/prom_man"), overwrite = true)
        commandLine("gzip", "-f", "-9", "$binaryDir/prom_man")
    }
    val copyLinuxX64ReleaseBinary by tasks.creating(Exec::class) {
        dependsOn(compressLinuxX64ReleaseBinary)
        val binaryDir = "$buildDir/bin/linuxX64/prom_manReleaseExecutable"
        commandLine("scp", "$binaryDir/prom_man.gz", "$username@$serverHost:$serverDestDir")
    }
    val decompressRemoteLinuxX64ReleaseBinary by tasks.creating(Exec::class) {
        dependsOn(copyLinuxX64ReleaseBinary)
        commandLine("ssh", "-t", "$username@$serverHost", "gunzip", "-f", "$serverDestDir/prom_man.gz")
    }
    val alterRemoteLinuxX64ReleaseBinary by tasks.creating(Exec::class) {
        dependsOn(decompressRemoteLinuxX64ReleaseBinary)
        commandLine("ssh", "-t", "$username@$serverHost", "chmod", "+x", "$serverDestDir/prom_man")
    }
    tasks.create<Exec>("runRemoteLinuxX64ReleaseBinary") {
        dependsOn(alterRemoteLinuxX64ReleaseBinary)
        commandLine("ssh", "-t", "$username@$serverHost", "$serverDestDir/prom_man",
            "<http://localhost:9090>")
    }
}