glusterfs-api
lily 8 years ago
parent b28af5fa13
commit 4daefb6d79

1
.gitignore vendored

@ -2,3 +2,4 @@
/build /build
.classpath .classpath
/bin/

@ -17,10 +17,7 @@ import org.apache.log4j.PropertyConfigurator;
import com.platform.utils.Constant; import com.platform.utils.Constant;
/** /**
* *
* nullmapipip
* ipPeerinCluster(Connected)
* ipPeerinCluster(Disconnected)
* @author liliy * @author liliy
* @version [2016912] * @version [2016912]
* @see [/] * @see [/]
@ -29,6 +26,14 @@ import com.platform.utils.Constant;
public class ClusterInfo { public class ClusterInfo {
public static Logger log = Logger.getLogger(ClusterInfo.class); public static Logger log = Logger.getLogger(ClusterInfo.class);
/**
*
* nullmapipip
* ipPeerinCluster(Connected)
* ipPeerinCluster(Disconnected)
* @return
* @see [##]
*/
public Map<String, String> showClusterInfo() { public Map<String, String> showClusterInfo() {
log.info("get cluster info"); log.info("get cluster info");
Map<String, String> peerIps = new HashMap<String, String>(); Map<String, String> peerIps = new HashMap<String, String>();
@ -98,8 +103,12 @@ public class ClusterInfo {
if(peerIps==null || peerIps.size()==0){ if(peerIps==null || peerIps.size()==0){
return null; return null;
} }
if(peerip.equals(Constant.hostIp)){
return Constant.peerincluster_connected;
}
if(!peerIps.containsKey(peerip)){ if(!peerIps.containsKey(peerip)){
return Constant.peerincluster_disconnected; return Constant.peerNotinCluster;
} }
return peerIps.get(peerip); return peerIps.get(peerip);
} }
@ -108,6 +117,7 @@ public class ClusterInfo {
public static void main(String[] args) { public static void main(String[] args) {
// PropertyConfigurator.configure("log4j.properties"); // PropertyConfigurator.configure("log4j.properties");
System.out.println(new ClusterInfo().showClusterInfo()); System.out.println(new ClusterInfo().showClusterInfo());
System.out.println(new ClusterInfo().getPeerStatus("192.168.0.116"));
} }
} }

@ -72,10 +72,26 @@ public class CopyData {
return 1; return 1;
} }
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);
}
return 1;
}
@Test @Test
public void testcreateFolders() {
createFolders("/aaa/vvv/ddd/www/rrrr");
}
//@Test
public void testCopyFolderFiles() { public void testCopyFolderFiles() {
PropertyConfigurator.configure("log4j.properties");
copyFolderFiles("/home", "/home/ubuntu", "system_data"); copyFolderFiles("/home", "/home/ubuntu", "system_data");
} }
} }

@ -16,17 +16,7 @@ import com.platform.utils.Constant;
public class RemoveData { public class RemoveData {
public static Logger log = Logger.getLogger ( RemoveData.class); public static Logger log = Logger.getLogger ( RemoveData.class);
public int deleteVolumeFiles(String volumeName,String fileName){
log.info("start delete "+volumeName+" "+fileName);
int status=-1;
/**
* get mount point of volumeName
*/
String folderName=volumeName;
status=deleteFolderFiles(folderName,fileName);
return status;
}
/** /**
* -1 :error; 0: the filename is not exists ; 1: right * -1 :error; 0: the filename is not exists ; 1: right
@ -34,65 +24,33 @@ public class RemoveData {
* @param fileName * @param fileName
* @return * @return
*/ */
public int deleteFolderFiles(String folderName,String fileName){ public int deleteFolder(String folderName){
log.info("start delete "+folderName+"/"+fileName); log.info("start delete "+folderName);
ShowData showData=new ShowData(); ShowData showData=new ShowData();
Map<String,String> reStrings=showData.showFolderData(folderName+"/"+fileName); Map<String,String> reStrings=showData.showFolderData(folderName);
if(reStrings==null){ if(reStrings==null){
log.error("3301 "+folderName+"/"+fileName+" is not exists"); log.error("3301 "+folderName+" is not exists");
return -1; return -1;
} }
String command="rm -r "+folderName+"/"+fileName; String command="rm -r "+folderName;
// int status=runCommand.runCommand(command); // int status=runCommand.runCommand(command);
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command); Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
log.info("delete "+folderName+" "+fileName+" running"); log.info("delete "+folderName+" running");
return 1; return 1;
} }
public int getFolderSize(String name) {
log.info("get "+name+" size");
String command="du -k -d 0 "+name;
/*
* RunCommand runCommand=new RunCommand();
List<String> reStrings=runCommand.runCommandWait(command);
*/
List<String> reStrings=Constant.ganymedSSH.execCmdWaitAcquiescent(command);
if(reStrings==null){
log.error("3302 the "+command+" return error");
return -1;
}
if(reStrings.size()<1){
log.error("3303 the "+command+" return error");
return -1;
}
if(reStrings.size()==1 && reStrings.get(0).contains("No such file or directory")){
log.info("3304 "+name+" is not exists");
return 0;
}
String strSize=(reStrings.get(0).split("\t"))[0];
int size=Integer.parseInt(strSize);
log.info(name +" size is "+size);
return size;
}
// @Test
public void test_getFolderSize() {
PropertyConfigurator.configure("log4j.properties");
getFolderSize("/home/ubuntu");
}
@Test @Test
public void testDeleteFolderFiles() { public void testDeleteFolderFiles() {
PropertyConfigurator.configure("log4j.properties"); PropertyConfigurator.configure("log4j.properties");
deleteFolderFiles("/home/ubuntu","system_data"); deleteFolder("/home/ubuntu");
} }
} }

@ -132,9 +132,16 @@ public class SetVolume {
return 1; return 1;
} }
/* /**
* volumebrick, 1 ; * volumebrick, 1 ;
* volumebrickcount * volumebrickcount
* <>
* @param volumeName
* @param brickName
* @param count
* @param type
* @return
* @see [##]
*/ */
public int addBrickVolume(String volumeName, List<String> brickName, int count, String type) { public int addBrickVolume(String volumeName, List<String> brickName, int count, String type) {
// 检查是否满足添加bricks的条件 // 检查是否满足添加bricks的条件
@ -169,9 +176,16 @@ public class SetVolume {
} }
} }
/*
/**
* volumebrick, 1 ; * volumebrick, 1 ;
* volumebrickcount * volumebrickcount
* @param volumeName
* @param brickName
* @param count
* @param type
* @return
* @see [##]
*/ */
public int deleteBrickVolume(String volumeName, List<String> brickName, int count, String type) { public int deleteBrickVolume(String volumeName, List<String> brickName, int count, String type) {
int able = isAble(volumeName, count, type, brickName); int able = isAble(volumeName, count, type, brickName);
@ -453,14 +467,14 @@ public class SetVolume {
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:/v1"); bricksToCreate.add("192.168.0.110:/v2");
bricksToCreate.add("192.168.0.116:/v1"); bricksToCreate.add("192.168.0.116:/v2");
operation = setVolume.createVolume("v1", 0, "distributed", bricksToCreate, "/home/v1_point"); operation = setVolume.createVolume("v2", 0, "distributed", bricksToCreate, "/home/v2_point");
// operation = setVolume.deleteVolume("v3"); // operation = setVolume.deleteVolume("v3");
// //
// // 以下是测试添加brick的代码 // // 以下是测试添加brick的代码

@ -19,12 +19,20 @@ import com.platform.utils.GanymedSSH;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/**
* volume <>
*
* @author liliy
* @version [2016913]
* @see [/]
* @since [/]
*/
public class VolumeInfo { public class VolumeInfo {
public static Logger log = Logger.getLogger(VolumeInfo.class); public static Logger log = Logger.getLogger(VolumeInfo.class);
/** /**
* volume * volume <>
* <> *
* @return * @return
* @see [##] * @see [##]
*/ */
@ -38,8 +46,8 @@ public class VolumeInfo {
* runCommand = new RunCommand(); List<String> reStrings = * runCommand = new RunCommand(); List<String> reStrings =
* runCommand.runCommandWait(command); * runCommand.runCommandWait(command);
*/ */
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, List<String> reStrings = Constant.ganymedSSH
Constant.rootPasswd, Constant.port, Constant.glusterVolumeInfo + "|grep ^Volume.Name"); .execCmdWaitAcquiescent(Constant.glusterVolumeInfo + "|grep ^Volume.Name");
// System.out.println(reStrings); // System.out.println(reStrings);
if (reStrings == null) { if (reStrings == null) {
log.error("1401 get result is null"); log.error("1401 get result is null");
@ -69,8 +77,8 @@ public class VolumeInfo {
} }
/** /**
* volumevolume * volumevolume <>
* <> *
* @param volumeName * @param volumeName
* @return * @return
* @see [##] * @see [##]
@ -79,8 +87,8 @@ public class VolumeInfo {
log.info("get volume type"); log.info("get volume type");
String volType = ""; String volType = "";
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, List<String> reStrings = Constant.ganymedSSH
Constant.rootPasswd, Constant.port, Constant.glusterVolumeInfo + volumeName + "|grep ^Type"); .execCmdWaitAcquiescent(Constant.glusterVolumeInfo + volumeName + "|grep ^Type");
// System.out.println(reStrings); // System.out.println(reStrings);
if (reStrings == null) { if (reStrings == null) {
log.error("1501 get result is null"); log.error("1501 get result is null");
@ -106,11 +114,19 @@ public class VolumeInfo {
return volType; return volType;
} }
/**
* volumeName nullvolumeNameVolume volumeName does not
* existStarted,Stopped,Created
*
* @param volumeName
* @return
* @see [##]
*/
public String getVolumeStatus(String volumeName) { public String getVolumeStatus(String volumeName) {
log.info("get volume status"); log.info("get volume status");
String volStatus = ""; String volStatus = "";
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, String cmd = Constant.glusterVolumeInfo + " " + volumeName + " |grep ^Status";
Constant.rootPasswd, Constant.port, Constant.glusterVolumeInfo + "|grep ^Status"); List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
// System.out.println(reStrings); // System.out.println(reStrings);
if (reStrings == null) { if (reStrings == null) {
log.error("1701 get result is null"); log.error("1701 get result is null");
@ -120,11 +136,15 @@ public class VolumeInfo {
log.error("1702 get result is nothing"); log.error("1702 get result is nothing");
return null; return null;
} }
if (reStrings.get(0).contains("does not exist")) {
log.error("1703 " + reStrings.get(0));
return reStrings.get(0);
}
if (!(reStrings.get(0).split(":")[0].contains("Status"))) { if (!(reStrings.get(0).split(":")[0].contains("Status"))) {
log.error("1703 get result string wrong"); log.error("1704 get result string wrong");
return null; return null;
} }
for (Iterator it = reStrings.iterator(); it.hasNext();) { for (Iterator it = reStrings.iterator(); it.hasNext();) {
String line = (String) it.next(); String line = (String) it.next();
String str[] = line.split(":"); String str[] = line.split(":");
@ -134,67 +154,70 @@ public class VolumeInfo {
return volStatus; return volStatus;
} }
public Double getVolumeAllSize(String volumeName) { /**
log.info("get volume allSize"); * volumeName
Double allSize = null; * volumeName-1-2 volumeName
* @param volumeName
* @return
* @see [##]
*/
public Long getVolumeAvailableSize(String volumeName) {
log.info("get volume availableSize");
Long allSize = 0L;
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, String cmd = Constant.df + " | grep " + volumeName + "|awk '{print $4}'";
Constant.rootPasswd, Constant.port, Constant.df + "|grep " + volumeName + "|awk '{print $2}'"); List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
// System.out.println(reStrings); // System.out.println(reStrings);
if (reStrings == null) { if (reStrings == null) {
log.error("1801 get result is null"); log.error("1802 get result is error");
return null; return -2L;
} }
if (reStrings.size() == 0) { if (reStrings.size() == 0) {
log.error("1802 get result is nothing"); log.error("1801 " + volumeName + " is not exists!");
return null; return -1L;
}
/*
* char flag = reStrings.get(0).trim().toCharArray()[0]; if (flag < 48
* || flag > 57) { log.error("1803 get result string wrong"); return
* null; }
*/
for (Iterator it = reStrings.iterator(); it.hasNext();) {
String line = (String) it.next();
String str[] = line.split("[^0-9]");
allSize = Double.parseDouble(str[0]);
} }
allSize = Long.parseLong(reStrings.get(0));
return allSize; return allSize;
} }
public Double getVolumeUseSize(String volumeName) { /**
* volumeName
* volumeName-1-2 volumeName
* @param volumeName
* @return
* @see [##]
*/
public Long getVolumeUseSize(String volumeName) {
log.info("get volume used size"); log.info("get volume used size");
Double usedSize = null; Long usedSize = 0L;
if (volumeIsExists(volumeName) == false) {
log.error("1901 " + volumeName + " is not exists!");
return -1L;
}
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, String cmd = "df | grep " + volumeName + "|awk '{print $3}'";
Constant.rootPasswd, Constant.port, Constant.df + "|grep " + volumeName + "|awk '{print $3}'"); List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
// System.out.println(reStrings); // System.out.println(reStrings);
if (reStrings == null) { if (reStrings == null) {
log.error("1901 get result is null"); log.error("1901 get result is null");
return null; return -2L;
} }
if (reStrings.size() == 0) { if (reStrings.size() == 0) {
log.error("1902 get result is nothing"); log.error("1902 " + volumeName + " is not exists!");
return null; return -1L;
}
char flag = reStrings.get(0).trim().toCharArray()[0];
if (flag < 48 || flag > 57) {
log.error("1903 get result string wrong");
return null;
} }
usedSize = Long.parseLong(reStrings.get(0));
for (Iterator it = reStrings.iterator(); it.hasNext();) {
String line = (String) it.next();
String str[] = line.split("[^0-9]");
usedSize = Double.parseDouble(str[0]);
}
return usedSize; return usedSize;
} }
/**
* volumeNamebricks
* birckslist ip:path,volumeNamenull
* @param volumeName
* @return
* @see [##]
*/
public List<String> getVolumeBricks(String volumeName) { public List<String> getVolumeBricks(String volumeName) {
log.info("get volume bricks"); log.info("get volume bricks");
@ -205,31 +228,41 @@ public class VolumeInfo {
log.error("1601 get volume bricks wrong"); log.error("1601 get volume bricks wrong");
return null; return null;
} }
if (reStrings.size()==0) {
log.error("1602 "+volumeName+" is not exists!");
return null;
}
return reStrings; return reStrings;
} }
/**
* volumeName
* <>
* @param volumeName
* @return
* @see [##]
*/
public List<String> getVolumeMountPoint(String volumeName) { public List<String> getVolumeMountPoint(String volumeName) {
log.info("get volume MountPoint"); log.info("get volume MountPoint");
List<String> mountPoints = new ArrayList<>(); List<String> mountPoints = new ArrayList<>();
List<String> reStrings = Constant.ganymedSSH.execCmdWait(Constant.hostIp, Constant.rootUser, Constant.rootPasswd, String cmd=Constant.df + "|grep " + volumeName + "|awk '{print $6}'";
Constant.port, Constant.df + "|grep " + volumeName + "|awk '{print $6}'"); List<String> reStrings = Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
// System.out.println(reStrings); // System.out.println(reStrings);
if (reStrings == null) { if (reStrings == null) {
log.error("11001 get result string wrong"); log.error("11001 get result string wrong");
return null; return null;
} }
if (reStrings.size() == 0) { if (reStrings.size() == 0) {
log.error("11002 "+volumeName+" is no mountpoint"); log.error("11002 " + volumeName + " is not exists or no mountpoint");
return mountPoints;
}
char flag = reStrings.get(0).trim().toCharArray()[0];
if (flag != '/') {
log.error("11003 get result string wrong");
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);
@ -237,6 +270,39 @@ public class VolumeInfo {
return mountPoints; return mountPoints;
} }
public String getOneVolumeMountPoint(String volumeName) {
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);
// System.out.println(reStrings);
if (reStrings == null) {
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);
return mountPoint;
}
/**
* volumeNamebrick
* mapbricks <ip:path,data_size>
* @param volumeName
* @return
* @see [##]
*/
public Map<String, Double> getVolumebricksDataSize(String volumeName) { public Map<String, Double> getVolumebricksDataSize(String volumeName) {
List<String> bricks = getVolumeBricks(volumeName); List<String> bricks = getVolumeBricks(volumeName);
Map<String, Double> brick_size = new HashMap<>(); Map<String, Double> brick_size = new HashMap<>();
@ -256,7 +322,7 @@ public class VolumeInfo {
return null; return null;
} }
if (reStrings.size() == 0) { if (reStrings.size() == 0) {
log.error("1902 get result is nothing"); log.error("1902 "+brick+" is not exits!");
return null; return null;
} }
Pattern pattern = Pattern.compile("[0-9]*"); Pattern pattern = Pattern.compile("[0-9]*");
@ -270,6 +336,13 @@ public class VolumeInfo {
return brick_size; return brick_size;
} }
/**
* volumeNamebrick
* mapbricks <ip:path,available_size>
* @param volumeName
* @return
* @see [##]
*/
public Map<String, Double> getVolumebricksAvailableSize(String volumeName) { public Map<String, Double> getVolumebricksAvailableSize(String volumeName) {
List<String> bricks = getVolumeBricks(volumeName); List<String> bricks = getVolumeBricks(volumeName);
Map<String, Double> brick_size = new HashMap<>(); Map<String, Double> brick_size = new HashMap<>();
@ -303,6 +376,24 @@ public class VolumeInfo {
return brick_size; return brick_size;
} }
/**
* volumeNametruefalse <>
*
* @param volumeName
* @return
* @see [##]
*/
public boolean volumeIsExists(String volumeName) {
List<String> volumes = showAllVolumeName();
if (volumes == null) {
return false;
}
if (volumes.contains(volumeName)) {
return true;
}
return false;
}
// @Test // @Test
public void test_getVolumebricksDataSize() { public void test_getVolumebricksDataSize() {
System.out.println(getVolumebricksDataSize("gfs_ftp")); System.out.println(getVolumebricksDataSize("gfs_ftp"));
@ -319,6 +410,11 @@ public class VolumeInfo {
} }
@Test @Test
public void test_getVolumeStatus() {
System.out.println(getVolumeStatus("gs_fp"));
}
// @Test
public void test_getVolumeMountPoint() { public void test_getVolumeMountPoint() {
System.out.println(getVolumeMountPoint("gfs_ftp")); System.out.println(getVolumeMountPoint("gfs_ftp"));
System.out.println(getVolumeMountPoint("v1")); System.out.println(getVolumeMountPoint("v1"));

@ -14,6 +14,7 @@ public class Constant {
public static String df = "df -k "; public static String df = "df -k ";
public static String peerincluster_connected = "PeerinCluster(Connected)"; public static String peerincluster_connected = "PeerinCluster(Connected)";
public static String peerincluster_disconnected = "PeerinCluster(Disconnected)"; public static String peerincluster_disconnected = "PeerinCluster(Disconnected)";
public static String peerNotinCluster = "PeerNotinCluster";
public static String distributed = "distributed"; public static String distributed = "distributed";
public static String replica = "replica"; public static String replica = "replica";
public static String stripe = "stripe"; public static String stripe = "stripe";

Loading…
Cancel
Save