AutoScalingCtrl.java
14.4 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com.ui.ctrl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ui.cloud.AwsModifyScalingGroupReq;
import com.ui.cloud.CommAutoModifyScalingGroupReq;
import com.ui.cloud.DeployProjectCloud;
import com.ui.cloud.QcloudModifyScalingGroupReq;
import com.ui.cloud.task.AutoScalingFactory;
import com.ui.contants.HttpUriContants;
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 com.ui.project.ProjectEnvironment;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.*;
/**
* 亚马逊云 腾讯云 伸缩组操作
* Created by xh on 2017/5/9.
*/
@Controller
@RequestMapping("/autoScalingTool")
public class AutoScalingCtrl {
@Autowired
private HttpRestClient httpClient;
@Autowired
private AutoScalingFactory autoScalingFactory;
/**
* 跳转到页面
* @param model
* @return
*/
@RequestMapping("/toautoscalingpage")
public ModelAndView toAutoScalingPage(Model model) {
return new ModelAndView("manager/autoscaling");
}
/**
* 显示配置信息
* @param cloudType 云类型 1:AWS 2:QCLOUD
* @return
*/
@RequestMapping("/getAutoScalingGroups")
@ResponseBody
public BaseResponse<?> getAutoScalingGroups(int cloudType) {
return autoScalingFactory.getAutoScalingGroups(cloudType);
}
/**
* 显示配置信息
* @param cloudType 云类型 1:AWS 2:QCLOUD
* @return
*/
@RequestMapping("/autoscalingconfig")
@ResponseBody
public BaseResponse<?> autoScalingConfig(int cloudType) {
BaseResponse<?> response = httpClient.defaultPost(
HttpUriContants.GET_CLOUD_AUTOSCALING_CONFIG + "?cloudType="+cloudType, null,
BaseResponse.class);
return response;
}
/**
* 显示配置信息
* @param qCloudReq 修改的相关参数,暂时只用了腾讯云的,日后考虑亚马逊的
* @return
*/
@RequestMapping("/modifyScalingGroup")
@ResponseBody
public BaseResponse<?> modifyScalingGroup(QcloudModifyScalingGroupReq qCloudReq, int cloudType) {
CommAutoModifyScalingGroupReq req = new CommAutoModifyScalingGroupReq();
req.setCloudType(cloudType);
if(1==cloudType){
AwsModifyScalingGroupReq awsReq=new AwsModifyScalingGroupReq();
awsReq.setScalingGroupName(qCloudReq.getScalingGroupName());
awsReq.setMaxSize(qCloudReq.getMaxSize());
awsReq.setMinSize(qCloudReq.getMinSize());
awsReq.setDesiredCapacity(qCloudReq.getDesiredCapacity());
req.setAwsModifyScalingGroupReq(awsReq);
}else{
req.setQCloudParms(qCloudReq);
}
BaseResponse<?> response = httpClient.defaultPost(
HttpUriContants.GET_CLOUD_MODIFY_SCALING_GROUP, req, BaseResponse.class);
return response;
}
/**
* 腾讯云:修改伸缩组后的下一个流程是查询伸缩活动
* 亚马逊云:修改伸缩组后的下一个流程待定,因为不清楚
* @param scalingGroupId 待查询的伸缩组Id
* @param startTime 开始时间,如果不填,则默认是当前时间前10秒之后发生的活动
* @return
*/
@RequestMapping("/describeScalingActivity")
@ResponseBody
public BaseResponse<?> describeScalingActivity(int cloudType,String scalingGroupName,String scalingGroupId, String startTime){
/*String url = "";
if(1==cloudType){
url= String.format(HttpUriContants.GET_CLOUD_DESCRIBE_SCALING_ACTIVITY+"?cloudType=1&scalingGroupName=%s",scalingGroupName);
}else if(2==cloudType){
url= String.format(HttpUriContants.GET_CLOUD_DESCRIBE_SCALING_ACTIVITY+"?cloudType=2&scalingGroupId=%s&startTime=%s",scalingGroupId,startTime);
}
BaseResponse<?> response = httpClient.defaultPost(url, null, BaseResponse.class);*/
String url= String.format(HttpUriContants.GET_CLOUD_DESCRIBE_SCALING_ACTIVITY+"?cloudType=%d&scalingGroupName=%s",cloudType,scalingGroupName);
BaseResponse<?> response = httpClient.defaultPost(url, null, BaseResponse.class);
return response;
}
/**
* 腾讯云:根据实例id获取内网ip
* 亚马逊云待定:不清楚流程
* @param ids 实例id集合
* @return
*/
@RequestMapping("/getQcloudDescribeInstances")
@ResponseBody
public BaseResponse<?> getQcloudDescribeInstances(String ids){
String url = String.format(HttpUriContants.GET_QCLOUD_DESCRIBE_INSTANCES+"?instanceIds=%s",ids);
BaseResponse<?> response = httpClient.defaultPost(url, null, BaseResponse.class);
return response;
}
/**
* 腾讯云:根据实例id获取内网ip
* 亚马逊云待定:不清楚流程
* @param groupId 伸缩组id
* @return
*/
@RequestMapping("/getIpsByScalingGroupId")
@ResponseBody
public BaseResponse<?> getIpsByScalingGroupId(int cloudType ,String groupName,String groupId){
String url = String.format(HttpUriContants.GET_QCLOUD_INSTANCES_IP_BY_SCALINGGROUPID+"?cloudType=%d&groupName=%s&groupId=%s",cloudType,groupName,groupId);
BaseResponse<?> response = httpClient.defaultPost(url, null, BaseResponse.class);
return response;
}
/**
* 系统发布的group下的所有项目
*/
@RequestMapping("/getDeployProjectNames")
@ResponseBody
public BaseResponse<?> getDeployProjectNames(String environment,String scalingGroupName )
{
BaseResponse t=new BaseResponse();
t.setData(AutoScalingGroupProjectConstant.getProjectNames(environment, scalingGroupName));
return t;
}
/**
* 系统发布
* @param session
* @param projectName 项目名称
* @param ips 所需发布的ip集合 ,分割
* @param branch 分支名称
* @param environment 发布环境 此为qcloud
* @return
*/
@RequestMapping("/deployCloudProject")
@ResponseBody
public BaseResponse<?> deployCloudProject(HttpSession session, String projectName, String ips, String branch, String environment){
BaseResponse response = null;
try{
User user = (User) session.getAttribute("user");
List<BuildMessage> list = new ArrayList<>();
BuildRequest buildRequest = new BuildRequest();
//分支名称
buildRequest.setBranch(branch);
//登陆用户
buildRequest.setUser(user.getName());
//设置需要发布的ip
buildRequest.setHost(ips);
//发布环境
buildRequest.setEnvironment(environment);
//不存在回滚文件
//操作类型
buildRequest.setOperate("Deploy");
//关联工单
buildRequest.setWorkid("release0");
//项目名称
buildRequest.setProject(AutoScalingGroupProjectConstant.getProjectNames(environment, projectName));
String messageids = "";
BuildMessage buildMessage = httpClient.post(ProjectEnvironment.getUrl(environment) + "build", buildRequest, BuildMessage.class);
if (buildMessage != null) {
list.add(buildMessage);
messageids = buildMessage.getId() + ",";
}
DeployProjectCloud deploy = new DeployProjectCloud();
deploy.setMessageids(messageids);
deploy.setMessageList(list);
response = new BaseResponse();
response.setData(deploy);
}catch (Exception e){
response.setCode(300);
response.setMessage(e.getMessage());
}
return response;
}
/**
* 获取执行结果
*
* @param messageid qcloud_1233 或者 aws_1234
* @return
*/
@RequestMapping(value = "/getBuildMsgAfterDeploy", method = RequestMethod.POST)
@ResponseBody
public BaseResponse getBuildMsgAfterDeploy(String messageid, String project) {
try {
BaseResponse response=new BaseResponse();
String cloudType=messageid.split("_")[0];
String[] projectArray= AutoScalingGroupProjectConstant.getProjectNames(cloudType,project).split(",");
StringBuilder rtnMsg=new StringBuilder();
Boolean allOK=true;
String desc="";
for(String pro:projectArray){
Map<String, String> map = new HashMap<>();
map.put("messageid", messageid);
map.put("project", pro);
//rtnMsg.append("project is "+pro+"<br>");
String buildInfoJsonString=httpClient.get(ProjectEnvironment.getUrl(messageid.split("_")[0]) + "getbuildmsg", String.class, map);
if(StringUtils.isNotBlank(buildInfoJsonString)){
JSONObject jo=JSON.parseObject(buildInfoJsonString);
Integer code=(Integer)jo.get("code");
if(code==2|| code == 3 || code == 4){
if(desc.length()>0){
desc+=";";
}
desc += (pro+"-->"+code);
}else{
allOK=false;
}
}
}
/* Map<String,Object> dat=new HashMap<>();
dat.put("allOK",allOK);
dat.put("desc",desc);*/
response.setMessage(desc);
response.setData(allOK);
return response;
} catch (Exception ex) {
return new BaseResponse();
}
}
/**
* 项目发布后集成测试
* @param cloudType 云类型 1:AWS 2:QCLOUD
* @param projectName 项目名称
* @param ips 部署在哪台服务器上,内网ip集合, ,分割
* @return
*/
@RequestMapping("/integrationTest")
@ResponseBody
public BaseResponse<?> integrationTest(int cloudType, String projectName, String ips){
String cloudTypeStr="";
if(1==cloudType){
cloudTypeStr="aws";
}else if(2==cloudType){
cloudTypeStr="qcloud";
}
String url = String.format(HttpUriContants.GET_CLOUD_INTEGRATION_TEST+"?cloudType=%s&projectName=%s&ips=%s",cloudType, AutoScalingGroupProjectConstant.getProjectNames(cloudTypeStr,projectName), ips);
BaseResponse<?> response = httpClient.defaultPost(url, null, BaseResponse.class);
return response;
}
/**
* 自动伸缩后,修改ip,ip从云管理中心重新获取
* http://git.yoho.cn/yohoops/auto_deploy.git
* @param cloudType 云类型 1:AWS 2:QCLOUD
* @param projectName 项目名称
* @param oldInstanceIps 伸缩之前的实例ip
* @param upDownFlag 伸缩标志 :up down
* @return
*/
@RequestMapping("/updateIpToGit")
@ResponseBody
public BaseResponse<?> updateIpToGit(int cloudType, String projectName, String scalingGroupId,String oldInstanceIps,String newInstanceIps,String upDownFlag){
String cloudTypeStr="";
if(1==cloudType){
cloudTypeStr="aws";
}else if(2==cloudType){
cloudTypeStr="qcloud";
}
//获取新的伸缩实例ip
/*BaseResponse newInstanceIpsResponse=this.getIpsByScalingGroupId(cloudType,projectName,scalingGroupId);
if(newInstanceIpsResponse==null||newInstanceIpsResponse.getCode()!=200){
newInstanceIpsResponse.setMessage("获取伸缩组新的实例ip失败!");
return newInstanceIpsResponse;
}*/
String projectNameArray[]= AutoScalingGroupProjectConstant.getProjectNames(cloudTypeStr,projectName).split(",");
StringBuilder sb=new StringBuilder();
for(String project:projectNameArray){
String url = String.format(HttpUriContants.GET_CLOUD_UPDATE_IP_TO_GIT+"?cloudType=%d&projectName=%s&scalingGroupId=%s",cloudType,project, scalingGroupId);
BaseResponse<?> responseTmp = httpClient.defaultPost(url, null, BaseResponse.class);
if(responseTmp==null||responseTmp.getCode()!=200){
responseTmp.setMessage(responseTmp.getMessage()+"**********project:"+project);
return responseTmp;
}else{
sb.append(responseTmp.getMessage()+"**********project:"+project);
}
}
if("gateway".equals(projectName)){
//List<String> newInstanceIps=(List<String>)newInstanceIpsResponse.getData();
List<String> newInstanceIpList=null;
if(StringUtils.isNotBlank(newInstanceIps)){
newInstanceIpList=Arrays.asList(newInstanceIps.split(","));
}else{
newInstanceIpList=new ArrayList<String>();
}
if("up".equals(upDownFlag)){
List<String> oldIps= Arrays.asList(oldInstanceIps.split(","));
for(String strIP:newInstanceIpList){
if(!oldIps.contains(strIP)&&StringUtils.isNotBlank(strIP)){
String url = String.format(HttpUriContants.GET_CLOUD_UPDATE_GATEWAY_CHANGE_UPSTREAM+"?operate=%s&cloudTypeStr=%s&ip=%s","add",cloudTypeStr, strIP);
BaseResponse<?> responseTmp = httpClient.defaultPost(url, null, BaseResponse.class);
sb.append("gateway upstream add "+strIP);
}
}
}else if("down".equals(upDownFlag)){
List<String> oldIps= Arrays.asList(oldInstanceIps.split(","));
for(String strIP:oldIps){
if(!newInstanceIpList.contains(strIP)){
String url = String.format(HttpUriContants.GET_CLOUD_UPDATE_GATEWAY_CHANGE_UPSTREAM+"?operate=%s&cloudTypeStr=%s&ip=%s","del",cloudTypeStr, strIP);
BaseResponse<?> responseTmp = httpClient.defaultPost(url, null, BaseResponse.class);
sb.append("gateway upstream del "+strIP);
}
}
}
}
BaseResponse<?> response=new BaseResponse();
response.setMessage(sb.toString());
return response;
}
}