diff --git a/src/main/java/com/nike/cerberus/command/core/RollingRebootWithHealthCheckCommand.java b/src/main/java/com/nike/cerberus/command/core/RollingRebootWithHealthCheckCommand.java index 01274344..1db261a4 100644 --- a/src/main/java/com/nike/cerberus/command/core/RollingRebootWithHealthCheckCommand.java +++ b/src/main/java/com/nike/cerberus/command/core/RollingRebootWithHealthCheckCommand.java @@ -12,10 +12,14 @@ /** * Command to reboot the CMS cluster. */ -@Parameters(commandNames = COMMAND_NAME, commandDescription = "Performs a safe rolling reboot on instances in the given cluster.") +@Parameters( + commandNames = COMMAND_NAME, + commandDescription = "Performs a safe rolling reboot on instances in the given cluster, checking that " + + "the previous instance is healthy before rebooting the next one." +) public class RollingRebootWithHealthCheckCommand implements Command { - public static final String COMMAND_NAME = "reboot-cluster"; + public static final String COMMAND_NAME = "rolling-reboot-with-health-check"; @Parameter(names = {"--stack-name"}, required = true, description = "The stack name to reboot.") private StackName stackName = StackName.CMS; diff --git a/src/main/java/com/nike/cerberus/operation/core/RollingRebootWithHealthCheckOperation.java b/src/main/java/com/nike/cerberus/operation/core/RollingRebootWithHealthCheckOperation.java index b46283a4..c879aadd 100644 --- a/src/main/java/com/nike/cerberus/operation/core/RollingRebootWithHealthCheckOperation.java +++ b/src/main/java/com/nike/cerberus/operation/core/RollingRebootWithHealthCheckOperation.java @@ -13,6 +13,7 @@ import com.nike.cerberus.service.Ec2Service; import com.nike.cerberus.store.ConfigStore; import com.nike.vault.client.http.HttpStatus; +import okhttp3.Call; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; @@ -189,17 +190,16 @@ private int executeHealthCheck(final String healthCheckUrl) { .readTimeout(DEFAULT_HTTP_TIMEOUT, DEFAULT_HTTP_TIMEOUT_UNIT) .build(); - Request requestBuilder = new Request.Builder() + final Request requestBuilder = new Request.Builder() .url(healthCheckUrl) .get() .build(); - try { - final Response response = okHttpClient.newCall(requestBuilder).execute(); + final Call healthCheckCall = okHttpClient.newCall(requestBuilder); + + try(final Response response = healthCheckCall.execute()) { logger.debug("Health check returned status: {}, URL: {}", response.code(), healthCheckUrl); - int responseCode = response.code(); - response.close(); - return responseCode; + return response.code(); } catch (IOException ioe) { final String message = Chalk.on("Health check failed, Cause: {}, URL: {}").red().toString(); logger.debug(message, ioe.getMessage(), healthCheckUrl); @@ -212,14 +212,18 @@ private int executeHealthCheck(final String healthCheckUrl) { public boolean isRunnable(final RollingRebootWithHealthCheckCommand command) { final StackName stackName = command.getStackName(); + final String stackNameStr = stackName.getName(); final String stackId = configStore.getStackId(stackName); final Map stackParameters = cloudFormationService.getStackParameters(stackId); - if (HEALTH_CHECK_MAP.containsKey(stackName.getName()) && stackParameters.containsKey(MIN_INSTANCES_STACK_PARAMETER_KEY)) { + if (! HEALTH_CHECK_MAP.containsKey(stackNameStr)) { + logger.error("Cannot reboot cluster: {}. Allowed stacks: {}", stackName, HEALTH_CHECK_MAP.keySet()); + return false; + } else if (! stackParameters.containsKey(MIN_INSTANCES_STACK_PARAMETER_KEY)) { + logger.error("Could not find parameter 'minInstances' on stack: {}", stackId); + return false; + } else { return true; } - - logger.error("Cannot reboot cluster: {}. Allowed stacks: {}", stackName, HEALTH_CHECK_MAP.keySet()); - return false; } }