View Javadoc
1   /*
2    * Copyright 2017-2022 original authors
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    * https://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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package io.micronaut.build.services;
17  
18  import com.google.cloud.tools.jib.api.Credential;
19  import com.google.cloud.tools.jib.maven.MavenProjectProperties;
20  import com.google.cloud.tools.jib.plugins.common.PropertyNames;
21  import org.apache.maven.model.Plugin;
22  import org.apache.maven.project.MavenProject;
23  import org.codehaus.plexus.util.xml.Xpp3Dom;
24  
25  import javax.inject.Inject;
26  import javax.inject.Singleton;
27  import java.util.*;
28  import java.util.stream.Collectors;
29  
30  /**
31   * Exposes the Jib plugin configuration so that it can be read by other mojos.
32   *
33   * @author Álvaro Sánchez-Mariscal
34   * @since 1.1
35   */
36  @Singleton
37  public class JibConfigurationService {
38  
39      private static final String IMAGE = "image";
40      private static final String CONTAINER = "container";
41      private static final String WORKING_DIRECTORY = "workingDirectory";
42  
43      private Xpp3Dom configuration;
44      private Xpp3Dom to;
45      private Xpp3Dom from;
46  
47      @Inject
48      public JibConfigurationService(MavenProject mavenProject) {
49          final Plugin plugin = mavenProject.getPlugin(MavenProjectProperties.PLUGIN_KEY);
50          if (plugin != null && plugin.getConfiguration() != null) {
51              configuration = (Xpp3Dom) plugin.getConfiguration();
52              to = configuration.getChild("to");
53              from = configuration.getChild("from");
54          }
55      }
56  
57      /**
58       * Returns the <code>to.image</code> configuration.
59       */
60      public Optional<String> getToImage() {
61          Optional<String> result;
62          String propertyValue = System.getProperties().getProperty(PropertyNames.TO_IMAGE);
63          if (propertyValue != null) {
64              result = Optional.of(propertyValue);
65          } else if (to != null) {
66              result = Optional.ofNullable(to.getChild(IMAGE).getValue());
67          } else {
68              result = Optional.empty();
69          }
70          return result;
71      }
72  
73      /**
74       * Returns the <code>from.image</code> configuration.
75       */
76      public Optional<String> getFromImage() {
77          Optional<String> result;
78          String propertyValue = System.getProperties().getProperty(PropertyNames.FROM_IMAGE);
79          if (propertyValue != null) {
80              result = Optional.of(propertyValue);
81          } else if (from != null) {
82              result = Optional.ofNullable(from.getChild(IMAGE).getValue());
83          } else {
84              result = Optional.empty();
85          }
86          return result;
87      }
88  
89      /**
90       * Returns the <code>to.tags</code> configuration.
91       */
92      public Set<String> getTags() {
93          Set<String> result = null;
94          String propertyValue = System.getProperties().getProperty(PropertyNames.TO_TAGS);
95          if (propertyValue != null) {
96              result = new HashSet<>(parseCommaSeparatedList(propertyValue));
97          } else {
98              if (to != null) {
99                  Xpp3Dom tags = to.getChild("tags");
100                 if (tags != null && tags.getChildCount() > 0) {
101                     result = Arrays.stream(tags.getChildren())
102                             .map(Xpp3Dom::getValue)
103                             .collect(Collectors.toSet());
104                 }
105             }
106             if (result == null) {
107                 result = Collections.emptySet();
108             }
109         }
110         return result;
111     }
112 
113     /**
114      * Returns the <code>to.auth.username</code> and <code>to.auth.password</code> configuration.
115      */
116     public Optional<Credential> getCredentials() {
117         Optional<Credential> result = Optional.empty();
118         String usernameProp = System.getProperties().getProperty(PropertyNames.TO_AUTH_USERNAME);
119         String passwordProp = System.getProperties().getProperty(PropertyNames.TO_AUTH_PASSWORD);
120         if (usernameProp != null && passwordProp != null) {
121             result = Optional.of(Credential.from(usernameProp, passwordProp));
122         } else {
123             if (to != null) {
124                 Xpp3Dom auth = to.getChild("auth");
125                 if (auth != null) {
126                     Xpp3Dom username = auth.getChild("username");
127                     Xpp3Dom password = auth.getChild("password");
128                     if (username != null && password != null) {
129                         result = Optional.of(Credential.from(username.getValue(), password.getValue()));
130                     }
131                 }
132             }
133         }
134         return result;
135     }
136 
137     /**
138      * Returns the <code>container.workingDirectory</code> configuration.
139      */
140     public Optional<String> getWorkingDirectory() {
141         if (configuration != null) {
142             Xpp3Dom container = configuration.getChild(CONTAINER);
143             if (container != null && container.getChild(WORKING_DIRECTORY) != null) {
144                 return Optional.ofNullable(container.getChild(WORKING_DIRECTORY).getValue());
145             }
146         }
147         return Optional.empty();
148     }
149 
150     /**
151      * Returns the <code>container.args</code> configuration.
152      */
153     public List<String> getArgs() {
154         List<String> result = new ArrayList<>();
155         if (configuration != null) {
156             Xpp3Dom container = configuration.getChild(CONTAINER);
157             if (container != null) {
158                 Xpp3Dom args = container.getChild("args");
159                 if (args.getChildCount() > 0) {
160                     for (Xpp3Dom arg : args.getChildren()) {
161                         result.add(arg.getValue());
162                     }
163                 } else {
164                     result.add(args.getValue());
165                 }
166             }
167         }
168         return result;
169     }
170 
171     private static Set<String> parseCommaSeparatedList(String list) {
172         String[] parts = list.split(",");
173         Set<String> items = new HashSet<>(parts.length);
174         for (String part : parts) {
175             items.add(part.trim());
176         }
177         return items;
178     }
179 }