|
|
|
@ -1,13 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author 李乾坤
|
|
|
|
|
* 进行volume的一系列操作,如创建、开启停止volume,为volume添加或删除brick
|
|
|
|
|
*/
|
|
|
|
|
package com.platform.glusterfs;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
@ -17,17 +14,24 @@ import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
import org.apache.log4j.PropertyConfigurator;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import com.platform.utils.Constant;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class SetVolume {
|
|
|
|
|
public static Logger log = Logger.getLogger(SetVolume.class);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 创建volume 返回值:创建并挂载成功 1
|
|
|
|
|
/**
|
|
|
|
|
* 创建volume 返回值:创建并挂载成功 1 1:可以创建 ;-1:brick的ip不在集群中或者未连接; -2 -3
|
|
|
|
|
* -4:类型与brick数目不匹配 ; -5 :volumeName 已经存在;-6:挂载点存在且不为空,不能作为挂载点; -7:未知错误
|
|
|
|
|
*
|
|
|
|
|
* @param volumeName
|
|
|
|
|
* @param count
|
|
|
|
|
* @param type
|
|
|
|
|
* @param bricks
|
|
|
|
|
* @param mountPoint
|
|
|
|
|
* @return
|
|
|
|
|
* @see [类、类#方法、类#成员]
|
|
|
|
|
*/
|
|
|
|
|
public int createVolume(String volumeName, int count, String type, List bricks, String mountPoint) {
|
|
|
|
|
log.info("Creat new volume");
|
|
|
|
@ -48,10 +52,9 @@ public class SetVolume {
|
|
|
|
|
} else if (type.equals(Constant.replica) || type.equals(Constant.stripe)) {
|
|
|
|
|
command = "gluster volume create " + volumeName + " " + type + " " + count + " " + commandarg + "force";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 执行命令
|
|
|
|
|
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
|
|
|
|
|
Constant.port, command);
|
|
|
|
|
List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
|
|
|
|
|
|
|
|
|
|
// 创建成功时返回信息格式:volume create: volumename success:
|
|
|
|
|
if (reStrings == null || reStrings.size() == 0) {
|
|
|
|
@ -59,27 +62,24 @@ public class SetVolume {
|
|
|
|
|
return -7;
|
|
|
|
|
}
|
|
|
|
|
if (reStrings.get(0).contains("volume create: " + volumeName + ": " + "success:")) {
|
|
|
|
|
log.info("create volume "+volumeName+" successed!");
|
|
|
|
|
log.info("create volume " + volumeName + " successed!");
|
|
|
|
|
// 创建成功则启动并进行挂载
|
|
|
|
|
if (startVolume(volumeName) == 0) {
|
|
|
|
|
log.info("start volume "+volumeName+" successed!");
|
|
|
|
|
if ((new ShowData().showFolderData(mountPoint)) == null) {
|
|
|
|
|
Constant.ganymedSSH.execCmdWaitAcquiescent("mkdir " + mountPoint);
|
|
|
|
|
}
|
|
|
|
|
log.info("start volume " + volumeName + " successed!");
|
|
|
|
|
|
|
|
|
|
log.info("create "+mountPoint);
|
|
|
|
|
new CopyData().createFolders(mountPoint);
|
|
|
|
|
|
|
|
|
|
// 进行挂载
|
|
|
|
|
String command3 = "mount -t glusterfs " + Constant.hostIp + ":/" + volumeName + " " + mountPoint;
|
|
|
|
|
List<String> reStrings3 = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser,
|
|
|
|
|
Constant.rootPasswd, Constant.port, command3);
|
|
|
|
|
|
|
|
|
|
// 这里需要添加解析挂载命令返回字符串的语句,由于我的系统有问题,导致挂载成功后返回WARNING: getfattr
|
|
|
|
|
// not found, certain checks will be skipped..
|
|
|
|
|
// 所以这句代码未经测试
|
|
|
|
|
// 成功挂载时没有任何返回信息
|
|
|
|
|
if (reStrings3.size() == 0 || reStrings.get(0).contains("GlusterFS is already mounted"))
|
|
|
|
|
{
|
|
|
|
|
String command3 = "mount -t glusterfs " + Constant.hostIp + ":" + volumeName + " " + mountPoint;
|
|
|
|
|
List<String> reStrings3 = Constant.ganymedSSH.execCmdWaitAcquiescent(command3);
|
|
|
|
|
|
|
|
|
|
if (reStrings3.size() == 0 || reStrings.get(0).contains("GlusterFS is already mounted")) {
|
|
|
|
|
log.info("mount point successed!");
|
|
|
|
|
String addRecord = "echo \"" + volumeName + ":" + mountPoint + "\" >> " + Constant.MountRecord;
|
|
|
|
|
Constant.ganymedSSH.execCmdNoWaitAcquiescent(addRecord);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log.error("3104 volume create failed with error" + reStrings.get(0));
|
|
|
|
@ -96,46 +96,66 @@ public class SetVolume {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除volume
|
|
|
|
|
*
|
|
|
|
|
* 删除volume 1 表示成功 ;-1表示volume name不存在;-2表示停止volume 失败;
|
|
|
|
|
* -3表示删除失败,-4表示/gfsAutoMount/mountPoint.record文件不存在
|
|
|
|
|
* @param volumeName
|
|
|
|
|
* @return 1 表示成功 ;-1表示volume name不存在;-2表示停止volume 失败
|
|
|
|
|
* @return
|
|
|
|
|
* @see [类、类#方法、类#成员]
|
|
|
|
|
*/
|
|
|
|
|
public int deleteVolume(String volumeName) {
|
|
|
|
|
int status = 0;
|
|
|
|
|
List<String> volumeNames = new VolumeInfo().showAllVolumeName();
|
|
|
|
|
VolumeInfo volumeInfo = new VolumeInfo();
|
|
|
|
|
List<String> volumeNames = volumeInfo.showAllVolumeName();
|
|
|
|
|
if (!volumeNames.contains(volumeName)) {
|
|
|
|
|
log.error("3801 " + volumeName + " is not exists !");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<String> mountPoints = new VolumeInfo().getVolumeMountPoint(volumeName);
|
|
|
|
|
List<String> mountPoints = volumeInfo.getVolumeMountPoint(volumeName);
|
|
|
|
|
|
|
|
|
|
String cmd = "cat " + Constant.MountRecord;
|
|
|
|
|
List<String> mountRecord = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
|
|
|
|
|
|
|
|
|
|
if (stopVolume(volumeName) != 0) {
|
|
|
|
|
log.error("3802 " + volumeName + " stop failed !");
|
|
|
|
|
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
log.info("stop "+volumeName+" successed!");
|
|
|
|
|
for(String mountPoint:mountPoints){
|
|
|
|
|
if (mountPoint != null) {
|
|
|
|
|
String command = "umount -l " + mountPoint;
|
|
|
|
|
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
|
|
|
|
|
log.info("umount "+mountPoint+" successed!");
|
|
|
|
|
|
|
|
|
|
String command = "echo -e \"y\"| gluster volume delete " + volumeName;
|
|
|
|
|
List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
|
|
|
|
|
if (reStrings == null || reStrings.size() == 0
|
|
|
|
|
|| !(reStrings.get(0).contains("volume delete: " + volumeName + ": success"))) {
|
|
|
|
|
log.error("3803 : delete volume " + volumeName + " failed !");
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
|
|
|
|
log.info("delete " + volumeName + " successed!");
|
|
|
|
|
if (mountRecord.size() != 0 && mountPoints.get(0).contains(Constant.noSuchFile)) {
|
|
|
|
|
log.error("3804 : " + Constant.MountRecord + " is not exits");
|
|
|
|
|
return -4;
|
|
|
|
|
}
|
|
|
|
|
String command="echo -e \"y\"| gluster volume delete "+volumeName;
|
|
|
|
|
List<String> reStrings=Constant.ganymedSSH.execCmdWaitAcquiescent(command);
|
|
|
|
|
if(reStrings==null || reStrings.size()>0 || !(reStrings.get(0).contains("volume delete: "+volumeName+": success"))){
|
|
|
|
|
log.error("3803 : delete volume "+volumeName+" failed !");
|
|
|
|
|
return -3;
|
|
|
|
|
for (String mountPoint : mountPoints) {
|
|
|
|
|
command = "umount -l " + mountPoint;
|
|
|
|
|
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
|
|
|
|
|
log.info("umount " + mountPoint + " successed!");
|
|
|
|
|
String oneRecord=volumeName+":"+mountPoint;
|
|
|
|
|
if (mountRecord.contains(oneRecord)) {
|
|
|
|
|
mountRecord.remove(oneRecord);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
log.info("delete "+volumeName+" successed!");
|
|
|
|
|
String newRecords="";
|
|
|
|
|
for(String one:mountRecord){
|
|
|
|
|
newRecords+=one+"\n";
|
|
|
|
|
}
|
|
|
|
|
command="echo -ne \""+newRecords+"\" > "+Constant.MountRecord;
|
|
|
|
|
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 为指定的volume添加brick,参数中需要指定类型、数量等 返回值:1成功 ;其他失败
|
|
|
|
|
* 过程中需要先检查volume是否存在,还需检查给出的brick数量与类型、count是否相符
|
|
|
|
|
* <功能详细描述>
|
|
|
|
|
*
|
|
|
|
|
* @param volumeName
|
|
|
|
|
* @param brickName
|
|
|
|
|
* @param count
|
|
|
|
@ -162,8 +182,7 @@ public class SetVolume {
|
|
|
|
|
else if (type.equals(Constant.stripe))
|
|
|
|
|
command = "gluster volume add-brick " + volumeName + " " + "stripe " + count + " " + brick + "force";
|
|
|
|
|
|
|
|
|
|
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
|
|
|
|
|
Constant.port, command);
|
|
|
|
|
List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
|
|
|
|
|
|
|
|
|
|
// 添加成功的返回信息是:volume add-brick: success
|
|
|
|
|
if (reStrings != null && reStrings.size() > 0 && reStrings.get(0).contains("volume add-brick: success")) {
|
|
|
|
@ -171,15 +190,16 @@ public class SetVolume {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
log.error("3205 add brick failed,please check the system");
|
|
|
|
|
// System.out.println("3202 add brick failed,please check the system");
|
|
|
|
|
// System.out.println("3202 add brick failed,please check the
|
|
|
|
|
// system");
|
|
|
|
|
return -5;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 为指定的volume删除brick,参数中需要指定类型、数量等 返回值:1 成功 ;其他 失败
|
|
|
|
|
* 过程中需要先检查volume是否存在,还需检查给出的brick数量与类型、count是否相符
|
|
|
|
|
*
|
|
|
|
|
* @param volumeName
|
|
|
|
|
* @param brickName
|
|
|
|
|
* @param count
|
|
|
|
@ -203,7 +223,7 @@ public class SetVolume {
|
|
|
|
|
command = "echo -e \"y\" | gluster volume remove-brick " + volumeName + " " + brick + " force";
|
|
|
|
|
} else if (type.equals(Constant.replica)) {
|
|
|
|
|
command = "echo -e \"y\" | gluster volume remove-brick " + volumeName + " repli " + count + " " + brick
|
|
|
|
|
+ " force";
|
|
|
|
|
+ " force";
|
|
|
|
|
} else if (type.equals(Constant.stripe)) {
|
|
|
|
|
command = "echo -e \"y\" | gluster volume remove-brick " + volumeName + " stripe " + count + " " + brick
|
|
|
|
|
+ " force";
|
|
|
|
@ -214,8 +234,7 @@ public class SetVolume {
|
|
|
|
|
return -5;
|
|
|
|
|
}
|
|
|
|
|
log.info("即将执行删除命令");
|
|
|
|
|
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
|
|
|
|
|
Constant.port, command);
|
|
|
|
|
List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
|
|
|
|
|
// System.out.println(reStrings);
|
|
|
|
|
log.info("删除命令执行完毕");
|
|
|
|
|
|
|
|
|
@ -264,8 +283,7 @@ public class SetVolume {
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
String command = "echo -e \"y\"| gluster volume stop " + volumeName;
|
|
|
|
|
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
|
|
|
|
|
Constant.port, command);
|
|
|
|
|
List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
|
|
|
|
|
|
|
|
|
|
// 标记操作结果:operation = 1 操作成功
|
|
|
|
|
// operation = 0 操作失败
|
|
|
|
@ -309,12 +327,11 @@ public class SetVolume {
|
|
|
|
|
if (volumeExist) {
|
|
|
|
|
if (!(new VolumeInfo().getVolumeStatus(volumeName).equals("Started"))) {
|
|
|
|
|
String command = "gluster volume start " + volumeName;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int operation = 0;
|
|
|
|
|
|
|
|
|
|
// 执行命令
|
|
|
|
|
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd,
|
|
|
|
|
Constant.port, command);
|
|
|
|
|
List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
|
|
|
|
|
for (String temp2 : reStrings) {
|
|
|
|
|
if (temp2.equals("volume start: " + volumeName + ": " + "success")) {
|
|
|
|
|
operation = 1;
|
|
|
|
@ -393,20 +410,22 @@ public class SetVolume {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<String> volumeNames = new VolumeInfo().showAllVolumeName();
|
|
|
|
|
if(volumeNames==null){
|
|
|
|
|
if (volumeNames == null) {
|
|
|
|
|
log.error("3108 showAllVolumeName return error");
|
|
|
|
|
return -7;
|
|
|
|
|
}
|
|
|
|
|
if (volumeNames.contains(volumeName)) {
|
|
|
|
|
if (volumeNames.contains(volumeName)) {
|
|
|
|
|
log.error("3106 " + volumeName + " is already exists! ");
|
|
|
|
|
return -5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Map datas = new ShowData().showFolderData(mountPoint);
|
|
|
|
|
if (datas != null && datas.size() > 0) {
|
|
|
|
|
log.error("3107 " + mountPoint + " is not exists or not empty ! ");
|
|
|
|
|
return -6;
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -464,18 +483,29 @@ public class SetVolume {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test_deleteVolume(){
|
|
|
|
|
System.out.println(deleteVolume("lili_test1"));
|
|
|
|
|
}
|
|
|
|
|
// @Test
|
|
|
|
|
public void test_createVolume(){
|
|
|
|
|
List<String> bricksToCreate = new ArrayList<String>();
|
|
|
|
|
bricksToCreate.add("192.168.0.110:/lili_test1");
|
|
|
|
|
bricksToCreate.add("192.168.0.116:/lili_test1");
|
|
|
|
|
System.out.println(createVolume("lili_test1", 0, "distributed", bricksToCreate, "/home/lili_test1_point"));
|
|
|
|
|
}
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
SetVolume setVolume = new SetVolume();
|
|
|
|
|
int operation = 0;
|
|
|
|
|
// PropertyConfigurator.configure("log4j.properties");
|
|
|
|
|
// PropertyConfigurator.configure("log4j.properties");
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
// 测试创建volume的代码
|
|
|
|
|
|
|
|
|
|
List<String> bricksToCreate = new ArrayList<String>();
|
|
|
|
|
bricksToCreate.add("192.168.0.110:/v2");
|
|
|
|
|
bricksToCreate.add("192.168.0.116:/v2");
|
|
|
|
|
operation = setVolume.createVolume("v2", 0, "distributed", bricksToCreate, "/home/v2_point");
|
|
|
|
|
// operation = setVolume.deleteVolume("v3");
|
|
|
|
|
// List<String> bricksToCreate = new ArrayList<String>();
|
|
|
|
|
// bricksToCreate.add("192.168.0.110:/v2");
|
|
|
|
|
// bricksToCreate.add("192.168.0.116:/v2");
|
|
|
|
|
// operation = setVolume.createVolume("v2", 0, "distributed", bricksToCreate, "/home/v2_point");
|
|
|
|
|
// operation = setVolume.deleteVolume("v3");
|
|
|
|
|
//
|
|
|
|
|
// // 以下是测试添加brick的代码
|
|
|
|
|
//
|
|
|
|
@ -508,4 +538,3 @@ public class SetVolume {
|
|
|
|
|
// System.out.println(operation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|