View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2017 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.devops.conga.plugins.aem.maven;
21  
22  import static io.wcm.tooling.commons.packmgr.install.VendorInstallerFactory.COMPOSUM_URL;
23  import static io.wcm.tooling.commons.packmgr.install.VendorInstallerFactory.CRX_URL;
24  
25  import java.util.List;
26  
27  import javax.inject.Inject;
28  
29  import org.apache.commons.lang3.Strings;
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.plugin.AbstractMojo;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.settings.crypto.SettingsDecrypter;
35  
36  import io.wcm.tooling.commons.packmgr.PackageManagerProperties;
37  import io.wcm.tooling.commons.packmgr.install.VendorInstallerFactory;
38  import io.wcm.tooling.commons.packmgr.install.VendorInstallerFactory.Service;
39  
40  /**
41   * Common functionality for all mojos.
42   */
43  abstract class AbstractContentPackageMojo extends AbstractMojo {
44  
45    private static final String CONSOLE_URL = "/system/console";
46  
47    /**
48     * The URL of the HTTP service API of the CRX package manager.
49     *
50     * <p>
51     * See <a href=
52     * "http://dev.day.com/docs/en/crx/current/how_to/package_manager.html#Managing%20Packages%20on%20the%20Command%20Line"
53     * >CRX HTTP service Interface</a> for details on this interface.
54     * </p>
55     */
56    @Parameter(property = "vault.serviceURL", required = true, defaultValue = "http://localhost:4502/crx/packmgr/service")
57    private String serviceURL;
58  
59    /**
60     * The user name to authenticate as against the remote CRX system (package manager).
61     */
62    @Parameter(property = "vault.userId", defaultValue = "admin")
63    private String userId;
64  
65    /**
66     * The password to authenticate against the remote CRX system (package manager).
67     */
68    @Parameter(property = "vault.password", defaultValue = "admin")
69    private String password;
70  
71    /**
72     * OAuth 2 access token to authenticate against the remote CRX system (package manager).
73     * If this is configured, username and password are ignored.
74     */
75    @Parameter(property = "vault.oauth2AccessToken")
76    private String oauth2AccessToken;
77  
78    /**
79     * The user name to authenticate as against the remote CRX system (Felix console).
80     * Defaults to the value from <code>userId</code>.
81     */
82    @Parameter(property = "console.userId")
83    private String consoleUserId;
84  
85    /**
86     * The password to authenticate against the remote CRX system (Felix console).
87     * Defaults to the value from <code>password</code>.
88     */
89    @Parameter(property = "console.password")
90  
91    private String consolePassword;
92    /**
93     * OAuth 2 access token to authenticate against the remote CRX system (Felix console).
94     * If this is configured, username and password are ignored.
95     * Defaults to value from <code>authenticationBearerToken</code>.
96     */
97    @Parameter(property = "console.consoleOauth2AccessToken")
98    private String consoleOauth2AccessToken;
99  
100   /**
101    * Set this to "true" to skip installing packages to CRX although configured in the POM.
102    */
103   @Parameter(property = "vault.skip", defaultValue = "false")
104   private boolean skip;
105 
106   /**
107    * Number of times to retry upload and install via CRX HTTP interface if it fails.
108    */
109   @Parameter(property = "vault.retryCount", defaultValue = "24")
110   private int retryCount;
111 
112   /**
113    * Number of seconds between retry attempts.
114    */
115   @Parameter(property = "vault.retryDelay", defaultValue = "5")
116   private int retryDelay;
117 
118   /**
119    * Bundle status JSON URL. If an URL is configured the activation status of all bundles in the system is checked
120    * before it is tried to upload and install a new package and after each upload.
121    *
122    * <p>
123    * If not all bundles are activated the upload is delayed up to {@link #bundleStatusWaitLimit} seconds,
124    * every 5 seconds the activation status is checked anew.
125    * </p>
126    *
127    * <p>
128    * Expected is an URL like: http://localhost:4502/system/console/bundles/.json
129    * </p>
130    *
131    * <p>
132    * If the URL is not set it is derived from serviceURL. If set to "-" the status check is disabled.
133    * </p>
134    */
135   @Parameter(property = "vault.bundleStatusURL", required = false)
136   private String bundleStatusURL;
137 
138   /**
139    * Number of seconds to wait as maximum for a positive bundle status check.
140    * If this limit is reached and the bundle status is still not positive the install of the package proceeds anyway.
141    */
142   @Parameter(property = "vault.bundleStatusWaitLimit", defaultValue = "360")
143   private int bundleStatusWaitLimit;
144 
145   /**
146    * Package Manager install status JSON URL. If an URL is configured the installation status of packages and
147    * embedded packages is checked before it is tried to upload and install a new package and after each upload.
148    *
149    * <p>
150    * If not all packages are installed the upload is delayed up to {@link #packageManagerInstallStatusWaitLimit}
151    * seconds, every 5 seconds the installation status is checked anew.
152    * </p>
153    *
154    * <p>
155    * Expected is an URL like: http://localhost:4502/crx/packmgr/installstatus.jsp
156    * </p>
157    *
158    * <p>
159    * If the URL is not set it is derived from serviceURL. If set to "-" the status check is disabled.
160    * </p>
161    */
162   @Parameter(property = "vault.packageManagerInstallStatusURL", required = false)
163   private String packageManagerInstallStatusURL;
164 
165   /**
166    * Number of seconds to wait as maximum for a positive package manager install status check.
167    * If this limit is reached and the package manager status is still not positive the install of the package proceeds
168    * anyway.
169    */
170   @Parameter(property = "vault.packageManagerInstallStatusWaitLimit", defaultValue = "360")
171   private int packageManagerInstallStatusWaitLimit;
172 
173   /**
174    * Patterns for symbolic names of bundles that are expected to be not present in bundle list.
175    * If any of these bundles are found in the bundle list, this system is assumed as not ready for installing further
176    * packages because a previous installation (e.g. of AEM service pack) is still in progress.
177    */
178   @Parameter(property = "vault.bundleStatusBlacklistBundleNames", defaultValue = "^updater\\.aem.*$")
179   private String[] bundleStatusBlacklistBundleNames;
180 
181   /**
182    * Patterns for symbolic names of bundles that are ignored in bundle list.
183    * The state of these bundles has no effect on the bundle status check.
184    */
185   @Parameter(property = "vault.bundleStatusWhitelistBundleNames",
186       defaultValue = "^com\\.day\\.crx\\.crxde-support$,"
187           + "^com\\.adobe\\.granite\\.crx-explorer$,"
188           + "^com\\.adobe\\.granite\\.crxde-lite$,"
189           + "^org\\.apache\\.sling\\.jcr\\.webdav$,"
190           + "^org\\.apache\\.sling\\.jcr\\.davex$")
191   private String[] bundleStatusWhitelistBundleNames;
192 
193   /**
194    * System ready JSON URL. If an URL is configured the systemready status of the target instance is checked
195    * after installing finishing the upload. This works only for AEMaaCS SDK instances.
196    *
197    * <p>
198    * Expected is an URL like: http://localhost:4502/systemready
199    * </p>
200    *
201    * <p>
202    * If the URL is not set it is derived from serviceURL. If set to "-" the status check is disabled.
203    * </p>
204    */
205   @Parameter(property = "vault.systemReadyURL", required = false)
206   private String systemReadyURL;
207 
208   /**
209    * Number of seconds to wait as maximum for a positive system ready check.
210    * If this limit is reached and the system ready is still not positive the install of the package proceeds anyway.
211    */
212   @Parameter(property = "vault.systemReadyWaitLimitSec", defaultValue = "30")
213   private int systemReadyWaitLimitSec;
214 
215   /**
216    * If set to true also self-signed certificates are accepted.
217    */
218   @Parameter(property = "vault.relaxedSSLCheck", defaultValue = "false")
219   private boolean relaxedSSLCheck;
220 
221   /**
222    * HTTP connection timeout (in seconds).
223    */
224   @Parameter(property = "vault.httpConnectTimeoutSec", defaultValue = "10")
225   private int httpConnectTimeoutSec;
226 
227   /**
228    * HTTP socket timeout (in seconds).
229    */
230   @Parameter(property = "vault.httpSocketTimeoutSec", defaultValue = "60")
231   private int httpSocketTimeout;
232 
233   /**
234    * Log level to be used to log responses from package manager (which may get huge for large packages).
235    * Possible values are INFO (default) or DEBUG.
236    */
237   @Parameter(property = "vault.packageManagerOutputLogLevel", defaultValue = "INFO")
238   private String packageManagerOutputLogLevel;
239 
240   @Parameter(property = "session", defaultValue = "${session}", readonly = true)
241   private MavenSession session;
242 
243   @Inject
244   private SettingsDecrypter decrypter;
245 
246   protected final boolean isSkip() {
247     return this.skip;
248   }
249 
250   protected PackageManagerProperties getPackageManagerProperties() throws MojoExecutionException {
251     PackageManagerProperties props = new PackageManagerProperties();
252 
253     props.setPackageManagerUrl(buildPackageManagerUrl());
254     props.setUserId(this.userId);
255     props.setPassword(this.password);
256     props.setOAuth2AccessToken(this.oauth2AccessToken);
257     props.setConsoleUserId(this.consoleUserId);
258     props.setConsolePassword(this.consolePassword);
259     props.setConsoleOAuth2AccessToken(this.consoleOauth2AccessToken);
260     props.setRetryCount(this.retryCount);
261     props.setRetryDelaySec(this.retryDelay);
262     props.setBundleStatusUrl(buildBundleStatusUrl());
263     props.setBundleStatusWaitLimitSec(this.bundleStatusWaitLimit);
264     props.setBundleStatusBlacklistBundleNames(List.of(this.bundleStatusBlacklistBundleNames));
265     props.setBundleStatusWhitelistBundleNames(List.of(this.bundleStatusWhitelistBundleNames));
266     props.setSystemReadyUrl(buildSystemReadyUrl());
267     props.setSystemReadyWaitLimitSec(this.systemReadyWaitLimitSec);
268     props.setPackageManagerInstallStatusURL(buildPackageManagerInstallStatusUrl());
269     props.setPackageManagerInstallStatusWaitLimitSec(this.packageManagerInstallStatusWaitLimit);
270     props.setRelaxedSSLCheck(this.relaxedSSLCheck);
271     props.setHttpConnectTimeoutSec(this.httpConnectTimeoutSec);
272     props.setHttpSocketTimeoutSec(this.httpSocketTimeout);
273     props.setProxies(ProxySupport.getMavenProxies(session, decrypter));
274     props.setPackageManagerOutputLogLevel(this.packageManagerOutputLogLevel);
275 
276     return props;
277   }
278 
279   private String buildPackageManagerUrl() throws MojoExecutionException {
280     String serviceUrl = this.serviceURL;
281     switch (VendorInstallerFactory.identify(serviceUrl)) {
282       case CRX:
283         serviceUrl = VendorInstallerFactory.getBaseUrl(serviceUrl) + CRX_URL;
284         break;
285       case COMPOSUM:
286         serviceUrl = VendorInstallerFactory.getBaseUrl(serviceUrl) + COMPOSUM_URL;
287         break;
288       default:
289         throw new MojoExecutionException("Unsupported service URL: " + serviceUrl);
290     }
291     return serviceUrl;
292   }
293 
294   private String buildBundleStatusUrl() throws MojoExecutionException {
295     if (Strings.CS.equals(this.bundleStatusURL, "-")) {
296       return null;
297     }
298     if (this.bundleStatusURL != null) {
299       return this.bundleStatusURL;
300     }
301     // if not set use hostname from serviceURL and add default path to bundle status
302     String baseUrl = VendorInstallerFactory.getBaseUrl(buildPackageManagerUrl());
303     return baseUrl + "/system/console/bundles/.json";
304   }
305 
306   private String buildSystemReadyUrl() throws MojoExecutionException {
307     if (Strings.CS.equals(this.systemReadyURL, "-")) {
308       return null;
309     }
310     if (this.systemReadyURL != null) {
311       return this.systemReadyURL;
312     }
313     // if not set use hostname from serviceURL and add default path to bundle status
314     String baseUrl = VendorInstallerFactory.getBaseUrl(buildPackageManagerUrl());
315     return baseUrl + "/systemready";
316   }
317 
318   private String buildPackageManagerInstallStatusUrl() throws MojoExecutionException {
319     if (Strings.CS.equals(this.packageManagerInstallStatusURL, "-")
320         || VendorInstallerFactory.identify(this.serviceURL) != Service.CRX) {
321       return null;
322     }
323     if (this.packageManagerInstallStatusURL != null) {
324       return this.packageManagerInstallStatusURL;
325     }
326     // if not set use hostname from serviceURL and add default path to bundle status
327     String baseUrl = VendorInstallerFactory.getBaseUrl(buildPackageManagerUrl());
328     return baseUrl + "/crx/packmgr/installstatus.jsp";
329   }
330 
331   protected String buildConsoleUrl() {
332     return VendorInstallerFactory.getBaseUrl(this.serviceURL) + CONSOLE_URL;
333   }
334 
335   protected String getConsoleUser() {
336     return this.consoleUserId;
337   }
338 
339   protected String getConsolePassword() {
340     return this.consolePassword;
341   }
342 
343 }