Example usage for com.amazonaws.services.elasticloadbalancing.model Instance setInstanceId

List of usage examples for com.amazonaws.services.elasticloadbalancing.model Instance setInstanceId

Introduction

In this page you can find the example usage for com.amazonaws.services.elasticloadbalancing.model Instance setInstanceId.

Prototype


public void setInstanceId(String instanceId) 

Source Link

Document

The instance ID.

Usage

From source file:com.swap.aws.elb.client.AWSHelper.java

License:Apache License

public Instance transformInstace(com.amazonaws.services.ec2.model.Instance ec2Instance) {
    Instance elbInstance = new Instance();
    elbInstance.setInstanceId(ec2Instance.getInstanceId());
    return elbInstance;
}

From source file:org.apache.stratos.aws.extension.AWSLoadBalancer.java

License:Apache License

private Boolean addClusterMembersInfo(Collection<Member> clusterMembers, String loadBalancerName,
        String region) {//from   w  w  w.j  a  va 2  s  .c o m
    Boolean isUpdated = false;
    // Register instances in the cluster to load balancer
    List<Instance> instances = new ArrayList<Instance>();
    List<String> availabilityZones = new ArrayList<String>();

    for (Member member : clusterMembers) {
        isUpdated = true;
        String instanceId = member.getInstanceId();

        if (log.isDebugEnabled()) {
            log.debug("Instance " + awsHelper.getAWSInstanceName(instanceId)
                    + " needs to be registered to load balancer " + loadBalancerName);
        }

        Instance instance = new Instance();
        instance.setInstanceId(awsHelper.getAWSInstanceName(instanceId));

        instances.add(instance);
        // LB Common Member has a property 'EC2_AVAILABILITY_ZONE' which points to the ec2 availability
        // zone for this member. Use the property value to update the LB about the relevant zone
        String availabilityZone = getEC2AvaialbilityZoneOfMember(member);
        if (availabilityZone != null) {
            availabilityZones.add(availabilityZone);
        }

        // add stickiness policy
        if (awsHelper.getAppStickySessionCookie() != null && !awsHelper.getAppStickySessionCookie().isEmpty()) {
            CreateAppCookieStickinessPolicyResult result = awsHelper.createStickySessionPolicy(loadBalancerName,
                    awsHelper.getAppStickySessionCookie(), Constants.STICKINESS_POLICY, region);

            if (result != null) {
                // Take a single port mapping from a member, and apply the policy for
                // the LB Listener port (Proxy port of the port mapping)
                awsHelper.applyPolicyToLBListenerPorts(member.getPorts(), loadBalancerName,
                        Constants.STICKINESS_POLICY, region);
            }
        }

    }

    awsHelper.registerInstancesToLoadBalancer(loadBalancerName, instances, region);

    // update LB with the zones
    if (!availabilityZones.isEmpty() && !AWSExtensionContext.getInstance().isOperatingInVPC()) {
        awsHelper.addAvailabilityZonesForLoadBalancer(loadBalancerName, availabilityZones, region);
    }
    return isUpdated;
}

From source file:org.openinfinity.cloud.application.worker.component.EC2Worker.java

License:Apache License

private int deleteInstance(Job job) throws WorkerException {
    int returnValue = -1;
    String threadName = Thread.currentThread().getName();
    LOG.debug(threadName + ": EC2Worker:deleteInstance starting");
    EC2Wrapper ec2 = new EC2Wrapper();

    if (job.getCloud() == InstanceService.CLOUD_TYPE_AMAZON) {
        String endPoint = PropertyManager.getProperty("cloudadmin.worker.amazon.endpoint");
        ec2.setEndpoint(endPoint);//from   ww  w. ja v  a 2 s. c o m
        ec2.init(amazonCredentials, job.getCloud());
    } else if (job.getCloud() == InstanceService.CLOUD_TYPE_EUCALYPTUS) {
        String endPoint = PropertyManager.getProperty("cloudadmin.worker.eucalyptus.endpoint");
        ec2.setEndpoint(endPoint);
        ec2.init(eucaCredentials, job.getCloud());
    }
    Instance instance = instanceService.getInstance(job.getInstanceId());

    if (instance != null) {
        LOG.info(threadName + ": Instance id: " + instance.getInstanceId());
        Collection<Cluster> clusters = clusterService.getClusters(instance.getInstanceId());
        if (clusters == null || clusters.size() == 0) {
            Key key = keyService.getKeyByInstanceId(instance.getInstanceId());
            if (key != null) {
                ec2.deleteKeypair(key.getName());
                keyService.deleteKey(instance.getInstanceId());
            }
            instanceService.deleteInstance(job.getInstanceId());
            LOG.info(threadName + ": Instance " + instance.getInstanceId() + " deleted");
        } else {
            LOG.info(threadName + ": Found " + clusters.size() + " clusters");
            LOG.debug(threadName + ": Iterating clusters");
            Iterator<Cluster> i = clusters.iterator();

            while (i.hasNext()) {
                Cluster c = i.next();
                ElasticIP eIP = arService.getClustersElasticIP(c.getId());
                if (eIP != null && eIP.getIpAddress() != null) {
                    try {
                        ec2.removeElasticIPFromInstance(eIP.getIpAddress());
                    } catch (Exception ex) {
                        LOG.error("Error removing elastic IP " + eIP.getIpAddress() + " from cluster "
                                + c.getId());
                    }
                    arService.freeElasticIP(eIP);
                }

                if (job.getCloud() == InstanceService.CLOUD_TYPE_AMAZON) {
                    String cmtGroup = PropertyManager.getProperty("cloudadmin.worker.cmt.securitygroup.amazon");
                    String securityGroupOwner = PropertyManager
                            .getProperty("cloudadmin.worker.amazon.securitygroup.owner");
                    ec2.revokeGroup(cmtGroup, c.getSecurityGroupName(), securityGroupOwner, 0, 65535, "tcp");
                    ec2.revokeGroup(cmtGroup, c.getSecurityGroupName(), securityGroupOwner, 0, 65535, "udp");
                }
                Collection<Machine> machines = machineService.getMachinesInCluster(c.getId());
                ArrayList<com.amazonaws.services.elasticloadbalancing.model.Instance> instanceL = new ArrayList<com.amazonaws.services.elasticloadbalancing.model.Instance>();
                Iterator<Machine> j = machines.iterator();
                while (j.hasNext()) {
                    Machine machine = j.next();
                    com.amazonaws.services.elasticloadbalancing.model.Instance aInstance = new com.amazonaws.services.elasticloadbalancing.model.Instance();
                    aInstance.setInstanceId(machine.getInstanceId());
                    instanceL.add(aInstance);
                }
                try {
                    ec2.deregisterInstancesToLoadBalancer(instanceL, c.getLbName());
                } catch (Exception ex) {
                    LOG.warn(threadName + ": Error with the loadbalancer in cluster " + c.getName()
                            + ", continuing with the delete");
                }
                j = machines.iterator();
                while (j.hasNext()) {
                    Machine machine = j.next();
                    String ebsVolumeId = machine.getEbsVolumeId();
                    if (ebsVolumeId != null) {
                        LOG.info(threadName + ": We need to delete EBS volume too");
                        ec2.detachVolume(ebsVolumeId);
                        String volumeState = ec2.getVolumeState(ebsVolumeId);
                        int maxWaitCount = 60;
                        while (!volumeState.equalsIgnoreCase("available") && maxWaitCount > 0) {
                            LOG.info(threadName + ": Waiting the volume " + ebsVolumeId + " to be detached");
                            try {
                                Thread.sleep(3000);
                            } catch (InterruptedException e) {
                                LOG.warn(threadName + ": Someone just interrupted my sleep! " + e.getMessage());
                            }
                            maxWaitCount--;
                            volumeState = ec2.getVolumeState(ebsVolumeId);
                        }
                        LOG.info(threadName + ": Deleting volume " + ebsVolumeId);
                        ec2.deleteVolume(machine.getEbsVolumeId());
                    }
                    ec2.terminateInstance(machine.getInstanceId());
                    // usage
                    try {
                        usageService.stopVirtualMachineUsageMonitoring(instance.getOrganizationid(),
                                c.getType(), c.getId(), machine.getId());
                    } catch (Exception e) {
                        LOG.error(threadName + ": Error stopping usage monitoring " + e.getMessage());
                    }
                }
                try {
                    ec2.deleteLoadBalancer(c.getLbName(), c.getLbInstanceId());
                    if (instance.getCloudType() == InstanceService.CLOUD_TYPE_EUCALYPTUS) {
                        // usage
                        Machine m = machineService.getMachine(c.getLbInstanceId());
                        try {
                            usageService.stopVirtualMachineUsageMonitoring(instance.getOrganizationid(),
                                    c.getType(), c.getId(), m.getId());
                        } catch (Exception e) {
                            LOG.error(threadName + ": Error stopping usage monitoring " + e.getMessage());
                        }
                    }
                } catch (Exception ex) {
                    LOG.warn(threadName + ": Error deleting loadbalacer, continuing with the delete");
                }
                boolean secGroupDeleted = false;
                int secGroupDeleteCounter = 0;
                while (!secGroupDeleted) {
                    try {
                        ec2.deleteSecurityGroup(c.getSecurityGroupName());
                        secGroupDeleted = true;
                    } catch (Exception e) {
                        LOG.info(
                                threadName + ": Could not delete security group yet, trying again in a moment");
                        secGroupDeleted = false;
                        if (secGroupDeleteCounter >= 30) {
                            throw new WorkerException("Could not delete security group");
                        }
                        try {
                            Thread.sleep(10000);
                        } catch (Exception ex) {
                            LOG.error(threadName + ": Someone is trying to stop my sleep!");
                        }
                        secGroupDeleteCounter++;
                    }
                }
                if (c.getMulticastAddress() != null && c.getMulticastAddress().length() > 0) {
                    maService.deleteMulticastAddress(c.getMulticastAddress());
                }

                clusterService.deleteCluster(c);
                deployerService.terminateDeploymentsForCluster(c.getId());
                centralizedPropertiesService.terminatePropertiesForCluster(c.getId());
                LOG.info(threadName + ": Cluster " + c.getName() + " deleted");
            }
            Key key = keyService.getKeyByInstanceId(job.getInstanceId());
            if (key != null) {
                ec2.deleteKeypair(key.getName());
                keyService.deleteKey(job.getInstanceId());
            }

            arService.deleteInstanceIPs(job.getInstanceId());
            instanceService.deleteInstance(job.getInstanceId());
            LOG.info(threadName + ": Instance " + job.getInstanceId() + " deleted");
        }
    }
    returnValue = 1;

    return returnValue;
}

From source file:org.openinfinity.cloud.application.worker.component.EC2Worker.java

License:Apache License

private void deleteMachinesInCluster(Cluster c, String threadName, EC2Wrapper ec2) {
    Instance oiInstance = instanceService.getInstance(c.getInstanceId());
    Collection<Machine> machines = machineService.getMachinesInCluster(c.getId());
    ArrayList<com.amazonaws.services.elasticloadbalancing.model.Instance> instanceL = new ArrayList<com.amazonaws.services.elasticloadbalancing.model.Instance>();
    Iterator<Machine> j = machines.iterator();
    while (j.hasNext()) {
        Machine machine = j.next();//from ww w  .  ja  v  a  2 s  .c o m
        com.amazonaws.services.elasticloadbalancing.model.Instance aInstance = new com.amazonaws.services.elasticloadbalancing.model.Instance();
        aInstance.setInstanceId(machine.getInstanceId());
        instanceL.add(aInstance);
    }
    try {
        ec2.deregisterInstancesToLoadBalancer(instanceL, c.getLbName());
    } catch (Exception ex) {
        LOG.warn(threadName + ": Error with the loadbalancer in cluster " + c.getName()
                + ", continuing with the delete");
    }
    j = machines.iterator();
    while (j.hasNext()) {
        Machine machine = j.next();
        String ebsVolumeId = machine.getEbsVolumeId();
        if (ebsVolumeId != null) {
            LOG.info(threadName + ": We need to delete EBS volume too");
            ec2.detachVolume(ebsVolumeId);
            String volumeState = ec2.getVolumeState(ebsVolumeId);
            int maxWaitCount = 60;
            while (!volumeState.equalsIgnoreCase("available") && maxWaitCount > 0) {
                LOG.info(threadName + ": Waiting the volume " + ebsVolumeId + " to be detached");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    LOG.warn(threadName + ": Someone just interrupted my sleep! " + e.getMessage());
                }
                maxWaitCount--;
                volumeState = ec2.getVolumeState(ebsVolumeId);
            }
            LOG.info(threadName + ": Deleting volume " + ebsVolumeId);
            ec2.deleteVolume(machine.getEbsVolumeId());
        }
        ec2.terminateInstance(machine.getInstanceId());
        // usage
        try {
            usageService.stopVirtualMachineUsageMonitoring(oiInstance.getOrganizationid(), c.getType(),
                    c.getId(), machine.getId());
        } catch (Exception e) {
            LOG.error(threadName + ": Error stopping usage monitoring " + e.getMessage());
        }
    }
    try {
        ec2.deleteLoadBalancer(c.getLbName(), c.getLbInstanceId());
        if (oiInstance.getCloudType() == InstanceService.CLOUD_TYPE_EUCALYPTUS) {
            // usage
            Machine m = machineService.getMachine(c.getLbInstanceId());
            try {
                usageService.stopVirtualMachineUsageMonitoring(oiInstance.getOrganizationid(), c.getType(),
                        c.getId(), m.getId());
            } catch (Exception e) {
                LOG.error(threadName + ": Error stopping usage monitoring " + e.getMessage());
            }
        }
    } catch (Exception ex) {
        LOG.warn(threadName + ": Error deleting loadbalacer, continuing with the delete");
    }
}