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.init; 16 : 17 : import static com.google.gerrit.pgm.init.api.InitUtil.die; 18 : import static com.google.gerrit.pgm.init.api.InitUtil.username; 19 : 20 : import com.google.common.io.ByteStreams; 21 : import com.google.gerrit.launcher.GerritLauncher; 22 : import com.google.gerrit.pgm.init.api.ConsoleUI; 23 : import com.google.gerrit.pgm.init.api.InitStep; 24 : import com.google.gerrit.pgm.init.api.Section; 25 : import com.google.gerrit.server.config.SitePaths; 26 : import com.google.inject.Inject; 27 : import com.google.inject.Singleton; 28 : import java.io.FileNotFoundException; 29 : import java.io.IOException; 30 : import java.io.InputStream; 31 : import java.io.OutputStream; 32 : import java.nio.file.Files; 33 : import java.nio.file.Path; 34 : import org.eclipse.jgit.internal.storage.file.LockFile; 35 : 36 : /** Initialize the {@code container} configuration section. */ 37 : @Singleton 38 : class InitContainer implements InitStep { 39 : private final ConsoleUI ui; 40 : private final SitePaths site; 41 : private final Section container; 42 : 43 : @Inject 44 15 : InitContainer(ConsoleUI ui, SitePaths site, Section.Factory sections) { 45 15 : this.ui = ui; 46 15 : this.site = site; 47 15 : this.container = sections.get("container", null); 48 15 : } 49 : 50 : @Override 51 : public void run() throws FileNotFoundException, IOException { 52 15 : ui.header("Container Process"); 53 : 54 15 : container.string("Run as", "user", username()); 55 15 : container.string("Java runtime", "javaHome", javaHome()); 56 : 57 : Path myWar; 58 : try { 59 15 : myWar = GerritLauncher.getDistributionArchive().toPath(); 60 0 : } catch (FileNotFoundException e) { 61 0 : System.err.println("warn: Cannot find distribution archive (e.g. gerrit.war)"); 62 0 : myWar = null; 63 15 : } 64 : 65 15 : String path = container.get("war"); 66 15 : if (path != null) { 67 0 : path = 68 0 : container.string( 69 0 : "Gerrit runtime", "war", myWar != null ? myWar.toAbsolutePath().toString() : null); 70 0 : if (path == null || path.isEmpty()) { 71 0 : throw die("container.war is required"); 72 : } 73 : 74 15 : } else if (myWar != null) { 75 : final boolean copy; 76 15 : final Path siteWar = site.gerrit_war; 77 15 : if (Files.exists(siteWar)) { 78 1 : if (Files.isSameFile(siteWar, myWar)) { 79 0 : copy = false; 80 : } else { 81 1 : copy = ui.yesno(true, "Upgrade %s", siteWar); 82 : } 83 : } else { 84 15 : copy = ui.yesno(true, "Copy %s to %s", myWar.getFileName(), siteWar); 85 15 : if (copy) { 86 15 : container.unset("war"); 87 : } else { 88 0 : container.set("war", myWar.toAbsolutePath().toString()); 89 : } 90 : } 91 15 : if (copy) { 92 15 : if (!ui.isBatch()) { 93 0 : System.err.format("Copying %s to %s", myWar.getFileName(), siteWar); 94 0 : System.err.println(); 95 : } 96 : 97 15 : try (InputStream in = Files.newInputStream(myWar)) { 98 15 : Files.createDirectories(siteWar.getParent()); 99 : 100 15 : LockFile lf = new LockFile(siteWar.toFile()); 101 15 : if (!lf.lock()) { 102 0 : throw new IOException("Cannot lock " + siteWar); 103 : } 104 : try { 105 15 : try (OutputStream out = lf.getOutputStream()) { 106 15 : ByteStreams.copy(in, out); 107 : } 108 15 : if (!lf.commit()) { 109 0 : throw new IOException("Cannot commit " + siteWar); 110 : } 111 : } finally { 112 15 : lf.unlock(); 113 : } 114 : } 115 : } 116 : } 117 15 : } 118 : 119 : private static String javaHome() { 120 15 : return System.getProperty("java.home"); 121 : } 122 : }