From 7271066a082e4813fe40ba18c87cd4c1a19e7a07 Mon Sep 17 00:00:00 2001 From: lily Date: Wed, 14 Sep 2016 15:50:02 +0800 Subject: [PATCH] fix mountPoint --- src/com/platform/glusterfs/CopyData.java | 45 +++++- src/com/platform/glusterfs/SetVolume.java | 161 ++++++++++++--------- src/com/platform/glusterfs/ShowData.java | 3 +- src/com/platform/glusterfs/VolumeInfo.java | 28 +--- src/com/platform/utils/Constant.java | 7 +- 5 files changed, 144 insertions(+), 100 deletions(-) diff --git a/src/com/platform/glusterfs/CopyData.java b/src/com/platform/glusterfs/CopyData.java index 3af2dbea..694aec06 100644 --- a/src/com/platform/glusterfs/CopyData.java +++ b/src/com/platform/glusterfs/CopyData.java @@ -61,24 +61,57 @@ public class CopyData { return -2; } String command = "cp -r " + sourceFolderName + "/" + fileName+" "+destFolderName; - /* - * RunCommand runCommand = new RunCommand(); - - List reStrings = runCommand.runCommandWait(command); - */ + Constant.ganymedSSH.execCmdNoWaitAcquiescent(command); log.info("copy " + sourceFolderName +"/" + fileName+ " to " + destFolderName + " running"); return 1; } + /** + * 将sourceFolderName拷贝到destFolderName + * 如果拷贝正常返回1,如果sourceFolderName不存在返回-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 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 reStrings=Constant.ganymedSSH.execCmdWaitAcquiescent(cmd); } return 1; } diff --git a/src/com/platform/glusterfs/SetVolume.java b/src/com/platform/glusterfs/SetVolume.java index 5502aaee..6b4dddcc 100644 --- a/src/com/platform/glusterfs/SetVolume.java +++ b/src/com/platform/glusterfs/SetVolume.java @@ -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 reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, - Constant.port, command); + List 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 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 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 volumeNames = new VolumeInfo().showAllVolumeName(); + VolumeInfo volumeInfo = new VolumeInfo(); + List volumeNames = volumeInfo.showAllVolumeName(); if (!volumeNames.contains(volumeName)) { log.error("3801 " + volumeName + " is not exists !"); return -1; } - List mountPoints = new VolumeInfo().getVolumeMountPoint(volumeName); + List mountPoints = volumeInfo.getVolumeMountPoint(volumeName); + + String cmd = "cat " + Constant.MountRecord; + List 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 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 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 reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, - Constant.port, command); + List 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 reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, - Constant.port, command); + List 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 reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, - Constant.port, command); + List 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 reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, - Constant.port, command); + List 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 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 bricksToCreate = new ArrayList(); + 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 bricksToCreate = new ArrayList(); - 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 bricksToCreate = new ArrayList(); +// 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); } } - diff --git a/src/com/platform/glusterfs/ShowData.java b/src/com/platform/glusterfs/ShowData.java index de218e5e..b20afba4 100644 --- a/src/com/platform/glusterfs/ShowData.java +++ b/src/com/platform/glusterfs/ShowData.java @@ -55,8 +55,7 @@ public class ShowData { * RunCommand runCommand=new RunCommand(); List * reStrings=runCommand.runCommandWait(command); */ - List reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, - Constant.rootPasswd, Constant.port, command); + List reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(command); if (reStrings == null) { log.error("2101 command get result is null"); return null; diff --git a/src/com/platform/glusterfs/VolumeInfo.java b/src/com/platform/glusterfs/VolumeInfo.java index 15e4cb14..9a65a86f 100644 --- a/src/com/platform/glusterfs/VolumeInfo.java +++ b/src/com/platform/glusterfs/VolumeInfo.java @@ -77,7 +77,7 @@ public class VolumeInfo { } /** - * 给定参数volume的名称获得volume的类型 <功能详细描述> + * 给定参数volume的名称获得volume的类型 * * @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 reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd); + + List 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 { } /** - * 判断volumeName是否存在,存在返回true,不存在返回false <功能详细描述> + * 判断volumeName是否存在,存在返回true,不存在返回false * * @param volumeName * @return diff --git a/src/com/platform/utils/Constant.java b/src/com/platform/utils/Constant.java index b1d439f6..2db87411 100644 --- a/src/com/platform/utils/Constant.java +++ b/src/com/platform/utils/Constant.java @@ -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 获取的线程休眠时间 */