int128 / gradle-ssh-plugin

Gradle SSH Plugin
https://gradle-ssh-plugin.github.io
Apache License 2.0
319 stars 60 forks source link

Session reusage #275

Open matthiasbalke opened 8 years ago

matthiasbalke commented 8 years ago

My gradle-ssh-plugin script is using a lot of small methods which performs some actions on a remote host. As far as I understand it each time I use the session() keyword a new SSH connections is established, right?

Is it possible to reuse an existing SSH session and passing it around? Maybe like this: My current version

def tomcatShutdown(def stage) {
    ssh.run {
        session(remotes.allRoles('tomcat', stage)) {

            execute "${sudo_prefix} echo 'Shutting down Tomcat ...'"
            execute "if ${sudo_prefix} test -f '${tomcat_home}/catalina.pid'; then ${sudo_prefix} shutdown.sh; else echo 'Tomcat not running.'; fi"
        }
    }
}

def tomcatStartup(def stage) {
    ssh.run {
        session(remotes.allRoles('tomcat', stage)) {
            execute "echo 'Starting Tomcat ...'"
            execute "${sudo_prefix} startup.sh"
        }
    }
}

Version reusing sessions

def tomcatShutdown(def stage, def establishedSession) {
    ssh.run(establishedSession) {
            execute "${sudo_prefix} echo 'Shutting down Tomcat ...'"
            execute "if ${sudo_prefix} test -f '${tomcat_home}/catalina.pid'; then ${sudo_prefix} shutdown.sh; else echo 'Tomcat not running.'; fi"
    }
}

def tomcatStartup(def stage, def establishedSession) {
    ssh.run(establishedSession) {
            execute "echo 'Starting Tomcat ...'"
            execute "${sudo_prefix} startup.sh"
    }
}