View Javadoc
1   /*
2    * Copyright (c) 2016 ingenieux Labs
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package br.com.ingenieux.mojo.beanstalk.env;
18  
19  import com.amazonaws.services.elasticbeanstalk.model.ConfigurationOptionDescription;
20  import com.amazonaws.services.elasticbeanstalk.model.ConfigurationOptionSetting;
21  import com.amazonaws.services.elasticbeanstalk.model.ConfigurationSettingsDescription;
22  import com.amazonaws.services.elasticbeanstalk.model.DescribeConfigurationOptionsRequest;
23  import com.amazonaws.services.elasticbeanstalk.model.DescribeConfigurationOptionsResult;
24  import com.amazonaws.services.elasticbeanstalk.model.DescribeConfigurationSettingsRequest;
25  import com.amazonaws.services.elasticbeanstalk.model.DescribeConfigurationSettingsResult;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.plugins.annotations.Parameter;
30  
31  import java.io.File;
32  import java.io.FileOutputStream;
33  import java.io.PrintStream;
34  import java.util.LinkedHashMap;
35  import java.util.Map;
36  import java.util.Properties;
37  import java.util.TreeMap;
38  
39  import br.com.ingenieux.mojo.beanstalk.AbstractNeedsEnvironmentMojo;
40  
41  /**
42   * Dumps the current Environment Settings into stdout or an output file (a java properties file)
43   *
44   * TODO: Export to .ebextensions file format
45   *
46   * @since 1.1.0
47   */
48  @Mojo(name = "dump-environment-settings", requiresDirectInvocation = true)
49  public class DumpEnvironmentSettings extends AbstractNeedsEnvironmentMojo {
50  
51    /**
52     * (Optional) output file to output to
53     */
54    @Parameter(property = "beanstalk.outputFile")
55    private File outputFile;
56  
57    /**
58     * <p> Output File Format </p>
59     *
60     * <p> Possible values: </p> <ul> <li>yaml</li> <li>properties</li> </ul>
61     *
62     * (defaults to properties)
63     */
64    @Parameter(property = "beanstalk.outputFileFormat", defaultValue = "properties")
65    private String outputFileFormat;
66  
67    @Parameter(property = "beanstalk.changedOnly", defaultValue = "true")
68    private boolean changedOnly;
69  
70    private Map<String, ConfigurationOptionDescription> defaultSettings = new TreeMap<String, ConfigurationOptionDescription>();
71  
72    protected Object executeInternal() throws Exception {
73      DescribeConfigurationOptionsResult configOptions =
74          getService()
75              .describeConfigurationOptions(
76                  new DescribeConfigurationOptionsRequest().withApplicationName(applicationName).withEnvironmentName(curEnv.getEnvironmentName()));
77  
78      for (ConfigurationOptionDescription o : configOptions.getOptions()) {
79        String key = String.format("beanstalk.env.%s.%s", o.getNamespace().replace(":", "."), o.getName());
80  
81        for (Map.Entry<String, ConfigurationOptionSetting> entry : COMMON_PARAMETERS.entrySet()) {
82          ConfigurationOptionSetting cos = entry.getValue();
83  
84          if (cos.getNamespace().equals(o.getNamespace()) && cos.getOptionName().equals(o.getName())) {
85            key = entry.getKey();
86            break;
87          }
88        }
89  
90        defaultSettings.put(key, o);
91      }
92  
93      DescribeConfigurationSettingsResult configurationSettings =
94          getService()
95              .describeConfigurationSettings(
96                  new DescribeConfigurationSettingsRequest().withApplicationName(applicationName).withEnvironmentName(curEnv.getEnvironmentName()));
97  
98      Properties newProperties = new Properties();
99  
100     if (configurationSettings.getConfigurationSettings().isEmpty()) {
101       throw new IllegalStateException("No Configuration Settings received");
102     }
103 
104     ConfigurationSettingsDescription configSettings = configurationSettings.getConfigurationSettings().get(0);
105 
106     Map<String, ConfigurationOptionSetting> keyMap = new LinkedHashMap<String, ConfigurationOptionSetting>();
107 
108     for (ConfigurationOptionSetting d : configSettings.getOptionSettings()) {
109       String key = String.format("beanstalk.env.%s.%s", d.getNamespace().replaceAll(":", "."), d.getOptionName());
110       String defaultValue = "";
111       String outputKey = key;
112 
113       keyMap.put(key, d);
114 
115       for (Map.Entry<String, ConfigurationOptionSetting> cosEntry : COMMON_PARAMETERS.entrySet()) {
116         ConfigurationOptionSetting v = cosEntry.getValue();
117 
118         boolean match = v.getNamespace().equals(d.getNamespace()) && v.getOptionName().equals(d.getOptionName());
119 
120         if (match) {
121           outputKey = cosEntry.getKey();
122           break;
123         }
124       }
125 
126       if (defaultSettings.containsKey(outputKey)) {
127         defaultValue = StringUtils.defaultString(defaultSettings.get(outputKey).getDefaultValue());
128       }
129 
130       String value = d.getValue();
131 
132       if (null == value || StringUtils.isBlank("" + value)) {
133         continue;
134       }
135 
136       if (!defaultValue.equals(value)) {
137         if (!value.contains(curEnv.getEnvironmentId())) {
138           getLog().info("Adding property " + key);
139 
140           if (changedOnly) {
141             String curValue = project.getProperties().getProperty(outputKey);
142 
143             if (!value.equals(curValue)) {
144               newProperties.put(outputKey, value);
145             }
146           } else {
147             newProperties.put(outputKey, value);
148           }
149         } else {
150           getLog().info("Ignoring property " + outputKey + "(value=" + value + ") due to containing references to the environment id");
151         }
152 
153       } else {
154         getLog().debug("Ignoring property " + key + " (defaulted)");
155       }
156     }
157 
158     if ("properties".equals(this.outputFileFormat)) {
159       String comment = "elastic beanstalk environment properties for " + curEnv.getEnvironmentName();
160       if (null != outputFile) {
161         newProperties.store(new FileOutputStream(outputFile), comment);
162       } else {
163         newProperties.store(System.out, comment);
164       }
165     } else if ("yaml".equals(this.outputFileFormat)) {
166       PrintStream printStream = System.out;
167 
168       if (null != outputFile) {
169         printStream = new PrintStream(outputFile);
170       }
171 
172       printStream.println("option_settings:");
173 
174       for (Map.Entry<Object, Object> e : newProperties.entrySet()) {
175         ConfigurationOptionSetting c = keyMap.get("" + e.getKey());
176         String value = "" + e.getValue();
177 
178         printStream.println("  - namespace: " + c.getNamespace());
179         printStream.println("    option_name: " + c.getOptionName());
180         printStream.println("    value: " + value);
181       }
182 
183       printStream.close();
184     }
185 
186     return null;
187   }
188 }