
package com.platform.glusterfs;

import java.util.List;

import org.apache.log4j.Logger;
import org.hamcrest.core.Is;

import com.platform.utils.Constant;
import com.platform.utils.Support;

public class SetCluster {
	public static Logger log =   Logger.getLogger ( SetCluster.class); 
	
	/**
	 * 向集群中添加节点
	 * 返回1表示添加成功；-1表示ip不合法，-2 表示出错，0表示添加失败
	 * @param peerip
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public int probePeer(String peerip){
		if(!Support.checkIP(peerip)){
			log.error(peerip +"is illegal!" );
			return -1;
		}
		String cmd="gluster peer probe "+peerip;
		List<String> reStrings=Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
		if(reStrings==null || reStrings.size()==0){
			log.error("detach error! ");
			return -2;
		}
		if(reStrings.contains(Constant.success)){
			log.info("probe success!");
			return 1;
		}
		log.info("probe failed!");
		return 0;
	}
	
	/**
	 * 删除集群中节点
	 * 返回1表示删除成功；-1表示ip不合法，-2 表示出错，0表示添加失败
	 * @param peerip
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public int detachPeer(String peerip){
		if(!Support.checkIP(peerip)){
			log.error(peerip +"is illegal!" );
			return -1;
		}
		String cmd="gluster peer detach "+peerip;
		List<String> reStrings=Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
		if(reStrings==null || reStrings.size()==0){
			log.error("detach error! ");
			return -2;
		}
		if(reStrings.contains(Constant.success)){
			log.info("detach success!");
			return 1;
		}
		log.info("detach failed!");
		return 0;
	}
}

