airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sma...@apache.org
Subject [3/4] airavata git commit: Reuse existing floating IPs if present instead of requesting new every time.
Date Sun, 12 Jun 2016 21:04:12 GMT
Reuse existing floating IPs if present instead of requesting new every time.


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

Branch: refs/heads/develop
Commit: a69c789e205eff1f27020de8c1fb3185be39af21
Parents: 4708005
Author: Mangirish Wagle <vaglomangirish@gmail.com>
Authored: Wed May 25 22:59:06 2016 -0500
Committer: Mangirish Wagle <vaglomangirish@gmail.com>
Committed: Wed May 25 22:59:06 2016 -0500

----------------------------------------------------------------------
 .../cloud/intf/impl/OpenstackIntfImpl.java      | 35 ++++++++++++--
 .../airavata/cloud/util/OpenstackIntfUtil.java  | 49 ++++++++++++++++++++
 2 files changed, 79 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/airavata/blob/a69c789e/modules/cloud/cloud-provisioning/src/main/java/org/apache/airavata/cloud/intf/impl/OpenstackIntfImpl.java
----------------------------------------------------------------------
diff --git a/modules/cloud/cloud-provisioning/src/main/java/org/apache/airavata/cloud/intf/impl/OpenstackIntfImpl.java
b/modules/cloud/cloud-provisioning/src/main/java/org/apache/airavata/cloud/intf/impl/OpenstackIntfImpl.java
index 650f491..9e7a56e 100644
--- a/modules/cloud/cloud-provisioning/src/main/java/org/apache/airavata/cloud/intf/impl/OpenstackIntfImpl.java
+++ b/modules/cloud/cloud-provisioning/src/main/java/org/apache/airavata/cloud/intf/impl/OpenstackIntfImpl.java
@@ -31,6 +31,7 @@ import org.apache.airavata.cloud.intf.CloudInterface;
 import org.apache.airavata.cloud.openstack.OS4JClientProvider;
 import org.apache.airavata.cloud.util.Constants;
 import org.apache.airavata.cloud.util.IPType;
+import org.apache.airavata.cloud.util.OpenstackIntfUtil;
 import org.openstack4j.api.Builders;
 import org.openstack4j.api.OSClient;
 import org.openstack4j.model.compute.ActionResponse;
@@ -254,8 +255,29 @@ public class OpenstackIntfImpl implements CloudInterface {
 		try {
 			Server server = this.getServer(serverId);
 
+			// Floating IP to allocate.
+			FloatingIP floatIp = null;
+
 			if(server != null) {
-				FloatingIP floatIp = os.compute().floatingIps().allocateIP(properties.getProperty(Constants.OS_FLOATING_IP_POOL));
+				List<? extends FloatingIP> floatIPList = os.compute().floatingIps().list();
+
+				// Iterate through the floating ips from the pool present if any.
+				if(floatIPList.size() > 0) {
+					for(FloatingIP ip : floatIPList) {
+						logger.info("Checking if floating ip : " + ip.getFloatingIpAddress() + " is free to
use.");
+						Boolean isFloatingIpUsed = OpenstackIntfUtil.isFloatingIPUsed(ip);
+						if( isFloatingIpUsed != null && !isFloatingIpUsed ) {
+							floatIp = ip;
+							logger.info("Floating ip " + ip.getFloatingIpAddress() + " found to be free.");
+							break;
+						}
+					}
+				}
+				// If all floating IPs are used, or there are no free floating ips, create new one.
+				if(floatIp == null){
+					floatIp = os.compute().floatingIps().allocateIP(properties.getProperty(Constants.OS_FLOATING_IP_POOL));
+					logger.info("Created new floating ip " + floatIp.getFloatingIpAddress());
+				}
 
 				if(floatIp != null) {
 					String ipAddr = floatIp.getFloatingIpAddress();
@@ -264,12 +286,15 @@ public class OpenstackIntfImpl implements CloudInterface {
 						ActionResponse response = os.compute().floatingIps().addFloatingIP(server, ipAddr);
 						logger.info(response.isSuccess() + ":" + response.getCode() + ":" + response.getFault()
+ ":" + response.toString());
 
-						logger.info("Floating IP "+ ipAddr + " assigned successfully to server with ID: " +
serverId);
+						if(response.isSuccess()) {
+							logger.info("Floating IP "+ ipAddr + " assigned successfully to server with ID: "
+ serverId);
+						}
+						else {
+							logger.error("Failed to associate Floating IP.");
+						}
 					}
 				}
-				else {
-					logger.error("Failed to associate Floating IP.");
-				}
+
 			}
 		}
 		catch( Exception ex ) {

http://git-wip-us.apache.org/repos/asf/airavata/blob/a69c789e/modules/cloud/cloud-provisioning/src/main/java/org/apache/airavata/cloud/util/OpenstackIntfUtil.java
----------------------------------------------------------------------
diff --git a/modules/cloud/cloud-provisioning/src/main/java/org/apache/airavata/cloud/util/OpenstackIntfUtil.java
b/modules/cloud/cloud-provisioning/src/main/java/org/apache/airavata/cloud/util/OpenstackIntfUtil.java
new file mode 100644
index 0000000..4524260
--- /dev/null
+++ b/modules/cloud/cloud-provisioning/src/main/java/org/apache/airavata/cloud/util/OpenstackIntfUtil.java
@@ -0,0 +1,49 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.airavata.cloud.util;
+
+import org.openstack4j.model.compute.FloatingIP;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class OpenstackIntfUtil {
+
+	// Initializing Logger
+	private static Logger logger = LoggerFactory.getLogger(OpenstackIntfUtil.class);
+
+	public static Boolean isFloatingIPUsed(FloatingIP floatIp) {
+
+		try {
+
+			Boolean isUsed = floatIp.getInstanceId() != null;
+
+			return isUsed;
+
+		} catch( Exception ex ) {
+			ex.printStackTrace();
+			// TODO: Check with the team on how to handle exceptions.
+			logger.error("Failed to check if the Floating IP " + floatIp.getFloatingIpAddress() +
"is used.");
+			return null;
+		}
+	}
+
+}


Mime
View raw message