|
|
package com.monitor.nginxsync.service;
|
|
|
|
|
|
import com.google.common.base.Preconditions;
|
|
|
import com.monitor.common.ProjectConstant;
|
|
|
import com.monitor.model.response.BaseResponse;
|
|
|
import com.monitor.nginxsync.constant.Constants;
|
|
|
import com.monitor.nginxsync.model.CmdTaskInfo;
|
|
|
import com.monitor.nginxsync.model.LogMsgResp;
|
|
|
import com.monitor.nginxsync.model.TaskInfo;
|
|
|
import com.monitor.nginxsync.nio.model.LogMsg;
|
|
|
import com.monitor.nginxsync.nio.service.LogService;
|
|
|
import org.apache.commons.exec.CommandLine;
|
|
|
import org.apache.commons.exec.DefaultExecutor;
|
|
|
import org.apache.commons.exec.PumpStreamHandler;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Created by yoho on 2016/8/23.
|
|
|
*
|
|
|
* 原来是有个ops-agent项目,通过http调研ops-agent项目,然后在ops-agent调sh脚本
|
|
|
* 现在改为直接 ssh 调sh脚本,不再需要通过ops-agent项目了
|
|
|
*
|
|
|
*/
|
|
|
//@RestController
|
|
|
//@RequestMapping(value = "/nginxsync")
|
|
|
public class NginxSyncService {
|
|
|
public static final Logger DEBUG = LoggerFactory.getLogger(NginxSyncService.class);
|
|
|
|
|
|
|
|
|
@Value("${nginxsync.agent}")
|
|
|
String agent;
|
|
|
|
|
|
@Autowired
|
|
|
LogService logService;
|
|
|
|
|
|
@Autowired
|
|
|
RestTemplate restTemplate;
|
|
|
|
|
|
@RequestMapping(value = "/test")
|
|
|
public BaseResponse test() {
|
|
|
BaseResponse baseResponse=new BaseResponse();
|
|
|
String out="";
|
|
|
String error ="";
|
|
|
String command = "ssh master@"+ ProjectConstant.DEPLOY_IP+" "+"pwd";
|
|
|
try {
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
|
|
|
//命令行处理
|
|
|
CommandLine commandline = CommandLine.parse(command);
|
|
|
//进行执行体
|
|
|
DefaultExecutor exec= new DefaultExecutor();
|
|
|
exec.setExitValues(null);
|
|
|
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
|
|
|
exec.setStreamHandler(streamHandler);
|
|
|
exec.execute(commandline);//执行
|
|
|
out =outputStream.toString("gbk");
|
|
|
error =errorStream.toString("gbk");
|
|
|
DEBUG.info("nginxsync test out {} ,error {}",out,error);
|
|
|
} catch (Exception e) {
|
|
|
DEBUG.info("nginxsync exception {}",e);
|
|
|
baseResponse.setCode(201);
|
|
|
baseResponse.setMessage("error happens "+e.getCause());
|
|
|
return baseResponse;
|
|
|
}
|
|
|
|
|
|
baseResponse.setData("out is "+out+" ; error is "+error);
|
|
|
return baseResponse;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/pushtask")
|
|
|
public BaseResponse pushTask(@RequestBody TaskInfo taskInfo) {
|
|
|
DEBUG.info("Recive nginx sync task {}", taskInfo);
|
|
|
|
|
|
Preconditions.checkNotNull(taskInfo);
|
|
|
|
|
|
//如果任务类型为空
|
|
|
if (0 == taskInfo.getType()) {
|
|
|
return new BaseResponse();
|
|
|
}
|
|
|
|
|
|
CmdTaskInfo cmdTaskInfo = new CmdTaskInfo();
|
|
|
|
|
|
//设置运行目录
|
|
|
cmdTaskInfo.setDir(Constants.SCRIPT_PATH);
|
|
|
|
|
|
String args = StringUtils.EMPTY;
|
|
|
|
|
|
//参数选择
|
|
|
switch (taskInfo.getSelect()) {
|
|
|
case 1:
|
|
|
args = " -c all";
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
args = " -c qcloud";
|
|
|
break;
|
|
|
|
|
|
case 3:
|
|
|
args = " -c aws";
|
|
|
break;
|
|
|
|
|
|
case 4:
|
|
|
args = " -c gray";
|
|
|
break;
|
|
|
|
|
|
case 5:
|
|
|
args = " -c qcloud_az2";
|
|
|
break;
|
|
|
|
|
|
case 6:
|
|
|
args = " -c qcloud_az3";
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
//根据前台选项选择执行脚本
|
|
|
switch (taskInfo.getType()) {
|
|
|
case 1:
|
|
|
cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.N_SCRIPT + args);
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.H5_SCRIPT + args);
|
|
|
break;
|
|
|
//DOCKER_H5_NGINX 与 2相似
|
|
|
case 7:
|
|
|
cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.DOCKER_H5_SCRIPT + args);
|
|
|
break;
|
|
|
|
|
|
case 3:
|
|
|
cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.DNS_SCRIPT + args);
|
|
|
break;
|
|
|
|
|
|
case 4:
|
|
|
cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.ZIXUN_SCRIPT + args);
|
|
|
break;
|
|
|
|
|
|
case 5:
|
|
|
cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.MARS_SCRIPT + args);
|
|
|
break;
|
|
|
|
|
|
case 6:
|
|
|
cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.GLOBAL_SCRIPT + args);
|
|
|
break;
|
|
|
|
|
|
case 8:
|
|
|
cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.NGINX_AZ2 + args);
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if (taskInfo.getSelect() == 4 && taskInfo.getType() == 1){//灰度nginx但是使用
|
|
|
cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.GRAY_N_SCRIPT + args);
|
|
|
}
|
|
|
|
|
|
//设置唯一的任务ID
|
|
|
cmdTaskInfo.setTaskId(String.valueOf(System.currentTimeMillis()));
|
|
|
|
|
|
BaseResponse response = new BaseResponse();
|
|
|
|
|
|
taskInfo.setTaskId(cmdTaskInfo.getTaskId());
|
|
|
|
|
|
response.setData(taskInfo);
|
|
|
|
|
|
response.setCode(200);
|
|
|
|
|
|
try {
|
|
|
restTemplate.postForObject("http://" + agent + "/handle", cmdTaskInfo, String.class);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
DEBUG.error("Failed to dispatch nginx sync task {}...error {}", cmdTaskInfo, e);
|
|
|
|
|
|
response.setCode(500);
|
|
|
|
|
|
response.setMessage("agent error: " + e.getMessage());
|
|
|
}
|
|
|
|
|
|
//返回结果
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/getasklog")
|
|
|
public BaseResponse getTaskLog(@RequestBody TaskInfo taskInfo) {
|
|
|
|
|
|
BaseResponse baseResponse = new BaseResponse();
|
|
|
|
|
|
LogMsg logMsg = logService.outLogMsg(taskInfo.getTaskId());
|
|
|
|
|
|
baseResponse.setData(convertLogMsg(logMsg));
|
|
|
|
|
|
return baseResponse;
|
|
|
}
|
|
|
|
|
|
|
|
|
private LogMsgResp convertLogMsg(LogMsg logMsg) {
|
|
|
LogMsgResp logMsgResp = new LogMsgResp();
|
|
|
|
|
|
logMsgResp.setIsFinished(logMsg.getIsFinished());
|
|
|
|
|
|
logMsgResp.setTaskId(logMsg.getTaskId());
|
|
|
|
|
|
String str = StringUtils.EMPTY;
|
|
|
|
|
|
logMsgResp.setLog(logMsg.getMsg());
|
|
|
|
|
|
return logMsgResp;
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/collectNginxAgentLog")
|
|
|
@ResponseBody
|
|
|
public BaseResponse collectNginxAgentLog(@RequestBody LogMsg logMsg) {
|
|
|
BaseResponse baseResponse = new BaseResponse();
|
|
|
// 插入日志管理
|
|
|
logService.inLogMsg(logMsg);
|
|
|
return baseResponse;
|
|
|
}
|
|
|
} |