Line data Source code
1 : // Copyright (C) 2016 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.common.FileUtil.chmod; 18 : import static com.google.gerrit.pgm.init.api.InitUtil.die; 19 : import static com.google.gerrit.pgm.init.api.InitUtil.extract; 20 : import static com.google.gerrit.pgm.init.api.InitUtil.mkdir; 21 : import static com.google.gerrit.pgm.init.api.InitUtil.savePublic; 22 : import static com.google.gerrit.pgm.init.api.InitUtil.version; 23 : 24 : import com.google.gerrit.common.FileUtil; 25 : import com.google.gerrit.common.Nullable; 26 : import com.google.gerrit.pgm.init.api.ConsoleUI; 27 : import com.google.gerrit.pgm.init.api.InitFlags; 28 : import com.google.gerrit.pgm.init.api.InitStep; 29 : import com.google.gerrit.pgm.init.api.Section; 30 : import com.google.gerrit.server.config.SitePaths; 31 : import com.google.gerrit.server.mail.EmailModule; 32 : import com.google.inject.Binding; 33 : import com.google.inject.Inject; 34 : import com.google.inject.Injector; 35 : import com.google.inject.TypeLiteral; 36 : import java.io.IOException; 37 : import java.nio.file.Files; 38 : import java.nio.file.Path; 39 : import java.util.ArrayList; 40 : import java.util.List; 41 : 42 : /** Initialize (or upgrade) an existing site. */ 43 : public class SitePathInitializer { 44 : private final ConsoleUI ui; 45 : private final InitFlags flags; 46 : private final SitePaths site; 47 : private final List<InitStep> steps; 48 : private final Section.Factory sectionFactory; 49 : private final SecureStoreInitData secureStoreInitData; 50 : 51 : @Inject 52 : public SitePathInitializer( 53 : final Injector injector, 54 : final ConsoleUI ui, 55 : final InitFlags flags, 56 : final SitePaths site, 57 : final Section.Factory sectionFactory, 58 15 : @Nullable final SecureStoreInitData secureStoreInitData) { 59 15 : this.ui = ui; 60 15 : this.flags = flags; 61 15 : this.site = site; 62 15 : this.sectionFactory = sectionFactory; 63 15 : this.secureStoreInitData = secureStoreInitData; 64 15 : this.steps = stepsOf(injector); 65 15 : } 66 : 67 : public void run() throws Exception { 68 15 : ui.header("Gerrit Code Review %s", version()); 69 : 70 15 : if (site.isNew) { 71 0 : if (!ui.yesno(true, "Create '%s'", site.site_path.toAbsolutePath())) { 72 0 : throw die("aborted by user"); 73 : } 74 0 : FileUtil.mkdirsOrDie(site.site_path, "Cannot make directory"); 75 0 : flags.deleteOnFailure = true; 76 : } 77 : 78 15 : mkdir(site.bin_dir); 79 15 : mkdir(site.etc_dir); 80 15 : mkdir(site.lib_dir); 81 15 : mkdir(site.tmp_dir); 82 15 : mkdir(site.logs_dir); 83 15 : mkdir(site.mail_dir); 84 15 : mkdir(site.static_dir); 85 15 : mkdir(site.plugins_dir); 86 15 : mkdir(site.data_dir); 87 : 88 15 : for (InitStep step : steps) { 89 15 : if (step instanceof InitPlugins && flags.skipPlugins) { 90 15 : continue; 91 : } 92 15 : step.run(); 93 15 : } 94 : 95 15 : saveSecureStore(); 96 15 : savePublic(flags.cfg); 97 : 98 15 : extract(site.gerrit_sh, getClass(), "gerrit.sh"); 99 15 : chmod(0755, site.gerrit_sh); 100 15 : extract(site.gerrit_service, getClass(), "gerrit.service"); 101 15 : chmod(0755, site.gerrit_service); 102 15 : extract(site.gerrit_socket, getClass(), "gerrit.socket"); 103 15 : chmod(0755, site.gerrit_socket); 104 15 : chmod(0700, site.tmp_dir); 105 : 106 15 : extractMailExample("Abandoned.soy"); 107 15 : extractMailExample("AbandonedHtml.soy"); 108 15 : extractMailExample("AddKey.soy"); 109 15 : extractMailExample("AddKeyHtml.soy"); 110 15 : extractMailExample("AddToAttentionSet.soy"); 111 15 : extractMailExample("AddToAttentionSetHtml.soy"); 112 15 : extractMailExample("ChangeFooter.soy"); 113 15 : extractMailExample("ChangeFooterHtml.soy"); 114 15 : extractMailExample("ChangeSubject.soy"); 115 15 : extractMailExample("Comment.soy"); 116 15 : extractMailExample("CommentHtml.soy"); 117 15 : extractMailExample("CommentFooter.soy"); 118 15 : extractMailExample("CommentFooterHtml.soy"); 119 15 : extractMailExample("DeleteKey.soy"); 120 15 : extractMailExample("DeleteKeyHtml.soy"); 121 15 : extractMailExample("DeleteReviewer.soy"); 122 15 : extractMailExample("DeleteReviewerHtml.soy"); 123 15 : extractMailExample("DeleteVote.soy"); 124 15 : extractMailExample("DeleteVoteHtml.soy"); 125 15 : extractMailExample("Footer.soy"); 126 15 : extractMailExample("FooterHtml.soy"); 127 15 : extractMailExample("ChangeHeader.soy"); 128 15 : extractMailExample("ChangeHeaderHtml.soy"); 129 15 : extractMailExample("HttpPasswordUpdate.soy"); 130 15 : extractMailExample("HttpPasswordUpdateHtml.soy"); 131 15 : extractMailExample("InboundEmailRejection.soy"); 132 15 : extractMailExample("InboundEmailRejectionHtml.soy"); 133 15 : extractMailExample("Merged.soy"); 134 15 : extractMailExample("MergedHtml.soy"); 135 15 : extractMailExample("NewChange.soy"); 136 15 : extractMailExample("NewChangeHtml.soy"); 137 15 : extractMailExample("RegisterNewEmail.soy"); 138 15 : extractMailExample("RegisterNewEmailHtml.soy"); 139 15 : extractMailExample("RemoveFromAttentionSet.soy"); 140 15 : extractMailExample("RemoveFromAttentionSetHtml.soy"); 141 15 : extractMailExample("ReplacePatchSet.soy"); 142 15 : extractMailExample("ReplacePatchSetHtml.soy"); 143 15 : extractMailExample("Restored.soy"); 144 15 : extractMailExample("RestoredHtml.soy"); 145 15 : extractMailExample("Reverted.soy"); 146 15 : extractMailExample("RevertedHtml.soy"); 147 15 : extractMailExample("SetAssignee.soy"); 148 15 : extractMailExample("SetAssigneeHtml.soy"); 149 : 150 15 : if (!ui.isBatch()) { 151 0 : System.err.println(); 152 : } 153 15 : } 154 : 155 : public void postRun(Injector injector) throws Exception { 156 15 : for (InitStep step : steps) { 157 15 : if (step instanceof InitPlugins && flags.skipPlugins) { 158 15 : continue; 159 : } 160 15 : injector.injectMembers(step); 161 15 : step.postRun(); 162 15 : } 163 15 : } 164 : 165 : private void saveSecureStore() throws IOException { 166 15 : if (secureStoreInitData != null) { 167 0 : Path dst = site.lib_dir.resolve(secureStoreInitData.jarFile.getFileName()); 168 0 : Files.copy(secureStoreInitData.jarFile, dst); 169 0 : Section gerritSection = sectionFactory.get("gerrit", null); 170 0 : gerritSection.set("secureStoreClass", secureStoreInitData.className); 171 : } 172 15 : } 173 : 174 : private void extractMailExample(String orig) throws Exception { 175 15 : Path ex = site.mail_dir.resolve(orig + ".example"); 176 15 : extract(ex, EmailModule.class, orig); 177 15 : chmod(0444, ex); 178 15 : } 179 : 180 : private static List<InitStep> stepsOf(Injector injector) { 181 15 : final ArrayList<InitStep> r = new ArrayList<>(); 182 15 : for (Binding<InitStep> b : all(injector)) { 183 15 : r.add(b.getProvider().get()); 184 15 : } 185 15 : return r; 186 : } 187 : 188 : private static List<Binding<InitStep>> all(Injector injector) { 189 15 : return injector.findBindingsByType(new TypeLiteral<InitStep>() {}); 190 : } 191 : }