提交 0eea0651 authored 作者: chenshiying's avatar chenshiying

Merge remote-tracking branch 'origin/master'

package com.loit.common.script;
import com.loit.common.script.dto.BackendFrontEnum;
import com.loit.common.script.dto.DeployInfoDataDTO;
import com.loit.common.script.dto.FrontGroupEnum;
import com.loit.common.script.dto.FrontInstanceDto;
import com.loit.common.utils.DateUtils;
import com.loit.common.utils.ListUtil;
import com.loit.common.utils.StringUtils;
import com.loit.common.utils.excel.ImportExcel;
import com.loit.common.utils.file.FileUtils;
import com.loit.common.utils.freemarker.FreeMarkerUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
@Slf4j
public class GeneratorInstanceSQL {
protected static String root_path = "D:\\gitwork\\loit-build-common\\loit-build-component\\loit-build-deploy-env\\src\\main\\resources\\bin";
protected static String root_path_full = root_path + "\\instance";
protected static String sqlFile = root_path_full + "\\instancesql.sql";
private static Map< String, FrontInstanceDto > frontInstanceDtoMap = new LinkedHashMap<>();
public static void main(String[] args) {
try {
//String filePathStr = Thread.currentThread().getContextClassLoader().getResource("deployInfo.xlsx").getPath();
String filePathStr = "D:\\gitwork\\loit-build-common\\loit-build-component\\loit-build-deploy-env\\src\\main\\resources\\deployInfo.xlsx";
File pdfFile = new File(filePathStr);
FileInputStream fileInputStream = new FileInputStream(pdfFile);
MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
ImportExcel ei = new ImportExcel(multipartFile, 1, 0);
List< DeployInfoDataDTO > grayVersionDataDTOList = ei.getDataList(DeployInfoDataDTO.class);
if (ListUtil.isEmpty(grayVersionDataDTOList)) {
return;
}
clearInfoForFile(sqlFile);
for (DeployInfoDataDTO grayVersionDataDTO : grayVersionDataDTOList) {
buildInstanceSQL(grayVersionDataDTO);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
* 生成实例初始化脚本
*/
private static void buildInstanceSQL(DeployInfoDataDTO grayVersionDataDTO) throws IOException {
String serviceName = grayVersionDataDTO.getServiceName();
String fixedIp = grayVersionDataDTO.getFixedIp();
String port = grayVersionDataDTO.getPort();
String nacosServiceName = grayVersionDataDTO.getNacosServiceName();
String backendFrontType = grayVersionDataDTO.getBackendFrontType();
if (StringUtils.isBlank(serviceName)) {
return;
}
String modelId = grayVersionDataDTO.getModelId();
String frontGroup = grayVersionDataDTO.getFrontGroup();
String instance_id = "";
if (BackendFrontEnum.BACKEND.getCode().equals(backendFrontType)) {
instance_id = fixedIp + "#" + port + "#DEFAULT#DEFAULT_GROUP@@" + nacosServiceName;
frontGroup = "";
}
String jenkinsJobName = createJenkinsJobName(grayVersionDataDTO);
String dateTime = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
String needCreateInstanceSql = grayVersionDataDTO.getNeedCreateInstanceSql();
if (!"no".equals(needCreateInstanceSql)) {
String sql = "insert into loit_gray_instance ( model_id, group_name, instance_id, instance_ip, instance_port, instance_version, instance_status, jenkins_job_name, jenkins_job_url, jenkins_job_check_url, restart_status, create_time, create_user, restart_time, restart_user, restart_log, down_jenkins_job_name, down_jenkins_job_url, down_jenkins_job_check_url, down_instance_user, down_instance_log, try_jenkins_count, try_down_jenkins_count, del_flag, network_key, instance_network_type) " +
"values('" + modelId + "','" + frontGroup + "','" + instance_id + "','" + fixedIp + "','" + port + "','1.0.0','0','" + jenkinsJobName + "',NULL,NULL,'3','" + dateTime + "',NULL,NULL,NULL,'','',NULL,NULL,NULL,NULL,'0','0','0','net1','net1');";
FileUtils.appendNewLine(sqlFile, sql);
}
}
private static String createJenkinsJobName(DeployInfoDataDTO grayVersionDataDTO) {
String serviceName = grayVersionDataDTO.getServiceName();
String floatingIp = grayVersionDataDTO.getFloatingIp();
String fixedIp = grayVersionDataDTO.getFixedIp();
String port = grayVersionDataDTO.getPort();
if ("getway".equals(serviceName)) {
serviceName = "gateway";
}
BackendFrontEnum backendFrontType = BackendFrontEnum.getEnumByCode(grayVersionDataDTO.getBackendFrontType());
String[] fIps = floatingIp.split("\\.");
String fIp = fIps[3];
String[] fixIps = fixedIp.split("\\.");
String fixIp = fixIps[3];
String jenkinsJobName = "";
if (BackendFrontEnum.BACKEND.equals(backendFrontType)) {
jenkinsJobName = "loit-deploy-" + serviceName + "-" + port + "-" + fIp + "-" + fixIp;
}
if (!BackendFrontEnum.BACKEND.equals(backendFrontType)) {
jenkinsJobName = "loit-front-deploy-" + serviceName + "-" + port + "-" + fIp + "-" + fixIp;
}
return jenkinsJobName;
}
public static void clearInfoForFile(String fileName) {
File file = new File(fileName);
try {
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("");
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论