From dcffa6a8e9f9649f639618648a8ca6c37c79cde7 Mon Sep 17 00:00:00 2001 From: Alex Earl Date: Tue, 24 Sep 2019 14:59:24 -0700 Subject: [PATCH 1/3] Add -version and -help --- src/main/java/hudson/remoting/Launcher.java | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/main/java/hudson/remoting/Launcher.java b/src/main/java/hudson/remoting/Launcher.java index 6a252af57..1436b5286 100644 --- a/src/main/java/hudson/remoting/Launcher.java +++ b/src/main/java/hudson/remoting/Launcher.java @@ -270,12 +270,31 @@ public void setNoCertificateCheck(boolean ignored) throws NoSuchAlgorithmExcepti depends = "-workDir") @Nonnull public boolean failIfWorkDirIsMissing = WorkDirManager.DEFAULT_FAIL_IF_WORKDIR_IS_MISSING; + + /** + * Shows help message and then exits + * @since 3.36 + */ + @Option(name="-help",usage="Show this help message") + public boolean showHelp = false; + + /** + * Shows version information and then exits + * @since 3.36 + */ + @Option(name="-version",usage="Shows the version of the remoting jar and then exits") + public boolean showVersion = false; + public static void main(String... args) throws Exception { Launcher launcher = new Launcher(); CmdLineParser parser = new CmdLineParser(launcher); try { parser.parseArgument(args); + if (launcher.showHelp && !launcher.showVersion) { + parser.printUsage(System.out); + return; + } launcher.run(); } catch (CmdLineException e) { System.err.println(e.getMessage()); @@ -288,6 +307,21 @@ public static void main(String... args) throws Exception { @edu.umd.cs.findbugs.annotations.SuppressFBWarnings("DM_DEFAULT_ENCODING") // log file, just like console output, should be in platform default encoding public void run() throws Exception { + if (showVersion) { + try(InputStream propertyInputStream = this.getClass().getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF")) { + if (propertyInputStream == null) { + System.err.println("No version information available"); + return; + } + Properties properties = new Properties(); + properties.load(propertyInputStream); + System.out.println("agent.jar version " + properties.getProperty("Version")); + } catch(IOException e) { + System.err.println("No version information available"); + } + return; + } + // Create and verify working directory and logging // TODO: The pass-through for the JNLP mode has been added in JENKINS-39817. But we still need to keep this parameter in // consideration for other modes (TcpServer, TcpClient, etc.) to retain the legacy behavior. From b226f930694d246f9832227db34b67e4a2512c7a Mon Sep 17 00:00:00 2001 From: Alex Earl Date: Tue, 29 Oct 2019 11:16:06 -0700 Subject: [PATCH 2/3] Update PR based on some feedback. --- src/main/java/hudson/remoting/Launcher.java | 14 +++------- src/main/java/hudson/remoting/Util.java | 27 ++++++++++++++++++++ src/main/java/hudson/remoting/jnlp/Main.java | 22 ++++++++++++++++ 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/main/java/hudson/remoting/Launcher.java b/src/main/java/hudson/remoting/Launcher.java index 1436b5286..bbaf7ced1 100644 --- a/src/main/java/hudson/remoting/Launcher.java +++ b/src/main/java/hudson/remoting/Launcher.java @@ -306,18 +306,10 @@ public static void main(String... args) throws Exception { @edu.umd.cs.findbugs.annotations.SuppressFBWarnings("DM_DEFAULT_ENCODING") // log file, just like console output, should be in platform default encoding public void run() throws Exception { - if (showVersion) { - try(InputStream propertyInputStream = this.getClass().getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF")) { - if (propertyInputStream == null) { - System.err.println("No version information available"); - return; - } - Properties properties = new Properties(); - properties.load(propertyInputStream); - System.out.println("agent.jar version " + properties.getProperty("Version")); - } catch(IOException e) { - System.err.println("No version information available"); + String version = Util.getVersion(); + if(version != null) { + System.out.println(version); } return; } diff --git a/src/main/java/hudson/remoting/Util.java b/src/main/java/hudson/remoting/Util.java index 92dde7405..09bb9f6b5 100644 --- a/src/main/java/hudson/remoting/Util.java +++ b/src/main/java/hudson/remoting/Util.java @@ -22,6 +22,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Base64; +import java.util.Enumeration; +import java.util.jar.JarFile; +import java.util.jar.Manifest; /** * Misc. I/O utilities @@ -160,4 +163,28 @@ static void mkdirs(@Nonnull File file) throws IOException { Files.createDirectories(PathUtils.fileToPath(file)); } + static public String getVersion() { + String version = "unknown"; + try { + Enumeration resEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME); + while (resEnum.hasMoreElements()) { + try { + URL url = (URL) resEnum.nextElement(); + InputStream is = url.openStream(); + if (is != null) { + Manifest manifest = new Manifest(is); + version = manifest.getMainAttributes().getValue("Version"); + if(version != null) { + break; + } + } + } catch (Exception e) { + System.out.println("Could not access manifest"); + } + } + } catch (IOException e) { + System.out.println("Could not access manifest"); + } + return version; + } } diff --git a/src/main/java/hudson/remoting/jnlp/Main.java b/src/main/java/hudson/remoting/jnlp/Main.java index 22d7d7d3e..b65ec8dd3 100644 --- a/src/main/java/hudson/remoting/jnlp/Main.java +++ b/src/main/java/hudson/remoting/jnlp/Main.java @@ -32,6 +32,7 @@ import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; +import hudson.remoting.Util; import org.jenkinsci.remoting.engine.WorkDirManager; import org.jenkinsci.remoting.util.PathUtils; import org.kohsuke.args4j.Option; @@ -202,6 +203,20 @@ public class Main { usage="Specify the remoting protocols to attempt when instanceIdentity is provided.") public List protocols = new ArrayList<>(); + /** + * Shows help message and then exits + * @since 3.36 + */ + @Option(name="-help",usage="Show this help message") + public boolean showHelp = false; + + /** + * Shows version information and then exits + * @since 3.36 + */ + @Option(name="-version",usage="Shows the version of the remoting jar and then exits") + public boolean showVersion = false; + /** * Two mandatory parameters: secret key, and agent name. */ @@ -239,6 +254,13 @@ public static void _main(String[] args) throws IOException, InterruptedException Main m = new Main(); CmdLineParser p = new CmdLineParser(m); p.parseArgument(args); + if (m.showHelp && !m.showVersion) { + p.printUsage(System.out); + return; + } else if(m.showVersion) { + System.out.println(Util.getVersion()); + return; + } if(m.args.size()!=2) { throw new CmdLineException(p, "two arguments required, but got " + m.args, null); } From 89328fd13d6bff9b017d1923d9eb3d2b499cb416 Mon Sep 17 00:00:00 2001 From: Alex Earl Date: Fri, 1 Nov 2019 11:48:54 -0700 Subject: [PATCH 3/3] Fix spotbugs and add try-with-resource --- src/main/java/hudson/remoting/Util.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/hudson/remoting/Util.java b/src/main/java/hudson/remoting/Util.java index 09bb9f6b5..8878f5725 100644 --- a/src/main/java/hudson/remoting/Util.java +++ b/src/main/java/hudson/remoting/Util.java @@ -168,9 +168,8 @@ static public String getVersion() { try { Enumeration resEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME); while (resEnum.hasMoreElements()) { - try { - URL url = (URL) resEnum.nextElement(); - InputStream is = url.openStream(); + URL url = (URL) resEnum.nextElement(); + try(InputStream is = url.openStream()) { if (is != null) { Manifest manifest = new Manifest(is); version = manifest.getMainAttributes().getValue("Version"); @@ -178,8 +177,6 @@ static public String getVersion() { break; } } - } catch (Exception e) { - System.out.println("Could not access manifest"); } } } catch (IOException e) {