Move configuration files under $site_path/etc

Most applications store their configuration files below some sort of
etc directory within the application's "home place".  We should do
the same thing given that we have quite a few top level items in our
$site_path directory (db, cache, logs, static, etc).

Change-Id: Ia38ddab0174433acb59f271e33f32b5061121ea1
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2009-11-17 15:43:18 -08:00
parent 2fe738b4ad
commit 3cf7fbdf16
9 changed files with 100 additions and 41 deletions

View File

@@ -55,8 +55,8 @@ abstract class GitWebCssServlet extends HttpServlet {
} }
@Override @Override
protected File path(File sitePath, GitWebConfig gitWebConfig) { protected File path(File etc, GitWebConfig gitWebConfig) {
return new File(sitePath, "GerritSite.css"); return new File(etc, "GerritSite.css");
} }
} }
@@ -69,7 +69,7 @@ abstract class GitWebCssServlet extends HttpServlet {
} }
@Override @Override
protected File path(File sitePath, GitWebConfig gitWebConfig) { protected File path(File etc, GitWebConfig gitWebConfig) {
return gitWebConfig.getGitwebCSS(); return gitWebConfig.getGitwebCSS();
} }
} }
@@ -80,7 +80,7 @@ abstract class GitWebCssServlet extends HttpServlet {
GitWebCssServlet(@SitePath final File sitePath, GitWebCssServlet(@SitePath final File sitePath,
final GitWebConfig gitWebConfig) throws IOException { final GitWebConfig gitWebConfig) throws IOException {
final File src = path(sitePath, gitWebConfig); final File src = path(new File(sitePath, "etc"), gitWebConfig);
final File dir = src.getParentFile(); final File dir = src.getParentFile();
final String name = src.getName(); final String name = src.getName();
final String raw = HtmlDomUtil.readFile(dir, name); final String raw = HtmlDomUtil.readFile(dir, name);

View File

@@ -133,6 +133,7 @@ class GitWebServlet extends HttpServlet {
_env.set("GIT_DIR", "."); _env.set("GIT_DIR", ".");
_env.set("GITWEB_CONFIG", myconf.getAbsolutePath()); _env.set("GITWEB_CONFIG", myconf.getAbsolutePath());
final File etc = new File(sitePath, "etc");
final PrintWriter p = new PrintWriter(new FileWriter(myconf)); final PrintWriter p = new PrintWriter(new FileWriter(myconf));
try { try {
p.print("# Autogenerated by Gerrit Code Review \n"); p.print("# Autogenerated by Gerrit Code Review \n");
@@ -142,11 +143,11 @@ class GitWebServlet extends HttpServlet {
// We are mounted at the same level in the context as the main // We are mounted at the same level in the context as the main
// UI, so we can include the same header and footer scheme. // UI, so we can include the same header and footer scheme.
// //
final File hdr = new File(sitePath, "GerritSiteHeader.html"); final File hdr = new File(etc, "GerritSiteHeader.html");
if (hdr.isFile()) { if (hdr.isFile()) {
p.print("$site_header = " + quoteForPerl(hdr) + ";\n"); p.print("$site_header = " + quoteForPerl(hdr) + ";\n");
} }
final File ftr = new File(sitePath, "GerritSiteFooter.html"); final File ftr = new File(etc, "GerritSiteFooter.html");
if (ftr.isFile()) { if (ftr.isFile()) {
p.print("$site_footer = " + quoteForPerl(ftr) + ";\n"); p.print("$site_footer = " + quoteForPerl(ftr) + ";\n");
} }
@@ -159,7 +160,7 @@ class GitWebServlet extends HttpServlet {
p.print("$favicon = 'favicon.ico';\n"); p.print("$favicon = 'favicon.ico';\n");
p.print("$logo = 'gitweb-logo.png';\n"); p.print("$logo = 'gitweb-logo.png';\n");
p.print("@stylesheets = ('gitweb-default.css');\n"); p.print("@stylesheets = ('gitweb-default.css');\n");
final File css = new File(sitePath, "GerritSite.css"); final File css = new File(etc, "GerritSite.css");
if (css.isFile()) { if (css.isFile()) {
p.print("push @stylesheets, 'gitweb-site.css';\n"); p.print("push @stylesheets, 'gitweb-site.css';\n");
} }
@@ -209,7 +210,7 @@ class GitWebServlet extends HttpServlet {
// If the administrator has created a site-specific gitweb_config, // If the administrator has created a site-specific gitweb_config,
// load that before we perform any final overrides. // load that before we perform any final overrides.
// //
final File sitecfg = new File(sitePath, "gitweb_config.perl"); final File sitecfg = new File(etc, "gitweb_config.perl");
if (sitecfg.isFile()) { if (sitecfg.isFile()) {
p.print("$GITWEB_CONFIG = " + quoteForPerl(sitecfg) + ";\n"); p.print("$GITWEB_CONFIG = " + quoteForPerl(sitecfg) + ";\n");
p.print("if (-e $GITWEB_CONFIG) {\n"); p.print("if (-e $GITWEB_CONFIG) {\n");

View File

@@ -51,21 +51,19 @@ import javax.servlet.http.HttpServletResponse;
@Singleton @Singleton
public class HostPageServlet extends HttpServlet { public class HostPageServlet extends HttpServlet {
private final Provider<CurrentUser> currentUser; private final Provider<CurrentUser> currentUser;
private final File sitePath;
private final GerritConfig config; private final GerritConfig config;
private final Provider<String> urlProvider; private final Provider<String> urlProvider;
private final boolean wantSSL; private final boolean wantSSL;
private final Document hostDoc; private final Document hostDoc;
@Inject @Inject
HostPageServlet(final Provider<CurrentUser> cu, @SitePath final File path, HostPageServlet(final Provider<CurrentUser> cu,
final GerritConfig gc, @SitePath final File sitePath, final GerritConfig gc,
@CanonicalWebUrl @Nullable final Provider<String> up, @CanonicalWebUrl @Nullable final Provider<String> up,
@CanonicalWebUrl @Nullable final String configuredUrl, @CanonicalWebUrl @Nullable final String configuredUrl,
final ServletContext servletContext) throws IOException { final ServletContext servletContext) throws IOException {
currentUser = cu; currentUser = cu;
urlProvider = up; urlProvider = up;
sitePath = path;
config = gc; config = gc;
wantSSL = configuredUrl != null && configuredUrl.startsWith("https:"); wantSSL = configuredUrl != null && configuredUrl.startsWith("https:");
@@ -74,14 +72,16 @@ public class HostPageServlet extends HttpServlet {
if (hostDoc == null) { if (hostDoc == null) {
throw new FileNotFoundException("No " + pageName + " in webapp"); throw new FileNotFoundException("No " + pageName + " in webapp");
} }
final File etc = new File(sitePath, "etc");
fixModuleReference(hostDoc, servletContext); fixModuleReference(hostDoc, servletContext);
injectCssFile(hostDoc, "gerrit_sitecss", sitePath, "GerritSite.css"); injectCssFile(hostDoc, "gerrit_sitecss", etc, "GerritSite.css");
injectXmlFile(hostDoc, "gerrit_header", sitePath, "GerritSiteHeader.html"); injectXmlFile(hostDoc, "gerrit_header", etc, "GerritSiteHeader.html");
injectXmlFile(hostDoc, "gerrit_footer", sitePath, "GerritSiteFooter.html"); injectXmlFile(hostDoc, "gerrit_footer", etc, "GerritSiteFooter.html");
} }
private void injectXmlFile(final Document hostDoc, final String id, private void injectXmlFile(final Document hostDoc, final String id,
final File sitePath, final String fileName) throws IOException { final File etc, final String fileName) throws IOException {
final Element banner = HtmlDomUtil.find(hostDoc, id); final Element banner = HtmlDomUtil.find(hostDoc, id);
if (banner == null) { if (banner == null) {
return; return;
@@ -91,7 +91,7 @@ public class HostPageServlet extends HttpServlet {
banner.removeChild(banner.getFirstChild()); banner.removeChild(banner.getFirstChild());
} }
final Document html = HtmlDomUtil.parseFile(sitePath, fileName); final Document html = HtmlDomUtil.parseFile(etc, fileName);
if (html == null) { if (html == null) {
banner.getParentNode().removeChild(banner); banner.getParentNode().removeChild(banner);
return; return;
@@ -102,7 +102,7 @@ public class HostPageServlet extends HttpServlet {
} }
private void injectCssFile(final Document hostDoc, final String id, private void injectCssFile(final Document hostDoc, final String id,
final File sitePath, final String fileName) throws IOException { final File etc, final String fileName) throws IOException {
final Element banner = HtmlDomUtil.find(hostDoc, id); final Element banner = HtmlDomUtil.find(hostDoc, id);
if (banner == null) { if (banner == null) {
return; return;
@@ -112,7 +112,7 @@ public class HostPageServlet extends HttpServlet {
banner.removeChild(banner.getFirstChild()); banner.removeChild(banner.getFirstChild());
} }
final String css = HtmlDomUtil.readFile(sitePath, fileName); final String css = HtmlDomUtil.readFile(etc, fileName);
if (css == null) { if (css == null) {
banner.getParentNode().removeChild(banner); banner.getParentNode().removeChild(banner);
return; return;

View File

@@ -75,6 +75,8 @@ public class Init extends SiteProgram {
ui = ConsoleUI.getInstance(batchMode); ui = ConsoleUI.getInstance(batchMode);
try { try {
upgradeFrom_Pre2_0_25();
initSitePath(); initSitePath();
inject(); inject();
initGit(); initGit();
@@ -93,17 +95,60 @@ public class Init extends SiteProgram {
return 0; return 0;
} }
private void upgradeFrom_Pre2_0_25() throws IOException {
final File sitePath = getSitePath();
boolean isPre2_0_25 = false;
final String[] etcFiles =
{"gerrit.config", "secure.config", "replication.config",
"ssh_host_rsa_key", "ssh_host_rsa_key.pub", "ssh_host_dsa_key",
"ssh_host_dsa_key.pub", "ssh_host_key", "contact_information.pub",
"gitweb_config.perl", "keystore", "GerritSite.css",
"GerritSiteFooter.html", "GerritSiteHeader.html"};
for (String name : etcFiles) {
if (new File(sitePath, name).exists()) {
isPre2_0_25 = true;
break;
}
}
if (isPre2_0_25) {
if (!ui.yesno("Upgrade '%s'", sitePath.getCanonicalPath())) {
throw die("aborted by user");
}
final File etc_dir = new File(sitePath, "etc");
if (!etc_dir.exists() && !etc_dir.mkdirs()) {
throw die("Cannot create directory " + etc_dir);
}
for (String name : etcFiles) {
final File src = new File(sitePath, name);
final File dst = new File(etc_dir, name);
if (src.exists()) {
if (dst.exists()) {
throw die("File " + src + " would overwrite " + dst);
}
if (!src.renameTo(dst)) {
throw die("Cannot rename " + src + " to " + dst);
}
}
}
}
}
private void initSitePath() throws IOException, InterruptedException { private void initSitePath() throws IOException, InterruptedException {
final File sitePath = getSitePath(); final File sitePath = getSitePath();
final File gerrit_config = new File(sitePath, "gerrit.config"); final File etc_dir = new File(sitePath, "etc");
final File secure_config = new File(sitePath, "secure.config");
final File replication_config = new File(sitePath, "replication.config");
final File lib_dir = new File(sitePath, "lib"); final File lib_dir = new File(sitePath, "lib");
final File logs_dir = new File(sitePath, "logs"); final File logs_dir = new File(sitePath, "logs");
final File static_dir = new File(sitePath, "static"); final File static_dir = new File(sitePath, "static");
final File cache_dir = new File(sitePath, "cache"); final File cache_dir = new File(sitePath, "cache");
final File gerrit_config = new File(etc_dir, "gerrit.config");
final File secure_config = new File(etc_dir, "secure.config");
final File replication_config = new File(etc_dir, "replication.config");
if (gerrit_config.exists()) { if (gerrit_config.exists()) {
if (!gerrit_config.exists()) { if (!gerrit_config.exists()) {
throw die("'" + sitePath + "' is not a Gerrit server site"); throw die("'" + sitePath + "' is not a Gerrit server site");
@@ -117,6 +162,9 @@ public class Init extends SiteProgram {
if (!sitePath.mkdirs()) { if (!sitePath.mkdirs()) {
throw die("Cannot make directory " + sitePath); throw die("Cannot make directory " + sitePath);
} }
if (!etc_dir.mkdir()) {
throw die("Cannot make directory " + etc_dir);
}
deleteOnFailure = true; deleteOnFailure = true;
final FileBasedConfig cfg = new FileBasedConfig(gerrit_config); final FileBasedConfig cfg = new FileBasedConfig(gerrit_config);
@@ -139,16 +187,17 @@ public class Init extends SiteProgram {
} }
} }
etc_dir.mkdir();
lib_dir.mkdir();
logs_dir.mkdir();
static_dir.mkdir();
if (!secure_config.exists()) { if (!secure_config.exists()) {
chmod600(secure_config); chmod600(secure_config);
} }
if (!replication_config.exists()) { if (!replication_config.exists()) {
replication_config.createNewFile(); replication_config.createNewFile();
} }
lib_dir.mkdir();
logs_dir.mkdir();
static_dir.mkdir();
} }
private void initGit() throws OrmException, IOException { private void initGit() throws OrmException, IOException {
@@ -412,14 +461,15 @@ public class Init extends SiteProgram {
.setSHA1("6327a5f7a3dc45e0fd735adb5d08c5a74c05c20c").download(); .setSHA1("6327a5f7a3dc45e0fd735adb5d08c5a74c05c20c").download();
loadSiteLib(); loadSiteLib();
final File etc_dir = new File(getSitePath(), "etc");
System.err.print("Generating SSH host key ..."); System.err.print("Generating SSH host key ...");
System.err.flush(); System.err.flush();
if (SecurityUtils.isBouncyCastleRegistered()) { if (SecurityUtils.isBouncyCastleRegistered()) {
// Generate the SSH daemon host key using ssh-keygen. // Generate the SSH daemon host key using ssh-keygen.
// //
final String comment = "gerrit-code-review@" + hostname(); final String comment = "gerrit-code-review@" + hostname();
final File rsa = new File(getSitePath(), "ssh_host_rsa_key"); final File rsa = new File(etc_dir, "ssh_host_rsa_key");
final File dsa = new File(getSitePath(), "ssh_host_dsa_key"); final File dsa = new File(etc_dir, "ssh_host_dsa_key");
System.err.print(" rsa..."); System.err.print(" rsa...");
System.err.flush(); System.err.flush();
@@ -448,7 +498,7 @@ public class Init extends SiteProgram {
// short period of time. We try to reduce that risk by creating // short period of time. We try to reduce that risk by creating
// the key within a temporary directory. // the key within a temporary directory.
// //
final File tmpdir = new File(getSitePath(), "tmp.sshkeygen"); final File tmpdir = new File(etc_dir, "tmp.sshkeygen");
if (!tmpdir.mkdir()) { if (!tmpdir.mkdir()) {
throw die("Cannot create directory " + tmpdir); throw die("Cannot create directory " + tmpdir);
} }
@@ -466,7 +516,7 @@ public class Init extends SiteProgram {
p.loadKeys(); // forces the key to generate. p.loadKeys(); // forces the key to generate.
chmod600(tmpkey); chmod600(tmpkey);
final File key = new File(getSitePath(), keyname); final File key = new File(etc_dir, keyname);
if (!tmpkey.renameTo(key)) { if (!tmpkey.renameTo(key)) {
throw die("Cannot rename " + tmpkey + " to " + key); throw die("Cannot rename " + tmpkey + " to " + key);
} }
@@ -522,7 +572,8 @@ public class Init extends SiteProgram {
final String dname = final String dname =
"CN=" + certName + ",OU=Gerrit Code Review,O=" + domainOf(certName); "CN=" + certName + ",OU=Gerrit Code Review,O=" + domainOf(certName);
final File tmpdir = new File(getSitePath(), "tmp.sslcertgen"); final File etc_dir = new File(getSitePath(), "etc");
final File tmpdir = new File(etc_dir, "tmp.sslcertgen");
if (!tmpdir.mkdir()) { if (!tmpdir.mkdir()) {
throw die("Cannot create directory " + tmpdir); throw die("Cannot create directory " + tmpdir);
} }
@@ -541,7 +592,7 @@ public class Init extends SiteProgram {
}).waitFor(); }).waitFor();
chmod600(tmpstore); chmod600(tmpstore);
final File store = new File(getSitePath(), "keystore"); final File store = new File(etc_dir, "keystore");
if (!tmpstore.renameTo(store)) { if (!tmpstore.renameTo(store)) {
throw die("Cannot rename " + tmpstore + " to " + store); throw die("Cannot rename " + tmpstore + " to " + store);
} }

View File

@@ -58,9 +58,10 @@ import java.util.Set;
public class JettyServer { public class JettyServer {
static class Lifecycle implements LifecycleListener { static class Lifecycle implements LifecycleListener {
private final JettyServer server; private final JettyServer server;
@Inject @Inject
Lifecycle(final JettyServer server) { Lifecycle(final JettyServer server) {
this.server=server; this.server = server;
} }
@Override @Override
@@ -129,7 +130,7 @@ public class JettyServer {
} else if ("https".equals(u.getScheme())) { } else if ("https".equals(u.getScheme())) {
final SslSelectChannelConnector ssl = new SslSelectChannelConnector(); final SslSelectChannelConnector ssl = new SslSelectChannelConnector();
final File keystore = getFile(cfg, "sslkeystore", "keystore"); final File keystore = getFile(cfg, "sslkeystore", "etc/keystore");
String password = cfg.getString("httpd", null, "sslkeypassword"); String password = cfg.getString("httpd", null, "sslkeypassword");
if (password == null) { if (password == null) {
password = "gerrit"; password = "gerrit";

View File

@@ -41,8 +41,9 @@ class GerritServerConfigProvider implements Provider<Config> {
@Override @Override
public Config get() { public Config get() {
final File gerrit_config = new File(sitePath, "gerrit.config"); final File etc = new File(sitePath, "etc");
final File secure_config = new File(sitePath, "secure.config"); final File gerrit_config = new File(etc, "gerrit.config");
final File secure_config = new File(etc, "secure.config");
FileBasedConfig cfg = new FileBasedConfig(gerrit_config); FileBasedConfig cfg = new FileBasedConfig(gerrit_config);

View File

@@ -63,7 +63,7 @@ public class ContactStoreProvider implements Provider<ContactStore> {
} }
final String storeAPPSEC = config.getString("contactstore", null, "appsec"); final String storeAPPSEC = config.getString("contactstore", null, "appsec");
final File pubkey = new File(sitePath, "contact_information.pub"); final File pubkey = new File(sitePath, "etc/contact_information.pub");
if (!pubkey.exists()) { if (!pubkey.exists()) {
throw new ProvisionException("PGP public key file \"" throw new ProvisionException("PGP public key file \""
+ pubkey.getAbsolutePath() + "\" not found"); + pubkey.getAbsolutePath() + "\" not found");

View File

@@ -131,7 +131,8 @@ public class PushReplication implements ReplicationQueue {
private List<ReplicationConfig> allConfigs(final File path) private List<ReplicationConfig> allConfigs(final File path)
throws ConfigInvalidException, IOException { throws ConfigInvalidException, IOException {
final File cfgFile = new File(path, "replication.config"); final File etc = new File(path, "etc");
final File cfgFile = new File(etc, "replication.config");
final FileBasedConfig cfg = new FileBasedConfig(cfgFile); final FileBasedConfig cfg = new FileBasedConfig(cfgFile);
if (!cfg.getFile().exists()) { if (!cfg.getFile().exists()) {

View File

@@ -38,9 +38,10 @@ class HostKeyProvider implements Provider<KeyPairProvider> {
@Override @Override
public KeyPairProvider get() { public KeyPairProvider get() {
final File anyKey = new File(sitePath, "ssh_host_key"); final File etc = new File(sitePath, "etc");
final File rsaKey = new File(sitePath, "ssh_host_rsa_key"); final File anyKey = new File(etc, "ssh_host_key");
final File dsaKey = new File(sitePath, "ssh_host_dsa_key"); final File rsaKey = new File(etc, "ssh_host_rsa_key");
final File dsaKey = new File(etc, "ssh_host_dsa_key");
final List<String> keys = new ArrayList<String>(2); final List<String> keys = new ArrayList<String>(2);
if (rsaKey.exists()) { if (rsaKey.exists()) {
@@ -63,6 +64,9 @@ class HostKeyProvider implements Provider<KeyPairProvider> {
// //
final SimpleGeneratorHostKeyProvider keyp; final SimpleGeneratorHostKeyProvider keyp;
if (!etc.exists() && !etc.mkdirs()) {
throw new ProvisionException("Cannot create directory " + etc);
}
keyp = new SimpleGeneratorHostKeyProvider(); keyp = new SimpleGeneratorHostKeyProvider();
keyp.setPath(anyKey.getAbsolutePath()); keyp.setPath(anyKey.getAbsolutePath());
return keyp; return keyp;