}
/*
* Gets the current environment using this cname
*/
EnvironmentDescription curEnv = getEnvironmentFor(applicationName,
cnamePrefix);
if (curEnv.getVersionLabel().equals(versionLabel) && skipIfSameVersion) {
getLog().warn(
format("Environment is running version %s and skipIfSameVersion is true. Returning",
versionLabel));
return null;
}
/*
* Decides on a cnamePrefix, and launches a new environment
*/
String cnamePrefixToCreate = getCNamePrefixToCreate();
if (getLog().isInfoEnabled())
getLog().info(
"Creating a new environment on " + cnamePrefixToCreate
+ ".elasticbeanstalk.com");
copyOptionSettings(curEnv);
if (!solutionStack.equals(curEnv.getSolutionStackName())) {
if (getLog().isInfoEnabled())
getLog().warn(
format("(btw, we're launching with solutionStack/ set to '%s' instead of the default ('%s'). "
+ "If this is not the case, then we kindly ask you to file a bug report on the mailing list :)",
curEnv.getSolutionStackName(), solutionStack));
solutionStack = curEnv.getSolutionStackName();
}
String newEnvironmentName = getNewEnvironmentName(StringUtils
.defaultString(this.environmentName,
curEnv.getEnvironmentName()));
if (getLog().isInfoEnabled())
getLog().info("And it'll be named " + newEnvironmentName);
CreateEnvironmentResult createEnvResult = createEnvironment(
cnamePrefixToCreate, newEnvironmentName);
/*
* Waits for completion
*/
EnvironmentDescription newEnvDesc = null;
try {
newEnvDesc = waitForEnvironment(createEnvResult.getEnvironmentId());
} catch (Exception exc) {
/*
* Terminates the failed launched environment
*/
terminateEnvironment(createEnvResult.getEnvironmentId());
handleException(exc);
return null;
}
/*
* Swaps. Due to beanstalker-25, we're doing some extra logic we
* actually woudln't want to.
*/
{
boolean swapped = false;
for (int i = 1; i <= maxAttempts; i++) {
try {
swapEnvironmentCNames(newEnvDesc.getEnvironmentId(),
curEnv.getEnvironmentId(), cnamePrefix);
swapped = true;
break;
} catch (Throwable exc) {
if (exc instanceof MojoFailureException)
exc = Throwable.class.cast(MojoFailureException.class
.cast(exc).getCause());
getLog().warn(
format("Attempt #%d/%d failed. Sleeping and retrying. Reason: %s",
i, maxAttempts, exc.getMessage()));
sleepInterval(attemptRetryInterval);
}
}
if (!swapped) {
getLog().info(
"Failed to properly Replace Environment. Finishing the new one. And throwing you a failure");
terminateEnvironment(newEnvDesc.getEnvironmentId());
String message = "Unable to swap cnames. btw, see https://github.com/ingenieux/beanstalker/issues/25 and help us improve beanstalker";
getLog().warn(message);