Giter Club home page Giter Club logo

gradle-ssh's Introduction

gradle-ssh - SSH plugin for Gradle

Preface

This plugin is in very early stages, there will be bugs, missing features and breaking changes.

Primer

Simple plugin to execute fire and forget ssh commands as Gradle tasks.

It uses the sshj library to execute the commands.

Features

  • Ssh:
    • define and reuse remotes
    • define and reuse commands
    • execute commands on remotes

Usage

Apply the plugin

// build.gradle.kts
plugins {
    id("xyz.dussim.gradle-ssh").version("0.0.1")
}

Configure the remotes and commands

// build.gradle.kts
remotes {
    val firstRemote by publicKeyAuthenticated {
        host = "first-remote-host"
        user = "first-remote-username"
        // optionally, defaults to 22
        port = 40
    }

    val secondRemote by passwordAuthenticated {
        host = "second-remote-host"
        user = "second-remote-username"
        password = providers.environmentVariable("PASSWORD").get()
    }

    val bothRemotes by remoteCollection(firstRemote, secondRemote)
}

Configure remote executed commands

// build.gradle.kts
remoteExecCommands {
    val listFiles by command {
        command = "ls"
    }

    val helloWorld by command {
        command = "echo Hello, World!"
    }

    val bothCommands by commandCollection(listFiles, helloWorld)
}

Configure remote upload/download commands

// build.gradle.kts
remoteFileCommands {
    val downloadFile by downloadFileContent {
        remotePath = "directory/file"
        localFile = layout.projectDirectory.file("data/downloaded.txt").asFile
    }

    val upload by uploadFileContent {
        localFile = layout.projectDirectory.file("data/payload.txt").asFile
        remotePath = ""
    }

    val transfers by fileCommandCollection(
        upload,
        download,
    )
}

Configure tasks

// build.gradle.kts
tasks.register<SshRemoteExecutionTask>("remoteTasks") {
    remote = remotes["bothRemotes"]
    command = remoteExecCommands["bothCommands"]
    appendRemoteNameToLines = true
}

tasks.register<ScpRemoteFileTask>("fileCommands") {
    remote = remotes["firstRemote"]
    command = remoteFileCommands["transfers"]
}
Different ways to configure remotes and commands

Plugin supports typical options to create and configure objects in remoteExecCommands and remotes containers as well as some helper methods to lazily register them.

Fore people not familiar with Gradle's Kotlin DSL, here are some examples:

// build.gradle.kts

// top level declaration, this object is lazily created and configured
val bothCommands by remoteExecCommands.commandCollection(
    remoteExecCommands.named("listFiles"),
    remoteExecCommands.named("helloWorld")
)

// similar to the above, but for remotes, 
// all those remotes will be lazily created and configured

val remote1 by remotes.publicKeyAuthenticated {
    host = "host1"
    user = "user1"
}

val remote2 by remotes.publicKeyAuthenticated {
    host = "host2"
    user = "user2"
}

val bothRemotes by remotes.remoteCollection(remote1, remote2)

// those top level declarations can be used in tasks configuration

tasks.register<SshRemoteExecutionTask>("remoteTasks") {
    remote = bothRemotes
    command = bothCommands
    appendRemoteNameToLines = true
}

Run the task

./gradlew :remoteTasks
Output
> Task :remoteTasks
first-remote-username@first-remote-host:40|> Hello, World!
first-remote-username@first-remote-host:40|> file1.txt
first-remote-username@first-remote-host:40|> file2.sh
--------------------
second-remote-username@second-remote-host:22|> Hello, World!
second-remote-username@second-remote-host:22|> script.sh
second-remote-username@second-remote-host:22|> config.json

Future planned features

  • Ssh:
    • download/upload files via scp done, use ScpRemoteFileTask
    • download/upload files via sftp
  • Gradle:
    • implement missing features of sshj like custom key providers

Changelog

gradle-ssh 0.0.2

  • BREAKING CHANGES:
    • all existing models properties are not var anymore but Property<*>
    • some of the methods in RemoteContainer and RemoteExecCommandContainer were marked as @Deprecated and will be removed in 0.0.3. If you need that functionality it is not gone, it will just not be as nice to use, refer to PolymorphicDomainObjectContainer docs.
  • Implemented ScpRemoteFileTask and way to define upload download commands via respectively RemoteUploadCommand and RemoteDownloadCommand

gradle-ssh's People

Contributors

dussim avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.