Example usage for com.amazonaws.services.elasticbeanstalk.model EnvironmentDescription getHealth

List of usage examples for com.amazonaws.services.elasticbeanstalk.model EnvironmentDescription getHealth

Introduction

In this page you can find the example usage for com.amazonaws.services.elasticbeanstalk.model EnvironmentDescription getHealth.

Prototype


public String getHealth() 

Source Link

Document

Describes the health status of the environment.

Usage

From source file:br.com.ingenieux.mojo.beanstalk.cmd.env.waitfor.WaitForEnvironmentCommand.java

License:Apache License

protected List<Predicate<EnvironmentDescription>> getEnvironmentDescriptionPredicate(
        WaitForEnvironmentContext context) {
    // as well as those (which are used as predicate variables, thus being
    // final)/*from   www.  ja v  a  2 s.  co m*/
    final String environmentRef = context.getEnvironmentRef();
    final String statusToWaitFor = defaultString(context.getStatusToWaitFor(), "!Terminated");
    final String healthToWaitFor = context.getHealth();

    // Sanity Check
    Validate.isTrue(isNotBlank(environmentRef), "EnvironmentRef is blank or null", environmentRef);

    // some argument juggling

    final boolean negated = statusToWaitFor.startsWith("!");

    // argument juggling

    List<Predicate<EnvironmentDescription>> result = new ArrayList<Predicate<EnvironmentDescription>>();

    if (environmentRef.matches("e-\\p{Alnum}{10}")) {
        result.add(new Predicate<EnvironmentDescription>() {
            @Override
            public boolean apply(EnvironmentDescription t) {
                return t.getEnvironmentId().equals(environmentRef);
            }
        });

        info("... with environmentId equal to '%s'", environmentRef);
    } else if (environmentRef.matches(".*\\Q.elasticbeanstalk.com\\E")) {
        result.add(new Predicate<EnvironmentDescription>() {
            @Override
            public boolean apply(EnvironmentDescription t) {
                return defaultString(t.getCNAME()).equals(environmentRef);
            }
        });
        info("... with cname set to '%s'", environmentRef);
    } else {
        String tmpRE = Pattern.quote(environmentRef);

        if (environmentRef.endsWith("*")) {
            tmpRE = format("^\\Q%s\\E.*", environmentRef.substring(0, -1 + environmentRef.length()));
        }

        final String environmentRefNameRE = tmpRE;

        result.add(new Predicate<EnvironmentDescription>() {
            @Override
            public boolean apply(EnvironmentDescription t) {
                return t.getEnvironmentName().matches(environmentRefNameRE);
            }
        });

        info("... with environmentName matching re '%s'", environmentRefNameRE);
    }

    {
        // start building predicates with the status one - "![status]" must
        // be equal to status or not status
        final int offset = negated ? 1 : 0;
        final String vStatusToWaitFor = statusToWaitFor.substring(offset);

        result.add(new Predicate<EnvironmentDescription>() {
            public boolean apply(EnvironmentDescription t) {

                boolean result = vStatusToWaitFor.equals(t.getStatus());

                if (negated) {
                    result = !result;
                }

                debug("testing status '%s' as equal as '%s' (negated? %s, offset: %d): %s", vStatusToWaitFor,
                        t.getStatus(), negated, offset, result);

                return result;
            }
        });

        info("... with status %s set to '%s'", (negated ? "*NOT*" : " "), vStatusToWaitFor);
    }

    {
        if (isNotBlank(healthToWaitFor)) {
            result.add(new Predicate<EnvironmentDescription>() {
                @Override
                public boolean apply(EnvironmentDescription t) {
                    return t.getHealth().equals(healthToWaitFor);
                }
            });

            info("... with health equal to '%s'", healthToWaitFor);
        }
    }
    return result;
}

From source file:br.com.ingenieux.mojo.beanstalk.env.ReplaceEnvironmentMojo.java

License:Apache License

@Override
protected Object executeInternal() throws Exception {
    solutionStack = lookupSolutionStack(solutionStack);

    /*/*from   w  w w .ja v  a 2  s.co m*/
     * Is the desired cname not being used by other environments? If so,
     * just launch the environment
     */
    if (!hasEnvironmentFor(applicationName, cnamePrefix)) {
        if (getLog().isInfoEnabled()) {
            getLog().info("Just launching a new environment.");
        }

        return super.executeInternal();
    }

    /*
       * 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 environmentRef, and launches a new environment
     */
    String cnamePrefixToCreate = getCNamePrefixToCreate();

    if (getLog().isInfoEnabled()) {
        getLog().info("Creating a new environment on " + cnamePrefixToCreate + ".elasticbeanstalk.com");
    }

    if (copyOptionSettings) {
        copyOptionSettings(curEnv);
    }

    if (!solutionStack.equals(curEnv.getSolutionStackName()) && copySolutionStack) {
        if (getLog().isWarnEnabled()) {
            getLog().warn(format(
                    "(btw, we're launching with solutionStack/ set to '%s' based on the existing env instead of the "
                            + "default ('%s'). If this is not the desired behavior please set the copySolutionStack property to"
                            + " false.",
                    curEnv.getSolutionStackName(), solutionStack));
        }

        solutionStack = curEnv.getSolutionStackName();
    }

    /**
     * TODO: Spend a comfy Saturday Afternoon in a Coffee Shop trying to figure out this nice boolean logic with Karnaugh Maps sponsored by Allogy
     */

    boolean userWantsHealthyExitStatus = mustBeHealthy;
    final boolean currentEnvironmentIsRed = "Red".equals(curEnv.getHealth());

    if (redToRedOkay) {
        //Side-effect: must be before createEnvironment() and waitForEnvironment() to effect superclass behavior.
        mustBeHealthy = !currentEnvironmentIsRed;
    }

    /**
     * // I really meant it.
     */

    String newEnvironmentName = getNewEnvironmentName(
            StringUtils.defaultString(this.environmentName, curEnv.getEnvironmentName()));

    if (getLog().isInfoEnabled()) {
        getLog().info("And it'll be named " + newEnvironmentName);
        getLog().info("And it will replace a '" + curEnv.getHealth() + "' enviroment");
    }

    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,
                        newEnvDesc);
                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 (type: %s)", i,
                        maxAttempts, exc.getMessage(), exc.getClass()));

                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);

            throw new MojoFailureException(message);
        }
    }

    /*
       * Terminates the previous environment
     */
    terminateEnvironment(curEnv.getEnvironmentId());

    /**
     * TODO: I really need a Saturday Afternoon in order to understand this ffs.
     */
    if (currentEnvironmentIsRed && userWantsHealthyExitStatus) {
        final String newHealth = newEnvDesc.getHealth();

        if (newHealth.equals("Green")) {
            getLog().info("Previous environment was 'Red', new environment is 'Green' (for the moment)");
        } else {
            getLog().warn(format("Previous environment was 'Red', replacement environment is currently '%s'",
                    newHealth));

            //NB: we have already switched from one broken service to another, so this is more for the build status indicator...
            newEnvDesc = waitForGreenEnvironment(createEnvResult.getEnvironmentId());
        }
    }

    return createEnvResult;
}