Optionally, initialize site from WebAppInitializer.
This supports automatic site initialization on Gerrit server startup when Gerrit runs in a servlet container. Both creation of a new site and upgrade of an existing site are supported. This feature may be useful for such setups where Gerrit admins don't have direct access to the database and the file system of the server where Gerrit should be deployed and, therefore, cannot perform the init from their local machine prior to deploying Gerrit on such a server. It may also make deployment and testing in a local servlet container faster to setup as the init step could be skipped. The site initialization will be performed only if the 'gerrit.init' system property exists (the value of the property is not, used only the existence of the property matters). If the 'gerrit.site_path' system property is defined then the init is run for that site. The database connectivity, in that case, is defined in the etc/gerrit.config. If 'gerrit.site_path' is not defined then Gerrit will try to find an existing site by looking into the system_config table in the database defined via the 'jdbc/ReviewDb' JNDI property. If system_config table exists then the site_path from that table is used for initialization. Database connectivity is defined by the jdbc/ReviewDb JNDI property. Finally, if neither 'gerrit.site_path' property nor the system_config table exists, the 'gerrit.init_path' system property, if defined, will be used to determine the site path. Database connectivity, also for this case,is defined by the jdbc/ReviewDb JNDI property. Example 1: Prepare Tomcat so that a site is initialized at a given path using the H2 database (if the site doesn't exist yet) or using whatever database is defined in the etc/gerrit.config of that site: $ export CATALINA_OPTS='-Dgerrit.init -Dgerrit.site_path=/path/to/site' $ catalina.sh start Example 2: Prepare Tomcat so that an existing site with the path defined in the system_config table is initialized (upgraded) on Gerrit startup. The assumption is that the jdbc/ReviewDb JNDI property is defined in Tomcat: $ export CATALINA_OPTS='-Dgerrit.init' $ catalina.sh start Example 3: Assuming the database schema doesn't exist in the database defined via the jdbc/ReviewDb JNDI property, initialize a new site using that database and a given path: $ export CATALINA_OPTS='-Dgerrit.init -Dgerrit.init_path=/path/to/site' $ catalina.sh start Change-Id: Ic3e8c993087d2fbb38e14479a539dc62495ad908
This commit is contained in:

committed by
Shawn Pearce

parent
8e0f6d06fe
commit
e5fc90eb67
@@ -0,0 +1,87 @@
|
||||
// Copyright (C) 2013 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.httpd;
|
||||
|
||||
import com.google.gerrit.pgm.BaseInit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public final class SiteInitializer {
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(SiteInitializer.class);
|
||||
|
||||
final String sitePath;
|
||||
final String initPath;
|
||||
|
||||
SiteInitializer(String sitePath, String initPath) {
|
||||
this.sitePath = sitePath;
|
||||
this.initPath = initPath;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
try {
|
||||
|
||||
if (sitePath != null) {
|
||||
File site = new File(sitePath);
|
||||
log.info(String.format("Initializing site at %s",
|
||||
site.getAbsolutePath()));
|
||||
new BaseInit(site, false).run();
|
||||
return;
|
||||
}
|
||||
|
||||
Connection conn = connectToDb();
|
||||
try {
|
||||
File site = getSiteFromReviewDb(conn);
|
||||
if (site == null && initPath != null) {
|
||||
site = new File(initPath);
|
||||
}
|
||||
|
||||
if (site != null) {
|
||||
log.info(String.format("Initializing site at %s",
|
||||
site.getAbsolutePath()));
|
||||
new BaseInit(site, new ReviewDbDataSourceProvider(), false).run();
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Site init failed", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection connectToDb() throws SQLException {
|
||||
return new ReviewDbDataSourceProvider().get().getConnection();
|
||||
}
|
||||
|
||||
private File getSiteFromReviewDb(Connection conn) {
|
||||
try {
|
||||
ResultSet rs = conn.createStatement().executeQuery(
|
||||
"select site_path from system_config");
|
||||
if (rs.next()) {
|
||||
return new File(rs.getString(1));
|
||||
}
|
||||
return null;
|
||||
} catch (SQLException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -115,6 +115,10 @@ public class WebAppInitializer extends GuiceServletContextListener
|
||||
sitePath = new File(path);
|
||||
}
|
||||
|
||||
if (System.getProperty("gerrit.init") != null) {
|
||||
new SiteInitializer(path, System.getProperty("gerrit.init_path")).init();
|
||||
}
|
||||
|
||||
try {
|
||||
dbInjector = createDbInjector();
|
||||
} catch (CreationException ce) {
|
||||
|
Reference in New Issue
Block a user