Package org.wso2.carbon.mediator.autoscale

Source Code of org.wso2.carbon.mediator.autoscale.Initializer

/*                                                                            
* Copyright 2004,2005 The Apache Software Foundation.                        
*                                                                            
* Licensed 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.wso2.carbon.mediator.autoscale;

import org.wso2.carbon.ec2client.EC2Client;
import org.wso2.carbon.ec2client.data.Address;
import org.wso2.carbon.ec2client.data.InstanceState;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;

/**
*
*/
public class Initializer {

    private static Log log = LogFactory.getLog(Initializer.class);
   
    public static void main(String[] args) {
        log.debug("Initializing system...");
        try {
            EC2Client ec2Client = new EC2Client(args[0], args[1]);
            String elasticIP = System.getenv("ELASTIC_IP");

            // Assign the elastic IP to itself, if not already assigned
            List<Address> addressList = ec2Client.describeAddresses(new String[]{elasticIP});
            String localInstanceId = System.getenv("instance_id");

            // Associate this address with this instance only if any other instance has not assigned it to itself
            for (Address address : addressList) {
                if (address.getInstance() == null) {
                    ec2Client.associateAddress(localInstanceId, elasticIP);
                } else if (!address.getInstance().getInstanceId().equals(localInstanceId) &&
                           !(address.getInstance().getCurrentState().equals(InstanceState.RUNNING) || address.getInstance().getCurrentState().equals(InstanceState.PENDING))) {
                    ec2Client.associateAddress(localInstanceId, elasticIP);
                }
            }
        } catch (Exception e) {
            log.fatal("Cannot initialize system", e);
            e.printStackTrace();
        }
    }
}
TOP

Related Classes of org.wso2.carbon.mediator.autoscale.Initializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.