Authored by qinchao

pch5切换

... ... @@ -181,92 +181,4 @@ public class QcloudClientFactory {
}
});
}
public List<Host> getAllInstancesSet(){
List<Host> allHost=new ArrayList<Host>();
//List<QcloudInstanceSet> instanceSets_all = new ArrayList<QcloudInstanceSet>();
Set<String> instanceIDs=new HashSet<String>();
try{
// 第一步,先获取第一页的数据
int Limit=100;
int pageNumber=0;
TreeMap<String, Object> params = new TreeMap<>();
params.put("Version","2017-03-12");
params.put("Offset", pageNumber*Limit);
params.put("Limit",Limit);
logger.info("--------------getAllInstancesSet - start");
String rootJson = QcloudSdkUtil.getCommonData(new Cvm(), "GET", QcloudConstant.QCLOUD_API_INSTANCE_FUN, params, true);
if(StringUtils.isBlank(rootJson)){
return null;
}
int total=0;
JSONObject rootObj = JSON.parseObject(rootJson);
if(rootObj!=null&&rootObj.get("Response")!=null){
JSONObject response_jo=rootObj.getJSONObject("Response");
total=response_jo.getInteger("TotalCount");
JSONArray insts_ja=response_jo.getJSONArray("InstanceSet");
for(int i=0;i<insts_ja.size();i++){
JSONObject jo = insts_ja.getJSONObject(i);
String hostid=jo.getString("InstanceId");
String hostname=jo.getString("InstanceName");
String ip=jo.getJSONArray("PrivateIpAddresses").size()>0?jo.getJSONArray("PrivateIpAddresses").getString(0):"";
String restrictState=jo.getString("RestrictState"); ///状态,不要EXPIRED状态的主机,NORMAL表示正常状态的实例 ,EXPIRED:表示过期的实例 ,PROTECTIVELY_ISOLATED:表示被安全隔离的实例。
if("EXPIRED".equalsIgnoreCase(restrictState)){
continue;
}
if(!instanceIDs.contains(hostid)){
instanceIDs.add(hostid);
Host host=new Host(DataCenter.qcloud,hostid, ip, hostname);
allHost.add(host);
}
}
}
//第二步,分页获取
if(total>Limit){
pageNumber=1;
int pageCount=(int)Math.ceil(total/(double)Limit) ;
for(;pageNumber<pageCount;pageNumber++){
params.put("Offset", pageNumber*Limit);
rootJson = QcloudSdkUtil.getCommonData(new Cvm(), "GET", QcloudConstant.QCLOUD_API_INSTANCE_FUN, params, true);
if(StringUtils.isBlank(rootJson)){
continue;
}
rootObj = JSON.parseObject(rootJson);
if(rootObj!=null&&rootObj.get("Response")!=null){
JSONObject response_jo=rootObj.getJSONObject("Response");
JSONArray insts_ja=response_jo.getJSONArray("InstanceSet");
for(int i=0;i<insts_ja.size();i++){
JSONObject jo = insts_ja.getJSONObject(i);
String hostid=jo.getString("InstanceId");
String hostname=jo.getString("InstanceName");
String ip=jo.getJSONArray("PrivateIpAddresses").size()>0?jo.getJSONArray("PrivateIpAddresses").getString(0):"";
String restrictState=jo.getString("RestrictState"); ///状态,不要EXPIRED状态的主机,NORMAL表示正常状态的实例 ,EXPIRED:表示过期的实例 ,PROTECTIVELY_ISOLATED:表示被安全隔离的实例。
if("EXPIRED".equalsIgnoreCase(restrictState)){
continue;
}
if(!instanceIDs.contains(hostid)){
instanceIDs.add(hostid);
Host host=new Host(DataCenter.qcloud,hostid, ip, hostname);
allHost.add(host);
}
}
}
}
}
}catch (Exception e){
logger.error(" - QcloudToolServiceImpl - getScalingInstancesSet- error", e);
}
return allHost;
}
}
... ...
... ... @@ -5,6 +5,7 @@ import com.yoho.ops.cmdb.models.AutoScalingGroup;
import com.yoho.ops.cmdb.models.Host;
import com.yoho.ops.cmdb.models.QcloudModifyScalingGroupReq;
import com.yoho.ops.cmdb.qcloud.QcloudClientFactory;
import com.yoho.ops.cmdb.qcloud.util.QcloudSdkUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
... ... @@ -29,7 +30,7 @@ public class QcloudAutoScalingFetcher {
}
public List<Host> getAllInstancesSet(){
return qcloudClientFactory.getAllInstancesSet();
return QcloudSdkUtil.getAllInstancesSet();
}
public BaseResponse modifyScalingGroup(QcloudModifyScalingGroupReq req){
... ...
package com.yoho.ops.cmdb.qcloud.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qcloud.Module.*;
import com.qcloud.QcloudApiModuleCenter;
import com.yoho.ops.cmdb.models.DataCenter;
import com.yoho.ops.cmdb.models.Host;
import com.yoho.ops.cmdb.qcloud.constant.QcloudConstant;
import com.yoho.ops.cmdb.qcloud.model.QcloudAutoScalingGroup;
import com.yoho.ops.cmdb.qcloud.model.QcloudCLBModel;
... ... @@ -17,10 +20,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TreeMap;
import java.util.*;
/**
* Created by craig.qin on 2017/5/8.
... ... @@ -284,6 +284,35 @@ public class QcloudSdkUtil {
}
/**
* 根据过滤条件(内网ip),获取主机信息
* 过滤条件Filters.1.Name = private-ip-address
* 过滤值 Filters.1.Values.1=10.xx.xx.xx & Filters.2.Values.1=10.xx.xx.xx
* 索引从1开始
* ;Filter.Values的上限为5。参数不支持同时指定InstanceIds和Filters
*/
public static List<Host> getInstancesByPrivateIpAddress(List<String> privateIps){
//privateIps 最多5个
List<Host> instanceList = new ArrayList<>();
int count=0;
List<String> ipTmp=new ArrayList<String>();
for(String tmpID:privateIps){
if(count<5){
ipTmp.add(tmpID);
count++;
}else{
instanceList.addAll(describeInstancesByFilter("private-ip-address",ipTmp));
ipTmp= new ArrayList<String>();
ipTmp.add(tmpID);
count=1;
}
}
if(ipTmp!=null&&ipTmp.size()>0){
instanceList.addAll(describeInstancesByFilter("private-ip-address",ipTmp));
}
return instanceList;
}
/**
* 接口:DescribeInstances
* @param scalingInstanceIds 实例ID集合
* @return
... ... @@ -313,6 +342,124 @@ public class QcloudSdkUtil {
return instanceSets;
}
/**
* 接口:DescribeInstances
* 根据过滤条件查询实例
* @return
*/
private static List<Host> describeInstancesByFilter(String filterName,List<String> filterValuesList){
if(StringUtils.isBlank(filterName)||CollectionUtils.isEmpty(filterValuesList)){
return null;
}
List<Host> allHost=new ArrayList<>();
Set<String> instanceIDs=new HashSet<>();
try{
TreeMap<String, Object> params = new TreeMap<>();
params.put("Version","2017-03-12");
params.put("Filters.1.Name",filterName);
for (int index = 0; index < filterValuesList.size(); index++) {
params.put("Filters.1.Values."+(1+index), filterValuesList.get(index));
}
String rootJson = QcloudSdkUtil.getCommonData(new Cvm(), "GET", QcloudConstant.QCLOUD_API_INSTANCE_FUN, params, true);
if(StringUtils.isBlank(rootJson)){
return null;
}
JSONObject rootObj = JSON.parseObject(rootJson);
if(rootObj!=null&&rootObj.get("Response")!=null){
JSONObject response_jo=rootObj.getJSONObject("Response");
JSONArray insts_ja=response_jo.getJSONArray("InstanceSet");
for(int i=0;i<insts_ja.size();i++){
buildHost( instanceIDs, allHost, insts_ja.getJSONObject(i));
}
}
}catch (Exception e){
logger.error(" - QcloudSdkUtil - describeInstances- error", e);
}
return allHost;
}
/**
* 获取所有的主机
* @return
*/
public static List<Host> getAllInstancesSet(){
List<Host> allHost=new ArrayList<Host>();
Set<String> instanceIDs=new HashSet<String>();
try{
// 第一步,先获取第一页的数据
int Limit=100;
int pageNumber=0;
TreeMap<String, Object> params = new TreeMap<>();
params.put("Version","2017-03-12");
params.put("Offset", pageNumber*Limit);
params.put("Limit",Limit);
String rootJson = QcloudSdkUtil.getCommonData(new Cvm(), "GET", QcloudConstant.QCLOUD_API_INSTANCE_FUN, params, true);
if(StringUtils.isBlank(rootJson)){
return null;
}
int total=0;
JSONObject rootObj = JSON.parseObject(rootJson);
if(rootObj!=null&&rootObj.get("Response")!=null){
JSONObject response_jo=rootObj.getJSONObject("Response");
total=response_jo.getInteger("TotalCount");
JSONArray insts_ja=response_jo.getJSONArray("InstanceSet");
for(int i=0;i<insts_ja.size();i++){
JSONObject jo = insts_ja.getJSONObject(i);
buildHost( instanceIDs, allHost, jo);
}
}
//第二步,分页获取
if(total>Limit){
pageNumber=1;
int pageCount=(int)Math.ceil(total/(double)Limit) ;
for(;pageNumber<pageCount;pageNumber++){
params.put("Offset", pageNumber*Limit);
rootJson = QcloudSdkUtil.getCommonData(new Cvm(), "GET", QcloudConstant.QCLOUD_API_INSTANCE_FUN, params, true);
if(StringUtils.isBlank(rootJson)){
continue;
}
rootObj = JSON.parseObject(rootJson);
if(rootObj!=null&&rootObj.get("Response")!=null){
JSONObject response_jo=rootObj.getJSONObject("Response");
JSONArray insts_ja=response_jo.getJSONArray("InstanceSet");
for(int i=0;i<insts_ja.size();i++){
JSONObject jo = insts_ja.getJSONObject(i);
buildHost( instanceIDs, allHost, jo);
}
}
}
}
}catch (Exception e){
logger.error(" - QcloudToolServiceImpl - getScalingInstancesSet- error", e);
}
return allHost;
}
private static void buildHost(Set<String> instanceIDs,List<Host> allHost,JSONObject jo){
String hostid=jo.getString("InstanceId");
String hostname=jo.getString("InstanceName");
String ip=jo.getJSONArray("PrivateIpAddresses").size()>0?jo.getJSONArray("PrivateIpAddresses").getString(0):"";
String restrictState=jo.getString("RestrictState"); ///状态,不要EXPIRED状态的主机,NORMAL表示正常状态的实例 ,EXPIRED:表示过期的实例 ,PROTECTIVELY_ISOLATED:表示被安全隔离的实例。
if(!"EXPIRED".equalsIgnoreCase(restrictState)){
if(!instanceIDs.contains(hostid)){
instanceIDs.add(hostid);
Host host=new Host(DataCenter.qcloud,hostid, ip, hostname);
allHost.add(host);
}
}
}
/*******************************************************************
*************************** 2 负载均衡器 (传统) ************************
******************************************************************/
... ... @@ -417,13 +564,19 @@ public class QcloudSdkUtil {
}
public static void main(String args[]){
Calendar calendar = Calendar.getInstance();
/*Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, -60);
String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
List<Double> dd=getMonitorData("outtraffic",startTime);
System.out.println(dd);
System.out.println("");
List<Double> dd2=getMonitorData("intraffic",startTime);
System.out.println(dd2);
System.out.println(dd2);*/
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"};
List<Host> ll=getInstancesByPrivateIpAddress(Arrays.asList(ips));
System.out.println(ll);
}
}
\ No newline at end of file
... ...
package com.monitor.switchs;
import com.model.HostInfo;
import com.monitor.cmdb.service.IHostInfoService;
import com.monitor.model.response.BaseResponse;
import com.yoho.ops.cmdb.models.Host;
import com.yoho.ops.cmdb.models.LoadBalance;
import com.yoho.ops.cmdb.qcloud.lb.QcloudLoadBalance;
import com.yoho.ops.cmdb.qcloud.util.QcloudSdkUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author craig.qin
* pch5切换
*/
@Controller
@RequestMapping("/pch5Switch")
public class Pch5SwitchCtrl {
//Pch5的负载均衡器,传统型,生产
private final String ELB_PCH5_ID="lb-ceg44td5";
//Pch5的负载均衡器,传统型,灰度
private final String ELB_PCH5_GRAY_AZ2_ID="lb-kaucjq25";
//云主机信息
private final String const_host_cvm_map="host_cvm_map";
//当前生产elb下面挂载的机器
private final String const_current_host_under_prod_elb="const_current_host_under_prod_elb";
//所有az1区的机器是否都挂载到elb
private final String const_az1_host_under_prod_elb="az1_host_under_prod_elb";
private final String const_az2_host_under_prod_elb="az2_host_under_prod_elb";
private final List<String> const_tags_for_nginx_az1=new ArrayList<String>(){
{
add("pch5");
add("nginx");
add("az1");
}
};
private final List<String> const_tags_for_nginx_az2=new ArrayList<String>(){
{
add("pch5");
add("nginx");
add("az2");
}
};
private final List<String> const_tags_for_nginx_node_az2=new ArrayList<String>(){
{
add("pch5");
add("node");
add("az2");
}
};
@Autowired
private QcloudLoadBalance qcloudLoadBalance;
@Autowired
private IHostInfoService hostInfoService;
@RequestMapping(value = "/getPch5NodeHostUnderAz2")
@ResponseBody
public List<String> getPch5NodeHostIPs() {
return getHostByType(const_tags_for_nginx_node_az2);
}
@RequestMapping(value = "/getElbStatus")
@ResponseBody
public BaseResponse getElbStatus() {
BaseResponse response = new BaseResponse();
//az1 ,az2机器
List<String> az1_host_cmdb=getHostByType(const_tags_for_nginx_az1);
List<String> az2_host_cmdb=getHostByType(const_tags_for_nginx_az2);
BaseResponse checkAzResponse=checkAzCmdb(az1_host_cmdb,az2_host_cmdb);
if(checkAzResponse==null||checkAzResponse.getCode()!=200){
return checkAzResponse;
}
Map<String,Object> rntData=(Map<String, Object>) checkAzResponse.getData();
boolean inAz1=(boolean)(rntData.get(const_az1_host_under_prod_elb));
boolean inAz2=(boolean)(rntData.get(const_az2_host_under_prod_elb));
Map<String,Object> ipsUnderElbMap=new LinkedHashMap<>();
ipsUnderElbMap.put("az1Ips",az1_host_cmdb);
ipsUnderElbMap.put("az2Ips",az2_host_cmdb);
ipsUnderElbMap.put("inAz1",inAz1);
ipsUnderElbMap.put("inAz2",inAz2);
response.setData(ipsUnderElbMap);
return response;
}
/**
* 检查
* @param az1_host_cmdb
* @param az2_host_cmdb
* @return
*/
private BaseResponse checkAzCmdb(List<String> az1_host_cmdb ,List<String> az2_host_cmdb){
/****************************************************************************/
/**************** cmdb 标签配置检查 *********************/
/****************************************************************************/
//az1和az2不能重复
if(az1_host_cmdb==null||az1_host_cmdb.size()<0){
return new BaseResponse(201,"获取标签[pch5 nginx az1]的cmdb信息为空");
}
if(az2_host_cmdb==null||az2_host_cmdb.size()<0){
return new BaseResponse(201,"获取标签[pch5 nginx az2]的cmdb信息为空");
}
for(String tmp:az1_host_cmdb){
if(az2_host_cmdb.contains(tmp)){
return new BaseResponse(201,"获取标签[pch5 nginx az1]的cmdb"+tmp+"与az2重复");
}
}
for(String tmp:az2_host_cmdb){
if(az1_host_cmdb.contains(tmp)){
return new BaseResponse(201,"获取标签[pch5 nginx az1]的cmdb"+tmp+"与az2重复");
}
}
/****************************************************************************/
/**************** 云主机检查及云主机id获取 *********************/
/****************************************************************************/
List<String> all_host_cmdb=new ArrayList<>();
all_host_cmdb.addAll(az1_host_cmdb);
all_host_cmdb.addAll(az2_host_cmdb);
List<Host> hostList= QcloudSdkUtil.getInstancesByPrivateIpAddress(all_host_cmdb);
if(hostList==null||hostList.size()<=0||hostList.size()!=all_host_cmdb.size()){
return new BaseResponse(201,"获取pch5 az1 az2 标签相关云主机列表为空或者云主机与标签主机数量不一致");
}
Map<String,Host> hostMap=hostList.stream().collect(Collectors.toMap(Host::getIp, host -> host));
/****************************************************************************/
/**************** 灰度elb配置的主机检查 *************/
/****************************************************************************/
LoadBalance grayPch5Elb=qcloudLoadBalance.getAlbLoadBalancesById(ELB_PCH5_GRAY_AZ2_ID);
if(grayPch5Elb==null||grayPch5Elb.getHosts().size()<=0){
return new BaseResponse(201,"获取grayElb异常或者挂载的主机列表为空");
}
List<String> grayPch5Ips=grayPch5Elb.getHosts().stream().map(Host::getIp).collect(Collectors.toList());
//检查灰度机器的az2标签 cmdb信息必须与线上grayElb挂载的nginx一致
for(String tmp:grayPch5Ips){
if(!az2_host_cmdb.contains(tmp)){
return new BaseResponse(201,"获取grayElb["+tmp+"]未配置标签");
}
}
for(String hostIp:az2_host_cmdb){
if(!grayPch5Ips.contains(hostIp)){
return new BaseResponse(201,"获取pch5 nginx az2标签,存在不一致机器["+hostIp+"]");
}
}
/****************************************************************************/
/**************** 生产elb配置的主机检查 *************/
/****************************************************************************/
//生产环境
LoadBalance prodPch5Elb=qcloudLoadBalance.getAlbLoadBalancesById(ELB_PCH5_ID);
if(prodPch5Elb==null||prodPch5Elb.getHosts().size()<=0){
return new BaseResponse(201,"获取prodElb异常或者挂载的主机列表为空");
}
// prodElb挂载的nginx,要么是az1_host_cmdb,要么az2_host_cmdb,不能存在其他情况
List<String> az1Ips=new ArrayList<>();
List<String> az2Ips=new ArrayList<>();//挂载到prod下的az2机器
for(Host host:prodPch5Elb.getHosts()){
if(!az1_host_cmdb.contains(host.getIp()) && !az2_host_cmdb.contains(host.getIp())){
return new BaseResponse(201,"获取prodElb["+host.getIp()+"]在cmdb未配置正确标签");
}else{
if(az1_host_cmdb.contains(host.getIp())){
az1Ips.add(host.getIp());
}else if(az2_host_cmdb.contains(host.getIp())){
az2Ips.add(host.getIp());
}
}
}
//不允许az部分机器挂载的elb ,要么全部挂载,要么不挂载
boolean inAz1=false;
if(az1Ips.size()>0){
if(az1Ips.size()!=az1_host_cmdb.size()){
return new BaseResponse(201,"prodElb的az1 nginx挂载机器不等于cmdb配置");
}else{
inAz1=true;
}
}
boolean inAz2=false;
if(az2Ips.size()>0){
if(az2Ips.size()!=az2_host_cmdb.size()){
return new BaseResponse(201,"prodElb的az2 nginx挂载机器不等于cmdb配置");
}else{
inAz2=true;
}
}
BaseResponse baseResponse=new BaseResponse();
Map<String,Object> map=new HashMap<>();
map.put(const_host_cvm_map,hostMap);//云主机列表
map.put(const_current_host_under_prod_elb,prodPch5Elb.getHosts());
map.put(const_az1_host_under_prod_elb,inAz1);
map.put(const_az2_host_under_prod_elb,inAz2);
baseResponse.setData(map);
return baseResponse;
}
/**
* 从标签获取机器信息
* @return
*/
private List<String> getHostByType(List<String> tagList){
List<HostInfo> hostInfos = hostInfoService.getHostInfosByTagList(tagList);
List<String> ips=new ArrayList<>();
if(hostInfos!=null&&hostInfos.size()>0){
for(HostInfo hostInfo:hostInfos){
ips.add(hostInfo.getHostIp());
}
}
return ips;
}
/**
* 可用区切换
*
*/
@RequestMapping(value = "/switchArea")
@ResponseBody
public BaseResponse switchArea(String area) {
//az1 ,az2机器,及对应instanceId
List<String> az1_host_cmdb=getHostByType(const_tags_for_nginx_az1);
List<String> az2_host_cmdb=getHostByType(const_tags_for_nginx_az2);
BaseResponse checkAzResponse=checkAzCmdb(az1_host_cmdb,az2_host_cmdb);
if(checkAzResponse==null||checkAzResponse.getCode()!=200){
return checkAzResponse;
}
Map<String,Object> rntData=(Map<String, Object>) checkAzResponse.getData();
Map<String,Host> hostMap=(Map<String,Host>)(rntData.get(const_host_cvm_map));
//生产环境的当前主机
/*List<Host> currentHostUnderProdElb=(List<Host>)rntData.get(const_current_host_under_prod_elb) ;
Set<String> allHostIps_under_prod=new TreeSet<>();
for(Host host:currentHostUnderProdElb){
allHostIps_under_prod.add(host.getId());
}*/
if("all".equals(area)){
//指向all,挂载
List<String> addIds=new ArrayList<>();
for(String ip:hostMap.keySet()){
//if(!allHostIps_under_prod.contains(ip)){
addIds.add(hostMap.get(ip).getId());
//}
}
qcloudLoadBalance.registerInstancesWithLoadBalancer(ELB_PCH5_ID,addIds);
} else if("az1".equals(area)){
//指向az1 ,把az2卸载
//首先要确保az1,至少有一个host,才能卸载az2机器
pch5ElbChangeHost(az1_host_cmdb,az2_host_cmdb,hostMap);
} else if("az2".equals(area)){
//指向az2,把az1卸载
//先把az2挂载上
//卸载az1
pch5ElbChangeHost(az2_host_cmdb,az1_host_cmdb,hostMap);
}
return new BaseResponse();
}
/**
* 从elb卸载机器,一定要注意至少保留一个
* @param addOrKeepIps
* @param removeIps
* @param hostMap
*/
private void pch5ElbChangeHost(List<String> addOrKeepIps,List<String> removeIps,Map<String,Host> hostMap){
//先把az1挂载上
List<String> addIds=new ArrayList<>();
for(String ip:addOrKeepIps){
addIds.add(hostMap.get(ip).getId());
}
qcloudLoadBalance.registerInstancesWithLoadBalancer(ELB_PCH5_ID,addIds);
//再卸载az2
List<String> removeIds=new ArrayList<>();
for(String ip:removeIps){
removeIds.add(hostMap.get(ip).getId());
}
qcloudLoadBalance.deregisterInstancesWithLoadBalancer(ELB_PCH5_ID,removeIds);
}
/**
* az2切换
* 获取productElb下面的az2类型host
* 如果是切向gray: 即从productElb卸载 ,修改host文件
* 如果是切向online: 即从productElb挂载 ,修改host文件
*/
@RequestMapping(value = "/switchAz2")
@ResponseBody
public BaseResponse switchAz2(String onlineOrGray) {
if(!"online".equals(onlineOrGray)&&!"gray".equals(onlineOrGray)){
return new BaseResponse(201,"参数错误");
}
//az1 ,az2机器,及对应instanceId
List<String> az1_host_cmdb=getHostByType(const_tags_for_nginx_az1);
List<String> az2_host_cmdb=getHostByType(const_tags_for_nginx_az2);
BaseResponse checkAzResponse=checkAzCmdb(az1_host_cmdb,az2_host_cmdb);
if(checkAzResponse==null||checkAzResponse.getCode()!=200){
return checkAzResponse;
}
Map<String,Object> rntData=(Map<String, Object>) checkAzResponse.getData();
Map<String,Host> hostMap=(Map<String,Host>)(rntData.get(const_host_cvm_map));
List<Host> current_host_prodElb=(List<Host>)rntData.get(const_current_host_under_prod_elb);
List<String> az2HostIds=new ArrayList<>();
for(String ip:az2_host_cmdb){
az2HostIds.add(hostMap.get(ip).getId());
}
if("online".equals(onlineOrGray)){
//az2用作生产,productElb挂载 ,然后修改host
qcloudLoadBalance.registerInstancesWithLoadBalancer(ELB_PCH5_ID,az2HostIds);
}else if("gray".equals(onlineOrGray)){
//卸载之前先检查是否存在az1机器,否则不允许卸载
boolean allow=false;
for(Host host:current_host_prodElb){
if(!az2HostIds.contains(host.getId())){
allow=true;
break;
}
}
if(allow){
//az2用作灰度,从productElb卸载,然后修改host
qcloudLoadBalance.deregisterInstancesWithLoadBalancer(ELB_PCH5_ID,az2HostIds);
}else{
return new BaseResponse(201,"pch5 prod elb 请先挂载az1区域的host,再切换用作灰度环境");
}
}
return new BaseResponse();
}
}
... ...