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

1
.gitignore vendored

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

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

@ -55,7 +55,7 @@ public class CopyData {
return -3;
}
reStrings=showData.showFolderData(sourceFolderName+"/"+fileName);
reStrings=showData.showFolderData(sourceFolderName+"/"+fileName);
if(reStrings==null){
log.info("3202 "+sourceFolderName+"/"+fileName+" is not exists");
return -2;
@ -71,11 +71,27 @@ public class CopyData {
log.info("copy " + sourceFolderName +"/" + fileName+ " to " + destFolderName + " running");
return 1;
}
@Test
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
public void testcreateFolders() {
createFolders("/aaa/vvv/ddd/www/rrrr");
}
//@Test
public void testCopyFolderFiles() {
PropertyConfigurator.configure("log4j.properties");
copyFolderFiles("/home", "/home/ubuntu", "system_data");
}
}

@ -16,17 +16,7 @@ import com.platform.utils.Constant;
public class RemoveData {
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
@ -34,65 +24,33 @@ public class RemoveData {
* @param fileName
* @return
*/
public int deleteFolderFiles(String folderName,String fileName){
log.info("start delete "+folderName+"/"+fileName);
public int deleteFolder(String folderName){
log.info("start delete "+folderName);
ShowData showData=new ShowData();
Map<String,String> reStrings=showData.showFolderData(folderName+"/"+fileName);
Map<String,String> reStrings=showData.showFolderData(folderName);
if(reStrings==null){
log.error("3301 "+folderName+"/"+fileName+" is not exists");
log.error("3301 "+folderName+" is not exists");
return -1;
}
String command="rm -r "+folderName+"/"+fileName;
String command="rm -r "+folderName;
// int status=runCommand.runCommand(command);
Constant.ganymedSSH.execCmdNoWaitAcquiescent(command);
log.info("delete "+folderName+" "+fileName+" running");
log.info("delete "+folderName+" running");
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
public void testDeleteFolderFiles() {
PropertyConfigurator.configure("log4j.properties");
deleteFolderFiles("/home/ubuntu","system_data");
deleteFolder("/home/ubuntu");
}
}

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

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

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

Loading…
Cancel
Save