Showing
4 changed files
with
514 additions
and
95 deletions
@@ -181,92 +181,4 @@ public class QcloudClientFactory { | @@ -181,92 +181,4 @@ public class QcloudClientFactory { | ||
181 | } | 181 | } |
182 | }); | 182 | }); |
183 | } | 183 | } |
184 | - | ||
185 | - | ||
186 | - | ||
187 | - | ||
188 | - public List<Host> getAllInstancesSet(){ | ||
189 | - List<Host> allHost=new ArrayList<Host>(); | ||
190 | - //List<QcloudInstanceSet> instanceSets_all = new ArrayList<QcloudInstanceSet>(); | ||
191 | - Set<String> instanceIDs=new HashSet<String>(); | ||
192 | - try{ | ||
193 | - // 第一步,先获取第一页的数据 | ||
194 | - int Limit=100; | ||
195 | - int pageNumber=0; | ||
196 | - | ||
197 | - TreeMap<String, Object> params = new TreeMap<>(); | ||
198 | - params.put("Version","2017-03-12"); | ||
199 | - params.put("Offset", pageNumber*Limit); | ||
200 | - params.put("Limit",Limit); | ||
201 | - logger.info("--------------getAllInstancesSet - start"); | ||
202 | - String rootJson = QcloudSdkUtil.getCommonData(new Cvm(), "GET", QcloudConstant.QCLOUD_API_INSTANCE_FUN, params, true); | ||
203 | - if(StringUtils.isBlank(rootJson)){ | ||
204 | - return null; | ||
205 | - } | ||
206 | - int total=0; | ||
207 | - JSONObject rootObj = JSON.parseObject(rootJson); | ||
208 | - if(rootObj!=null&&rootObj.get("Response")!=null){ | ||
209 | - JSONObject response_jo=rootObj.getJSONObject("Response"); | ||
210 | - total=response_jo.getInteger("TotalCount"); | ||
211 | - JSONArray insts_ja=response_jo.getJSONArray("InstanceSet"); | ||
212 | - for(int i=0;i<insts_ja.size();i++){ | ||
213 | - JSONObject jo = insts_ja.getJSONObject(i); | ||
214 | - String hostid=jo.getString("InstanceId"); | ||
215 | - String hostname=jo.getString("InstanceName"); | ||
216 | - String ip=jo.getJSONArray("PrivateIpAddresses").size()>0?jo.getJSONArray("PrivateIpAddresses").getString(0):""; | ||
217 | - String restrictState=jo.getString("RestrictState"); ///状态,不要EXPIRED状态的主机,NORMAL表示正常状态的实例 ,EXPIRED:表示过期的实例 ,PROTECTIVELY_ISOLATED:表示被安全隔离的实例。 | ||
218 | - if("EXPIRED".equalsIgnoreCase(restrictState)){ | ||
219 | - continue; | ||
220 | - } | ||
221 | - if(!instanceIDs.contains(hostid)){ | ||
222 | - instanceIDs.add(hostid); | ||
223 | - Host host=new Host(DataCenter.qcloud,hostid, ip, hostname); | ||
224 | - allHost.add(host); | ||
225 | - } | ||
226 | - } | ||
227 | - } | ||
228 | - | ||
229 | - //第二步,分页获取 | ||
230 | - if(total>Limit){ | ||
231 | - pageNumber=1; | ||
232 | - int pageCount=(int)Math.ceil(total/(double)Limit) ; | ||
233 | - for(;pageNumber<pageCount;pageNumber++){ | ||
234 | - | ||
235 | - params.put("Offset", pageNumber*Limit); | ||
236 | - | ||
237 | - rootJson = QcloudSdkUtil.getCommonData(new Cvm(), "GET", QcloudConstant.QCLOUD_API_INSTANCE_FUN, params, true); | ||
238 | - if(StringUtils.isBlank(rootJson)){ | ||
239 | - continue; | ||
240 | - } | ||
241 | - rootObj = JSON.parseObject(rootJson); | ||
242 | - if(rootObj!=null&&rootObj.get("Response")!=null){ | ||
243 | - JSONObject response_jo=rootObj.getJSONObject("Response"); | ||
244 | - JSONArray insts_ja=response_jo.getJSONArray("InstanceSet"); | ||
245 | - for(int i=0;i<insts_ja.size();i++){ | ||
246 | - JSONObject jo = insts_ja.getJSONObject(i); | ||
247 | - String hostid=jo.getString("InstanceId"); | ||
248 | - String hostname=jo.getString("InstanceName"); | ||
249 | - String ip=jo.getJSONArray("PrivateIpAddresses").size()>0?jo.getJSONArray("PrivateIpAddresses").getString(0):""; | ||
250 | - String restrictState=jo.getString("RestrictState"); ///状态,不要EXPIRED状态的主机,NORMAL表示正常状态的实例 ,EXPIRED:表示过期的实例 ,PROTECTIVELY_ISOLATED:表示被安全隔离的实例。 | ||
251 | - if("EXPIRED".equalsIgnoreCase(restrictState)){ | ||
252 | - continue; | ||
253 | - } | ||
254 | - if(!instanceIDs.contains(hostid)){ | ||
255 | - instanceIDs.add(hostid); | ||
256 | - Host host=new Host(DataCenter.qcloud,hostid, ip, hostname); | ||
257 | - allHost.add(host); | ||
258 | - } | ||
259 | - } | ||
260 | - } | ||
261 | - } | ||
262 | - } | ||
263 | - }catch (Exception e){ | ||
264 | - logger.error(" - QcloudToolServiceImpl - getScalingInstancesSet- error", e); | ||
265 | - } | ||
266 | - | ||
267 | - return allHost; | ||
268 | - } | ||
269 | - | ||
270 | - | ||
271 | - | ||
272 | } | 184 | } |
@@ -5,6 +5,7 @@ import com.yoho.ops.cmdb.models.AutoScalingGroup; | @@ -5,6 +5,7 @@ import com.yoho.ops.cmdb.models.AutoScalingGroup; | ||
5 | import com.yoho.ops.cmdb.models.Host; | 5 | import com.yoho.ops.cmdb.models.Host; |
6 | import com.yoho.ops.cmdb.models.QcloudModifyScalingGroupReq; | 6 | import com.yoho.ops.cmdb.models.QcloudModifyScalingGroupReq; |
7 | import com.yoho.ops.cmdb.qcloud.QcloudClientFactory; | 7 | import com.yoho.ops.cmdb.qcloud.QcloudClientFactory; |
8 | +import com.yoho.ops.cmdb.qcloud.util.QcloudSdkUtil; | ||
8 | import org.springframework.beans.factory.annotation.Autowired; | 9 | import org.springframework.beans.factory.annotation.Autowired; |
9 | import org.springframework.stereotype.Service; | 10 | import org.springframework.stereotype.Service; |
10 | 11 | ||
@@ -29,7 +30,7 @@ public class QcloudAutoScalingFetcher { | @@ -29,7 +30,7 @@ public class QcloudAutoScalingFetcher { | ||
29 | } | 30 | } |
30 | 31 | ||
31 | public List<Host> getAllInstancesSet(){ | 32 | public List<Host> getAllInstancesSet(){ |
32 | - return qcloudClientFactory.getAllInstancesSet(); | 33 | + return QcloudSdkUtil.getAllInstancesSet(); |
33 | } | 34 | } |
34 | 35 | ||
35 | public BaseResponse modifyScalingGroup(QcloudModifyScalingGroupReq req){ | 36 | public BaseResponse modifyScalingGroup(QcloudModifyScalingGroupReq req){ |
1 | package com.yoho.ops.cmdb.qcloud.util; | 1 | package com.yoho.ops.cmdb.qcloud.util; |
2 | 2 | ||
3 | import com.alibaba.fastjson.JSON; | 3 | import com.alibaba.fastjson.JSON; |
4 | +import com.alibaba.fastjson.JSONArray; | ||
4 | import com.alibaba.fastjson.JSONObject; | 5 | import com.alibaba.fastjson.JSONObject; |
5 | import com.qcloud.Module.*; | 6 | import com.qcloud.Module.*; |
6 | import com.qcloud.QcloudApiModuleCenter; | 7 | import com.qcloud.QcloudApiModuleCenter; |
8 | +import com.yoho.ops.cmdb.models.DataCenter; | ||
9 | +import com.yoho.ops.cmdb.models.Host; | ||
7 | import com.yoho.ops.cmdb.qcloud.constant.QcloudConstant; | 10 | import com.yoho.ops.cmdb.qcloud.constant.QcloudConstant; |
8 | import com.yoho.ops.cmdb.qcloud.model.QcloudAutoScalingGroup; | 11 | import com.yoho.ops.cmdb.qcloud.model.QcloudAutoScalingGroup; |
9 | import com.yoho.ops.cmdb.qcloud.model.QcloudCLBModel; | 12 | import com.yoho.ops.cmdb.qcloud.model.QcloudCLBModel; |
@@ -17,10 +20,7 @@ import org.slf4j.LoggerFactory; | @@ -17,10 +20,7 @@ import org.slf4j.LoggerFactory; | ||
17 | import org.springframework.util.CollectionUtils; | 20 | import org.springframework.util.CollectionUtils; |
18 | 21 | ||
19 | import java.text.SimpleDateFormat; | 22 | import java.text.SimpleDateFormat; |
20 | -import java.util.ArrayList; | ||
21 | -import java.util.Calendar; | ||
22 | -import java.util.List; | ||
23 | -import java.util.TreeMap; | 23 | +import java.util.*; |
24 | 24 | ||
25 | /** | 25 | /** |
26 | * Created by craig.qin on 2017/5/8. | 26 | * Created by craig.qin on 2017/5/8. |
@@ -284,6 +284,35 @@ public class QcloudSdkUtil { | @@ -284,6 +284,35 @@ public class QcloudSdkUtil { | ||
284 | } | 284 | } |
285 | 285 | ||
286 | /** | 286 | /** |
287 | + * 根据过滤条件(内网ip),获取主机信息 | ||
288 | + * 过滤条件Filters.1.Name = private-ip-address | ||
289 | + * 过滤值 Filters.1.Values.1=10.xx.xx.xx & Filters.2.Values.1=10.xx.xx.xx | ||
290 | + * 索引从1开始 | ||
291 | + * ;Filter.Values的上限为5。参数不支持同时指定InstanceIds和Filters | ||
292 | + */ | ||
293 | + public static List<Host> getInstancesByPrivateIpAddress(List<String> privateIps){ | ||
294 | + //privateIps 最多5个 | ||
295 | + List<Host> instanceList = new ArrayList<>(); | ||
296 | + int count=0; | ||
297 | + List<String> ipTmp=new ArrayList<String>(); | ||
298 | + for(String tmpID:privateIps){ | ||
299 | + if(count<5){ | ||
300 | + ipTmp.add(tmpID); | ||
301 | + count++; | ||
302 | + }else{ | ||
303 | + instanceList.addAll(describeInstancesByFilter("private-ip-address",ipTmp)); | ||
304 | + ipTmp= new ArrayList<String>(); | ||
305 | + ipTmp.add(tmpID); | ||
306 | + count=1; | ||
307 | + } | ||
308 | + } | ||
309 | + if(ipTmp!=null&&ipTmp.size()>0){ | ||
310 | + instanceList.addAll(describeInstancesByFilter("private-ip-address",ipTmp)); | ||
311 | + } | ||
312 | + return instanceList; | ||
313 | + } | ||
314 | + | ||
315 | + /** | ||
287 | * 接口:DescribeInstances | 316 | * 接口:DescribeInstances |
288 | * @param scalingInstanceIds 实例ID集合 | 317 | * @param scalingInstanceIds 实例ID集合 |
289 | * @return | 318 | * @return |
@@ -313,6 +342,124 @@ public class QcloudSdkUtil { | @@ -313,6 +342,124 @@ public class QcloudSdkUtil { | ||
313 | return instanceSets; | 342 | return instanceSets; |
314 | } | 343 | } |
315 | 344 | ||
345 | + /** | ||
346 | + * 接口:DescribeInstances | ||
347 | + * 根据过滤条件查询实例 | ||
348 | + * @return | ||
349 | + */ | ||
350 | + private static List<Host> describeInstancesByFilter(String filterName,List<String> filterValuesList){ | ||
351 | + if(StringUtils.isBlank(filterName)||CollectionUtils.isEmpty(filterValuesList)){ | ||
352 | + return null; | ||
353 | + } | ||
354 | + List<Host> allHost=new ArrayList<>(); | ||
355 | + Set<String> instanceIDs=new HashSet<>(); | ||
356 | + try{ | ||
357 | + TreeMap<String, Object> params = new TreeMap<>(); | ||
358 | + params.put("Version","2017-03-12"); | ||
359 | + | ||
360 | + params.put("Filters.1.Name",filterName); | ||
361 | + for (int index = 0; index < filterValuesList.size(); index++) { | ||
362 | + params.put("Filters.1.Values."+(1+index), filterValuesList.get(index)); | ||
363 | + } | ||
364 | + String rootJson = QcloudSdkUtil.getCommonData(new Cvm(), "GET", QcloudConstant.QCLOUD_API_INSTANCE_FUN, params, true); | ||
365 | + if(StringUtils.isBlank(rootJson)){ | ||
366 | + return null; | ||
367 | + } | ||
368 | + | ||
369 | + JSONObject rootObj = JSON.parseObject(rootJson); | ||
370 | + if(rootObj!=null&&rootObj.get("Response")!=null){ | ||
371 | + JSONObject response_jo=rootObj.getJSONObject("Response"); | ||
372 | + JSONArray insts_ja=response_jo.getJSONArray("InstanceSet"); | ||
373 | + for(int i=0;i<insts_ja.size();i++){ | ||
374 | + buildHost( instanceIDs, allHost, insts_ja.getJSONObject(i)); | ||
375 | + } | ||
376 | + } | ||
377 | + }catch (Exception e){ | ||
378 | + logger.error(" - QcloudSdkUtil - describeInstances- error", e); | ||
379 | + } | ||
380 | + | ||
381 | + return allHost; | ||
382 | + } | ||
383 | + | ||
384 | + | ||
385 | + /** | ||
386 | + * 获取所有的主机 | ||
387 | + * @return | ||
388 | + */ | ||
389 | + public static List<Host> getAllInstancesSet(){ | ||
390 | + List<Host> allHost=new ArrayList<Host>(); | ||
391 | + Set<String> instanceIDs=new HashSet<String>(); | ||
392 | + try{ | ||
393 | + // 第一步,先获取第一页的数据 | ||
394 | + int Limit=100; | ||
395 | + int pageNumber=0; | ||
396 | + | ||
397 | + TreeMap<String, Object> params = new TreeMap<>(); | ||
398 | + params.put("Version","2017-03-12"); | ||
399 | + params.put("Offset", pageNumber*Limit); | ||
400 | + params.put("Limit",Limit); | ||
401 | + | ||
402 | + String rootJson = QcloudSdkUtil.getCommonData(new Cvm(), "GET", QcloudConstant.QCLOUD_API_INSTANCE_FUN, params, true); | ||
403 | + if(StringUtils.isBlank(rootJson)){ | ||
404 | + return null; | ||
405 | + } | ||
406 | + int total=0; | ||
407 | + JSONObject rootObj = JSON.parseObject(rootJson); | ||
408 | + if(rootObj!=null&&rootObj.get("Response")!=null){ | ||
409 | + JSONObject response_jo=rootObj.getJSONObject("Response"); | ||
410 | + total=response_jo.getInteger("TotalCount"); | ||
411 | + JSONArray insts_ja=response_jo.getJSONArray("InstanceSet"); | ||
412 | + for(int i=0;i<insts_ja.size();i++){ | ||
413 | + JSONObject jo = insts_ja.getJSONObject(i); | ||
414 | + buildHost( instanceIDs, allHost, jo); | ||
415 | + } | ||
416 | + } | ||
417 | + | ||
418 | + //第二步,分页获取 | ||
419 | + if(total>Limit){ | ||
420 | + pageNumber=1; | ||
421 | + int pageCount=(int)Math.ceil(total/(double)Limit) ; | ||
422 | + for(;pageNumber<pageCount;pageNumber++){ | ||
423 | + | ||
424 | + params.put("Offset", pageNumber*Limit); | ||
425 | + | ||
426 | + rootJson = QcloudSdkUtil.getCommonData(new Cvm(), "GET", QcloudConstant.QCLOUD_API_INSTANCE_FUN, params, true); | ||
427 | + if(StringUtils.isBlank(rootJson)){ | ||
428 | + continue; | ||
429 | + } | ||
430 | + rootObj = JSON.parseObject(rootJson); | ||
431 | + if(rootObj!=null&&rootObj.get("Response")!=null){ | ||
432 | + JSONObject response_jo=rootObj.getJSONObject("Response"); | ||
433 | + JSONArray insts_ja=response_jo.getJSONArray("InstanceSet"); | ||
434 | + for(int i=0;i<insts_ja.size();i++){ | ||
435 | + JSONObject jo = insts_ja.getJSONObject(i); | ||
436 | + buildHost( instanceIDs, allHost, jo); | ||
437 | + } | ||
438 | + } | ||
439 | + } | ||
440 | + } | ||
441 | + }catch (Exception e){ | ||
442 | + logger.error(" - QcloudToolServiceImpl - getScalingInstancesSet- error", e); | ||
443 | + } | ||
444 | + | ||
445 | + return allHost; | ||
446 | + } | ||
447 | + | ||
448 | + private static void buildHost(Set<String> instanceIDs,List<Host> allHost,JSONObject jo){ | ||
449 | + String hostid=jo.getString("InstanceId"); | ||
450 | + String hostname=jo.getString("InstanceName"); | ||
451 | + String ip=jo.getJSONArray("PrivateIpAddresses").size()>0?jo.getJSONArray("PrivateIpAddresses").getString(0):""; | ||
452 | + String restrictState=jo.getString("RestrictState"); ///状态,不要EXPIRED状态的主机,NORMAL表示正常状态的实例 ,EXPIRED:表示过期的实例 ,PROTECTIVELY_ISOLATED:表示被安全隔离的实例。 | ||
453 | + if(!"EXPIRED".equalsIgnoreCase(restrictState)){ | ||
454 | + if(!instanceIDs.contains(hostid)){ | ||
455 | + instanceIDs.add(hostid); | ||
456 | + Host host=new Host(DataCenter.qcloud,hostid, ip, hostname); | ||
457 | + allHost.add(host); | ||
458 | + } | ||
459 | + } | ||
460 | + | ||
461 | + } | ||
462 | + | ||
316 | /******************************************************************* | 463 | /******************************************************************* |
317 | *************************** 2 负载均衡器 (传统) ************************ | 464 | *************************** 2 负载均衡器 (传统) ************************ |
318 | ******************************************************************/ | 465 | ******************************************************************/ |
@@ -417,13 +564,19 @@ public class QcloudSdkUtil { | @@ -417,13 +564,19 @@ public class QcloudSdkUtil { | ||
417 | } | 564 | } |
418 | 565 | ||
419 | public static void main(String args[]){ | 566 | public static void main(String args[]){ |
420 | - Calendar calendar = Calendar.getInstance(); | 567 | + /*Calendar calendar = Calendar.getInstance(); |
421 | calendar.add(Calendar.MINUTE, -60); | 568 | calendar.add(Calendar.MINUTE, -60); |
422 | String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()); | 569 | String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()); |
423 | List<Double> dd=getMonitorData("outtraffic",startTime); | 570 | List<Double> dd=getMonitorData("outtraffic",startTime); |
424 | System.out.println(dd); | 571 | System.out.println(dd); |
425 | System.out.println(""); | 572 | System.out.println(""); |
426 | List<Double> dd2=getMonitorData("intraffic",startTime); | 573 | List<Double> dd2=getMonitorData("intraffic",startTime); |
427 | - System.out.println(dd2); | 574 | + System.out.println(dd2);*/ |
575 | + | ||
576 | + String[] ips={"10.66.1.105","10.66.1.127","10.66.103.13","10.66.103.14","10.66.70.234","10.66.70.174"}; | ||
577 | + List<Host> ll=getInstancesByPrivateIpAddress(Arrays.asList(ips)); | ||
578 | + | ||
579 | + System.out.println(ll); | ||
580 | + | ||
428 | } | 581 | } |
429 | } | 582 | } |
1 | +package com.monitor.switchs; | ||
2 | + | ||
3 | +import com.model.HostInfo; | ||
4 | +import com.monitor.cmdb.service.IHostInfoService; | ||
5 | +import com.monitor.model.response.BaseResponse; | ||
6 | +import com.yoho.ops.cmdb.models.Host; | ||
7 | +import com.yoho.ops.cmdb.models.LoadBalance; | ||
8 | +import com.yoho.ops.cmdb.qcloud.lb.QcloudLoadBalance; | ||
9 | +import com.yoho.ops.cmdb.qcloud.util.QcloudSdkUtil; | ||
10 | +import org.springframework.beans.factory.annotation.Autowired; | ||
11 | +import org.springframework.stereotype.Controller; | ||
12 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
13 | +import org.springframework.web.bind.annotation.ResponseBody; | ||
14 | + | ||
15 | +import java.util.*; | ||
16 | +import java.util.stream.Collectors; | ||
17 | + | ||
18 | +/** | ||
19 | + * @author craig.qin | ||
20 | + * pch5切换 | ||
21 | + */ | ||
22 | +@Controller | ||
23 | +@RequestMapping("/pch5Switch") | ||
24 | +public class Pch5SwitchCtrl { | ||
25 | + | ||
26 | + //Pch5的负载均衡器,传统型,生产 | ||
27 | + private final String ELB_PCH5_ID="lb-ceg44td5"; | ||
28 | + //Pch5的负载均衡器,传统型,灰度 | ||
29 | + private final String ELB_PCH5_GRAY_AZ2_ID="lb-kaucjq25"; | ||
30 | + | ||
31 | + //云主机信息 | ||
32 | + private final String const_host_cvm_map="host_cvm_map"; | ||
33 | + //当前生产elb下面挂载的机器 | ||
34 | + private final String const_current_host_under_prod_elb="const_current_host_under_prod_elb"; | ||
35 | + //所有az1区的机器是否都挂载到elb | ||
36 | + private final String const_az1_host_under_prod_elb="az1_host_under_prod_elb"; | ||
37 | + private final String const_az2_host_under_prod_elb="az2_host_under_prod_elb"; | ||
38 | + | ||
39 | + private final List<String> const_tags_for_nginx_az1=new ArrayList<String>(){ | ||
40 | + { | ||
41 | + add("pch5"); | ||
42 | + add("nginx"); | ||
43 | + add("az1"); | ||
44 | + } | ||
45 | + }; | ||
46 | + | ||
47 | + private final List<String> const_tags_for_nginx_az2=new ArrayList<String>(){ | ||
48 | + { | ||
49 | + add("pch5"); | ||
50 | + add("nginx"); | ||
51 | + add("az2"); | ||
52 | + } | ||
53 | + }; | ||
54 | + | ||
55 | + private final List<String> const_tags_for_nginx_node_az2=new ArrayList<String>(){ | ||
56 | + { | ||
57 | + add("pch5"); | ||
58 | + add("node"); | ||
59 | + add("az2"); | ||
60 | + } | ||
61 | + }; | ||
62 | + @Autowired | ||
63 | + private QcloudLoadBalance qcloudLoadBalance; | ||
64 | + | ||
65 | + @Autowired | ||
66 | + private IHostInfoService hostInfoService; | ||
67 | + | ||
68 | + @RequestMapping(value = "/getPch5NodeHostUnderAz2") | ||
69 | + @ResponseBody | ||
70 | + public List<String> getPch5NodeHostIPs() { | ||
71 | + return getHostByType(const_tags_for_nginx_node_az2); | ||
72 | + } | ||
73 | + | ||
74 | + @RequestMapping(value = "/getElbStatus") | ||
75 | + @ResponseBody | ||
76 | + public BaseResponse getElbStatus() { | ||
77 | + BaseResponse response = new BaseResponse(); | ||
78 | + //az1 ,az2机器 | ||
79 | + List<String> az1_host_cmdb=getHostByType(const_tags_for_nginx_az1); | ||
80 | + List<String> az2_host_cmdb=getHostByType(const_tags_for_nginx_az2); | ||
81 | + BaseResponse checkAzResponse=checkAzCmdb(az1_host_cmdb,az2_host_cmdb); | ||
82 | + if(checkAzResponse==null||checkAzResponse.getCode()!=200){ | ||
83 | + return checkAzResponse; | ||
84 | + } | ||
85 | + | ||
86 | + Map<String,Object> rntData=(Map<String, Object>) checkAzResponse.getData(); | ||
87 | + boolean inAz1=(boolean)(rntData.get(const_az1_host_under_prod_elb)); | ||
88 | + boolean inAz2=(boolean)(rntData.get(const_az2_host_under_prod_elb)); | ||
89 | + | ||
90 | + | ||
91 | + Map<String,Object> ipsUnderElbMap=new LinkedHashMap<>(); | ||
92 | + | ||
93 | + ipsUnderElbMap.put("az1Ips",az1_host_cmdb); | ||
94 | + ipsUnderElbMap.put("az2Ips",az2_host_cmdb); | ||
95 | + ipsUnderElbMap.put("inAz1",inAz1); | ||
96 | + ipsUnderElbMap.put("inAz2",inAz2); | ||
97 | + response.setData(ipsUnderElbMap); | ||
98 | + return response; | ||
99 | + } | ||
100 | + | ||
101 | + /** | ||
102 | + * 检查 | ||
103 | + * @param az1_host_cmdb | ||
104 | + * @param az2_host_cmdb | ||
105 | + * @return | ||
106 | + */ | ||
107 | + private BaseResponse checkAzCmdb(List<String> az1_host_cmdb ,List<String> az2_host_cmdb){ | ||
108 | + /****************************************************************************/ | ||
109 | + /**************** cmdb 标签配置检查 *********************/ | ||
110 | + /****************************************************************************/ | ||
111 | + //az1和az2不能重复 | ||
112 | + if(az1_host_cmdb==null||az1_host_cmdb.size()<0){ | ||
113 | + return new BaseResponse(201,"获取标签[pch5 nginx az1]的cmdb信息为空"); | ||
114 | + } | ||
115 | + if(az2_host_cmdb==null||az2_host_cmdb.size()<0){ | ||
116 | + return new BaseResponse(201,"获取标签[pch5 nginx az2]的cmdb信息为空"); | ||
117 | + } | ||
118 | + for(String tmp:az1_host_cmdb){ | ||
119 | + if(az2_host_cmdb.contains(tmp)){ | ||
120 | + return new BaseResponse(201,"获取标签[pch5 nginx az1]的cmdb"+tmp+"与az2重复"); | ||
121 | + } | ||
122 | + } | ||
123 | + for(String tmp:az2_host_cmdb){ | ||
124 | + if(az1_host_cmdb.contains(tmp)){ | ||
125 | + return new BaseResponse(201,"获取标签[pch5 nginx az1]的cmdb"+tmp+"与az2重复"); | ||
126 | + } | ||
127 | + } | ||
128 | + | ||
129 | + /****************************************************************************/ | ||
130 | + /**************** 云主机检查及云主机id获取 *********************/ | ||
131 | + /****************************************************************************/ | ||
132 | + List<String> all_host_cmdb=new ArrayList<>(); | ||
133 | + all_host_cmdb.addAll(az1_host_cmdb); | ||
134 | + all_host_cmdb.addAll(az2_host_cmdb); | ||
135 | + | ||
136 | + List<Host> hostList= QcloudSdkUtil.getInstancesByPrivateIpAddress(all_host_cmdb); | ||
137 | + if(hostList==null||hostList.size()<=0||hostList.size()!=all_host_cmdb.size()){ | ||
138 | + return new BaseResponse(201,"获取pch5 az1 az2 标签相关云主机列表为空或者云主机与标签主机数量不一致"); | ||
139 | + } | ||
140 | + Map<String,Host> hostMap=hostList.stream().collect(Collectors.toMap(Host::getIp, host -> host)); | ||
141 | + | ||
142 | + /****************************************************************************/ | ||
143 | + /**************** 灰度elb配置的主机检查 *************/ | ||
144 | + /****************************************************************************/ | ||
145 | + LoadBalance grayPch5Elb=qcloudLoadBalance.getAlbLoadBalancesById(ELB_PCH5_GRAY_AZ2_ID); | ||
146 | + if(grayPch5Elb==null||grayPch5Elb.getHosts().size()<=0){ | ||
147 | + return new BaseResponse(201,"获取grayElb异常或者挂载的主机列表为空"); | ||
148 | + } | ||
149 | + List<String> grayPch5Ips=grayPch5Elb.getHosts().stream().map(Host::getIp).collect(Collectors.toList()); | ||
150 | + //检查灰度机器的az2标签 cmdb信息必须与线上grayElb挂载的nginx一致 | ||
151 | + for(String tmp:grayPch5Ips){ | ||
152 | + if(!az2_host_cmdb.contains(tmp)){ | ||
153 | + return new BaseResponse(201,"获取grayElb["+tmp+"]未配置标签"); | ||
154 | + } | ||
155 | + } | ||
156 | + for(String hostIp:az2_host_cmdb){ | ||
157 | + if(!grayPch5Ips.contains(hostIp)){ | ||
158 | + return new BaseResponse(201,"获取pch5 nginx az2标签,存在不一致机器["+hostIp+"]"); | ||
159 | + } | ||
160 | + } | ||
161 | + | ||
162 | + /****************************************************************************/ | ||
163 | + /**************** 生产elb配置的主机检查 *************/ | ||
164 | + /****************************************************************************/ | ||
165 | + //生产环境 | ||
166 | + LoadBalance prodPch5Elb=qcloudLoadBalance.getAlbLoadBalancesById(ELB_PCH5_ID); | ||
167 | + if(prodPch5Elb==null||prodPch5Elb.getHosts().size()<=0){ | ||
168 | + return new BaseResponse(201,"获取prodElb异常或者挂载的主机列表为空"); | ||
169 | + } | ||
170 | + // prodElb挂载的nginx,要么是az1_host_cmdb,要么az2_host_cmdb,不能存在其他情况 | ||
171 | + List<String> az1Ips=new ArrayList<>(); | ||
172 | + List<String> az2Ips=new ArrayList<>();//挂载到prod下的az2机器 | ||
173 | + for(Host host:prodPch5Elb.getHosts()){ | ||
174 | + if(!az1_host_cmdb.contains(host.getIp()) && !az2_host_cmdb.contains(host.getIp())){ | ||
175 | + return new BaseResponse(201,"获取prodElb["+host.getIp()+"]在cmdb未配置正确标签"); | ||
176 | + }else{ | ||
177 | + if(az1_host_cmdb.contains(host.getIp())){ | ||
178 | + az1Ips.add(host.getIp()); | ||
179 | + }else if(az2_host_cmdb.contains(host.getIp())){ | ||
180 | + az2Ips.add(host.getIp()); | ||
181 | + } | ||
182 | + } | ||
183 | + } | ||
184 | + | ||
185 | + //不允许az部分机器挂载的elb ,要么全部挂载,要么不挂载 | ||
186 | + boolean inAz1=false; | ||
187 | + if(az1Ips.size()>0){ | ||
188 | + if(az1Ips.size()!=az1_host_cmdb.size()){ | ||
189 | + return new BaseResponse(201,"prodElb的az1 nginx挂载机器不等于cmdb配置"); | ||
190 | + }else{ | ||
191 | + inAz1=true; | ||
192 | + } | ||
193 | + } | ||
194 | + | ||
195 | + boolean inAz2=false; | ||
196 | + if(az2Ips.size()>0){ | ||
197 | + if(az2Ips.size()!=az2_host_cmdb.size()){ | ||
198 | + return new BaseResponse(201,"prodElb的az2 nginx挂载机器不等于cmdb配置"); | ||
199 | + }else{ | ||
200 | + inAz2=true; | ||
201 | + } | ||
202 | + } | ||
203 | + | ||
204 | + BaseResponse baseResponse=new BaseResponse(); | ||
205 | + Map<String,Object> map=new HashMap<>(); | ||
206 | + map.put(const_host_cvm_map,hostMap);//云主机列表 | ||
207 | + map.put(const_current_host_under_prod_elb,prodPch5Elb.getHosts()); | ||
208 | + map.put(const_az1_host_under_prod_elb,inAz1); | ||
209 | + map.put(const_az2_host_under_prod_elb,inAz2); | ||
210 | + baseResponse.setData(map); | ||
211 | + return baseResponse; | ||
212 | + } | ||
213 | + | ||
214 | + /** | ||
215 | + * 从标签获取机器信息 | ||
216 | + * @return | ||
217 | + */ | ||
218 | + private List<String> getHostByType(List<String> tagList){ | ||
219 | + | ||
220 | + List<HostInfo> hostInfos = hostInfoService.getHostInfosByTagList(tagList); | ||
221 | + List<String> ips=new ArrayList<>(); | ||
222 | + if(hostInfos!=null&&hostInfos.size()>0){ | ||
223 | + for(HostInfo hostInfo:hostInfos){ | ||
224 | + ips.add(hostInfo.getHostIp()); | ||
225 | + } | ||
226 | + } | ||
227 | + return ips; | ||
228 | + } | ||
229 | + | ||
230 | + | ||
231 | + /** | ||
232 | + * 可用区切换 | ||
233 | + * | ||
234 | + */ | ||
235 | + @RequestMapping(value = "/switchArea") | ||
236 | + @ResponseBody | ||
237 | + public BaseResponse switchArea(String area) { | ||
238 | + | ||
239 | + //az1 ,az2机器,及对应instanceId | ||
240 | + List<String> az1_host_cmdb=getHostByType(const_tags_for_nginx_az1); | ||
241 | + List<String> az2_host_cmdb=getHostByType(const_tags_for_nginx_az2); | ||
242 | + BaseResponse checkAzResponse=checkAzCmdb(az1_host_cmdb,az2_host_cmdb); | ||
243 | + if(checkAzResponse==null||checkAzResponse.getCode()!=200){ | ||
244 | + return checkAzResponse; | ||
245 | + } | ||
246 | + | ||
247 | + Map<String,Object> rntData=(Map<String, Object>) checkAzResponse.getData(); | ||
248 | + Map<String,Host> hostMap=(Map<String,Host>)(rntData.get(const_host_cvm_map)); | ||
249 | + | ||
250 | + //生产环境的当前主机 | ||
251 | + /*List<Host> currentHostUnderProdElb=(List<Host>)rntData.get(const_current_host_under_prod_elb) ; | ||
252 | + Set<String> allHostIps_under_prod=new TreeSet<>(); | ||
253 | + for(Host host:currentHostUnderProdElb){ | ||
254 | + allHostIps_under_prod.add(host.getId()); | ||
255 | + }*/ | ||
256 | + | ||
257 | + if("all".equals(area)){ | ||
258 | + //指向all,挂载 | ||
259 | + List<String> addIds=new ArrayList<>(); | ||
260 | + for(String ip:hostMap.keySet()){ | ||
261 | + //if(!allHostIps_under_prod.contains(ip)){ | ||
262 | + addIds.add(hostMap.get(ip).getId()); | ||
263 | + //} | ||
264 | + } | ||
265 | + qcloudLoadBalance.registerInstancesWithLoadBalancer(ELB_PCH5_ID,addIds); | ||
266 | + } else if("az1".equals(area)){ | ||
267 | + //指向az1 ,把az2卸载 | ||
268 | + //首先要确保az1,至少有一个host,才能卸载az2机器 | ||
269 | + pch5ElbChangeHost(az1_host_cmdb,az2_host_cmdb,hostMap); | ||
270 | + } else if("az2".equals(area)){ | ||
271 | + //指向az2,把az1卸载 | ||
272 | + //先把az2挂载上 | ||
273 | + //卸载az1 | ||
274 | + pch5ElbChangeHost(az2_host_cmdb,az1_host_cmdb,hostMap); | ||
275 | + } | ||
276 | + return new BaseResponse(); | ||
277 | + } | ||
278 | + | ||
279 | + /** | ||
280 | + * 从elb卸载机器,一定要注意至少保留一个 | ||
281 | + * @param addOrKeepIps | ||
282 | + * @param removeIps | ||
283 | + * @param hostMap | ||
284 | + */ | ||
285 | + private void pch5ElbChangeHost(List<String> addOrKeepIps,List<String> removeIps,Map<String,Host> hostMap){ | ||
286 | + //先把az1挂载上 | ||
287 | + List<String> addIds=new ArrayList<>(); | ||
288 | + for(String ip:addOrKeepIps){ | ||
289 | + addIds.add(hostMap.get(ip).getId()); | ||
290 | + } | ||
291 | + qcloudLoadBalance.registerInstancesWithLoadBalancer(ELB_PCH5_ID,addIds); | ||
292 | + | ||
293 | + //再卸载az2 | ||
294 | + List<String> removeIds=new ArrayList<>(); | ||
295 | + for(String ip:removeIps){ | ||
296 | + removeIds.add(hostMap.get(ip).getId()); | ||
297 | + } | ||
298 | + qcloudLoadBalance.deregisterInstancesWithLoadBalancer(ELB_PCH5_ID,removeIds); | ||
299 | + } | ||
300 | + | ||
301 | + /** | ||
302 | + * az2切换 | ||
303 | + * 获取productElb下面的az2类型host | ||
304 | + * 如果是切向gray: 即从productElb卸载 ,修改host文件 | ||
305 | + * 如果是切向online: 即从productElb挂载 ,修改host文件 | ||
306 | + */ | ||
307 | + @RequestMapping(value = "/switchAz2") | ||
308 | + @ResponseBody | ||
309 | + public BaseResponse switchAz2(String onlineOrGray) { | ||
310 | + if(!"online".equals(onlineOrGray)&&!"gray".equals(onlineOrGray)){ | ||
311 | + return new BaseResponse(201,"参数错误"); | ||
312 | + } | ||
313 | + | ||
314 | + //az1 ,az2机器,及对应instanceId | ||
315 | + List<String> az1_host_cmdb=getHostByType(const_tags_for_nginx_az1); | ||
316 | + List<String> az2_host_cmdb=getHostByType(const_tags_for_nginx_az2); | ||
317 | + BaseResponse checkAzResponse=checkAzCmdb(az1_host_cmdb,az2_host_cmdb); | ||
318 | + if(checkAzResponse==null||checkAzResponse.getCode()!=200){ | ||
319 | + return checkAzResponse; | ||
320 | + } | ||
321 | + | ||
322 | + Map<String,Object> rntData=(Map<String, Object>) checkAzResponse.getData(); | ||
323 | + Map<String,Host> hostMap=(Map<String,Host>)(rntData.get(const_host_cvm_map)); | ||
324 | + List<Host> current_host_prodElb=(List<Host>)rntData.get(const_current_host_under_prod_elb); | ||
325 | + List<String> az2HostIds=new ArrayList<>(); | ||
326 | + for(String ip:az2_host_cmdb){ | ||
327 | + az2HostIds.add(hostMap.get(ip).getId()); | ||
328 | + } | ||
329 | + if("online".equals(onlineOrGray)){ | ||
330 | + //az2用作生产,productElb挂载 ,然后修改host | ||
331 | + qcloudLoadBalance.registerInstancesWithLoadBalancer(ELB_PCH5_ID,az2HostIds); | ||
332 | + }else if("gray".equals(onlineOrGray)){ | ||
333 | + //卸载之前先检查是否存在az1机器,否则不允许卸载 | ||
334 | + boolean allow=false; | ||
335 | + for(Host host:current_host_prodElb){ | ||
336 | + if(!az2HostIds.contains(host.getId())){ | ||
337 | + allow=true; | ||
338 | + break; | ||
339 | + } | ||
340 | + } | ||
341 | + if(allow){ | ||
342 | + //az2用作灰度,从productElb卸载,然后修改host | ||
343 | + qcloudLoadBalance.deregisterInstancesWithLoadBalancer(ELB_PCH5_ID,az2HostIds); | ||
344 | + }else{ | ||
345 | + return new BaseResponse(201,"pch5 prod elb 请先挂载az1区域的host,再切换用作灰度环境"); | ||
346 | + } | ||
347 | + | ||
348 | + } | ||
349 | + return new BaseResponse(); | ||
350 | + | ||
351 | + } | ||
352 | + | ||
353 | +} |
-
Please register or login to post a comment