fix mountPoint

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

@ -61,24 +61,57 @@ public class CopyData {
return -2; return -2;
} }
String command = "cp -r " + sourceFolderName + "/" + fileName+" "+destFolderName; String command = "cp -r " + sourceFolderName + "/" + fileName+" "+destFolderName;
/*
* RunCommand runCommand = new RunCommand();
List<String> reStrings = runCommand.runCommandWait(command);
*/
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command); Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
log.info("copy " + sourceFolderName +"/" + fileName+ " to " + destFolderName + " running"); log.info("copy " + sourceFolderName +"/" + fileName+ " to " + destFolderName + " running");
return 1; 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){ public int createFolders(String folder){
log.info("create "+folder); log.info("create "+folder);
String splitFolder[]=folder.substring(1).split("/"); String splitFolder[]=folder.substring(1).split("/");
String cmd="mkdir "; String cmd="mkdir ";
for(String one:splitFolder){ for(String one:splitFolder){
cmd+="/"+one.replaceAll(" ", ""); cmd+="/"+one.replaceAll(" ", "");
Constant.ganymedSSH.execCmdWaitAcquiescent(cmd); List<String> reStrings=Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
} }
return 1; return 1;
} }

@ -1,13 +1,10 @@
/** /**
* @author * @author
* volumevolumevolumebrick * volumevolumevolumebrick
*/ */
package com.platform.glusterfs; package com.platform.glusterfs;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -17,17 +14,24 @@ import java.util.Map;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.PropertyConfigurator;
import org.junit.Test;
import com.platform.utils.Constant; import com.platform.utils.Constant;
public class SetVolume { public class SetVolume {
public static Logger log = Logger.getLogger(SetVolume.class); 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) { public int createVolume(String volumeName, int count, String type, List bricks, String mountPoint) {
log.info("Creat new volume"); log.info("Creat new volume");
@ -50,8 +54,7 @@ public class SetVolume {
} }
// 执行命令 // 执行命令
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
Constant.port, command);
// 创建成功时返回信息格式volume create: volumename success: // 创建成功时返回信息格式volume create: volumename success:
if (reStrings == null || reStrings.size() == 0) { if (reStrings == null || reStrings.size() == 0) {
@ -59,27 +62,24 @@ public class SetVolume {
return -7; return -7;
} }
if (reStrings.get(0).contains("volume create: " + volumeName + ": " + "success:")) { 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) { if (startVolume(volumeName) == 0) {
log.info("start volume "+volumeName+" successed!"); log.info("start volume " + volumeName + " successed!");
if ((new ShowData().showFolderData(mountPoint)) == null) {
Constant.ganymedSSH.execCmdWaitAcquiescent("mkdir " + mountPoint); log.info("create "+mountPoint);
} new CopyData().createFolders(mountPoint);
// 进行挂载 // 进行挂载
String command3 = "mount -t glusterfs " + Constant.hostIp + ":/" + volumeName + " " + mountPoint; String command3 = "mount -t glusterfs " + Constant.hostIp + ":" + volumeName + " " + mountPoint;
List<String> reStrings3 = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, List<String> reStrings3 = Constant.ganymedSSH.execCmdWaitAcquiescent(command3);
Constant.rootPasswd, Constant.port, command3);
if (reStrings3.size() == 0 || reStrings.get(0).contains("GlusterFS is already mounted")) {
// 这里需要添加解析挂载命令返回字符串的语句由于我的系统有问题导致挂载成功后返回WARNING: getfattr
// not found, certain checks will be skipped..
// 所以这句代码未经测试
// 成功挂载时没有任何返回信息
if (reStrings3.size() == 0 || reStrings.get(0).contains("GlusterFS is already mounted"))
{
log.info("mount point successed!"); log.info("mount point successed!");
String addRecord = "echo \"" + volumeName + ":" + mountPoint + "\" >> " + Constant.MountRecord;
Constant.ganymedSSH.execCmdNoWaitAcquiescent(addRecord);
return 1; return 1;
} }
} }
} else { } else {
log.error("3104 volume create failed with error" + reStrings.get(0)); 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 * @param volumeName
* @return 1 -1volume name-2volume * @return
* @see [##]
*/ */
public int deleteVolume(String volumeName) { public int deleteVolume(String volumeName) {
int status = 0; int status = 0;
List<String> volumeNames = new VolumeInfo().showAllVolumeName(); VolumeInfo volumeInfo = new VolumeInfo();
List<String> volumeNames = volumeInfo.showAllVolumeName();
if (!volumeNames.contains(volumeName)) { if (!volumeNames.contains(volumeName)) {
log.error("3801 " + volumeName + " is not exists !"); log.error("3801 " + volumeName + " is not exists !");
return -1; 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) { if (stopVolume(volumeName) != 0) {
log.error("3802 " + volumeName + " stop failed !");
return -2; return -2;
} }
log.info("stop "+volumeName+" successed!");
for(String mountPoint:mountPoints){ String command = "echo -e \"y\"| gluster volume delete " + volumeName;
if (mountPoint != null) { List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
String command = "umount -l " + mountPoint; if (reStrings == null || reStrings.size() == 0
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command); || !(reStrings.get(0).contains("volume delete: " + volumeName + ": success"))) {
log.info("umount "+mountPoint+" successed!"); 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; for (String mountPoint : mountPoints) {
List<String> reStrings=Constant.ganymedSSH.execCmdWaitAcquiescent(command); command = "umount -l " + mountPoint;
if(reStrings==null || reStrings.size()>0 || !(reStrings.get(0).contains("volume delete: "+volumeName+": success"))){ Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
log.error("3803 : delete volume "+volumeName+" failed !"); log.info("umount " + mountPoint + " successed!");
return -3; String oneRecord=volumeName+":"+mountPoint;
if (mountRecord.contains(oneRecord)) {
mountRecord.remove(oneRecord);
}
}
String newRecords="";
for(String one:mountRecord){
newRecords+=one+"\n";
} }
log.info("delete "+volumeName+" successed!"); command="echo -ne \""+newRecords+"\" > "+Constant.MountRecord;
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
return 1; return 1;
} }
/** /**
* volumebrick, 1 ; * volumebrick, 1 ;
* volumebrickcount * volumebrickcount
* <> *
* @param volumeName * @param volumeName
* @param brickName * @param brickName
* @param count * @param count
@ -162,8 +182,7 @@ public class SetVolume {
else if (type.equals(Constant.stripe)) else if (type.equals(Constant.stripe))
command = "gluster volume add-brick " + volumeName + " " + "stripe " + count + " " + brick + "force"; command = "gluster volume add-brick " + volumeName + " " + "stripe " + count + " " + brick + "force";
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
Constant.port, command);
// 添加成功的返回信息是volume add-brick: success // 添加成功的返回信息是volume add-brick: success
if (reStrings != null && reStrings.size() > 0 && reStrings.get(0).contains("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; return 1;
} else { } else {
log.error("3205 add brick failed,please check the system"); 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; return -5;
} }
} }
/** /**
* volumebrick, 1 ; * volumebrick, 1 ;
* volumebrickcount * volumebrickcount
*
* @param volumeName * @param volumeName
* @param brickName * @param brickName
* @param count * @param count
@ -214,8 +234,7 @@ public class SetVolume {
return -5; return -5;
} }
log.info("即将执行删除命令"); log.info("即将执行删除命令");
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
Constant.port, command);
// System.out.println(reStrings); // System.out.println(reStrings);
log.info("删除命令执行完毕"); log.info("删除命令执行完毕");
@ -264,8 +283,7 @@ public class SetVolume {
return -1; return -1;
} else { } else {
String command = "echo -e \"y\"| gluster volume stop " + volumeName; String command = "echo -e \"y\"| gluster volume stop " + volumeName;
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
Constant.port, command);
// 标记操作结果operation = 1 操作成功 // 标记操作结果operation = 1 操作成功
// operation = 0 操作失败 // operation = 0 操作失败
@ -313,8 +331,7 @@ public class SetVolume {
int operation = 0; int operation = 0;
// 执行命令 // 执行命令
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command);
Constant.port, command);
for (String temp2 : reStrings) { for (String temp2 : reStrings) {
if (temp2.equals("volume start: " + volumeName + ": " + "success")) { if (temp2.equals("volume start: " + volumeName + ": " + "success")) {
operation = 1; operation = 1;
@ -393,20 +410,22 @@ public class SetVolume {
} }
List<String> volumeNames = new VolumeInfo().showAllVolumeName(); List<String> volumeNames = new VolumeInfo().showAllVolumeName();
if(volumeNames==null){ if (volumeNames == null) {
log.error("3108 showAllVolumeName return error"); log.error("3108 showAllVolumeName return error");
return -7; return -7;
} }
if (volumeNames.contains(volumeName)) { if (volumeNames.contains(volumeName)) {
log.error("3106 " + volumeName + " is already exists! "); log.error("3106 " + volumeName + " is already exists! ");
return -5; return -5;
} }
/*
Map datas = new ShowData().showFolderData(mountPoint); Map datas = new ShowData().showFolderData(mountPoint);
if (datas != null && datas.size() > 0) { if (datas != null && datas.size() > 0) {
log.error("3107 " + mountPoint + " is not exists or not empty ! "); log.error("3107 " + mountPoint + " is not exists or not empty ! ");
return -6; return -6;
} }
*/
return 1; return 1;
} }
@ -464,18 +483,29 @@ public class SetVolume {
return 1; 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) { public static void main(String[] args) {
SetVolume setVolume = new SetVolume(); SetVolume setVolume = new SetVolume();
int operation = 0; int operation = 0;
// PropertyConfigurator.configure("log4j.properties"); // PropertyConfigurator.configure("log4j.properties");
// TODO Auto-generated method stub // TODO Auto-generated method stub
// 测试创建volume的代码 // 测试创建volume的代码
List<String> bricksToCreate = new ArrayList<String>(); // List<String> bricksToCreate = new ArrayList<String>();
bricksToCreate.add("192.168.0.110:/v2"); // bricksToCreate.add("192.168.0.110:/v2");
bricksToCreate.add("192.168.0.116:/v2"); // bricksToCreate.add("192.168.0.116:/v2");
operation = setVolume.createVolume("v2", 0, "distributed", bricksToCreate, "/home/v2_point"); // operation = setVolume.createVolume("v2", 0, "distributed", bricksToCreate, "/home/v2_point");
// operation = setVolume.deleteVolume("v3"); // operation = setVolume.deleteVolume("v3");
// //
// // 以下是测试添加brick的代码 // // 以下是测试添加brick的代码
// //
@ -508,4 +538,3 @@ public class SetVolume {
// System.out.println(operation); // System.out.println(operation);
} }
} }

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

@ -77,7 +77,7 @@ public class VolumeInfo {
} }
/** /**
* volumevolume <> * volumevolume
* *
* @param volumeName * @param volumeName
* @return * @return
@ -257,12 +257,6 @@ public class VolumeInfo {
return null; 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) { for (String mountPoint : reStrings) {
mountPoint = mountPoint.replaceAll(" ", ""); mountPoint = mountPoint.replaceAll(" ", "");
mountPoints.add(mountPoint); mountPoints.add(mountPoint);
@ -274,25 +268,15 @@ public class VolumeInfo {
log.info("get one volume MountPoint"); log.info("get one volume MountPoint");
String mountPoint=null; 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); // System.out.println(reStrings);
if (reStrings == null) { if (mountpoints == null || mountpoints.size() == 0) {
log.error("11001 get result string wrong"); log.error("11001 get result string wrong");
return null; 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; return mountPoint;
} }
@ -377,7 +361,7 @@ public class VolumeInfo {
} }
/** /**
* volumeNametruefalse <> * volumeNametruefalse
* *
* @param volumeName * @param volumeName
* @return * @return

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

Loading…
Cancel
Save