PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/java/com/google/gerrit/pgm/Init.java

https://gitlab.com/chenfengxu/gerrit
Java | 257 lines | 204 code | 37 blank | 16 comment | 20 complexity | 97eaa49100719ccc930a3075f58b8389 MD5 | raw file
  1. // Copyright (C) 2009 The Android Open Source Project
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package com.google.gerrit.pgm;
  15. import com.google.common.base.Joiner;
  16. import com.google.common.collect.Sets;
  17. import com.google.gerrit.common.IoUtil;
  18. import com.google.gerrit.common.PageLinks;
  19. import com.google.gerrit.common.PluginData;
  20. import com.google.gerrit.pgm.init.BaseInit;
  21. import com.google.gerrit.pgm.init.Browser;
  22. import com.google.gerrit.pgm.init.InitPlugins;
  23. import com.google.gerrit.pgm.init.api.ConsoleUI;
  24. import com.google.gerrit.pgm.util.ErrorLogFile;
  25. import com.google.gerrit.server.config.GerritServerConfigModule;
  26. import com.google.gerrit.server.config.SitePath;
  27. import com.google.gerrit.server.securestore.SecureStoreClassName;
  28. import com.google.gerrit.server.util.HostPlatform;
  29. import com.google.inject.AbstractModule;
  30. import com.google.inject.Guice;
  31. import com.google.inject.Inject;
  32. import com.google.inject.Module;
  33. import com.google.inject.util.Providers;
  34. import java.io.IOException;
  35. import java.nio.file.Path;
  36. import java.util.ArrayList;
  37. import java.util.Collections;
  38. import java.util.List;
  39. import java.util.Set;
  40. import org.kohsuke.args4j.Option;
  41. /** Initialize a new Gerrit installation. */
  42. public class Init extends BaseInit {
  43. @Option(
  44. name = "--batch",
  45. aliases = {"-b"},
  46. usage = "Batch mode; skip interactive prompting"
  47. )
  48. private boolean batchMode;
  49. @Option(name = "--delete-caches", usage = "Delete all persistent caches without asking")
  50. private boolean deleteCaches;
  51. @Option(name = "--no-auto-start", usage = "Don't automatically start daemon after init")
  52. private boolean noAutoStart;
  53. @Option(name = "--skip-plugins", usage = "Don't install plugins")
  54. private boolean skipPlugins;
  55. @Option(name = "--list-plugins", usage = "List available plugins")
  56. private boolean listPlugins;
  57. @Option(name = "--install-plugin", usage = "Install given plugin without asking")
  58. private List<String> installPlugins;
  59. @Option(name = "--install-all-plugins", usage = "Install all plugins from war without asking")
  60. private boolean installAllPlugins;
  61. @Option(
  62. name = "--secure-store-lib",
  63. usage = "Path to jar providing SecureStore implementation class"
  64. )
  65. private String secureStoreLib;
  66. @Option(name = "--dev", usage = "Setup site with default options suitable for developers")
  67. private boolean dev;
  68. @Option(name = "--skip-all-downloads", usage = "Don't download libraries")
  69. private boolean skipAllDownloads;
  70. @Option(name = "--skip-download", usage = "Don't download given library")
  71. private List<String> skippedDownloads;
  72. @Inject Browser browser;
  73. public Init() {
  74. super(new WarDistribution(), null);
  75. }
  76. public Init(Path sitePath) {
  77. super(sitePath, true, true, new WarDistribution(), null);
  78. batchMode = true;
  79. noAutoStart = true;
  80. }
  81. @Override
  82. protected boolean beforeInit(SiteInit init) throws Exception {
  83. ErrorLogFile.errorOnlyConsole();
  84. if (!skipPlugins) {
  85. final List<PluginData> plugins =
  86. InitPlugins.listPluginsAndRemoveTempFiles(init.site, pluginsDistribution);
  87. ConsoleUI ui = ConsoleUI.getInstance(false);
  88. if (installAllPlugins && !nullOrEmpty(installPlugins)) {
  89. ui.message("Cannot use --install-plugin together with --install-all-plugins.\n");
  90. return true;
  91. }
  92. verifyInstallPluginList(ui, plugins);
  93. if (listPlugins) {
  94. if (!plugins.isEmpty()) {
  95. ui.message("Available plugins:\n");
  96. for (PluginData plugin : plugins) {
  97. ui.message(" * %s version %s\n", plugin.name, plugin.version);
  98. }
  99. } else {
  100. ui.message("No plugins found.\n");
  101. }
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. @Override
  108. protected void afterInit(SiteRun run) throws Exception {
  109. List<Module> modules = new ArrayList<>();
  110. modules.add(
  111. new AbstractModule() {
  112. @Override
  113. protected void configure() {
  114. bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
  115. bind(Browser.class);
  116. bind(String.class)
  117. .annotatedWith(SecureStoreClassName.class)
  118. .toProvider(Providers.of(getConfiguredSecureStoreClass()));
  119. }
  120. });
  121. modules.add(new GerritServerConfigModule());
  122. Guice.createInjector(modules).injectMembers(this);
  123. start(run);
  124. }
  125. @Override
  126. protected List<String> getInstallPlugins() {
  127. return installPlugins;
  128. }
  129. @Override
  130. protected boolean installAllPlugins() {
  131. return installAllPlugins;
  132. }
  133. @Override
  134. protected ConsoleUI getConsoleUI() {
  135. return ConsoleUI.getInstance(batchMode);
  136. }
  137. @Override
  138. protected boolean getAutoStart() {
  139. return !noAutoStart;
  140. }
  141. @Override
  142. protected boolean getDeleteCaches() {
  143. return deleteCaches;
  144. }
  145. @Override
  146. protected boolean skipPlugins() {
  147. return skipPlugins;
  148. }
  149. @Override
  150. protected boolean isDev() {
  151. return dev;
  152. }
  153. @Override
  154. protected boolean skipAllDownloads() {
  155. return skipAllDownloads;
  156. }
  157. @Override
  158. protected List<String> getSkippedDownloads() {
  159. return skippedDownloads != null ? skippedDownloads : Collections.<String>emptyList();
  160. }
  161. @Override
  162. protected String getSecureStoreLib() {
  163. return secureStoreLib;
  164. }
  165. void start(SiteRun run) throws Exception {
  166. if (run.flags.autoStart) {
  167. if (HostPlatform.isWin32()) {
  168. System.err.println("Automatic startup not supported on Win32.");
  169. } else {
  170. startDaemon(run);
  171. if (!run.ui.isBatch()) {
  172. browser.open(PageLinks.ADMIN_PROJECTS);
  173. }
  174. }
  175. }
  176. }
  177. void startDaemon(SiteRun run) {
  178. String[] argv = {run.site.gerrit_sh.toAbsolutePath().toString(), "start"};
  179. Process proc;
  180. try {
  181. System.err.println("Executing " + argv[0] + " " + argv[1]);
  182. proc = Runtime.getRuntime().exec(argv);
  183. } catch (IOException e) {
  184. System.err.println("error: cannot start Gerrit: " + e.getMessage());
  185. return;
  186. }
  187. try {
  188. proc.getOutputStream().close();
  189. } catch (IOException e) {
  190. // Ignored
  191. }
  192. IoUtil.copyWithThread(proc.getInputStream(), System.err);
  193. IoUtil.copyWithThread(proc.getErrorStream(), System.err);
  194. for (; ; ) {
  195. try {
  196. int rc = proc.waitFor();
  197. if (rc != 0) {
  198. System.err.println("error: cannot start Gerrit: exit status " + rc);
  199. }
  200. break;
  201. } catch (InterruptedException e) {
  202. // retry
  203. }
  204. }
  205. }
  206. private void verifyInstallPluginList(ConsoleUI ui, List<PluginData> plugins) {
  207. if (nullOrEmpty(installPlugins) || nullOrEmpty(plugins)) {
  208. return;
  209. }
  210. Set<String> missing = Sets.newHashSet(installPlugins);
  211. plugins.stream().forEach(p -> missing.remove(p.name));
  212. if (!missing.isEmpty()) {
  213. ui.message("Cannot find plugin(s): %s\n", Joiner.on(", ").join(missing));
  214. listPlugins = true;
  215. }
  216. }
  217. private static boolean nullOrEmpty(List<?> list) {
  218. return list == null || list.isEmpty();
  219. }
  220. }