|
|
package Tools;
|
|
|
|
|
|
import cn.hutool.core.io.file.FileReader;
|
|
|
import cn.hutool.core.io.file.FileWriter;
|
|
|
import com.dsideal.dsBase.Util.SSHUtil;
|
|
|
import com.jfinal.kit.HttpKit;
|
|
|
import com.jfinal.kit.PathKit;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
public class DistributeToUndertow {
|
|
|
|
|
|
//在独立的main函数中,使用下面的方式进行声明logback对象
|
|
|
private final static Logger log = LoggerFactory.getLogger(DistributeToUndertow.class);
|
|
|
public static Map<String, String> map = new HashMap<>();
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
//修改undertow Path
|
|
|
//黄海添加于2021-10-29
|
|
|
updateUndertowPath();
|
|
|
//主机
|
|
|
String host = "192.168.15.68";
|
|
|
String user = "root";
|
|
|
String pwd = "DsideaL4r5t6y7u!@#";
|
|
|
int port = 22;
|
|
|
//声明SSH对象
|
|
|
SSHUtil ssh = new SSHUtil(user, pwd, host, port);
|
|
|
ssh.connect();
|
|
|
|
|
|
//准备工作
|
|
|
ssh.exec("mkdir -p /usr/local/tomcat8/webapps/dsBase/WEB-INF/lib");
|
|
|
ssh.exec("echo '' > /usr/local/tomcat8/logs/dsBase.log");
|
|
|
ssh.exec("cd /usr/local/tomcat8/webapps/dsBase/WEB-INF/classes/ && dos2unix *.sh");
|
|
|
ssh.exec("chmod +x /usr/local/tomcat8/webapps/dsBase/WEB-INF/classes/start.sh");
|
|
|
ssh.exec("chmod +x /usr/local/tomcat8/webapps/dsBase/WEB-INF/classes/debug.sh");
|
|
|
ssh.exec("chmod +x /usr/local/tomcat8/webapps/dsBase/WEB-INF/classes/stop.sh");
|
|
|
|
|
|
//源目录
|
|
|
String basePath = PathKit.getRootClassPath();
|
|
|
String[] dirs = new String[]{"\\com", "\\Backup", "\\Plugin", "\\Sql", "\\ExcelExportTemplate", "\\ExcelImportTemplate"};
|
|
|
//目标目录
|
|
|
String targetPath = "/usr/local/tomcat8/webapps/dsBase/WEB-INF/classes";
|
|
|
List<String> uploadDir = new ArrayList<>();
|
|
|
for (int i = 0; i < dirs.length; i++) {
|
|
|
uploadDir.add(basePath + dirs[i]);
|
|
|
}
|
|
|
for (int i = 0; i < dirs.length; i++) {
|
|
|
String deletePath = targetPath + dirs[i].replace("\\", "/");
|
|
|
ssh.exec("rm -rf " + deletePath);
|
|
|
}
|
|
|
|
|
|
//上传配置文件
|
|
|
File file = new File(basePath);
|
|
|
File[] tempList = file.listFiles();
|
|
|
for (int i = 0; i < tempList.length; i++) {
|
|
|
if (tempList[i].isFile())
|
|
|
ssh.upload(tempList[i].getAbsolutePath(), targetPath + "/" + tempList[i].getName());
|
|
|
}
|
|
|
//上传其它目录下的文件
|
|
|
for (int i = 0; i < uploadDir.size(); i++) {
|
|
|
//上传
|
|
|
map.clear();
|
|
|
getFileList(uploadDir.get(i));
|
|
|
for (Map.Entry<String, String> entry : map.entrySet()) {
|
|
|
String fullPath = entry.getKey();
|
|
|
file = new File(fullPath);
|
|
|
String childPath = fullPath.replace(basePath, "").replace("\\", "/").replace(file.getName(), "");
|
|
|
ssh.mkdir(targetPath + childPath);
|
|
|
ssh.upload(file.getAbsolutePath(), targetPath + childPath+"/" + file.getName());
|
|
|
log.info("成功上传文件:" + fullPath);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//重启undertow
|
|
|
ssh.exec("ps aux | grep \"tail\" |grep -v grep| cut -c 9-15 | xargs kill -9");
|
|
|
ssh.exec("cd /usr/local/tomcat8/webapps/dsBase/WEB-INF/classes && dos2unix *.sh");
|
|
|
ssh.exec("cd /usr/local/tomcat8/webapps/dsBase/WEB-INF/classes && ./stop.sh");
|
|
|
Thread.sleep(3000);
|
|
|
ssh.exec("cd /usr/local/tomcat8/webapps/dsBase/WEB-INF/classes && ./start.sh");
|
|
|
//检查是不是启动成功了
|
|
|
while (true) {
|
|
|
try {
|
|
|
log.info("正在等待undertow启动...");
|
|
|
HttpKit.get("http://" + host);
|
|
|
break;
|
|
|
} catch (Exception err) {
|
|
|
Thread.sleep(500);
|
|
|
}
|
|
|
}
|
|
|
//断开连接
|
|
|
ssh.disconnect();
|
|
|
log.info("恭喜,所有工作成功完成!");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 功能:获取指定目录下的所有文件
|
|
|
* 作者:黄海
|
|
|
* 时间:2018-12-14
|
|
|
*
|
|
|
* @param strPath
|
|
|
* @return
|
|
|
*/
|
|
|
public static void getFileList(String strPath) {
|
|
|
File f = new File(strPath);
|
|
|
if (f.isDirectory()) {
|
|
|
File[] fs = f.listFiles();
|
|
|
for (int i = 0; i < fs.length; i++) {
|
|
|
String fsPath = fs[i].getAbsolutePath();
|
|
|
getFileList(fsPath);
|
|
|
}
|
|
|
} else if (f.isFile()) {
|
|
|
String fsPath = f.getAbsolutePath();
|
|
|
map.put(fsPath, "1");
|
|
|
} else {
|
|
|
log.error("路径不正确!" + f.getAbsolutePath());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 功能:更新undertowPath
|
|
|
*/
|
|
|
public static void updateUndertowPath() {
|
|
|
String fileName = "undertow.properties";
|
|
|
FileReader fileReader = new FileReader(fileName);
|
|
|
String result = fileReader.readString();
|
|
|
String lines[] = result.split("\\r?\\n");
|
|
|
String finalStr = "";
|
|
|
final String oldStr = "#undertow.resourcePath";
|
|
|
final String newStr = "undertow.resourcePath";
|
|
|
for (String line : lines) {
|
|
|
if (line.startsWith(oldStr)) {
|
|
|
|
|
|
} else if (line.startsWith(newStr))
|
|
|
finalStr += "undertow.resourcePath =/usr/local/tomcat8/webapps/dsBase,classpath:static\n";
|
|
|
else
|
|
|
finalStr += line + "\n";
|
|
|
}
|
|
|
//写入
|
|
|
FileWriter writer = new FileWriter(fileName);
|
|
|
writer.write(finalStr);
|
|
|
}
|
|
|
}
|