From 3ae80029b20ed84728b40065a32f67f01f259f44 Mon Sep 17 00:00:00 2001 From: Ykus Date: Sun, 30 Jul 2017 18:29:33 +0200 Subject: [PATCH] * [JENKINS-45781] Remote ssh host management and configurable host name (or alias) --- .../hudson/plugins/CredentialsSSHSite.java | 20 ++++++++--- .../jvnet/hudson/plugins/SSHBuildWrapper.java | 5 +-- .../plugins/SSHBuildWrapper/global.jelly | 3 ++ src/main/webapp/help-name.html | 1 + src/main/webapp/help-name_ja.html | 1 + .../hudson/plugins/SSHBuildWrapperTest.java | 12 +++++++ .../checkNameForSSHConfiguration/config.xml | 16 +++++++++ ...g.jvnet.hudson.plugins.SSHBuildWrapper.xml | 33 +++++++++++++++++++ 8 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 src/main/webapp/help-name.html create mode 100644 src/main/webapp/help-name_ja.html create mode 100644 src/test/resources/org/jvnet/hudson/plugins/SSHBuildWrapperTest/checkNameForSSHConfiguration/config.xml create mode 100644 src/test/resources/org/jvnet/hudson/plugins/SSHBuildWrapperTest/checkNameForSSHConfiguration/org.jvnet.hudson.plugins.SSHBuildWrapper.xml diff --git a/src/main/java/org/jvnet/hudson/plugins/CredentialsSSHSite.java b/src/main/java/org/jvnet/hudson/plugins/CredentialsSSHSite.java index 9d2b49f..9d1624b 100755 --- a/src/main/java/org/jvnet/hudson/plugins/CredentialsSSHSite.java +++ b/src/main/java/org/jvnet/hudson/plugins/CredentialsSSHSite.java @@ -41,6 +41,7 @@ import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; +import hudson.Util; public class CredentialsSSHSite { @@ -49,6 +50,8 @@ public static class LegacySSHSite extends CredentialsSSHSite { transient String keyfile; } + String name; + String hostname; String username; int port; @@ -67,11 +70,12 @@ private CredentialsSSHSite() { } @DataBoundConstructor - public CredentialsSSHSite(final String hostname, final String port, final String credentialId, + public CredentialsSSHSite(final String name, final String hostname, final String port, final String credentialId, final String serverAliveInterval, final String timeout) { final StandardUsernameCredentials credentials = lookupCredentialsById(credentialId); this.username = credentials.getUsername(); + this.name = Util.fixEmptyAndTrim(name); this.hostname = hostname; try { this.port = Integer.parseInt(port); @@ -190,7 +194,7 @@ legacy.username, new FileOnMasterPrivateKeySource(legacy.keyfile), legacy.passwo credentialId = existingCredentials.getId(); } - return new CredentialsSSHSite(legacy.hostname, String.valueOf(legacy.port), credentialId, + return new CredentialsSSHSite(null, legacy.hostname, String.valueOf(legacy.port), credentialId, String.valueOf(legacy.serverAliveInterval), String.valueOf(legacy.timeout)); } @@ -297,7 +301,7 @@ public String getServerAliveInterval() { /** Returns "identifier" for ssh site: username@hostname:port */ public String getSitename() { - return username + "@" + hostname + ":" + port; + return (Util.fixEmptyAndTrim(name) == null) ? username + "@" + hostname + ":" + port : name + " - " + username + "@" + hostname + ":" + port; } public String getTimeout() { @@ -327,6 +331,14 @@ public void setPort(final String port) { } } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = Util.fixEmptyAndTrim(name); + } + @DataBoundSetter public void setPty(final Boolean pty) { this.pty = pty; @@ -363,7 +375,7 @@ public void testConnection(final PrintStream logger) throws JSchException, IOExc @Override public String toString() { - return "SSHSite [username=" + username + ", hostname=" + hostname + ",port=" + port + ", credentialId=" + return "SSHSite [name=" + name + ", username=" + username + ", hostname=" + hostname + ",port=" + port + ", credentialId=" + credentialId + ", pty=" + pty + "]"; } diff --git a/src/main/java/org/jvnet/hudson/plugins/SSHBuildWrapper.java b/src/main/java/org/jvnet/hudson/plugins/SSHBuildWrapper.java index 349a08c..a892171 100644 --- a/src/main/java/org/jvnet/hudson/plugins/SSHBuildWrapper.java +++ b/src/main/java/org/jvnet/hudson/plugins/SSHBuildWrapper.java @@ -218,11 +218,12 @@ public ListBoxModel doFillCredentialIdItems(final @AncestorInPath ItemGroup c /** * Validates ssh connection - currently this is executed on master node */ - public FormValidation doLoginCheck(@QueryParameter("hostname") String hostname, + public FormValidation doLoginCheck(@QueryParameter("name") String name, @QueryParameter("hostname") String hostname, @QueryParameter("port") String port, @QueryParameter("credentialId") String credentialId, @QueryParameter("serverAliveInterval") String serverAliveInterval, @QueryParameter("timeout") String timeout) { + name = Util.fixEmptyAndTrim(name); hostname = Util.fixEmpty(hostname); port = Util.fixEmpty(port); credentialId = Util.fixEmpty(credentialId); @@ -231,7 +232,7 @@ public FormValidation doLoginCheck(@QueryParameter("hostname") String hostname, return FormValidation.warning("Please fill host, port and credentials."); } - final CredentialsSSHSite site = new CredentialsSSHSite(hostname, port, credentialId, + final CredentialsSSHSite site = new CredentialsSSHSite(name, hostname, port, credentialId, serverAliveInterval, timeout); try { try { diff --git a/src/main/resources/org/jvnet/hudson/plugins/SSHBuildWrapper/global.jelly b/src/main/resources/org/jvnet/hudson/plugins/SSHBuildWrapper/global.jelly index dad66b1..a945bc3 100644 --- a/src/main/resources/org/jvnet/hudson/plugins/SSHBuildWrapper/global.jelly +++ b/src/main/resources/org/jvnet/hudson/plugins/SSHBuildWrapper/global.jelly @@ -5,6 +5,9 @@ description="${%SSH sites that projects will want to connect}"> + + + diff --git a/src/main/webapp/help-name.html b/src/main/webapp/help-name.html new file mode 100644 index 0000000..7cea843 --- /dev/null +++ b/src/main/webapp/help-name.html @@ -0,0 +1 @@ +
SSH name/alias
diff --git a/src/main/webapp/help-name_ja.html b/src/main/webapp/help-name_ja.html new file mode 100644 index 0000000..e0e8be7 --- /dev/null +++ b/src/main/webapp/help-name_ja.html @@ -0,0 +1 @@ +
SSH名/エイリアス
diff --git a/src/test/java/org/jvnet/hudson/plugins/SSHBuildWrapperTest.java b/src/test/java/org/jvnet/hudson/plugins/SSHBuildWrapperTest.java index f763702..e8c7b92 100644 --- a/src/test/java/org/jvnet/hudson/plugins/SSHBuildWrapperTest.java +++ b/src/test/java/org/jvnet/hudson/plugins/SSHBuildWrapperTest.java @@ -82,4 +82,16 @@ public void loadAllThreePossibleSiteConfigs() throws Exception { } } } + + @LocalData + @Test + public void checkNameForSSHConfiguration() throws Exception { + SSHBuildWrapper.DescriptorImpl desc = new SSHBuildWrapper.DescriptorImpl(); + assertEquals(3, desc.getSites().length); + + CredentialsSSHSite[] site = desc.getSites(); + assertEquals("SSH worker1 - root@worker1.example.com:22", site[0].getSitename()); + assertEquals("root@worker2.example.com:22", site[1].getSitename()); + assertEquals("root@worker3.example.com:22", site[2].getSitename()); + } } diff --git a/src/test/resources/org/jvnet/hudson/plugins/SSHBuildWrapperTest/checkNameForSSHConfiguration/config.xml b/src/test/resources/org/jvnet/hudson/plugins/SSHBuildWrapperTest/checkNameForSSHConfiguration/config.xml new file mode 100644 index 0000000..a85cd17 --- /dev/null +++ b/src/test/resources/org/jvnet/hudson/plugins/SSHBuildWrapperTest/checkNameForSSHConfiguration/config.xml @@ -0,0 +1,16 @@ + + + 1.609.3 + 1 + NORMAL + true + + hudson.model.Hudson.Read:anonymous + hudson.model.Item.Read:anonymous + + + + + 5 + + diff --git a/src/test/resources/org/jvnet/hudson/plugins/SSHBuildWrapperTest/checkNameForSSHConfiguration/org.jvnet.hudson.plugins.SSHBuildWrapper.xml b/src/test/resources/org/jvnet/hudson/plugins/SSHBuildWrapperTest/checkNameForSSHConfiguration/org.jvnet.hudson.plugins.SSHBuildWrapper.xml new file mode 100644 index 0000000..3fae72c --- /dev/null +++ b/src/test/resources/org/jvnet/hudson/plugins/SSHBuildWrapperTest/checkNameForSSHConfiguration/org.jvnet.hudson.plugins.SSHBuildWrapper.xml @@ -0,0 +1,33 @@ + + + + + SSH worker1 + worker1.example.com + 22 + root + ca0fcca1-8d34-4f6d-91d3-61a58a1ebb8a + 0 + false + + + + worker2.example.com + 22 + root + ca0fcca1-8d34-4f6d-91d3-61a58a1ebb8a + 0 + false + + + + worker3.example.com + 22 + root + ca0fcca1-8d34-4f6d-91d3-61a58a1ebb8a + 0 + false + + + +