/hudson-core/src/main/java/hudson/cli/InstallPluginCommand.java

http://github.com/hudson/hudson · Java · 138 lines · 89 code · 15 blank · 34 comment · 16 complexity · c1670ec4d86a9f3775c2d5bfd007447b MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2010, Sun Microsystems, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.cli;
  25. import hudson.Extension;
  26. import hudson.FilePath;
  27. import hudson.model.Hudson;
  28. import hudson.model.UpdateSite;
  29. import hudson.model.UpdateSite.Data;
  30. import hudson.util.EditDistance;
  31. import org.kohsuke.args4j.Argument;
  32. import org.kohsuke.args4j.Option;
  33. import java.io.File;
  34. import java.net.URL;
  35. import java.net.MalformedURLException;
  36. import java.util.HashSet;
  37. import java.util.List;
  38. import java.util.ArrayList;
  39. import java.util.Set;
  40. /**
  41. * Installs a plugin either from a file, an URL, or from update center.
  42. *
  43. * @author Kohsuke Kawaguchi
  44. * @since 1.331
  45. */
  46. @Extension
  47. public class InstallPluginCommand extends CLICommand {
  48. public String getShortDescription() {
  49. return "Installs a plugin either from a file, an URL, or from update center";
  50. }
  51. @Argument(metaVar="SOURCE",required=true,usage="If this points to a local file, that file will be installed. " +
  52. "If this is an URL, Hudson downloads the URL and installs that as a plugin." +
  53. "Otherwise the name is assumed to be the short name of the plugin in the existing update center (like \"findbugs\")," +
  54. "and the plugin will be installed from the update center")
  55. public List<String> sources = new ArrayList<String>();
  56. @Option(name="-name",usage="If specified, the plugin will be installed as this short name (whereas normally the name is inferred from the source name automatically.)")
  57. public String name;
  58. @Option(name="-restart",usage="Restart Hudson upon successful installation")
  59. public boolean restart;
  60. protected int run() throws Exception {
  61. Hudson h = Hudson.getInstance();
  62. h.checkPermission(Hudson.ADMINISTER);
  63. for (String source : sources) {
  64. // is this a file?
  65. FilePath f = new FilePath(channel, source);
  66. if (f.exists()) {
  67. stdout.println(Messages.InstallPluginCommand_InstallingPluginFromLocalFile(f));
  68. if (name==null)
  69. name = f.getBaseName();
  70. f.copyTo(getTargetFile());
  71. continue;
  72. }
  73. // is this an URL?
  74. try {
  75. URL u = new URL(source);
  76. stdout.println(Messages.InstallPluginCommand_InstallingPluginFromUrl(u));
  77. if (name==null) {
  78. name = u.getPath();
  79. name = name.substring(name.indexOf('/')+1);
  80. name = name.substring(name.indexOf('\\')+1);
  81. int idx = name.lastIndexOf('.');
  82. if (idx>0) name = name.substring(0,idx);
  83. }
  84. getTargetFile().copyFrom(u);
  85. continue;
  86. } catch (MalformedURLException e) {
  87. // not an URL
  88. }
  89. // is this a plugin the update center?
  90. UpdateSite.Plugin p = h.getUpdateCenter().getPlugin(source);
  91. if (p!=null) {
  92. stdout.println(Messages.InstallPluginCommand_InstallingFromUpdateCenter(source));
  93. p.deploy().get();
  94. continue;
  95. }
  96. stdout.println(Messages.InstallPluginCommand_NotAValidSourceName(source));
  97. if (!source.contains(".") && !source.contains(":") && !source.contains("/") && !source.contains("\\")) {
  98. // looks like a short plugin name. Why did we fail to find it in the update center?
  99. if (h.getUpdateCenter().getSites().isEmpty()) {
  100. stdout.println(Messages.InstallPluginCommand_NoUpdateCenterDefined());
  101. } else {
  102. Set<String> candidates = new HashSet<String>();
  103. for (UpdateSite s : h.getUpdateCenter().getSites()) {
  104. Data dt = s.getData();
  105. if (dt==null) {
  106. stdout.println(Messages.InstallPluginCommand_NoUpdateDataRetrieved(s.getUrl()));
  107. } else {
  108. candidates.addAll(dt.plugins.keySet());
  109. }
  110. }
  111. stdout.println(Messages.InstallPluginCommand_DidYouMean(source,EditDistance.findNearest(source,candidates)));
  112. }
  113. }
  114. return 1;
  115. }
  116. if (restart)
  117. h.restart();
  118. return 0; // all success
  119. }
  120. private FilePath getTargetFile() {
  121. return new FilePath(new File(Hudson.getInstance().getPluginManager().rootDir,name+".hpi"));
  122. }
  123. }