Example usage for com.amazonaws.services.autoscaling.model CreateOrUpdateTagsRequest CreateOrUpdateTagsRequest

List of usage examples for com.amazonaws.services.autoscaling.model CreateOrUpdateTagsRequest CreateOrUpdateTagsRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.autoscaling.model CreateOrUpdateTagsRequest CreateOrUpdateTagsRequest.

Prototype

CreateOrUpdateTagsRequest

Source Link

Usage

From source file:com.liferay.amazontools.AsgardAMIDeployer.java

License:Open Source License

protected String createAutoScalingGroup() throws Exception {
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = amazonAutoScalingClient
            .describeAutoScalingGroups();

    List<AutoScalingGroup> autoScalingGroups = describeAutoScalingGroupsResult.getAutoScalingGroups();

    int oldAutoScalingGroupsSize = autoScalingGroups.size();

    String availabilityZone = properties.getProperty("availability.zone");

    Map<String, String> parameters = new HashMap<String, String>();

    parameters.put("checkHealth", "true");
    parameters.put("imageId", getImageId(_imageName));

    String asgardClusterName = properties.getProperty("asgard.cluster.name");

    parameters.put("name", asgardClusterName);

    parameters.put("trafficAllowed", "true");

    if (!_parallelDeployment) {
        parameters.put("desiredCapacity", "1");
        parameters.put("min", "1");
    }/*from w  w w  .ja va  2 s .co m*/

    _jsonWebServiceClient.doPost("/" + availabilityZone + "/cluster/createNextGroup", parameters);

    for (int i = 0; i < 30; i++) {
        describeAutoScalingGroupsResult = amazonAutoScalingClient.describeAutoScalingGroups();

        autoScalingGroups = describeAutoScalingGroupsResult.getAutoScalingGroups();

        int newAutoScalingGroupsSize = autoScalingGroups.size();

        if (oldAutoScalingGroupsSize == newAutoScalingGroupsSize) {
            sleep(15);
        } else {
            break;
        }
    }

    String autoScalingGroupName = null;
    boolean created = false;
    int maxSize = 0;

    for (int i = 0; i < 30; i++) {
        String json = _jsonWebServiceClient.doGet(
                "/" + availabilityZone + "/cluster/show/" + asgardClusterName + ".json",
                Collections.<String, String>emptyMap());

        JSONArray autoScalingGroupsJSONArray = new JSONArray(json);

        JSONObject autoScalingGroupJSONObject = autoScalingGroupsJSONArray
                .getJSONObject(autoScalingGroupsJSONArray.length() - 1);

        autoScalingGroupName = autoScalingGroupJSONObject.getString("autoScalingGroupName");
        maxSize = autoScalingGroupJSONObject.getInt("maxSize");

        List<String> instanceIds = new ArrayList<String>();

        JSONArray instancesJSONArray = autoScalingGroupJSONObject.getJSONArray("instances");

        for (int j = 0; j < instancesJSONArray.length(); j++) {
            JSONObject instanceJSONObject = instancesJSONArray.getJSONObject(j);

            instanceIds.add(instanceJSONObject.getString("instanceId"));
        }

        if (instanceIds.isEmpty() || !isInService(autoScalingGroupJSONObject)) {

            sleep(15);
        } else {
            CreateTagsRequest createTagsRequest = new CreateTagsRequest();

            createTagsRequest.setResources(instanceIds);

            List<Tag> tags = new ArrayList<Tag>();

            Tag tag = new Tag();

            tag.withKey("Name");
            tag.withValue(properties.getProperty("instance.name"));

            tags.add(tag);

            createTagsRequest.setTags(tags);

            amazonEC2Client.createTags(createTagsRequest);

            CreateOrUpdateTagsRequest createOrUpdateTagsRequest = new CreateOrUpdateTagsRequest();

            com.amazonaws.services.autoscaling.model.Tag autoScalingTag = new com.amazonaws.services.autoscaling.model.Tag();

            autoScalingTag.setKey("Name");
            autoScalingTag.setPropagateAtLaunch(true);
            autoScalingTag.setResourceId(autoScalingGroupName);
            autoScalingTag.setResourceType("auto-scaling-group");
            autoScalingTag.setValue(properties.getProperty("instance.name"));

            createOrUpdateTagsRequest.withTags(autoScalingTag);

            amazonAutoScalingClient.createOrUpdateTags(createOrUpdateTagsRequest);

            created = true;

            break;
        }
    }

    if (!created) {
        throw new RuntimeException("Unable to create Auto Scaling Group " + autoScalingGroupName);
    }

    if (!_parallelDeployment) {
        int minSize = Integer.parseInt(properties.getProperty("instance.min.size"));

        if (minSize > 1) {
            checkAutoScalingGroup(autoScalingGroupName, 1);

            parameters.clear();

            parameters.put("maxSize", String.valueOf(maxSize));
            parameters.put("minSize", String.valueOf(minSize));
            parameters.put("name", autoScalingGroupName);

            _jsonWebServiceClient.doPost("/" + availabilityZone + "/cluster/resize", parameters);

            for (int i = 0; i < 30; i++) {
                String json = _jsonWebServiceClient.doGet(
                        "/" + availabilityZone + "/cluster/show/" + asgardClusterName + ".json",
                        Collections.<String, String>emptyMap());

                JSONArray autoScalingGroupsJSONArray = new JSONArray(json);

                JSONObject autoScalingGroupJSONObject = autoScalingGroupsJSONArray
                        .getJSONObject(autoScalingGroupsJSONArray.length() - 1);

                JSONArray instancesJSONArray = autoScalingGroupJSONObject.getJSONArray("instances");

                if (instancesJSONArray.length() == 1) {
                    sleep(15);
                } else {
                    break;
                }
            }
        }
    }

    return autoScalingGroupName;
}