stratos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject [16/32] stratos git commit: Finding the region and availability zone from member id.
Date Fri, 09 Oct 2015 14:35:29 GMT
Finding the region and availability zone from member id.


Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/da1963a0
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/da1963a0
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/da1963a0

Branch: refs/heads/gsoc-projects-2015
Commit: da1963a07f737d6cf08dbdf0dad5763828de479b
Parents: f4d8087
Author: swapnilpatilRajaram <swapnil.patil@students.iiit.ac.in>
Authored: Fri Jul 24 09:36:22 2015 +0000
Committer: swapnilpatilRajaram <swapnil.patil@students.iiit.ac.in>
Committed: Fri Jul 24 09:36:22 2015 +0000

----------------------------------------------------------------------
 .../src/main/conf/aws-credentials.conf          |  4 +-
 .../apache/stratos/aws/extension/AWSHelper.java | 93 +++++++++++++++-----
 .../stratos/aws/extension/AWSLoadBalancer.java  | 69 +++++++++++----
 .../apache/stratos/aws/extension/Constants.java |  2 -
 4 files changed, 123 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/da1963a0/extensions/load-balancer/aws-extension/src/main/conf/aws-credentials.conf
----------------------------------------------------------------------
diff --git a/extensions/load-balancer/aws-extension/src/main/conf/aws-credentials.conf b/extensions/load-balancer/aws-extension/src/main/conf/aws-credentials.conf
index 0dbcb4a..d9d296c 100644
--- a/extensions/load-balancer/aws-extension/src/main/conf/aws-credentials.conf
+++ b/extensions/load-balancer/aws-extension/src/main/conf/aws-credentials.conf
@@ -1,4 +1,2 @@
 access-key=
-secret-key=
-availability-zone=
-region=
\ No newline at end of file
+secret-key=
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/stratos/blob/da1963a0/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
----------------------------------------------------------------------
diff --git a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
index 924d3bd..9ec10de 100644
--- a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
+++ b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSHelper.java
@@ -41,8 +41,6 @@ import com.amazonaws.services.elasticloadbalancing.model.*;
 public class AWSHelper {
 	private String awsAccessKey;
 	private String awsSecretKey;
-	private String availabilityZone;
-	private String region;
 
 	private BasicAWSCredentials awsCredentials;
 	private ClientConfiguration clientConfiguration;
@@ -71,17 +69,12 @@ public class AWSHelper {
 					.getProperty(Constants.AWS_ACCESS_KEY);
 			this.awsSecretKey = properties
 					.getProperty(Constants.AWS_SECRET_KEY);
-			this.availabilityZone = properties
-					.getProperty(Constants.AVAILABILITY_ZONE_KEY);
-			this.region = properties.getProperty(Constants.REGION_KEY);
 
 			awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
 			clientConfiguration = new ClientConfiguration();
 
 			lbClient = new AmazonElasticLoadBalancingClient(awsCredentials,
 					clientConfiguration);
-			lbClient.setEndpoint("elasticloadbalancing." + this.region
-					+ ".amazonaws.com");
 
 		} catch (IOException e) {
 			log.error("Error reading aws configuration file.");
@@ -102,9 +95,11 @@ public class AWSHelper {
 	 * 
 	 * @param name
 	 * @param listeners
+	 * @param region
 	 * @return DNS name of newly created load balancer
 	 */
-	public String createLoadBalancer(String name, List<Listener> listeners) {
+	public String createLoadBalancer(String name, List<Listener> listeners,
+			String region) {
 
 		log.info("Creating load balancer " + name);
 
@@ -114,10 +109,13 @@ public class AWSHelper {
 		createLoadBalancerRequest.setListeners(listeners);
 
 		Set<String> availabilityZones = new HashSet<String>();
-		availabilityZones.add(availabilityZone);
+		availabilityZones.add(getAvailabilityZoneFromRegion(region));
 
 		createLoadBalancerRequest.setAvailabilityZones(availabilityZones);
 
+		lbClient.setEndpoint("elasticloadbalancing." + region
+				+ ".amazonaws.com");
+
 		try {
 
 			CreateLoadBalancerResult clbResult = lbClient
@@ -138,8 +136,9 @@ public class AWSHelper {
 	 * with which this load balancer was associated, is removed.
 	 * 
 	 * @param loadBalancerName
+	 * @param region
 	 */
-	public void deleteLoadBalancer(String loadBalancerName) {
+	public void deleteLoadBalancer(String loadBalancerName, String region) {
 
 		log.info("Deleting load balancer " + loadBalancerName);
 
@@ -147,6 +146,9 @@ public class AWSHelper {
 		deleteLoadBalancerRequest.setLoadBalancerName(loadBalancerName);
 
 		try {
+			lbClient.setEndpoint("elasticloadbalancing." + region
+					+ ".amazonaws.com");
+
 			lbClient.deleteLoadBalancer(deleteLoadBalancerRequest);
 			return;
 
@@ -163,9 +165,10 @@ public class AWSHelper {
 	 * 
 	 * @param loadBalancerName
 	 * @param instances
+	 * @param region
 	 */
 	public void registerInstancesToLoadBalancer(String loadBalancerName,
-			List<Instance> instances) {
+			List<Instance> instances, String region) {
 
 		log.info("Attaching instance " + instances.get(0)
 				+ " to load balancer + " + loadBalancerName);
@@ -174,7 +177,9 @@ public class AWSHelper {
 				loadBalancerName, instances);
 
 		try {
-
+			lbClient.setEndpoint("elasticloadbalancing." + region
+					+ ".amazonaws.com");
+			
 			RegisterInstancesWithLoadBalancerResult result = lbClient
 					.registerInstancesWithLoadBalancer(registerInstancesWithLoadBalancerRequest);
 			return;
@@ -193,9 +198,10 @@ public class AWSHelper {
 	 * 
 	 * @param loadBalancerName
 	 * @param instances
+	 * @param region
 	 */
 	public void deregisterInstancesFromLoadBalancer(String loadBalancerName,
-			List<Instance> instances) {
+			List<Instance> instances, String region) {
 
 		log.info("Detaching instance " + instances.get(0)
 				+ " from load balancer + " + loadBalancerName);
@@ -204,6 +210,9 @@ public class AWSHelper {
 				loadBalancerName, instances);
 
 		try {
+			lbClient.setEndpoint("elasticloadbalancing." + region
+					+ ".amazonaws.com");
+			
 			DeregisterInstancesFromLoadBalancerResult result = lbClient
 					.deregisterInstancesFromLoadBalancer(deregisterInstancesFromLoadBalancerRequest);
 			return;
@@ -220,10 +229,11 @@ public class AWSHelper {
 	 * instances, listeners associated with load balancer
 	 * 
 	 * @param loadBalancerName
+	 * @param region
 	 * @return description of the load balancer
 	 */
 	private LoadBalancerDescription getLoadBalancerDescription(
-			String loadBalancerName) {
+			String loadBalancerName, String region) {
 
 		List<String> loadBalancers = new ArrayList<String>();
 
@@ -233,6 +243,9 @@ public class AWSHelper {
 				loadBalancers);
 
 		try {
+			lbClient.setEndpoint("elasticloadbalancing." + region
+					+ ".amazonaws.com");
+			
 			DescribeLoadBalancersResult result = lbClient
 					.describeLoadBalancers(describeLoadBalancersRequest);
 
@@ -254,11 +267,12 @@ public class AWSHelper {
 	 * all attached instances are required or some should be detached.
 	 * 
 	 * @param loadBalancerName
+	 * @param region
 	 * @return list of instances attached
 	 */
-	public List<Instance> getAttachedInstances(String loadBalancerName) {
+	public List<Instance> getAttachedInstances(String loadBalancerName, String region)
{
 		try {
-			LoadBalancerDescription lbDescription = getLoadBalancerDescription(loadBalancerName);
+			LoadBalancerDescription lbDescription = getLoadBalancerDescription(loadBalancerName, region);
 
 			if (lbDescription == null) {
 				log.warn("Could not find description of load balancer "
@@ -283,9 +297,10 @@ public class AWSHelper {
 	 * 
 	 * @param loadBalancerName
 	 * @param listeners
+	 * @param region
 	 */
 	public void addListenersToLoadBalancer(String loadBalancerName,
-			List<Listener> listeners) {
+			List<Listener> listeners, String region) {
 		if (listeners.size() == 0)
 			return;
 
@@ -295,6 +310,8 @@ public class AWSHelper {
 				.setLoadBalancerName(loadBalancerName);
 
 		try {
+			lbClient.setEndpoint("elasticloadbalancing." + region
+					+ ".amazonaws.com");
 
 			lbClient.createLoadBalancerListeners(createLoadBalancerListenersRequest);
 			return;
@@ -312,9 +329,10 @@ public class AWSHelper {
 	 * 
 	 * @param loadBalancerName
 	 * @param listeners
+	 * @param region
 	 */
 	public void removeListenersFromLoadBalancer(String loadBalancerName,
-			List<Listener> listeners) {
+			List<Listener> listeners, String region) {
 		if (listeners.size() == 0)
 			return;
 
@@ -332,6 +350,8 @@ public class AWSHelper {
 				.setLoadBalancerPorts(loadBalancerPorts);
 
 		try {
+			lbClient.setEndpoint("elasticloadbalancing." + region
+					+ ".amazonaws.com");
 
 			lbClient.deleteLoadBalancerListeners(deleteLoadBalancerListenersRequest);
 			return;
@@ -348,11 +368,12 @@ public class AWSHelper {
 	 * deciding if all the listeners are necessary or some should be removed.
 	 * 
 	 * @param loadBalancerName
+	 * @param region
 	 * @return list of instances attached to load balancer
 	 */
-	public List<Listener> getAttachedListeners(String loadBalancerName) {
+	public List<Listener> getAttachedListeners(String loadBalancerName, String region)
{
 		try {
-			LoadBalancerDescription lbDescription = getLoadBalancerDescription(loadBalancerName);
+			LoadBalancerDescription lbDescription = getLoadBalancerDescription(loadBalancerName, region);
 
 			if (lbDescription == null) {
 				log.warn("Could not find description of load balancer "
@@ -421,8 +442,10 @@ public class AWSHelper {
 	}
 
 	/**
+	 * Extract instance id in IaaS side from member instance name
+	 * 
 	 * @param memberInstanceName
-	 * @return
+	 * @return instance id in IaaS
 	 */
 	public String getAWSInstanceName(final String memberInstanceName) {
 		if (memberInstanceName.contains("/")) {
@@ -432,4 +455,32 @@ public class AWSHelper {
 			return memberInstanceName;
 		}
 	}
+
+	/**
+	 * Extract IaaS region from member instance name
+	 * 
+	 * @param memberInstanceName
+	 * @return IaaS region to which member belongs
+	 */
+	public String getAWSRegion(final String memberInstanceName) {
+		if (memberInstanceName.contains("/")) {
+			return memberInstanceName.substring(0,
+					memberInstanceName.indexOf("/"));
+		} else {
+			return null;
+		}
+	}
+
+	/**
+	 * Get availability zone from region
+	 * 
+	 * @param region
+	 * @return Availability zone of IaaS
+	 */
+	public String getAvailabilityZoneFromRegion(String region) {
+		if (region != null) {
+			return region + "a";
+		} else
+			return null;
+	}
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/da1963a0/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
----------------------------------------------------------------------
diff --git a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
index 99d6906..2d4f40c 100644
--- a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
+++ b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/AWSLoadBalancer.java
@@ -39,12 +39,12 @@ public class AWSLoadBalancer implements LoadBalancer {
 	private static final Log log = LogFactory.getLog(AWSLoadBalancer.class);
 
 	// A map <clusterId, load balancer id>
-	private HashMap<String, String> clusterIdToLoadBalancerMap;
+	private HashMap<String, LoadBalancerInfo> clusterIdToLoadBalancerMap;
 
 	private AWSHelper awsHelper;
 
 	public AWSLoadBalancer() throws LoadBalancerExtensionException {
-		clusterIdToLoadBalancerMap = new HashMap<String, String>();
+		clusterIdToLoadBalancerMap = new HashMap<String, LoadBalancerInfo>();
 		awsHelper = new AWSHelper();
 	}
 
@@ -70,14 +70,17 @@ public class AWSLoadBalancer implements LoadBalancer {
 						// A load balancer is already present for this cluster
 						// Get the load balancer and update it.
 
-						String loadBalancerName = clusterIdToLoadBalancerMap
+						LoadBalancerInfo loadBalancerInfo = clusterIdToLoadBalancerMap
 								.get(cluster.getClusterId());
 
+						String loadBalancerName = loadBalancerInfo.getName();
+						String region = loadBalancerInfo.getRegion();
+
 						// 1. Get all the instances attached
 						// Add/remove instances as necessary
 
 						List<Instance> attachedInstances = awsHelper
-								.getAttachedInstances(loadBalancerName);
+								.getAttachedInstances(loadBalancerName, region);
 
 						Collection<Member> clusterInstances = cluster
 								.getMembers();
@@ -89,6 +92,7 @@ public class AWSLoadBalancer implements LoadBalancer {
 						}
 
 						List<Instance> instancesToAddToLoadBalancer = new ArrayList<Instance>();
+						List<Instance> awsInstancesInCluster = new ArrayList<Instance>();
 
 						for (Member member : clusterInstances) {
 							// if instance id of member is not in
@@ -105,6 +109,8 @@ public class AWSLoadBalancer implements LoadBalancer {
 									awsHelper.getAWSInstanceName(member
 											.getInstanceId()));
 
+							awsInstancesInCluster.add(instance);
+
 							if (attachedInstances == null
 									|| !attachedInstances.contains(instance)) {
 								instancesToAddToLoadBalancer.add(instance);
@@ -115,8 +121,7 @@ public class AWSLoadBalancer implements LoadBalancer {
 						List<Instance> instancesToRemoveFromLoadBalancer = new ArrayList<Instance>();
 
 						for (Instance instance : attachedInstances) {
-							if (!clusterInstances.contains(instance
-									.getInstanceId())) {
+							if (!awsInstancesInCluster.contains(instance)) {
 								instancesToRemoveFromLoadBalancer.add(instance);
 							}
 						}
@@ -124,12 +129,12 @@ public class AWSLoadBalancer implements LoadBalancer {
 						if (instancesToRemoveFromLoadBalancer.size() > 0)
 							awsHelper.deregisterInstancesFromLoadBalancer(
 									loadBalancerName,
-									instancesToRemoveFromLoadBalancer);
+									instancesToRemoveFromLoadBalancer, region);
 
 						if (instancesToAddToLoadBalancer.size() > 0)
 							awsHelper.registerInstancesToLoadBalancer(
 									loadBalancerName,
-									instancesToAddToLoadBalancer);
+									instancesToAddToLoadBalancer, region);
 
 						// 2. Get all the listeners
 						// Add/Remove listeners as necessary
@@ -145,7 +150,7 @@ public class AWSLoadBalancer implements LoadBalancer {
 						// Need to remove only if ... same for above reason
 
 						List<Listener> attachedListeners = awsHelper
-								.getAttachedListeners(loadBalancerName);
+								.getAttachedListeners(loadBalancerName, region);
 
 						List<Listener> listenersToAddToLoadBalancer = new ArrayList<Listener>();
 
@@ -167,12 +172,12 @@ public class AWSLoadBalancer implements LoadBalancer {
 						if (listenersToRemoveFromLoadBalancer.size() > 0)
 							awsHelper.removeListenersFromLoadBalancer(
 									loadBalancerName,
-									listenersToRemoveFromLoadBalancer);
+									listenersToRemoveFromLoadBalancer, region);
 
 						if (listenersToAddToLoadBalancer.size() > 0)
 							awsHelper.addListenersToLoadBalancer(
 									loadBalancerName,
-									listenersToAddToLoadBalancer);
+									listenersToAddToLoadBalancer, region);
 
 						// Update domain mappings
 
@@ -187,9 +192,12 @@ public class AWSLoadBalancer implements LoadBalancer {
 						String loadBalancerName = awsHelper
 								.getLoadBalancerName(cluster.getClusterId());
 
+						String region = awsHelper.getAWSRegion(clusterInstances
+								.iterator().next().getInstanceId());
+
 						String loadBalancerDNSName = awsHelper
 								.createLoadBalancer(loadBalancerName,
-										listenersForThisService);
+										listenersForThisService, region);
 
 						log.info("Load balancer '" + loadBalancerDNSName
 								+ "' created for cluster '"
@@ -213,12 +221,15 @@ public class AWSLoadBalancer implements LoadBalancer {
 						}
 
 						awsHelper.registerInstancesToLoadBalancer(
-								loadBalancerName, instances);
+								loadBalancerName, instances, region);
 
 						// Create domain mappings
 
+						LoadBalancerInfo loadBalancerInfo = new LoadBalancerInfo(
+								loadBalancerName, region);
+
 						clusterIdToLoadBalancerMap.put(cluster.getClusterId(),
-								loadBalancerName);
+								loadBalancerInfo);
 						activeClusters.add(cluster.getClusterId());
 					}
 				}
@@ -231,7 +242,9 @@ public class AWSLoadBalancer implements LoadBalancer {
 				if (!activeClusters.contains(clusterId)) {
 					// Remove load balancer for that cluster.
 					awsHelper.deleteLoadBalancer(clusterIdToLoadBalancerMap
-							.get(clusterId));
+							.get(clusterId).getName(),
+							clusterIdToLoadBalancerMap.get(clusterId)
+									.getRegion());
 					clusterIdToLoadBalancerMap.remove(clusterId);
 				}
 			}
@@ -256,14 +269,32 @@ public class AWSLoadBalancer implements LoadBalancer {
 
 	public void stop() throws LoadBalancerExtensionException {
 		// Remove all load balancers
-
-		for (String loadBalancerName : clusterIdToLoadBalancerMap.values()) {
+		for (LoadBalancerInfo loadBalancerInfo : clusterIdToLoadBalancerMap
+				.values()) {
 			// remove load balancer
-			awsHelper.deleteLoadBalancer(loadBalancerName);
-
+			awsHelper.deleteLoadBalancer(loadBalancerInfo.getName(),
+					loadBalancerInfo.getRegion());
 			// Check what all needs to be done
 		}
 
 		// Remove domain mappings
 	}
 }
+
+class LoadBalancerInfo {
+	private String name;
+	private String region;
+
+	public LoadBalancerInfo(String name, String region) {
+		this.name = name;
+		this.region = region;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public String getRegion() {
+		return region;
+	}
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/da1963a0/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Constants.java
----------------------------------------------------------------------
diff --git a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Constants.java
b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Constants.java
index d3c4d42..71c7ef7 100644
--- a/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Constants.java
+++ b/extensions/load-balancer/aws-extension/src/main/java/org/apache/stratos/aws/extension/Constants.java
@@ -32,6 +32,4 @@ public class Constants {
     public static final String AWS_CREDENTIALS_FILE="aws.credentials.file";
     public static final String AWS_ACCESS_KEY = "access-key";
     public static final String AWS_SECRET_KEY = "secret-key";
-    public static final String AVAILABILITY_ZONE_KEY = "availability-zone";
-    public static final String REGION_KEY = "region";
 }


Mime
View raw message