NodeBuildCtrl.java
13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package com.ui.ctrl;
import com.ui.contants.HttpUriContants;
import com.ui.ctrl.util.ReleaseCheck;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.domain.BuildMessage;
import com.ui.model.req.BuildRequest;
import com.ui.model.req.User;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("nodeBuild")
public class NodeBuildCtrl {
Logger log = LoggerFactory.getLogger(NodeBuildCtrl.class);
@Autowired
private HttpRestClient httpRestClient;
@Autowired
private ReleaseCheck releaseCheck;
@Autowired
private Pch5SwitchCtrl pch5SwitchCtrl;
private final String AZ2_Auto_Switch_Online_CHECK="az2AutoSwitchOnlineCheck";
private String URL = "http://172.31.16.167:8883/node/";
@RequestMapping("/toProject")
public ModelAndView toProject(Model model,String workJobId) {
if(StringUtils.isNotBlank(workJobId)){
BaseResponse response = httpRestClient.defaultGet(HttpUriContants.GET_RELEASEWORKJOB_ID + "?id=" + workJobId, BaseResponse.class);
if(response!=null&&response.getCode()==200&&response.getData()!=null){
Map map=(Map)response.getData();
String releaseWorkJob_environment=(String)(map.get("environment")==null?"":map.get("environment"));
if(releaseWorkJob_environment!=null&&releaseWorkJob_environment.length()>0&&!releaseWorkJob_environment.endsWith(",")){
releaseWorkJob_environment+=",";
}
String releaseWorkJob_project = map.get("project")==null?"":(String)map.get("project");
if(releaseWorkJob_project!=null&&releaseWorkJob_project.length()>0&&!releaseWorkJob_project.endsWith(",")){
releaseWorkJob_project += ",";
}
int releaseWorkJob_nodeProjectCount=0;
for(String tmp:releaseWorkJob_project.split(",")){
if(tmp!=null&&tmp.length()>0){
releaseWorkJob_nodeProjectCount++;
}
}
model.addAttribute("releaseWorkJob_nodeProjectCount", releaseWorkJob_nodeProjectCount);
model.addAttribute("releaseWorkJob_environment", releaseWorkJob_environment);
model.addAttribute("releaseWorkJob_branch", map.get("branch")==null?"":(String)map.get("branch"));
model.addAttribute("releaseWorkJob_project",releaseWorkJob_project);
model.addAttribute("releaseWorkJob_id", workJobId);
}
}
return new ModelAndView("project/node_project");
}
/**
* 获取所有项目
*
* @return
*/
@RequestMapping(value = "getProjects", method = RequestMethod.POST)
@ResponseBody
public String getProjects() {
try {
return httpRestClient.get(URL + "getProjects", String.class, null);
/*String tt= "[{\"exeOrder\":0,\"name\":\"YOHOBUY-Portal-fe2\",\"group\":\"Node项目\",\"order\":\"1\"},{\"exeOrder\":0,\"name\":\"YOHOBUY-Shop-fe2\",\"group\":\"Node项目\",\"order\":\"1\"},{\"exeOrder\":0,\"name\":\"YOHOBUY-shop-fe\",\"group\":\"Node项目\",\"order\":\"1\"},{\"exeOrder\":0,\"name\":\"YOHOBLK-WWW\",\"group\":\"Node项目\",\"order\":\"1\"},{\"exeOrder\":0,\"name\":\"YOHOBUY-WAP-NODE\",\"group\":\"Node项目\",\"order\":\"1\"},{\"exeOrder\":0,\"name\":\"YOHOBUY-PC-NODE\",\"group\":\"Node项目\",\"order\":\"1\"},{\"exeOrder\":0,\"name\":\"YOHOBLK-WAP\",\"group\":\"Node项目\",\"order\":\"1\"}]";
return JSONArray.toJSONString(JSON.parseArray(tt));*/
} catch (Exception ex) {
return "failed";
}
}
/**
* 根据项目名字获取项目想起
* 获取所有项目
*
* @return
*/
@RequestMapping(value = "getProject", method = RequestMethod.POST)
@ResponseBody
public String getProjectByName(String name) {
try {
return httpRestClient.get(URL + "getProject?name="+name , String.class, null);
//return "{\"project\":\"yohobuy-node\",\"name\":\"YOHOBUY-PC-NODE\",\"group\":\"Node项目\",\"groupid\":1,\"envs\":[{\"name\":\"qcloud_gray\",\"host_ips\":[\"10.66.101.9\"]},{\"name\":\"az1\",\"host_ips\":[\"10.66.1.6\",\"10.66.1.7\",\"10.66.1.8\"]},{\"name\":\"az2\",\"host_ips\":[\"10.66.103.6\",\"10.66.103.7\"]},{\"name\":\"az1-az2\",\"host_ips\":[\"10.66.1.6\",\"10.66.1.7\",\"10.66.1.8\",\"10.66.103.6\",\"10.66.103.7\"]}]}";
} catch (Exception ex) {
return "failed";
}
}
/**
* 发布前校验
*
* @return
*/
@RequestMapping(value = "check")
@ResponseBody
public BaseResponse check(String operate_name,String project,String branch, String environment,String workid,HttpSession httpSession) {
BaseResponse rtnResponse=releaseCheck.check(workid,httpSession);
if(200!=rtnResponse.getCode()){
return rtnResponse;
}
if(environment.contains("az")&&"Deploy".equalsIgnoreCase(operate_name)){
//获取az2灰度情况
BaseResponse<Boolean> checkGrayResponse = httpRestClient.defaultGet("/pch5Switch/queryAz2InGray",BaseResponse.class);
if (checkGrayResponse==null||checkGrayResponse.getCode() != 200){
rtnResponse.setCode(201);
rtnResponse.setMessage("检查az2灰度失败");
return rtnResponse;
}
boolean az2InGray=checkGrayResponse.getData();
//检查分支和可用区的冲突
if(environment.contains("az1")){
if(!"master".equals(branch)){
rtnResponse.setCode(201);
rtnResponse.setMessage("az1区域只能发布master分支");
return rtnResponse;
}
}else{
//单独发布az2,环境可能还有aws等(其它不需要校验的项目)
if(environment.contains("az2")){
if("master".equals(branch)){
//z2区如果发master分支,需要检查所有项目(现在是3个:YOHOBUY-WAP-NODE , YOHOBUY-PC-NODE ,YOHOBLK-WAP)
//如果所有项目最近一次发送成功都是master分支,则自动关闭灰度环境
if(az2InGray){
Map<String,String> releaseRecordMap=this.getReleaseRecordMap();
if(releaseRecordMap!=null&&releaseRecordMap.size()>0){
rtnResponse.setData(AZ2_Auto_Switch_Online_CHECK);
for(Map.Entry<String,String> entry:releaseRecordMap.entrySet()){
if(project.equals(entry.getKey())){
continue;
}
if(!"master".equals(entry.getValue())){
rtnResponse.setData("");//其它项目最近发布的是非master,不需要切换
break;
}
}
}
}else{
rtnResponse.setData("az2AlreadyOnline");
}
}else{
//az2区如果发非master分支,发布之前自动切换到灰度环境
if(az2InGray){
rtnResponse.setData("az2AlreadyInGray");
}else{
rtnResponse.setData("az2AutoSwitchGray");
}
}
}
}
}
return rtnResponse;
}
@RequestMapping(value = "getReleaseRecordMap")
@ResponseBody
public Map<String,String> getReleaseRecordMap(){
BaseResponse<Map> releaseRecordAz2Response = httpRestClient.defaultGet("/pch5Switch/getAzProjectLatestReleaseHistoryMap",BaseResponse.class);
if(releaseRecordAz2Response!=null&&200==releaseRecordAz2Response.getCode()){
Map<String,String> releaseRecordMap=releaseRecordAz2Response.getData();
return releaseRecordMap;
}
return null;
}
/**
* 执行任务
*
* @return
*/
@RequestMapping(value = "build", method = RequestMethod.POST)
public ModelAndView build(String workid_name,String project_name, String environment_name,
String operate_name, String branch_name, String rollbackfile_name, String hosts_name
, boolean force_name, Model model, HttpSession session,String az2AutoSwitchOnlineCheck) {
try {
User user = (User) session.getAttribute("user");
BaseResponse checkResponse=releaseCheck.checkAndUpdate(workid_name,operate_name,user.getName());
if(200!=checkResponse.getCode()){
model.addAttribute("environment_name", checkResponse.getMessage());
return new ModelAndView("project/node_project_build");
}
List<BuildMessage> list = new ArrayList<>();
BuildRequest buildRequest = new BuildRequest();
buildRequest.setBranch(branch_name);
buildRequest.setUser(user.getName());
buildRequest.setEnvironment(environment_name);
buildRequest.setRollbackfile(rollbackfile_name);
buildRequest.setOperate(operate_name);
buildRequest.setHost(hosts_name);
buildRequest.setIsForce(String.valueOf(force_name));
buildRequest.setWorkid(workid_name);
String messageids = "";
buildRequest.setProject(project_name);
BuildMessage buildMessage = httpRestClient.post(URL + "build", buildRequest, BuildMessage.class);
if (buildMessage != null) {
list.add(buildMessage);
messageids = messageids + buildMessage.getId() + ",";
}
model.addAttribute("environment_name", environment_name);
model.addAttribute("operate_name", operate_name);
model.addAttribute("branch_name", branch_name);
model.addAttribute("rollbackfile_name", rollbackfile_name);
model.addAttribute("messageids", messageids);
model.addAttribute("messageList", list);
model.addAttribute("hosts_name", hosts_name);
model.addAttribute("az2AutoSwitchOnlineCheck", az2AutoSwitchOnlineCheck);
} catch (Exception ex) {
log.error("build", ex.toString());
}
return new ModelAndView("project/node_project_build");
}
/**
* 切换az2区域
*
* @return
*/
@RequestMapping(value = "switchAz2", method = RequestMethod.POST)
@ResponseBody
public BaseResponse switchAz2(String onlineOrGray) {
return pch5SwitchCtrl.switchAz2(onlineOrGray);
}
/**
* 切换az2区域到online
* 检测符合条件之后才切换
*
* @return
*/
@RequestMapping(value = "switchAz2ToOnLineCheck")
@ResponseBody
public BaseResponse switchAz2ToOnLineCheck(String environment_name,
String operate_name, String branch_name) {
Map<String,String> releaseRecordMap=this.getReleaseRecordMap();
boolean switchFlag=false;
if(releaseRecordMap!=null&&releaseRecordMap.size()>0){
for(Map.Entry<String,String> entry:releaseRecordMap.entrySet()){
if(!"master".equals(entry.getValue())){
switchFlag=false;
break;
}else{
switchFlag=true;
}
}
}
return new BaseResponse(switchFlag);
}
/**
* 获取执行结果
*
* @param messageid
* @return
*/
@RequestMapping(value = "getbuildmsg", method = RequestMethod.POST)
@ResponseBody
public String getbuildmsg(String messageid, String project) {
return releaseCheck.getbuildmsg(URL,messageid,project);
}
@RequestMapping(value = "cancelBuild")
@ResponseBody
public String cancelBuild(String id) {
return releaseCheck.cancelBuild(URL,id);
}
@RequestMapping(value = "rollbackList", method = RequestMethod.POST)
@ResponseBody
public String rollbackList(String project, String environment) {
return releaseCheck.rollbackList(URL,project,environment);
}
@RequestMapping(value = "refreshGit", method = RequestMethod.POST)
@ResponseBody
public String refreshGit() {
return releaseCheck.refreshGit(URL);
}
}