public static void createStackIfNotExists(AmazonCloudFormation client, KinesisConnectorConfiguration config) {
String stackName = config.ELASTICSEARCH_CLOUDFORMATION_STACK_NAME;
if (stackExists(client, stackName)) {
StackStatus status = stackStatus(client, stackName);
switch (status) {
case CREATE_COMPLETE:
case UPDATE_COMPLETE:
LOG.info("Stack " + stackName + " already exists.");
return;
case CREATE_IN_PROGRESS:
case UPDATE_IN_PROGRESS:
case UPDATE_COMPLETE_CLEANUP_IN_PROGRESS:
LOG.info("Stack " + stackName + " exists with status: " + status + ". Waiting for completion.");
break;
default:
throw new IllegalStateException("Stack " + stackName + " exists but has an invalid status: "
+ status);
}
} else {
CreateStackRequest createStackRequest = new CreateStackRequest();
createStackRequest.setStackName(stackName);
String templateBody = null;
try {
templateBody = loadTemplate(config.ELASTICSEARCH_CLOUDFORMATION_TEMPLATE_URL);
} catch (IOException ioe) {
LOG.error("Error reading template", ioe);
throw new RuntimeException("Could not load template at location: "
+ config.ELASTICSEARCH_CLOUDFORMATION_TEMPLATE_URL);
}
createStackRequest.setTemplateBody(templateBody);
List<Parameter> parameters = new ArrayList<Parameter>();
parameters.add(new Parameter().withParameterKey("KeyName")
.withParameterValue(config.ELASTICSEARCH_CLOUDFORMATION_KEY_PAIR_NAME));
parameters.add(new Parameter().withParameterKey("InstanceType")
.withParameterValue(config.ELASTICSEARCH_CLOUDFORMATION_CLUSTER_INSTANCE_TYPE));
parameters.add(new Parameter().withParameterKey("SSHLocation")
.withParameterValue(config.ELASTICSEARCH_CLOUDFORMATION_SSH_LOCATION));
parameters.add(new Parameter().withParameterKey("ClusterSize")
.withParameterValue(config.ELASTICSEARCH_CLOUDFORMATION_CLUSTER_SIZE));
parameters.add(new Parameter().withParameterKey("ElasticsearchVersion")
.withParameterValue(config.ELASTICSEARCH_VERSION_NUMBER));
createStackRequest.setParameters(parameters);
List<String> capabilities = new ArrayList<String>();
capabilities.add("CAPABILITY_IAM");
createStackRequest.setCapabilities(capabilities);
client.createStack(createStackRequest);
LOG.info("Stack " + stackName + " is creating");
}
// now wait for good status
while (true) {
try {
Thread.sleep(1000 * 10);
} catch (Exception e) {
}
StackStatus status = stackStatus(client, stackName);
switch (status) {
case CREATE_COMPLETE:
case UPDATE_COMPLETE:
return;
case CREATE_IN_PROGRESS: