|
|
package com.monitor.switchs;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.google.common.base.Function;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.model.HostInfo;
|
|
|
import com.monitor.common.service.AlarmMsgService;
|
|
|
import com.monitor.common.util.HandlebarsUtils;
|
|
|
import com.monitor.common.util.RandomUtil;
|
|
|
import com.monitor.model.domain.SmsTicket;
|
|
|
import com.monitor.model.response.BaseResponse;
|
|
|
import com.monitor.mysql.mapper.HostInfoMapper;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.collections.map.HashedMap;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.apache.http.HttpRequest;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
...
|
...
|
@@ -12,6 +23,7 @@ import org.springframework.stereotype.Controller; |
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.*;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
...
|
...
|
@@ -33,6 +45,9 @@ public class NginxSwitchCtrl { |
|
|
@Value("${system.envi}")
|
|
|
private String env;
|
|
|
|
|
|
@Autowired
|
|
|
HostInfoMapper hostInfoMapper;
|
|
|
|
|
|
/**
|
|
|
* 查看nginx配置
|
|
|
*
|
...
|
...
|
@@ -41,49 +56,125 @@ public class NginxSwitchCtrl { |
|
|
@RequestMapping(value = "viewNginxConf")
|
|
|
@ResponseBody
|
|
|
public BaseResponse viewNginxConf() {
|
|
|
Map<String,List<Map<String, Object>>> map = new HashMap<>();
|
|
|
if ("test".equals(env)) {
|
|
|
Map<String, List<Map<String, Object>>> map = new HashMap<>();
|
|
|
/*if ("test".equals(env)) {
|
|
|
List<Map<String, Object>> awsList = localTest("aws");
|
|
|
List<Map<String, Object>> qcloudList = localTest("qq");
|
|
|
map.put("awsList", awsList);
|
|
|
map.put("qcloudList", qcloudList);
|
|
|
return new BaseResponse(map);
|
|
|
}*/
|
|
|
|
|
|
List<HostInfo> awsNginxHosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 1);
|
|
|
if(CollectionUtils.isEmpty(awsNginxHosts)){
|
|
|
map.put("awsList", Lists.newArrayList());
|
|
|
}else {
|
|
|
List<Map<String, Object>> awsList = analyseConfigFile(exe("view", awsNginxHosts.get(0).getHostIp(), null));
|
|
|
map.put("awsList", awsList);
|
|
|
}
|
|
|
List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 2);
|
|
|
if(CollectionUtils.isEmpty(qcloudNginxHosts)){
|
|
|
map.put("qcloudList", Lists.newArrayList());
|
|
|
}else {
|
|
|
List<Map<String, Object>> qcloudList = analyseConfigFile(exe("view", qcloudNginxHosts.get(0).getHostIp(), null));
|
|
|
map.put("qcloudList", qcloudList);
|
|
|
}
|
|
|
|
|
|
List<Map<String, Object>> awsList = analyseConfigFile(exe("aws", "view", null));
|
|
|
List<Map<String, Object>> qcloudList = analyseConfigFile(exe("qcloud", "view", null));
|
|
|
map.put("awsList", awsList);
|
|
|
map.put("qcloudList", qcloudList);
|
|
|
return new BaseResponse(map);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改配置
|
|
|
* type:环境类型 1:线上 2:灰度
|
|
|
*
|
|
|
* @return 2016年5月12日下午1:49:48
|
|
|
*/
|
|
|
@RequestMapping(value = "viewToChangeNginxConf")
|
|
|
@ResponseBody
|
|
|
public BaseResponse viewToChangeNginxConf(String cloudName, String target, String ips, String onlineOrGray, HttpServletRequest httpRequest) {
|
|
|
|
|
|
//切线上 灰度ips不能为空
|
|
|
if (StringUtils.isEmpty(ips)) {
|
|
|
return new BaseResponse(500, "线上或灰度服务器入参不能为空!");
|
|
|
}
|
|
|
|
|
|
//线上切换
|
|
|
String tag = StringUtils.equals("gray", target) ? "灰度" : "gateway";
|
|
|
|
|
|
//需要查询的gateway机子
|
|
|
int gatewayCloudType = StringUtils.equals("aws", target) ? 1 : (StringUtils.equals("qcloud", target) ? 2 : (StringUtils.equals("aws", cloudName) ? 1 : 2));
|
|
|
|
|
|
//获取gateway或graygateway服务器
|
|
|
List<HostInfo> gatewayHosts = hostInfoMapper.selectHostInfosByTagAndCloudType(tag, gatewayCloudType);
|
|
|
if (CollectionUtils.isEmpty(gatewayHosts)) {
|
|
|
return new BaseResponse(500, cloudName + "在cmdb中未找到对应" + tag + "的服务器!");
|
|
|
}
|
|
|
|
|
|
//模板参数对象
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
List<String> ipList = Lists.transform(gatewayHosts, new Function<HostInfo, String>() {
|
|
|
@Override
|
|
|
public String apply(HostInfo input) {
|
|
|
return input.getHostIp();
|
|
|
}
|
|
|
});
|
|
|
//切线上 灰度ips不变
|
|
|
if (StringUtils.equals("online", onlineOrGray)) {
|
|
|
map.put("grayips", JSON.parseArray(ips));
|
|
|
map.put("ips", ipList);
|
|
|
}
|
|
|
//切灰度 线上ips不变
|
|
|
else if (StringUtils.equals("gray", onlineOrGray)) {
|
|
|
map.put("ips", JSON.parseArray(ips));
|
|
|
map.put("grayips", ipList);
|
|
|
}
|
|
|
|
|
|
String path = httpRequest.getSession().getServletContext().getRealPath("/") + "java-nginx/" + cloudName + "/nginx.conf";
|
|
|
logger.info("nginx.conf create path is {}",path);
|
|
|
String result = HandlebarsUtils.replace(map, "template/" + cloudName + "-nginx", path);
|
|
|
Map<String,Object> resultMap = new HashMap();
|
|
|
resultMap.put("cloudName",cloudName);
|
|
|
resultMap.put("result",result);
|
|
|
return new BaseResponse(200, "success",resultMap);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改配置
|
|
|
* type:环境类型 1:线上 2:灰度
|
|
|
*
|
|
|
* @return 2016年5月12日下午1:49:48
|
|
|
*/
|
|
|
@RequestMapping(value = "switchNginxConf")
|
|
|
@ResponseBody
|
|
|
public BaseResponse switchNginxConf(String cloudName, String target) {
|
|
|
String result = exe(cloudName, "switch", target);
|
|
|
BaseResponse<String> baseResponse = new BaseResponse<>();
|
|
|
baseResponse.setCode(200);
|
|
|
baseResponse.setData(result);
|
|
|
return baseResponse;
|
|
|
public BaseResponse switchNginxConf(String cloudName, HttpServletRequest httpRequest) {
|
|
|
int nginxCloudType = StringUtils.equals("aws", cloudName) ? 1 : (StringUtils.equals("qcloud", cloudName) ? 2 : 0);
|
|
|
if (0 == nginxCloudType) {
|
|
|
return new BaseResponse(500, "没有匹配到要切换的云");
|
|
|
}
|
|
|
//获取对应cloud的机器 aws 1 qcloud 2
|
|
|
List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", nginxCloudType);
|
|
|
if (CollectionUtils.isEmpty(nginxHosts)) {
|
|
|
return new BaseResponse(500, cloudName + "在cmdb中未找到对应的nginx服务器!");
|
|
|
}
|
|
|
List<String> ips = new ArrayList<String>();
|
|
|
for(HostInfo hostInfo : nginxHosts){
|
|
|
ips.add(hostInfo.getHostIp());
|
|
|
}
|
|
|
String tempPath = httpRequest.getSession().getServletContext().getRealPath("/") + "java-nginx/" + cloudName + "/nginx.conf";
|
|
|
return new BaseResponse(200,"success", exe("switch",StringUtils.join(ips,","),tempPath));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行命令
|
|
|
*
|
|
|
* @param cloudName
|
|
|
* @param commod
|
|
|
* @param host
|
|
|
* @param templatePath
|
|
|
* @return 2016年5月17日下午3:26:29
|
|
|
*/
|
|
|
public String exe(String cloudName, String commod, String target) {
|
|
|
public String exe(String commod, String host, String templatePath) {
|
|
|
StringBuilder commond = new StringBuilder();
|
|
|
commond.append(" -s " + cloudName);
|
|
|
commond.append(" -e " + commod);
|
|
|
commond.append(" -d " + target);
|
|
|
commond.append(" " + commod).append(" " + host).append(" " + templatePath);
|
|
|
return CommodUtil.exe(CommodUtil.NGINX_SWITCH_SH, commond.toString());
|
|
|
}
|
|
|
|
...
|
...
|
|