提交 ea6482d0 authored 作者: chenshiying's avatar chenshiying

[新增] 部署脚本更新

上级 1f535a31
......@@ -2,6 +2,8 @@ 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.ListUtil;
import com.loit.common.utils.StringUtils;
import com.loit.common.utils.excel.ImportExcel;
......@@ -16,6 +18,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
......@@ -28,6 +31,8 @@ public class GeneratorScript {
protected static String root_path_full = root_path + "\\server";
private static Map<String, FrontInstanceDto> frontInstanceDtoMap = new LinkedHashMap<>();
public static void main(String[] args) {
......@@ -48,16 +53,69 @@ public class GeneratorScript {
return;
}
for (DeployInfoDataDTO grayVersionDataDTO : grayVersionDataDTOList) {
buildInitEnv(grayVersionDataDTO);
buildBackendConfig(grayVersionDataDTO);
buildFrontConfig(grayVersionDataDTO);
}
//对前端实例进行分组
for (DeployInfoDataDTO deployDataDTO : grayVersionDataDTOList) {
BackendFrontEnum backendFrontType = BackendFrontEnum.getEnumByCode(deployDataDTO.getBackendFrontType());
if (BackendFrontEnum.BACKEND.equals(backendFrontType)) {
continue;
}
String serviceName = deployDataDTO.getServiceName();
FrontInstanceDto frontInstanceDto = frontInstanceDtoMap.get(serviceName);
if (frontInstanceDto == null) {
frontInstanceDto = new FrontInstanceDto();
String serviceNameSub = "loit-" + serviceName.replace("-web", "").replace("-h5", "");
String serviceNameCookie = "loit" + serviceName.replaceAll("-", "");
frontInstanceDto.setServiceName(serviceName);
frontInstanceDto.setServiceNameSub(serviceNameSub);
frontInstanceDto.setServiceNameCookie(serviceNameCookie);
frontInstanceDto.setServiceNameAbb(deployDataDTO.getServiceNameAbb());
frontInstanceDtoMap.put(serviceName, frontInstanceDto);
}
FrontGroupEnum frontGroup = FrontGroupEnum.getEnumByCode(deployDataDTO.getFrontGroup());
String ipAndPort = deployDataDTO.getFixedIp() + ":" + deployDataDTO.getPort();
if (FrontGroupEnum.gray.equals(frontGroup)) {
frontInstanceDto.getGrayList().add(ipAndPort);
}
if (FrontGroupEnum.normal.equals(frontGroup)) {
frontInstanceDto.getNormalList().add(ipAndPort);
}
}
//生成前端nginx 配置文件
frontInstanceDtoMap.forEach((key, frontInstanceDto) -> {
if (StringUtils.isBlank(key)) {
return;
}
Map model = new HashMap();
model.put("serviceNameSub", frontInstanceDto.getServiceNameSub());
model.put("serviceNameCookie", frontInstanceDto.getServiceNameCookie());
model.put("serviceName", frontInstanceDto.getServiceName());
model.put("serviceNameAbb", frontInstanceDto.getServiceNameAbb());
model.put("grayList", frontInstanceDto.getGrayList());
model.put("normalList", frontInstanceDto.getNormalList());
//创建政务网nginx配置
buildFrontZwwNginxConfig(frontInstanceDto, model);
buildFrontHlwNginxConfig(frontInstanceDto, model);
});
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
......@@ -79,14 +137,10 @@ public class GeneratorScript {
nginxConfigFolder.mkdirs();
}
String filePath = fileDir + "\\initEnv.sh";
File file = new File(filePath);
if (!file.exists()) {
FileUtils.write(filePath, "#!/bin/bash ");
//
// //TODO
// FileUtils.appendNewLine(filePath, "rm -rf /home/soft/*");
}
String deployPath = grayVersionDataDTO.getDeployPath();
......@@ -167,6 +221,7 @@ public class GeneratorScript {
String deployJar = grayVersionDataDTO.getDeployJar();
String serviceNameAbb = grayVersionDataDTO.getServiceNameAbb();
String nginxConfigName = grayVersionDataDTO.getNginxConfigName();
String frontGroup = grayVersionDataDTO.getFrontGroup();
if (StringUtils.isBlank(serviceName)) {
......@@ -192,6 +247,7 @@ public class GeneratorScript {
model.put("deployJar", deployJar);
model.put("serviceNameAbb", serviceNameAbb);
model.put("nginxConfigName", nginxConfigName);
model.put("frontGroup", frontGroup);
String deployShFileName = fileDir + "\\deploy-" + serviceName + "-" + port + ".sh";
String deployShResult = FreeMarkerUtils.process("deploy-web-sh.ftl", model);
......@@ -207,5 +263,69 @@ public class GeneratorScript {
FileUtils.write(nginxName, nginxConfigResult);
}
}
private static void buildFrontZwwNginxConfig(FrontInstanceDto frontInstanceDto, Map model) {
String serviceName = frontInstanceDto.getServiceName();
//TODO
// if ("version-web".equals(serviceName)) {
// String serviceNameSub = "loit-" + serviceName.replace("-web", "").replace("-h5", "");
// model.put("serviceNameSub", serviceNameSub);
// String serviceNameCookie = "loit" + serviceName.replaceAll("-", "");
// model.put("serviceNameCookie", serviceNameCookie);
//
// //TODO
// List<String> grayList = new ArrayList<>();
// grayList.add("10.0.120.37:9125");
// grayList.add("10.0.120.128:9125");
//
// List<String> normalList = new ArrayList<>();
// normalList.add("10.0.120.37:9126");
// normalList.add("10.0.120.128:9126");
//
// model.put("grayList", grayList);
// model.put("normalList", normalList);
//
// String nginxConfig = FreeMarkerUtils.process("zww-nginx-upstream.ftl", model);
// System.out.println(nginxConfig);
// }
//
// if ("gray-server-web".equals(serviceName)) {
// String serviceNameSub = "loit-" + serviceName.replace("-web", "").replace("-h5", "");
// model.put("serviceNameSub", serviceNameSub);
// String serviceNameCookie = "loit" + serviceName.replaceAll("-", "");
// model.put("serviceNameCookie", serviceNameCookie);
//
// //TODO
// List<String> grayList = new ArrayList<>();
// grayList.add("10.0.120.135:9127");
// grayList.add("10.0.120.190:9127");
//
// List<String> normalList = new ArrayList<>();
// normalList.add("10.0.120.135:9128");
// normalList.add("10.0.120.190:9128");
//
// model.put("grayList", grayList);
// model.put("normalList", normalList);
//
// String nginxConfig = FreeMarkerUtils.process("zww-nginx-upstream.ftl", model);
// System.out.println(nginxConfig);
// }
String nginxConfig = FreeMarkerUtils.process("zww-nginx-upstream.ftl", model);
System.out.println(nginxConfig);
}
private static void buildFrontHlwNginxConfig(FrontInstanceDto frontInstanceDto, Map model) {
String nginxConfig = FreeMarkerUtils.process("hlw-nginx-upstream.ftl", model);
System.out.println(nginxConfig);
}
}
......@@ -56,6 +56,11 @@ public class DeployInfoDataDTO implements Serializable {
private String nginxConfigName;
@ApiModelProperty(value = "frontGroup")
@ExcelField(title = "frontGroup", sort = 11)
private String frontGroup;
public String getFixedIp() {
return fixedIp;
}
......@@ -135,4 +140,12 @@ public class DeployInfoDataDTO implements Serializable {
public void setNginxConfigName(String nginxConfigName) {
this.nginxConfigName = nginxConfigName;
}
public String getFrontGroup() {
return frontGroup;
}
public void setFrontGroup(String frontGroup) {
this.frontGroup = frontGroup;
}
}
package com.loit.common.script.dto;
/**
* 前端分组
*/
public enum FrontGroupEnum {
/**
* 灰度
*/
gray("gray", "灰度"),
/**
* normal 正常
*/
normal("normal", "正常");
private String code;
private String name;
private FrontGroupEnum(String code, String name) {
this.code = code;
this.name = name;
}
public String getName() {
return name;
}
public String getCode() {
return code;
}
/**
* 根据Code 获取枚举
*/
public static FrontGroupEnum getEnumByCode(String code) {
for (FrontGroupEnum bfEnum : FrontGroupEnum.values()) {
if (bfEnum.getCode().equals(code)) {
return bfEnum;
}
}
return null;
}
}
package com.loit.common.script.dto;
import java.util.ArrayList;
import java.util.List;
public class FrontInstanceDto {
private String serviceName;
private String serviceNameSub;
private String serviceNameCookie;
private String serviceNameAbb;
List<String> grayList = new ArrayList<>();
List<String> normalList = new ArrayList<>();
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public String getServiceNameSub() {
return serviceNameSub;
}
public void setServiceNameSub(String serviceNameSub) {
this.serviceNameSub = serviceNameSub;
}
public String getServiceNameCookie() {
return serviceNameCookie;
}
public void setServiceNameCookie(String serviceNameCookie) {
this.serviceNameCookie = serviceNameCookie;
}
public List<String> getGrayList() {
return grayList;
}
public void setGrayList(List<String> grayList) {
this.grayList = grayList;
}
public List<String> getNormalList() {
return normalList;
}
public void setNormalList(List<String> normalList) {
this.normalList = normalList;
}
public String getServiceNameAbb() {
return serviceNameAbb;
}
public void setServiceNameAbb(String serviceNameAbb) {
this.serviceNameAbb = serviceNameAbb;
}
}
10.0.120.162 root
10.0.120.218 root
10.0.120.193 root
10.0.120.221 root
10.0.120.103 root
10.0.120.61 root
10.0.120.124 root
10.0.120.151 root
10.0.120.248 root
10.0.120.237 root
10.0.120.154 root
10.0.120.89 root
10.0.120.122 root
10.0.120.147 root
10.0.120.250 root
10.0.120.18 root
10.0.120.199 root
10.0.120.192 root
10.0.120.245 root
10.0.120.58 root
10.0.120.187 root
10.0.120.36 root
10.0.120.92 root
10.0.120.130 root
10.0.120.21 root
10.0.120.127 root
10.0.120.53 root
10.0.120.242 root
10.0.120.68 root
10.0.120.225 root
10.0.120.69 root
10.0.120.116 root
10.0.120.94 root
10.0.120.212 root
10.0.120.143 root
10.0.120.44 root
10.0.120.26 root
10.0.120.134 root
10.0.120.184 root
10.0.120.195 root
10.0.120.16 root
10.0.120.132 root
10.0.120.35 root
10.0.120.238 root
10.0.120.104 root
10.0.120.3 root
10.0.120.247 root
10.0.120.153 root
10.0.120.152 root
10.0.120.37 root
10.0.120.128 root
10.0.120.135 root
10.0.120.190 root
10.0.120.118 root
10.0.120.13 root
10.0.120.78 root
upstream sone.${serviceNameSub}.web {
server 192.169.201.191:80 weight=5 max_fails=5 fail_timeout=30s;
server 192.169.201.111:80 weight=5 max_fails=5 fail_timeout=30s;
server 192.169.201.184:80 weight=5 max_fails=5 fail_timeout=30s;
}
location /${serviceNameAbb} {
proxy_pass http://sone.${serviceNameSub}.web;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
<?xml version='1.1' encoding='UTF-8'?>
<project>
<actions/>
<description></description>
<keepDependencies>false</keepDependencies>
<properties>
<com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="gitlab-plugin@1.5.13">
<gitLabConnection>gitlab140</gitLabConnection>
</com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>
<jenkins.model.BuildDiscarderProperty>
<strategy class="hudson.tasks.LogRotator">
<daysToKeep>-1</daysToKeep>
<numToKeep>3</numToKeep>
<artifactDaysToKeep>-1</artifactDaysToKeep>
<artifactNumToKeep>-1</artifactNumToKeep>
</strategy>
</jenkins.model.BuildDiscarderProperty>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<hudson.model.StringParameterDefinition>
<name>deployName</name>
<description>文件名称</description>
<defaultValue></defaultValue>
<trim>false</trim>
</hudson.model.StringParameterDefinition>
<hudson.model.StringParameterDefinition>
<name>modelVersion</name>
<description>模块版本</description>
<defaultValue></defaultValue>
<trim>false</trim>
</hudson.model.StringParameterDefinition>
<hudson.model.StringParameterDefinition>
<name>modelServiceName</name>
<description>模块服务名</description>
<defaultValue></defaultValue>
<trim>false</trim>
</hudson.model.StringParameterDefinition>
<hudson.model.StringParameterDefinition>
<name>projectCode</name>
<description>项目编号</description>
<defaultValue></defaultValue>
<trim>false</trim>
</hudson.model.StringParameterDefinition>
<hudson.model.ChoiceParameterDefinition>
<name>status</name>
<description>start 启动 ,stop停止,deploy部署
(deploy代码传递参数,界面不要选择此项)</description>
<choices class="java.util.Arrays$ArrayList">
<a class="string-array">
<string>start</string>
<string>stop</string>
<string>deploy</string>
</a>
</choices>
</hudson.model.ChoiceParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<authToken>1156ddb265bc6ec3c190a17d74735d8f28</authToken>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>
echo &quot;###################begin start#############&quot;
echo &apos;deployName:&apos; + $deployName
echo &apos;modelServiceName:&apos; + $modelServiceName
echo &apos;modelVersion:&apos; + $modelVersion
echo &apos;projectCode:&apos; + $projectCode
echo &apos;status:&apos;+ $status
echo &quot;###################end print#############&quot;
echo &quot;###################begin write#############&quot;
rm -rf updowncommand.txt
echo $status &gt; updowncommand.txt
echo &quot;###################end write###############&quot;
ls -la
if [ &quot;$status&quot; = &quot;deploy&quot; ]; then
rm -rf $deployName
destDir=&quot;/home/version/push-jar/${projectCode}/${modelServiceName}/${modelVersion}/${deployName}&quot;
echo &apos;destDir:&apos; + $destDir
# 将版本下的jar复制到容器下
scp root@10.0.120.26:$destDir .
tempFileName=&quot;${deployName}.temp&quot;
rm -rf tempFileName
echo &quot;###################重命名zip文件#############&quot;
mv $deployName $tempFileName
mv $tempFileName dist.zip
fi
ls -la</command>
</hudson.tasks.Shell>
</builders>
<publishers>
<jenkins.plugins.publish__over__ssh.BapSshPublisherPlugin plugin="publish-over-ssh@1.20.1">
<consolePrefix>SSH: </consolePrefix>
<delegate plugin="publish-over@0.22">
<publishers>
<jenkins.plugins.publish__over__ssh.BapSshPublisher plugin="publish-over-ssh@1.20.1">
<configName>192.169.200.51(10.0.120.184)</configName>
<verbose>false</verbose>
<transfers>
<jenkins.plugins.publish__over__ssh.BapSshTransfer>
<remoteDirectory>/home/soft/loit-portal-web-9527</remoteDirectory>
<sourceFiles>dist.zip,updowncommand.txt</sourceFiles>
<excludes></excludes>
<removePrefix></removePrefix>
<remoteDirectorySDF>false</remoteDirectorySDF>
<flatten>false</flatten>
<cleanRemote>false</cleanRemote>
<noDefaultExcludes>false</noDefaultExcludes>
<makeEmptyDirs>false</makeEmptyDirs>
<patternSeparator>[, ]+</patternSeparator>
<execCommand>cd /home/soft/loit-portal-web-9527
exec bash deploy-portal-web-9527.sh start</execCommand>
<execTimeout>120000</execTimeout>
<usePty>false</usePty>
<useAgentForwarding>false</useAgentForwarding>
</jenkins.plugins.publish__over__ssh.BapSshTransfer>
</transfers>
<useWorkspaceInPromotion>false</useWorkspaceInPromotion>
<usePromotionTimestamp>false</usePromotionTimestamp>
</jenkins.plugins.publish__over__ssh.BapSshPublisher>
</publishers>
<continueOnError>false</continueOnError>
<failOnError>false</failOnError>
<alwaysPublishFromMaster>false</alwaysPublishFromMaster>
<hostConfigurationAccess class="jenkins.plugins.publish_over_ssh.BapSshPublisherPlugin" reference="../.."/>
</delegate>
</jenkins.plugins.publish__over__ssh.BapSshPublisherPlugin>
</publishers>
<buildWrappers/>
upstream sone.${serviceNameSub}.gray.web {
least_conn;
<#list grayList as gray>
server ${gray} weight=5 max_fails=5 fail_timeout=30s;
</#list>
}
upstream sone.${serviceNameSub}.normal.web {
least_conn;
<#list normalList as normal>
server ${normal} weight=5 max_fails=5 fail_timeout=30s;
</#list>
}
upstream sone.${serviceNameSub}.all.web {
<#list grayList as gray>
server ${gray} weight=5 max_fails=5 fail_timeout=30s;
</#list>
<#list normalList as normal>
server ${normal} weight=5 max_fails=5 fail_timeout=30s;
</#list>
}
location /${serviceNameAbb} {
set $gray_url "";
set $gray_url $cookie_${serviceNameCookie}_fronturl;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
if ($gray_url = "none") {
return 404;
}
if ($gray_url = "normal"){
proxy_pass http://sone.${serviceNameSub}.normal.web;
}
if ($gray_url = "gray"){
proxy_pass http://sone.${serviceNameSub}.gray.web;
}
if ($gray_url ~* ^(.*?)\.(.*?)\.(.*?)$){
proxy_pass http://$gray_url;
}
if ($gray_url = ''){
proxy_pass http://sone.${serviceNameSub}.all.web;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论