com.carrotgarden.maven.aws.cfn.CloudForm Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of carrot-maven-aws-plugin Show documentation
Show all versions of carrot-maven-aws-plugin Show documentation
${project.organization.name} AWS Maven Plugin
/**
* Copyright (C) 2010-2012 Andrei Pozolotin
*
* All rights reserved. Licensed under the OSI BSD License.
*
* http://www.opensource.org/licenses/bsd-license.php
*/
package com.carrotgarden.maven.aws.cfn;
import java.io.File;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.commons.io.FileUtils;
import org.apache.maven.settings.Server;
import org.slf4j.Logger;
import com.amazonaws.auth.AWSCredentials;
import com.carrotgarden.maven.aws.CarrotMojo;
import com.carrotgarden.maven.aws.util.AWSCredentialsImpl;
import com.carrotgarden.maven.aws.util.Util;
/**
* base for cloud formation goals
*/
public abstract class CloudForm extends CarrotMojo {
/**
* amazon template entry:
*
* parameters-section-structure
*
*/
public static final String TEMPLATE_PARAMETERS = "Parameters";
/**
* AWS CloudFormation stack name; must be unique under your aws account /
* region; alternatively, see {@link #stackNameProperty}
*
* @required
* @parameter default-value="amazon-builder"
*/
private String stackName;
/**
* name of project.property which, if set dynamically, will be used instead
* of plug-in property {@link #stackName}
*
* @parameter
*/
private String stackNameProperty;
/** prefer project.property over plug-in property */
protected String stackName() {
return projectValue(stackName, stackNameProperty);
}
/**
* AWS CloudFormation
*
* amazon security credentials
*
* stored in
*
* maven settings.xml
*
* under server id entry; username="Access Key ID",
* password="Secret Access Key";
*
* @required
* @parameter default-value="com.example.aws.stack"
*/
protected String stackServerId;
/**
* AWS CloudFormation operation timeout; seconds
*
* @parameter default-value="600"
*/
protected String stackTimeout;
/**
* AWS CloudFormation
*
* optional api end point url
*
* which controls amazon region selection;
*
* when omitted, will be constructed from {@link #stackEndpintFormat} and
* {@link #amazonRegion}
*
* @parameter
*/
private String stackEndpoint;
/**
* AWS CloudFormation end point format
*
* @parameter default-value="https://cloudformation.%s.amazonaws.com"
*/
private String stackEndpintFormat;
protected String stackEndpoint() {
return amazonEndpoint(stackEndpoint, stackEndpintFormat);
}
//
protected Map loadPluginProperties() throws Exception {
final Map pluginParams = new TreeMap();
return pluginParams;
}
protected Map mergePluginProps(final Properties inputProps,
final Map inputParams) throws Exception {
/** merge template parameters */
final Map pluginProps = new TreeMap();
/** from properties file */
pluginProps.putAll(Util.safeMap(inputProps));
/** from maven pom.xml */
pluginProps.putAll(Util.safeMap(inputParams));
return pluginProps;
}
protected CarrotCloudForm newCloudFormation( //
final File templateFile, //
final Map stackParams //
) throws Exception {
/** */
final String stackTemplate = safeTemplate(templateFile);
/** */
final Server server = settings().getServer(stackServerId);
if (server == null) {
throw new IllegalArgumentException(
"settings.xml : server definition is missing for serverId="
+ stackServerId);
}
final AWSCredentials credentials = new AWSCredentialsImpl(server);
/** */
final Logger logger = getLogger(CarrotCloudForm.class);
/** */
final long stackTimeout = Util.safeNumber(getLog(), this.stackTimeout,
600);
final CarrotCloudForm formation = new CarrotCloudForm(logger,
stackName(), stackTemplate, stackParams, stackTimeout,
credentials, stackEndpoint());
return formation;
}
protected String safeTemplate(final File templateFile) throws Exception {
if (templateFile == null || !templateFile.exists()) {
return "{}";
} else {
return FileUtils.readFileToString(templateFile);
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Set loadParameterNames(final File templateFile)
throws Exception {
final Set nameSet = new TreeSet();
if (templateFile == null || !templateFile.exists()) {
return nameSet;
}
final Map templateMap = Util.jsonLoad(templateFile, Map.class);
final Map paramMap = (Map) templateMap.get(TEMPLATE_PARAMETERS);
if (paramMap == null) {
return nameSet;
}
nameSet.addAll(paramMap.keySet());
return nameSet;
}
protected Map loadTemplateParameters(
final File templateFile, final Map pluginParams)
throws Exception {
final Map stackParams = new TreeMap();
final Set nameSet = loadParameterNames(templateFile);
final Properties propsProject = project().getProperties();
final Properties propsCommand = session().getUserProperties();
final Properties propsSystem = session().getSystemProperties();
for (final String name : nameSet) {
if (pluginParams.containsKey(name)) {
stackParams.put(name, pluginParams.get(name));
continue;
}
if (propsProject.containsKey(name)) {
stackParams.put(name, propsProject.get(name).toString());
continue;
}
if (propsCommand.containsKey(name)) {
stackParams.put(name, propsCommand.get(name).toString());
continue;
}
if (propsSystem.containsKey(name)) {
stackParams.put(name, propsSystem.get(name).toString());
continue;
}
}
return stackParams;
}
}