fix mountPoint

glusterfs-api
lily 9 years ago
parent 4daefb6d79
commit 7271066a08

@ -61,24 +61,57 @@ public class CopyData {
return -2;
}
String command = "cp -r " + sourceFolderName + "/" + fileName+" "+destFolderName;
/*
* RunCommand runCommand = new RunCommand();
List<String> reStrings = runCommand.runCommandWait(command);
*/
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
log.info("copy " + sourceFolderName +"/" + fileName+ " to " + destFolderName + " running");
return 1;
}
/**
* sourceFolderNamedestFolderName
* 1sourceFolderName-2 destFolderName-3
* @param sourceFolderName
* @param destFolderName
* @return
* @see [##]
*/
public int copyFolder(String sourceFolderName, String destFolderName) {
int progress=0;
log.info("start copy " + sourceFolderName + " to " + destFolderName);
ShowData showData=new ShowData();
Map<String,String> reStrings=showData.showFolderData(destFolderName);
if(reStrings==null){
log.info("3201 "+destFolderName+" is not exists");
return -3;
}
reStrings=showData.showFolderData(sourceFolderName);
if(reStrings==null){
log.info("3202 "+sourceFolderName+" is not exists");
return -2;
}
String command = "cp -r " + sourceFolderName+" "+destFolderName;
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
log.info("copy " + sourceFolderName + " to " + destFolderName + " running");
return 1;
}
/**
*
* <>
* @param folder
* @return
* @see [##]
*/
public int createFolders(String folder){
log.info("create "+folder);
String splitFolder[]=folder.substring(1).split("/");
String cmd="mkdir ";
for(String one:splitFolder){
cmd+="/"+one.replaceAll(" ", "");
Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
List<String> reStrings=Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
}
return 1;
}

@ -1,13 +1,10 @@
/**
* @author
* volumevolumevolumebrick
*/
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: ;-1brickip; -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 -1volume name-2volume
* -3-4/gfsAutoMount/mountPoint.record
* @param volumeName
* @return 1 -1volume name-2volume
* @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;
}
/**
* volumebrick, 1 ;
* volumebrickcount
* <>
*
* @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;
}
}
/**
* volumebrick, 1 ;
* volumebrickcount
*
* @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);
}
}

@ -55,8 +55,7 @@ public class ShowData {
* RunCommand runCommand=new RunCommand(); List<String>
* reStrings=runCommand.runCommandWait(command);
*/
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser,
Constant.rootPasswd, Constant.port, command);
List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
if (reStrings == null) {
log.error("2101 command get result is null");
return null;

@ -77,7 +77,7 @@ public class VolumeInfo {
}
/**
* volumevolume <>
* volumevolume
*
* @param volumeName
* @return
@ -257,12 +257,6 @@ public class VolumeInfo {
return null;
}
// char flag = reStrings.get(0).trim().toCharArray()[0];
// if (flag != '/') {
// log.error("11003 get result string wrong");
// return null;
// }
for (String mountPoint : reStrings) {
mountPoint = mountPoint.replaceAll(" ", "");
mountPoints.add(mountPoint);
@ -274,25 +268,15 @@ public class VolumeInfo {
log.info("get one volume MountPoint");
String mountPoint=null;
String cmd=Constant.df + "|grep " + volumeName + "|awk '{print $6}'";
List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
List<String> mountpoints = getVolumeMountPoint(volumeName);
// System.out.println(reStrings);
if (reStrings == null) {
if (mountpoints == null || mountpoints.size() == 0) {
log.error("11001 get result string wrong");
return null;
}
if (reStrings.size() == 0) {
log.error("11002 " + volumeName + " is not exists or no mountpoint");
return null;
}
// char flag = reStrings.get(0).trim().toCharArray()[0];
// if (flag != '/') {
// log.error("11003 get result string wrong");
// return null;
// }
mountPoint=reStrings.get(0);
mountPoint=mountpoints.get(0);
return mountPoint;
}
@ -377,7 +361,7 @@ public class VolumeInfo {
}
/**
* volumeNametruefalse <>
* volumeNametruefalse
*
* @param volumeName
* @return

@ -23,16 +23,15 @@ public class Constant {
public static String failed = "failed";
public static String noSuchFile = "No such file or directory";
public static GanymedSSH ganymedSSH = new GanymedSSH(hostIp, rootUser, rootPasswd, port);
public static String fileGetTreeData = "WebContent\\WEB-INF\\config\\getTreedata.sh";
public static String AutoMountfile="/gfsAutoMount/AutoRun.sh";
public static String MountRecord="/gfsAutoMount/mountPoint.record";
public static String strGetTreeData = "function ergodic(){\n "
+ "for file in \\`ls \\$1\\`\n do\n if [ \"\\$file\" != \"app\" -a -d \\$1\\\"/\\\"\\$file ]\n "
+ "then\n ergodic \\$1\"/\"\\$file\n else\n local path=\\$1\"/\"\\$file\n "
+ "echo \\$path \n fi\n done\n}\n\nIFS=\\$\\'\\n\\' "
+ "#这个必须要,否则会在文件名中有空格时出错\nINIT_PATH=\".\";\nergodic \\$1\n";
/**
* volume 线
*/

Loading…
Cancel
Save