Remove bouncycastle jar files from lib folder during init

Bouncycastle is now shipped in the war file, and does not need to be
separately downloaded. However, when upgrading from an older version,
the previously downloaded jar files remain in the site's lib folder.

The LibraryDownloader already has functionality to remove (actually
rename) the stale libraries, but that only works when those libraries
are handled as downloads.

Factor the stale library removal functionality out to a separate class
and use that to remove the bouncycastle jar files during init.

Change-Id: I048250026cb1040b75c9b34a4aaf852e5e3384c6
This commit is contained in:
David Pursehouse 2017-03-30 14:24:51 +09:00
parent 819343f9c9
commit 50c9b5bdea
4 changed files with 75 additions and 32 deletions

View File

@ -41,12 +41,14 @@ class InitSshd implements InitStep {
private final ConsoleUI ui;
private final SitePaths site;
private final Section sshd;
private final StaleLibraryRemover remover;
@Inject
InitSshd(final ConsoleUI ui, final SitePaths site, final Section.Factory sections) {
InitSshd(ConsoleUI ui, SitePaths site, Section.Factory sections, StaleLibraryRemover remover) {
this.ui = ui;
this.site = site;
this.sshd = sections.get("sshd", null);
this.remover = remover;
}
@Override
@ -74,6 +76,7 @@ class InitSshd implements InitStep {
sshd.set("listenAddress", SocketUtil.format(hostname, port));
generateSshHostKeys();
remover.remove("bc(pg|pkix|prov)-.*[.]jar");
}
private static boolean isOff(String listenHostname) {

View File

@ -14,7 +14,6 @@
package com.google.gerrit.pgm.init;
import com.google.common.base.Strings;
import com.google.common.hash.Funnels;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
@ -33,7 +32,6 @@ import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -45,6 +43,7 @@ import org.eclipse.jgit.util.HttpSupport;
class LibraryDownloader {
private final ConsoleUI ui;
private final Path lib_dir;
private final StaleLibraryRemover remover;
private boolean required;
private String name;
@ -59,9 +58,10 @@ class LibraryDownloader {
private boolean skipDownload;
@Inject
LibraryDownloader(ConsoleUI ui, SitePaths site) {
LibraryDownloader(ConsoleUI ui, SitePaths site, StaleLibraryRemover remover) {
this.ui = ui;
this.lib_dir = site.lib_dir;
this.remover = remover;
this.needs = new ArrayList<>(2);
}
@ -169,7 +169,7 @@ class LibraryDownloader {
}
try {
removeStaleVersions();
remover.remove(remove);
if (download) {
doGetByHttp();
} else {
@ -216,32 +216,6 @@ class LibraryDownloader {
}
}
private void removeStaleVersions() {
if (!Strings.isNullOrEmpty(remove)) {
DirectoryStream.Filter<Path> filter =
new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) {
return entry.getFileName().toString().matches("^" + remove + "$");
}
};
try (DirectoryStream<Path> paths = Files.newDirectoryStream(lib_dir, filter)) {
for (Path p : paths) {
String old = p.getFileName().toString();
String bak = "." + old + ".backup";
ui.message("Renaming %s to %s\n", old, bak);
try {
Files.move(p, p.resolveSibling(bak));
} catch (IOException e) {
throw new Die("cannot rename " + old, e);
}
}
} catch (IOException e) {
throw new Die("cannot remove stale library versions", e);
}
}
}
private void doGetByLocalCopy() throws IOException {
System.err.print("Copying " + jarUrl + " ...");
Path p = url2file(jarUrl);

View File

@ -0,0 +1,65 @@
// Copyright (C) 2017 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.pgm.init;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import com.google.common.base.Strings;
import com.google.gerrit.common.Die;
import com.google.gerrit.pgm.init.api.ConsoleUI;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@Singleton
public class StaleLibraryRemover {
private final ConsoleUI ui;
private final Path lib_dir;
@Inject
StaleLibraryRemover(ConsoleUI ui, SitePaths site) {
this.ui = ui;
this.lib_dir = site.lib_dir;
}
public void remove(String pattern) {
if (!Strings.isNullOrEmpty(pattern)) {
DirectoryStream.Filter<Path> filter =
new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) {
return entry.getFileName().toString().matches("^" + pattern + "$");
}
};
try (DirectoryStream<Path> paths = Files.newDirectoryStream(lib_dir, filter)) {
for (Path p : paths) {
String old = p.getFileName().toString();
String bak = "." + old + ".backup";
ui.message("Renaming %s to %s\n", old, bak);
try {
Files.move(p, p.resolveSibling(bak));
} catch (IOException e) {
throw new Die("cannot rename " + old, e);
}
}
} catch (IOException e) {
throw new Die("cannot remove stale library versions", e);
}
}
}
}

View File

@ -31,6 +31,7 @@ public class LibrariesTest {
public void create() throws Exception {
final SitePaths site = new SitePaths(Paths.get("."));
final ConsoleUI ui = createStrictMock(ConsoleUI.class);
final StaleLibraryRemover remover = createStrictMock(StaleLibraryRemover.class);
replay(ui);
@ -39,7 +40,7 @@ public class LibrariesTest {
new Provider<LibraryDownloader>() {
@Override
public LibraryDownloader get() {
return new LibraryDownloader(ui, site);
return new LibraryDownloader(ui, site, remover);
}
},
Collections.<String>emptyList(),