Search in sources :

Example 1 with Die

use of com.google.gerrit.common.Die in project gerrit by GerritCodeReview.

the class BaseInit method createSiteInit.

private SiteInit createSiteInit() {
    final ConsoleUI ui = getConsoleUI();
    final Path sitePath = getSitePath();
    final List<Module> m = new ArrayList<>();
    final SecureStoreInitData secureStoreInitData = discoverSecureStoreClass();
    final String currentSecureStoreClassName = getConfiguredSecureStoreClass();
    if (secureStoreInitData != null && currentSecureStoreClassName != null && !currentSecureStoreClassName.equals(secureStoreInitData.className)) {
        String err = String.format("Different secure store was previously configured: %s. " + "Use SwitchSecureStore program to switch between implementations.", currentSecureStoreClassName);
        throw die(err);
    }
    m.add(new GerritServerConfigModule());
    m.add(new InitModule(standalone));
    m.add(new AbstractModule() {

        @Override
        protected void configure() {
            bind(ConsoleUI.class).toInstance(ui);
            bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath);
            List<String> plugins = MoreObjects.firstNonNull(getInstallPlugins(), new ArrayList<>());
            bind(new TypeLiteral<List<String>>() {
            }).annotatedWith(InstallPlugins.class).toInstance(plugins);
            bind(new TypeLiteral<Boolean>() {
            }).annotatedWith(InstallAllPlugins.class).toInstance(installAllPlugins());
            bind(PluginsDistribution.class).toInstance(pluginsDistribution);
            String secureStoreClassName;
            if (secureStoreInitData != null) {
                secureStoreClassName = secureStoreInitData.className;
            } else {
                secureStoreClassName = currentSecureStoreClassName;
            }
            if (secureStoreClassName != null) {
                ui.message("Using secure store: %s\n", secureStoreClassName);
            }
            bind(SecureStoreInitData.class).toProvider(Providers.of(secureStoreInitData));
            bind(String.class).annotatedWith(SecureStoreClassName.class).toProvider(Providers.of(secureStoreClassName));
            bind(SecureStore.class).toProvider(SecureStoreProvider.class).in(SINGLETON);
            bind(new TypeLiteral<List<String>>() {
            }).annotatedWith(LibraryDownload.class).toInstance(getSkippedDownloads());
            bind(Boolean.class).annotatedWith(LibraryDownload.class).toInstance(skipAllDownloads());
            bind(MetricMaker.class).to(DisabledMetricMaker.class);
        }
    });
    try {
        return Guice.createInjector(PRODUCTION, m).getInstance(SiteInit.class);
    } catch (CreationException ce) {
        final Message first = ce.getErrorMessages().iterator().next();
        Throwable why = first.getCause();
        if (why instanceof Die) {
            throw (Die) why;
        }
        final StringBuilder buf = new StringBuilder(ce.getMessage());
        while (why != null) {
            buf.append("\n");
            buf.append(why.getMessage());
            why = why.getCause();
            if (why != null) {
                buf.append("\n  caused by ");
            }
        }
        throw die(buf.toString(), new RuntimeException("InitInjector failed", ce));
    }
}
Also used : Path(java.nio.file.Path) SitePath(com.google.gerrit.server.config.SitePath) Die(com.google.gerrit.common.Die) Message(com.google.inject.spi.Message) ArrayList(java.util.ArrayList) CreationException(com.google.inject.CreationException) SecureStore(com.google.gerrit.server.securestore.SecureStore) ConsoleUI(com.google.gerrit.pgm.init.api.ConsoleUI) AbstractModule(com.google.inject.AbstractModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) TypeLiteral(com.google.inject.TypeLiteral) List(java.util.List) ArrayList(java.util.ArrayList) Module(com.google.inject.Module) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) IndexModule(com.google.gerrit.server.index.IndexModule) AbstractModule(com.google.inject.AbstractModule) DisabledMetricMaker(com.google.gerrit.metrics.DisabledMetricMaker)

Example 2 with Die

use of com.google.gerrit.common.Die in project gerrit by GerritCodeReview.

the class LibraryDownloader method doGet.

private void doGet() {
    if (!Files.exists(lib_dir)) {
        try {
            Files.createDirectories(lib_dir);
        } catch (IOException e) {
            throw new Die("Cannot create " + lib_dir, e);
        }
    }
    try {
        remover.remove(remove);
        if (download) {
            doGetByHttp();
        } else {
            doGetByLocalCopy();
        }
        verifyFileChecksum();
    } catch (IOException err) {
        try {
            Files.delete(dst);
        } catch (IOException e) {
        // Delete failed; leave alone.
        }
        if (ui.isBatch()) {
            throw new Die("error: Cannot get " + jarUrl, err);
        }
        System.err.println();
        System.err.println();
        System.err.println("error: " + err.getMessage());
        System.err.println("Please download:");
        System.err.println();
        System.err.println("  " + jarUrl);
        System.err.println();
        System.err.println("and save as:");
        System.err.println();
        System.err.println("  " + dst.toAbsolutePath());
        System.err.println();
        System.err.flush();
        ui.waitForUser();
        if (Files.exists(dst)) {
            verifyFileChecksum();
        } else if (!ui.yesno(!required, "Continue without this library")) {
            throw new Die("aborted by user");
        }
    }
    if (Files.exists(dst)) {
        exists = true;
        IoUtil.loadJARs(dst);
    }
}
Also used : Die(com.google.gerrit.common.Die) IOException(java.io.IOException)

Example 3 with Die

use of com.google.gerrit.common.Die in project gerrit by GerritCodeReview.

the class LibraryDownloader method verifyFileChecksum.

private void verifyFileChecksum() {
    if (sha1 == null) {
        System.err.println();
        System.err.flush();
        return;
    }
    Hasher h = Hashing.sha1().newHasher();
    try (InputStream in = Files.newInputStream(dst);
        OutputStream out = Funnels.asOutputStream(h)) {
        ByteStreams.copy(in, out);
    } catch (IOException e) {
        deleteDst();
        throw new Die("cannot checksum " + dst, e);
    }
    if (sha1.equals(h.hash().toString())) {
        System.err.println("Checksum " + dst.getFileName() + " OK");
        System.err.flush();
    } else if (ui.isBatch()) {
        deleteDst();
        throw new Die(dst + " SHA-1 checksum does not match");
    } else if (!ui.yesno(null, //
    "error: SHA-1 checksum does not match\nUse %s anyway", dst.getFileName())) {
        deleteDst();
        throw new Die("aborted by user");
    }
}
Also used : Die(com.google.gerrit.common.Die) Hasher(com.google.common.hash.Hasher) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 4 with Die

use of com.google.gerrit.common.Die in project gerrit by GerritCodeReview.

the class AbstractProgram method main.

public final int main(String[] argv) throws Exception {
    final CmdLineParser clp = new CmdLineParser(OptionHandlers.empty(), this);
    try {
        clp.parseArgument(argv);
    } catch (CmdLineException err) {
        if (!clp.wasHelpRequestedByOption()) {
            System.err.println("fatal: " + err.getMessage());
            return 1;
        }
    }
    if (clp.wasHelpRequestedByOption()) {
        StringWriter msg = new StringWriter();
        clp.printDetailedUsage(getName(), msg);
        System.err.println(msg.toString());
        return 1;
    }
    try {
        ProxyUtil.configureHttpProxy();
        return run();
    } catch (Die err) {
        if (showStackTrace) {
            err.printStackTrace();
        } else {
            final Throwable cause = err.getCause();
            final String diemsg = err.getMessage();
            if (cause != null && !cause.getMessage().equals(diemsg)) {
                System.err.println("fatal: " + cause.getMessage().replace("\n", "\nfatal: "));
            }
            System.err.println("fatal: " + diemsg.replace("\n", "\nfatal: "));
        }
        return 128;
    }
}
Also used : Die(com.google.gerrit.common.Die) CmdLineParser(com.google.gerrit.util.cli.CmdLineParser) StringWriter(java.io.StringWriter) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 5 with Die

use of com.google.gerrit.common.Die in project gerrit by GerritCodeReview.

the class StaleLibraryRemover method remove.

public void remove(String pattern) {
    if (!Strings.isNullOrEmpty(pattern)) {
        DirectoryStream.Filter<Path> filter = entry -> 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";
                Path dest = p.resolveSibling(bak);
                if (Files.exists(dest)) {
                    ui.message("WARNING: not renaming %s to %s: already exists\n", old, bak);
                    continue;
                }
                ui.message("Renaming %s to %s\n", old, bak);
                try {
                    Files.move(p, dest);
                } catch (IOException e) {
                    throw new Die("cannot rename " + old, e);
                }
            }
        } catch (IOException e) {
            throw new Die("cannot remove stale library versions", e);
        }
    }
}
Also used : Path(java.nio.file.Path) Strings(com.google.common.base.Strings) Die(com.google.gerrit.common.Die) DirectoryStream(java.nio.file.DirectoryStream) Files(java.nio.file.Files) SitePaths(com.google.gerrit.server.config.SitePaths) Inject(com.google.inject.Inject) ConsoleUI(com.google.gerrit.pgm.init.api.ConsoleUI) IOException(java.io.IOException) Path(java.nio.file.Path) Singleton(com.google.inject.Singleton) Die(com.google.gerrit.common.Die) DirectoryStream(java.nio.file.DirectoryStream) IOException(java.io.IOException)

Aggregations

Die (com.google.gerrit.common.Die)5 IOException (java.io.IOException)3 ConsoleUI (com.google.gerrit.pgm.init.api.ConsoleUI)2 Path (java.nio.file.Path)2 Strings (com.google.common.base.Strings)1 Hasher (com.google.common.hash.Hasher)1 DisabledMetricMaker (com.google.gerrit.metrics.DisabledMetricMaker)1 GerritServerConfigModule (com.google.gerrit.server.config.GerritServerConfigModule)1 SitePath (com.google.gerrit.server.config.SitePath)1 SitePaths (com.google.gerrit.server.config.SitePaths)1 IndexModule (com.google.gerrit.server.index.IndexModule)1 SecureStore (com.google.gerrit.server.securestore.SecureStore)1 CmdLineParser (com.google.gerrit.util.cli.CmdLineParser)1 AbstractModule (com.google.inject.AbstractModule)1 CreationException (com.google.inject.CreationException)1 Inject (com.google.inject.Inject)1 Module (com.google.inject.Module)1 Singleton (com.google.inject.Singleton)1 TypeLiteral (com.google.inject.TypeLiteral)1 Message (com.google.inject.spi.Message)1