Authored by qinchao

pch5切换

... ... @@ -35,7 +35,7 @@ public class Pch5SwitchCtrl {
//Pch5的负载均衡器,传统型,灰度
private final String ELB_PCH5_GRAY_AZ2_ID="lb-kaucjq25";
private static final String COMMON_PREFIX = "/usr/bin/python /home/master/ops_sh/diamond/";
private static final String COMMON_SWITCH_PY = "/usr/bin/python /home/master/ops_sh/pch5-az2-node-host-switch/switch.py ";
//云主机信息
private final String const_host_cvm_map="host_cvm_map";
... ... @@ -195,7 +195,7 @@ public class Pch5SwitchCtrl {
//检查是否启用了灰度
//如果az2机器不再生产环境下,且host为gray,则是灰度
//TODO 检查是否灰度
String hostFileContent=exe("pch5_get.py");
String hostFileContent="";
if(StringUtils.isNotBlank(hostFileContent)&&hostFileContent.indexOf("gray")>=0){
az2InGray=true;
}
... ... @@ -234,24 +234,13 @@ public class Pch5SwitchCtrl {
return checkAzResponse;
}
Map<String,Object> rntData=(Map<String, Object>) checkAzResponse.getData();
boolean az2InGray=(boolean)(rntData.get(const_az2_in_gray));
if(az2InGray){
return new BaseResponse(201,"prodElb的az2为灰度状态,不允许切换可用区");
}
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<>();
... ... @@ -317,10 +306,13 @@ public class Pch5SwitchCtrl {
qcloudLoadBalance.deregisterInstancesWithLoadBalancer(ELB_PCH5_ID,removeIds);
}
@RequestMapping(value = "/getPch5NodeHostUnderAz2")
@ResponseBody
public List<String> getPch5NodeHostIPs() {
return hostInfoService.getHostIpByTags(const_tags_for_nginx_node_az2);
/**
* 获取az2 nginx下挂的node机器
* @return
*/
private String getPch5NodeHostIPsWithComm() {
List<String> ls = hostInfoService.getHostIpByTags(const_tags_for_nginx_node_az2);
return String.join(",", ls);
}
... ... @@ -363,8 +355,11 @@ public class Pch5SwitchCtrl {
//az2用作生产,productElb挂载 ,然后修改host
//先改host,再挂载
//TODO 修改host
exe("pch5_prod.py");
String arg="";
arg += " --env prod ";
arg += " --h ";
arg +=getPch5NodeHostIPsWithComm();
exe(arg);
qcloudLoadBalance.registerInstancesAndWeightWithLoadBalancer(ELB_PCH5_ID,az2HostIds,weights);
}else if("gray".equals(onlineOrGray)){
... ... @@ -390,7 +385,11 @@ public class Pch5SwitchCtrl {
//再改host
//TODO 修改host
exe("pch5_gray.py");
String arg="";
arg += " --env gray ";
arg += " --h ";
arg +=getPch5NodeHostIPsWithComm();
exe(arg);
}else{
return new BaseResponse(201,"pch5 prod elb 请先挂载az1区域的host,再切换用作灰度环境");
}
... ... @@ -400,14 +399,14 @@ public class Pch5SwitchCtrl {
}
private String exe( String commondPy) {
logger.info("start to exe commond:" + COMMON_PREFIX+commondPy);
private String exe( String arg) {
logger.info("start to exe commond:" + COMMON_SWITCH_PY+arg);
Process process = null;
StringBuilder sb = new StringBuilder();
BufferedReader input = null;
try {
process = Runtime.getRuntime().exec(COMMON_PREFIX + commondPy);
process = Runtime.getRuntime().exec(COMMON_SWITCH_PY + arg);
input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
... ... @@ -424,34 +423,7 @@ public class Pch5SwitchCtrl {
}
}
}
logger.info("end to exe commond:" + COMMON_PREFIX+commondPy + ".result:" + sb.toString());
logger.info("end to exe commond:" + COMMON_SWITCH_PY+arg + ".result:" + sb.toString());
return sb.toString();
}
@RequestMapping(value = "/getAz2NodeHosts")
@ResponseBody
public BaseResponse getAz2NodeHosts() {
BaseResponse rnt=new BaseResponse();
String mm = exe("pch5_get.py");
rnt.setData(mm);
return rnt;
}
@RequestMapping(value = "/testSwitchHost")
@ResponseBody
public BaseResponse testSwitchHost(String onlineOrGray) {
BaseResponse rnt=new BaseResponse();
String mm="";
if("online".equals(onlineOrGray)){
mm= exe("pch5_prod.py");
}else if("gray".equals(onlineOrGray)){
mm = exe("pch5_gray.py");
}else{
mm = exe("pch5_get.py");
}
rnt.setData(mm);
return rnt;
}
}
... ...