View Javadoc
1   /*
2    * Copyright 2012-2017 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.admin.upgrade;
17  
18  import java.text.SimpleDateFormat;
19  import java.util.Date;
20  
21  import javax.annotation.Resource;
22  
23  import org.codelibs.fess.Constants;
24  import org.codelibs.fess.app.service.ScheduledJobService;
25  import org.codelibs.fess.app.web.base.FessAdminAction;
26  import org.codelibs.fess.es.client.FessEsClient;
27  import org.codelibs.fess.es.config.exbhv.DataConfigBhv;
28  import org.codelibs.fess.es.config.exbhv.DataConfigToRoleBhv;
29  import org.codelibs.fess.es.config.exbhv.ElevateWordBhv;
30  import org.codelibs.fess.es.config.exbhv.FileConfigBhv;
31  import org.codelibs.fess.es.config.exbhv.FileConfigToRoleBhv;
32  import org.codelibs.fess.es.config.exbhv.LabelToRoleBhv;
33  import org.codelibs.fess.es.config.exbhv.LabelTypeBhv;
34  import org.codelibs.fess.es.config.exbhv.RoleTypeBhv;
35  import org.codelibs.fess.es.config.exbhv.WebConfigBhv;
36  import org.codelibs.fess.es.config.exbhv.WebConfigToRoleBhv;
37  import org.codelibs.fess.es.user.exbhv.RoleBhv;
38  import org.codelibs.fess.util.UpgradeUtil;
39  import org.elasticsearch.action.ActionListener;
40  import org.elasticsearch.action.support.IndicesOptions;
41  import org.elasticsearch.client.IndicesAdminClient;
42  import org.lastaflute.web.Execute;
43  import org.lastaflute.web.response.HtmlResponse;
44  import org.lastaflute.web.ruts.process.ActionRuntime;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  public class AdminUpgradeAction extends FessAdminAction {
49  
50      // ===================================================================================
51      //                                                                            Constant
52      //
53      private static final Logger logger = LoggerFactory.getLogger(AdminUpgradeAction.class);
54  
55      private static final String VERSION_11_0 = "11.0";
56  
57      private static final String VERSION_11_1 = "11.1";
58  
59      private static final String VERSION_11_2 = "11.2";
60  
61      // ===================================================================================
62      //                                                                           Attribute
63      //
64      @Resource
65      protected RoleBhv roleBhv;
66  
67      @Resource
68      protected RoleTypeBhv roleTypeBhv;
69  
70      @Resource
71      protected LabelToRoleBhv labelToRoleBhv;
72  
73      @Resource
74      protected LabelTypeBhv labelTypeBhv;
75  
76      @Resource
77      protected WebConfigToRoleBhv webConfigToRoleBhv;
78  
79      @Resource
80      protected WebConfigBhv webConfigBhv;
81  
82      @Resource
83      protected FileConfigToRoleBhv fileConfigToRoleBhv;
84  
85      @Resource
86      protected FileConfigBhv fileConfigBhv;
87  
88      @Resource
89      protected DataConfigToRoleBhv dataConfigToRoleBhv;
90  
91      @Resource
92      protected DataConfigBhv dataConfigBhv;
93  
94      @Resource
95      protected ElevateWordBhv elevateWordBhv;
96  
97      @Resource
98      protected FessEsClient fessEsClient;
99  
100     @Resource
101     private ScheduledJobService scheduledJobService;
102 
103     // ===================================================================================
104     //                                                                               Hook
105     //                                                                              ======
106     @Override
107     protected void setupHtmlData(final ActionRuntime runtime) {
108         super.setupHtmlData(runtime);
109         runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameUpgrade()));
110     }
111 
112     // ===================================================================================
113     //                                                                      Search Execute
114     //                                                                      ==============
115 
116     @Execute
117     public HtmlResponse index() {
118         saveToken();
119         return asIndexHtml();
120     }
121 
122     private HtmlResponse asIndexHtml() {
123         return asHtml(path_AdminUpgrade_AdminUpgradeJsp).useForm(UpgradeForm.class);
124     }
125 
126     @Execute
127     public HtmlResponse reindexOnly(final UpgradeForm form) {
128         validate(form, messages -> {}, () -> {
129             return asIndexHtml();
130         });
131         verifyToken(() -> asIndexHtml());
132         if (startReindex(Constants.ON.equalsIgnoreCase(form.replaceAliases))) {
133             saveInfo(messages -> messages.addSuccessStartedDataUpdate(GLOBAL));
134         }
135         return redirect(getClass());
136     }
137 
138     @Execute
139     public HtmlResponse upgradeFrom(final UpgradeForm form) {
140         validate(form, messages -> {}, () -> {
141             return asIndexHtml();
142         });
143         verifyToken(() -> asIndexHtml());
144 
145         if (VERSION_11_0.equals(form.targetVersion)) {
146             try {
147                 upgradeFrom11_0();
148                 upgradeFrom11_1();
149                 upgradeFrom11_2();
150                 upgradeFromAll();
151 
152                 saveInfo(messages -> messages.addSuccessStartedDataUpdate(GLOBAL));
153 
154                 systemHelper.reloadConfiguration();
155             } catch (final Exception e) {
156                 logger.warn("Failed to upgrade data.", e);
157                 saveError(messages -> messages.addErrorsFailedToUpgradeFrom(GLOBAL, VERSION_11_0, e.getLocalizedMessage()));
158             }
159         } else if (VERSION_11_1.equals(form.targetVersion)) {
160             try {
161                 upgradeFrom11_1();
162                 upgradeFrom11_2();
163                 upgradeFromAll();
164 
165                 saveInfo(messages -> messages.addSuccessStartedDataUpdate(GLOBAL));
166 
167                 systemHelper.reloadConfiguration();
168             } catch (final Exception e) {
169                 logger.warn("Failed to upgrade data.", e);
170                 saveError(messages -> messages.addErrorsFailedToUpgradeFrom(GLOBAL, VERSION_11_1, e.getLocalizedMessage()));
171             }
172         } else if (VERSION_11_2.equals(form.targetVersion)) {
173             try {
174                 upgradeFrom11_2();
175                 upgradeFromAll();
176 
177                 saveInfo(messages -> messages.addSuccessStartedDataUpdate(GLOBAL));
178 
179                 systemHelper.reloadConfiguration();
180             } catch (final Exception e) {
181                 logger.warn("Failed to upgrade data.", e);
182                 saveError(messages -> messages.addErrorsFailedToUpgradeFrom(GLOBAL, VERSION_11_2, e.getLocalizedMessage()));
183             }
184         } else {
185             saveError(messages -> messages.addErrorsUnknownVersionForUpgrade(GLOBAL));
186         }
187         return redirect(getClass());
188     }
189 
190     private void upgradeFrom11_0() {
191     }
192 
193     private void upgradeFrom11_1() {
194         final IndicesAdminClient indicesClient = fessEsClient.admin().indices();
195         final String configIndex = ".fess_config";
196 
197         // update mapping
198         UpgradeUtil.addFieldMapping(indicesClient, configIndex, "thumbnail_queue", "thumbnail_id",
199                 "{\"properties\":{\"thumbnail_id\":{\"type\":\"keyword\"}}}");
200     }
201 
202     private void upgradeFrom11_2() {
203         final IndicesAdminClient indicesClient = fessEsClient.admin().indices();
204         final String configIndex = ".fess_config";
205         final String searchLogIndex = "fess_log";
206 
207         // update mapping
208         UpgradeUtil.addMapping(indicesClient, configIndex, "related_content", "fess_indices/.fess_config");
209         UpgradeUtil.addMapping(indicesClient, configIndex, "related_query", "fess_indices/.fess_config");
210 
211         // update mapping
212         if (UpgradeUtil.addFieldMapping(indicesClient, searchLogIndex, "search_log", "virtualHost",
213                 "{\"properties\":{\"virtualHost\":{\"type\":\"keyword\"}}}")) {
214             UpgradeUtil.putMapping(indicesClient, searchLogIndex, "search_log", "{\"dynamic_templates\": ["
215                     + "{\"search_fields\": {\"path_match\": \"searchField.*\",\"mapping\": {\"type\": \"keyword\"}}}"//
216                     + "]}");
217         }
218         UpgradeUtil.addFieldMapping(indicesClient, configIndex, "web_config", "virtualHost",
219                 "{\"properties\":{\"virtualHost\":{\"type\":\"keyword\"}}}");
220         UpgradeUtil.addFieldMapping(indicesClient, configIndex, "data_config", "virtualHost",
221                 "{\"properties\":{\"virtualHost\":{\"type\":\"keyword\"}}}");
222         UpgradeUtil.addFieldMapping(indicesClient, configIndex, "file_config", "virtualHost",
223                 "{\"properties\":{\"virtualHost\":{\"type\":\"keyword\"}}}");
224         UpgradeUtil.addFieldMapping(indicesClient, configIndex, "key_match", "virtualHost",
225                 "{\"properties\":{\"virtualHost\":{\"type\":\"keyword\"}}}");
226         UpgradeUtil.addFieldMapping(indicesClient, configIndex, "label_type", "virtualHost",
227                 "{\"properties\":{\"virtualHost\":{\"type\":\"keyword\"}}}");
228     }
229 
230     private void upgradeFromAll() {
231         final IndicesAdminClient indicesClient = fessEsClient.admin().indices();
232         final String crawlerIndex = fessConfig.getIndexDocumentCrawlerIndex();
233 
234         // .crawler
235         if (UpgradeUtil.existsIndex(indicesClient, crawlerIndex, IndicesOptions.fromOptions(false, true, true, true))) {
236             UpgradeUtil.deleteIndex(indicesClient, crawlerIndex, response -> {});
237         }
238     }
239 
240     private boolean startReindex(final boolean replaceAliases) {
241         final String docIndex = "fess";
242         final String fromIndex = fessConfig.getIndexDocumentUpdateIndex();
243         final String toIndex = docIndex + "." + new SimpleDateFormat("yyyyMMddHHmm").format(new Date());
244         if (fessEsClient.createIndex(docIndex, "doc", toIndex)) {
245             fessEsClient.admin().cluster().prepareHealth(toIndex).setWaitForYellowStatus().execute(ActionListener.wrap(response -> {
246                 fessEsClient.addMapping(docIndex, "doc", toIndex);
247                 fessEsClient.reindex(fromIndex, toIndex, replaceAliases);
248                 if (replaceAliases && !fessEsClient.updateAlias(toIndex)) {
249                     logger.warn("Failed to update aliases for " + fromIndex + " and " + toIndex);
250                 }
251             }, e -> {
252                 logger.warn("Failed to reindex from " + fromIndex + " to " + toIndex, e);
253             }));
254             return true;
255         }
256         saveError(messages -> messages.addErrorsFailedToReindex(GLOBAL, fromIndex, toIndex));
257         return false;
258     }
259 
260 }