Line data Source code
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 : 15 : package com.google.gerrit.pgm.util; 16 : 17 : import static com.google.gerrit.server.config.GerritServerConfigModule.getSecureStoreClassName; 18 : import static com.google.inject.Stage.PRODUCTION; 19 : 20 : import com.google.gerrit.common.Die; 21 : import com.google.gerrit.lifecycle.LifecycleModule; 22 : import com.google.gerrit.metrics.DisabledMetricMaker; 23 : import com.google.gerrit.metrics.MetricMaker; 24 : import com.google.gerrit.metrics.dropwizard.DropWizardMetricMaker; 25 : import com.google.gerrit.server.LibModuleLoader; 26 : import com.google.gerrit.server.LibModuleType; 27 : import com.google.gerrit.server.ModuleOverloader; 28 : import com.google.gerrit.server.config.GerritRuntime; 29 : import com.google.gerrit.server.config.GerritServerConfigModule; 30 : import com.google.gerrit.server.config.SitePath; 31 : import com.google.gerrit.server.experiments.ConfigExperimentFeatures.ConfigExperimentFeaturesModule; 32 : import com.google.gerrit.server.git.GitRepositoryManagerModule; 33 : import com.google.gerrit.server.git.SystemReaderInstaller; 34 : import com.google.gerrit.server.schema.SchemaModule; 35 : import com.google.gerrit.server.securestore.SecureStoreClassName; 36 : import com.google.inject.AbstractModule; 37 : import com.google.inject.CreationException; 38 : import com.google.inject.Guice; 39 : import com.google.inject.Injector; 40 : import com.google.inject.Module; 41 : import com.google.inject.spi.Message; 42 : import com.google.inject.util.Providers; 43 : import java.nio.file.Files; 44 : import java.nio.file.Path; 45 : import java.nio.file.Paths; 46 : import java.util.ArrayList; 47 : import java.util.List; 48 : import org.kohsuke.args4j.Option; 49 : 50 : public abstract class SiteProgram extends AbstractProgram { 51 : @Option( 52 : name = "--site-path", 53 : aliases = {"-d"}, 54 : usage = "Local directory containing site data") 55 : void setSitePath(String path) { 56 15 : sitePath = Paths.get(path).normalize(); 57 15 : } 58 : 59 138 : private Path sitePath = Paths.get("."); 60 : 61 15 : protected SiteProgram() {} 62 : 63 138 : protected SiteProgram(Path sitePath) { 64 138 : this.sitePath = sitePath.normalize(); 65 138 : } 66 : 67 : /** Returns the site path specified on the command line. */ 68 : protected Path getSitePath() { 69 138 : return sitePath; 70 : } 71 : 72 : /** Ensures we are running inside of a valid site, otherwise throws a Die. */ 73 : protected void mustHaveValidSite() throws Die { 74 15 : if (!Files.exists(sitePath.resolve("etc").resolve("gerrit.config"))) { 75 0 : throw die("not a Gerrit site: '" + getSitePath() + "'\nPerhaps you need to run init first?"); 76 : } 77 15 : } 78 : 79 : /** Provides database connectivity and site path. */ 80 : protected Injector createDbInjector() { 81 15 : return createDbInjector(false); 82 : } 83 : 84 : /** Provides database connectivity and site path. */ 85 : protected Injector createDbInjector(boolean enableMetrics) { 86 15 : List<Module> modules = new ArrayList<>(); 87 : 88 15 : Module sitePathModule = 89 15 : new AbstractModule() { 90 : @Override 91 : protected void configure() { 92 15 : bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath()); 93 15 : bind(String.class) 94 15 : .annotatedWith(SecureStoreClassName.class) 95 15 : .toProvider(Providers.of(getConfiguredSecureStoreClass())); 96 15 : } 97 : }; 98 15 : modules.add(sitePathModule); 99 : 100 15 : if (enableMetrics) { 101 15 : modules.add(new DropWizardMetricMaker.ApiModule()); 102 : } else { 103 15 : modules.add( 104 15 : new AbstractModule() { 105 : @Override 106 : protected void configure() { 107 15 : bind(MetricMaker.class).to(DisabledMetricMaker.class); 108 15 : } 109 : }); 110 : } 111 : 112 15 : modules.add( 113 15 : new LifecycleModule() { 114 : @Override 115 : protected void configure() { 116 15 : listener().to(SystemReaderInstaller.class); 117 15 : } 118 : }); 119 15 : Module configModule = new GerritServerConfigModule(); 120 15 : modules.add(configModule); 121 15 : modules.add( 122 15 : new AbstractModule() { 123 : @Override 124 : protected void configure() { 125 15 : bind(GerritRuntime.class).toInstance(getGerritRuntime()); 126 15 : } 127 : }); 128 15 : Injector cfgInjector = Guice.createInjector(sitePathModule, configModule); 129 : 130 15 : modules.add(new SchemaModule()); 131 15 : modules.add(cfgInjector.getInstance(GitRepositoryManagerModule.class)); 132 : // The only implementation of experiments is available in all programs that can use 133 : // gerrit.config 134 15 : modules.add(new ConfigExperimentFeaturesModule()); 135 : 136 : try { 137 15 : return Guice.createInjector( 138 : PRODUCTION, 139 15 : ModuleOverloader.override( 140 15 : modules, LibModuleLoader.loadModules(cfgInjector, LibModuleType.DB_MODULE_TYPE))); 141 0 : } catch (CreationException ce) { 142 0 : Message first = ce.getErrorMessages().iterator().next(); 143 0 : Throwable why = first.getCause(); 144 : 145 0 : StringBuilder buf = new StringBuilder(); 146 0 : if (why != null) { 147 0 : buf.append(why.getMessage()); 148 0 : why = why.getCause(); 149 : } else { 150 0 : buf.append(first.getMessage()); 151 : } 152 0 : while (why != null) { 153 0 : buf.append("\n caused by "); 154 0 : buf.append(why.toString()); 155 0 : why = why.getCause(); 156 : } 157 0 : throw die(buf.toString(), new RuntimeException("DbInjector failed", ce)); 158 : } 159 : } 160 : 161 : /** Returns the current runtime used by this Gerrit program. */ 162 : protected GerritRuntime getGerritRuntime() { 163 15 : return GerritRuntime.BATCH; 164 : } 165 : 166 : protected final String getConfiguredSecureStoreClass() { 167 15 : return getSecureStoreClassName(sitePath); 168 : } 169 : }