package com.huawei.seco.configbackup.servicemgr.common;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.huawei.enterprise.naas.core.util.IoStreamUtil;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class TomcatUtil
{
private static Logger LOGGER = LoggerFactory.getLogger(TomcatUtil.class);
private static final int BUFFER_SIZE = 2048;
public int uploadFile(String path) throws IOException
{
String puppetMasterIp = getPuppetMasterIp();
if (StringUtils.isEmpty(path))
{
LOGGER.error("file path empty");
return HttpURLConnection.HTTP_INTERNAL_ERROR;
}
Path fileNamePath = Paths.get(path).getFileName();
if (fileNamePath == null)
{
LOGGER.error("file path illegal");
return HttpURLConnection.HTTP_INTERNAL_ERROR;
}
String fileName = URLEncoder.encode(fileNamePath.toString(), "UTF-8");
URL url = new URL("https://" + puppetMasterIp + ":8081" + "/UploadX/UploadServlet?" + "fileName=" + fileName);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setConnectTimeout(0);
connection.setUseCaches(false);
OutputStream outputStream = null;
FileInputStream inputStream = null;
try
{
outputStream = connection.getOutputStream();
inputStream = new FileInputStream(path);
byte[] buffer = new byte[BUFFER_SIZE];
int size;
while ((size = inputStream.read(buffer)) != -1)
{
outputStream.write(buffer, 0, size);
}
outputStream.flush();
int responseCode = connection.getResponseCode();
LOGGER.info("responseCode:{}, ResponseMessage={}", responseCode, connection.getResponseMessage());
return responseCode;
}
catch (IOException e)
{
LOGGER.error("IOException");
return HttpURLConnection.HTTP_INTERNAL_ERROR;
}
finally
{
IoStreamUtil.closeStream(inputStream);
IoStreamUtil.closeStream(outputStream);
}
}
public int removeFile(String path)
{
String puppetMasterIp = getPuppetMasterIp();
LOGGER.info("[removeFile] ip={}, path={}", puppetMasterIp, path);
try
{
URL url = new URL("https://" + puppetMasterIp + ":8081" + "/UploadX/UploadServlet?" + "path=" + URLEncoder.encode(path, "UTF-8"));
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
int responseCode = connection.getResponseCode();
LOGGER.info("responseCode:{}", responseCode);
return responseCode;
}
catch (IOException e)
{
LOGGER.error("IOException");
return HttpURLConnection.HTTP_INTERNAL_ERROR;
}
}
private String getPuppetMasterIp()
{
String masterName = null;
String masterIp = null;
Path puppetConf = Paths.get(File.separator, "etc", "puppet", "puppet.conf");
List<String> confLines = null;
try
{
confLines = Files.readAllLines(puppetConf);
}
catch (IOException e)
{
return null;
}
for (String line : confLines)
{
if (line.startsWith("server="))
{
masterName = line.substring("server=".length());
break;
}
}
Path hosts = Paths.get(File.separator, "etc", "hosts");
List<String> hostsLines = null;
try
{
hostsLines = Files.readAllLines(hosts);
}
catch (IOException e)
{
return null;
}
for (String line : hostsLines)
{
if (line.startsWith("#"))
{
continue;
}
String[] elements = line.split(" ");
for (String element : elements)
{
if (element.equals(masterName))
{
masterIp = elements[0];
break;
}
}
}
if (StringUtils.isEmpty(masterIp))
{
LOGGER.error("[getPuppetMasterIp] masterIp is empty.");
}
return masterIp;
}
}
# The configuration file for master. Note that this file
# is likely to have unused settings in it; any setting that's
# valid anywhere in Puppet can be in any config file, even if it's not used.
[main]
server=controller-10-184-207-245
listen=true
report=false
[agent]
certname=controller-10-184-207-245
waitforcert=10
bindaddress=10.184.207.245
#
# hosts This file describes a number of hostname-to-address
# mappings for the TCP/IP subsystem. It is mostly
# used at boot time, when no name servers are running.
# On small systems, this file can be used instead of a
# "named" name server.
# Syntax:
#
# IP-Address Full-Qualified-Hostname Short-Hostname
#
127.0.0.1 localhost
# special IPv6 addresses
::1 localhost ipv6-localhost ipv6-loopback
fe00::0 ipv6-localnet
ff00::0 ipv6-mcastprefix
ff02::1 ipv6-allnodes
ff02::2 ipv6-allrouters
ff02::3 ipv6-allhosts
10.171.143.198 SZV1000113500
10.120.94.138 SZV1000120866
100.106.92.110 SHA1000122567
100.107.213.32 SHA1000122624
100.107.164.151 SHA1000123355
100.106.48.178 SHA1000123392
100.107.244.196 SHA1000124053
100.107.245.253 SHA1000124402
#add by gaussdb install
127.0.0.1 localhost
10.184.207.245 controller-10-184-207-245
private String getPuppetMasterIp()
{
String masterName = null;
String masterIp = null;
Path puppetConf = Paths.get(File.separator, "etc", "puppet", "puppet.conf");
List<String> confLines = null;
try
{
confLines = Files.readAllLines(puppetConf);
}
catch (IOException e)
{
return null;
}
for (String line : confLines)
{
if (line.startsWith("server="))
{
masterName = line.substring("server=".length());
break;
}
}
Path hosts = Paths.get(File.separator, "etc", "hosts");
List<String> hostsLines = null;
try
{
hostsLines = Files.readAllLines(hosts);
}
catch (IOException e)
{
return null;
}
for (String line : hostsLines)
{
if (line.startsWith("#"))
{
continue;
}
String[] elements = line.split(" ");
for (String element : elements)
{
if (element.equals(masterName))
{
masterIp = elements[0];
break;
}
}
}
if (StringUtils.isEmpty(masterIp))
{
LOGGER.error("[getPuppetMasterIp] masterIp is empty.");
}
return masterIp;
}
package com.huawei.seco.configbackup.common.util;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FilesUtil
{
private static Logger log = LoggerFactory.getLogger(FilesUtil.class);
//每个设备最多存储的文件个数
public static int MAX_FILE_NUM = 10;
/**
* 在环境上创建文件夹,并返回文件路径
* <一句话功能简述>
* <功能详细描述>
* @param directory 当前文件路径
* @param fileName 文件夹名称
* @return
* @author l00416108
*/
public static String createFile(String directory, String fileName)
{
//String separator = File.separator;
// 在内存中创建一个文件对象,注意:此时还没有在硬盘对应目录下创建实实在在的文件
File f = new File(directory,fileName);
if(!f.exists())
{
f.getParentFile().mkdirs();
f.mkdir();
}
StringBuffer curPath = new StringBuffer(directory);
curPath.append(File.separator).append(fileName);
return curPath.toString();
}
//将指定文件夹下的所有文件(除压缩文件除外)压缩,该文件夹下没有子文件夹,否则需递归进行处理
//压缩文件名为日期名
public static void zipFiles(String sourceFilePath, String backupType)
{
//判断是否有指定文件夹
File sourceFile = new File(sourceFilePath);
if(!sourceFile.exists())
{
//tmpFile.mkdirs();
log.info("待压缩的文件目录:"+sourceFile+"不存在");
return;
}
//生产压缩文件名
String zipName = generateId(backupType);
File zipFile = new File(sourceFile+"\\"+zipName+".zip");
File[] sourceFiles = sourceFile.listFiles();
if(null == sourceFiles || sourceFiles.length<1)
{
log.info("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
return;
}
BufferedInputStream bis = null;
ZipOutputStream zos = null;
byte[] bufs = new byte[1024*10];
FileInputStream fis = null;
try
{
zos = new ZipOutputStream(new FileOutputStream(zipFile));
for(int i=0; i<sourceFiles.length; i++)
{
//创建zip实体,并添加进压缩包
String tmpFileName = sourceFiles[i].getName();
if(tmpFileName.endsWith(".zip"))
continue;
ZipEntry zipEntry = new ZipEntry(tmpFileName);
zos.putNextEntry(zipEntry);
//读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10))!=-1)
{
zos.write(bufs, 0, read);
}
fis.close();//很重要,否则删除不掉!
sourceFiles[i].delete();//将要进行压缩的源文件删除
}//end for
log.info("文件打包成功!");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
//关闭流
try
{
if(null!=bis)
bis.close();
if(null!=zos)
//zos.closeEntry();
zos.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 删除指定文件夹下的压缩文件
* <一句话功能简述>
* <功能详细描述>
* @param filePath
* @param isAll
* @throws ParseException
* @author l00416108
*/
public static void deleteZipFiles(String filePath, boolean isAll) throws ParseException
{
log.info("filePath is {}",filePath);
File delFile = new File(filePath);
if(!delFile.exists())
{
//tmpFile.mkdirs();
log.info("待删除的文件目录:"+delFile+"不存在");
return;
}
File[] delFiles = delFile.listFiles();
if(null == delFiles || delFiles.length < 1)
{
log.info(filePath+"下没有要删除的文件.");
return;
}
//收集压缩文件,过滤掉非压缩的文件以及文件夹
List<File> delFilesTarget = new ArrayList<File>();
for(int i=0; i<delFiles.length; i++)
{
String tmpFileName = delFiles[i].getName();
if(tmpFileName.endsWith(".zip"))
{
delFilesTarget.add(delFiles[i]);
}
}
//删除所有的文件
if(isAll)
{
for(File file : delFilesTarget)
{
file.delete();
}
}
orderByNameDate(delFilesTarget);//按文件名的日期排序(倒序)
//如果文件个数超过最大个数,删除文件
int lastIndex = delFilesTarget.size()-1;
while(lastIndex >= MAX_FILE_NUM)
{
File delF = delFilesTarget.remove(lastIndex);
lastIndex -= 1;
if(!delF.delete())
{
log.info("文件"+delF.getName()+"删除失败!");
}
else
{
log.info("文件"+delF.getName()+"删除成功!");
}
}
}
/**
* 删除指定名称的文件
* <一句话功能简述>
* <功能详细描述>
* @param filePath
* @throws ParseException
* @author l00416108
*/
public static void deleteZipFilesByName(String filePath, List<String> fileNames) throws ParseException
{
File delFile = new File(filePath);
if(!delFile.exists())
{
//tmpFile.mkdirs();
log.info("待删除的文件目录:"+delFile+"不存在");
return;
}
File[] delFiles = delFile.listFiles();
if(null == delFiles || delFiles.length < 1)
{
log.info(filePath+"下没有要删除的文件.");
return;
}
for(File file: delFiles)
{
//if(file.getName().equals(fileName))
if(fileNames.contains(file.getName()))
{
if(!file.delete())
{
log.info("文件"+file.getName()+"删除失败!");
}
else
{
log.info("文件"+file.getName()+"删除成功!");
}
}
}
}
/**
* 判断文件是否存在
* <一句话功能简述>
* <功能详细描述>
* @param filePath
* @return
* @author l00416108
*/
public static boolean isExsitFile(String filePath)
{
File file = new File(filePath);
if(file.exists())
{
return true;
}
return false;
}
/**
* 以日期+ 备份文件类型生成文件名
* <一句话功能简述>
* <功能详细描述>
* @return
* @author l00416108
*/
public static String generateId(String fileType)
{
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
StringBuffer sb = new StringBuffer();
//获取当前系统时间
sb.append(df.format(new Date()));
sb.append(fileType);
return sb.toString();
}
//按文件名的日期排序(倒序)
public static void orderByNameDate(List<File> files)
{
final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Collections.sort(files, new Comparator<File>()
{
public int compare(File f1, File f2)
{
// TODO Auto-generated method stub
try
{
String name1 = f1.getName();
String name2 = f2.getName();
Date f1Date = sdf.parse(name1.substring(0,name1.length()-1));
Date f2Date = sdf.parse(name2.substring(0,name2.length()-1));
//return f1Date.compareTo(f2Date);//正序
return f2Date.compareTo(f1Date);//逆序
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
});
}
}