nginx 同步功能 去掉ops-agent项目
Showing
1 changed file
with
0 additions
and
237 deletions
monitor-service-nginxsync/src/main/java/com/monitor/nginxsync/service/NginxSyncService.java
deleted
100644 → 0
1 | -package com.monitor.nginxsync.service; | ||
2 | - | ||
3 | -import com.google.common.base.Preconditions; | ||
4 | -import com.monitor.common.ProjectConstant; | ||
5 | -import com.monitor.model.response.BaseResponse; | ||
6 | -import com.monitor.nginxsync.constant.Constants; | ||
7 | -import com.monitor.nginxsync.model.CmdTaskInfo; | ||
8 | -import com.monitor.nginxsync.model.LogMsgResp; | ||
9 | -import com.monitor.nginxsync.model.TaskInfo; | ||
10 | -import com.monitor.nginxsync.nio.model.LogMsg; | ||
11 | -import com.monitor.nginxsync.nio.service.LogService; | ||
12 | -import org.apache.commons.exec.CommandLine; | ||
13 | -import org.apache.commons.exec.DefaultExecutor; | ||
14 | -import org.apache.commons.exec.PumpStreamHandler; | ||
15 | -import org.apache.commons.lang.StringUtils; | ||
16 | -import org.slf4j.Logger; | ||
17 | -import org.slf4j.LoggerFactory; | ||
18 | -import org.springframework.beans.factory.annotation.Autowired; | ||
19 | -import org.springframework.beans.factory.annotation.Value; | ||
20 | -import org.springframework.web.bind.annotation.RequestBody; | ||
21 | -import org.springframework.web.bind.annotation.RequestMapping; | ||
22 | -import org.springframework.web.bind.annotation.ResponseBody; | ||
23 | -import org.springframework.web.bind.annotation.RestController; | ||
24 | -import org.springframework.web.client.RestTemplate; | ||
25 | - | ||
26 | -import java.io.ByteArrayOutputStream; | ||
27 | - | ||
28 | - | ||
29 | -/** | ||
30 | - * Created by yoho on 2016/8/23. | ||
31 | - * | ||
32 | - * 原来是有个ops-agent项目,通过http调研ops-agent项目,然后在ops-agent调sh脚本 | ||
33 | - * 现在改为直接 ssh 调sh脚本,不再需要通过ops-agent项目了 | ||
34 | - * | ||
35 | - */ | ||
36 | -//@RestController | ||
37 | -//@RequestMapping(value = "/nginxsync") | ||
38 | -public class NginxSyncService { | ||
39 | - public static final Logger DEBUG = LoggerFactory.getLogger(NginxSyncService.class); | ||
40 | - | ||
41 | - | ||
42 | - @Value("${nginxsync.agent}") | ||
43 | - String agent; | ||
44 | - | ||
45 | - @Autowired | ||
46 | - LogService logService; | ||
47 | - | ||
48 | - @Autowired | ||
49 | - RestTemplate restTemplate; | ||
50 | - | ||
51 | - @RequestMapping(value = "/test") | ||
52 | - public BaseResponse test() { | ||
53 | - BaseResponse baseResponse=new BaseResponse(); | ||
54 | - String out=""; | ||
55 | - String error =""; | ||
56 | - String command = "ssh master@"+ ProjectConstant.DEPLOY_IP+" "+"pwd"; | ||
57 | - try { | ||
58 | - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
59 | - ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); | ||
60 | - //命令行处理 | ||
61 | - CommandLine commandline = CommandLine.parse(command); | ||
62 | - //进行执行体 | ||
63 | - DefaultExecutor exec= new DefaultExecutor(); | ||
64 | - exec.setExitValues(null); | ||
65 | - PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream); | ||
66 | - exec.setStreamHandler(streamHandler); | ||
67 | - exec.execute(commandline);//执行 | ||
68 | - out =outputStream.toString("gbk"); | ||
69 | - error =errorStream.toString("gbk"); | ||
70 | - DEBUG.info("nginxsync test out {} ,error {}",out,error); | ||
71 | - } catch (Exception e) { | ||
72 | - DEBUG.info("nginxsync exception {}",e); | ||
73 | - baseResponse.setCode(201); | ||
74 | - baseResponse.setMessage("error happens "+e.getCause()); | ||
75 | - return baseResponse; | ||
76 | - } | ||
77 | - | ||
78 | - baseResponse.setData("out is "+out+" ; error is "+error); | ||
79 | - return baseResponse; | ||
80 | - } | ||
81 | - | ||
82 | - | ||
83 | - | ||
84 | - | ||
85 | - @RequestMapping(value = "/pushtask") | ||
86 | - public BaseResponse pushTask(@RequestBody TaskInfo taskInfo) { | ||
87 | - DEBUG.info("Recive nginx sync task {}", taskInfo); | ||
88 | - | ||
89 | - Preconditions.checkNotNull(taskInfo); | ||
90 | - | ||
91 | - //如果任务类型为空 | ||
92 | - if (0 == taskInfo.getType()) { | ||
93 | - return new BaseResponse(); | ||
94 | - } | ||
95 | - | ||
96 | - CmdTaskInfo cmdTaskInfo = new CmdTaskInfo(); | ||
97 | - | ||
98 | - //设置运行目录 | ||
99 | - cmdTaskInfo.setDir(Constants.SCRIPT_PATH); | ||
100 | - | ||
101 | - String args = StringUtils.EMPTY; | ||
102 | - | ||
103 | - //参数选择 | ||
104 | - switch (taskInfo.getSelect()) { | ||
105 | - case 1: | ||
106 | - args = " -c all"; | ||
107 | - break; | ||
108 | - | ||
109 | - case 2: | ||
110 | - args = " -c qcloud"; | ||
111 | - break; | ||
112 | - | ||
113 | - case 3: | ||
114 | - args = " -c aws"; | ||
115 | - break; | ||
116 | - | ||
117 | - case 4: | ||
118 | - args = " -c gray"; | ||
119 | - break; | ||
120 | - | ||
121 | - case 5: | ||
122 | - args = " -c qcloud_az2"; | ||
123 | - break; | ||
124 | - | ||
125 | - case 6: | ||
126 | - args = " -c qcloud_az3"; | ||
127 | - break; | ||
128 | - | ||
129 | - default: | ||
130 | - break; | ||
131 | - } | ||
132 | - | ||
133 | - //根据前台选项选择执行脚本 | ||
134 | - switch (taskInfo.getType()) { | ||
135 | - case 1: | ||
136 | - cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.N_SCRIPT + args); | ||
137 | - break; | ||
138 | - | ||
139 | - case 2: | ||
140 | - cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.H5_SCRIPT + args); | ||
141 | - break; | ||
142 | - //DOCKER_H5_NGINX 与 2相似 | ||
143 | - case 7: | ||
144 | - cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.DOCKER_H5_SCRIPT + args); | ||
145 | - break; | ||
146 | - | ||
147 | - case 3: | ||
148 | - cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.DNS_SCRIPT + args); | ||
149 | - break; | ||
150 | - | ||
151 | - case 4: | ||
152 | - cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.ZIXUN_SCRIPT + args); | ||
153 | - break; | ||
154 | - | ||
155 | - case 5: | ||
156 | - cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.MARS_SCRIPT + args); | ||
157 | - break; | ||
158 | - | ||
159 | - case 6: | ||
160 | - cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.GLOBAL_SCRIPT + args); | ||
161 | - break; | ||
162 | - | ||
163 | - case 8: | ||
164 | - cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.NGINX_AZ2 + args); | ||
165 | - break; | ||
166 | - | ||
167 | - default: | ||
168 | - break; | ||
169 | - } | ||
170 | - | ||
171 | - if (taskInfo.getSelect() == 4 && taskInfo.getType() == 1){//灰度nginx但是使用 | ||
172 | - cmdTaskInfo.setCmd("/bin/bash " + Constants.SCRIPT_PATH + Constants.GRAY_N_SCRIPT + args); | ||
173 | - } | ||
174 | - | ||
175 | - //设置唯一的任务ID | ||
176 | - cmdTaskInfo.setTaskId(String.valueOf(System.currentTimeMillis())); | ||
177 | - | ||
178 | - BaseResponse response = new BaseResponse(); | ||
179 | - | ||
180 | - taskInfo.setTaskId(cmdTaskInfo.getTaskId()); | ||
181 | - | ||
182 | - response.setData(taskInfo); | ||
183 | - | ||
184 | - response.setCode(200); | ||
185 | - | ||
186 | - try { | ||
187 | - restTemplate.postForObject("http://" + agent + "/handle", cmdTaskInfo, String.class); | ||
188 | - | ||
189 | - } catch (Exception e) { | ||
190 | - DEBUG.error("Failed to dispatch nginx sync task {}...error {}", cmdTaskInfo, e); | ||
191 | - | ||
192 | - response.setCode(500); | ||
193 | - | ||
194 | - response.setMessage("agent error: " + e.getMessage()); | ||
195 | - } | ||
196 | - | ||
197 | - //返回结果 | ||
198 | - return response; | ||
199 | - } | ||
200 | - | ||
201 | - | ||
202 | - @RequestMapping(value = "/getasklog") | ||
203 | - public BaseResponse getTaskLog(@RequestBody TaskInfo taskInfo) { | ||
204 | - | ||
205 | - BaseResponse baseResponse = new BaseResponse(); | ||
206 | - | ||
207 | - LogMsg logMsg = logService.outLogMsg(taskInfo.getTaskId()); | ||
208 | - | ||
209 | - baseResponse.setData(convertLogMsg(logMsg)); | ||
210 | - | ||
211 | - return baseResponse; | ||
212 | - } | ||
213 | - | ||
214 | - | ||
215 | - private LogMsgResp convertLogMsg(LogMsg logMsg) { | ||
216 | - LogMsgResp logMsgResp = new LogMsgResp(); | ||
217 | - | ||
218 | - logMsgResp.setIsFinished(logMsg.getIsFinished()); | ||
219 | - | ||
220 | - logMsgResp.setTaskId(logMsg.getTaskId()); | ||
221 | - | ||
222 | - String str = StringUtils.EMPTY; | ||
223 | - | ||
224 | - logMsgResp.setLog(logMsg.getMsg()); | ||
225 | - | ||
226 | - return logMsgResp; | ||
227 | - } | ||
228 | - | ||
229 | - @RequestMapping(value = "/collectNginxAgentLog") | ||
230 | - @ResponseBody | ||
231 | - public BaseResponse collectNginxAgentLog(@RequestBody LogMsg logMsg) { | ||
232 | - BaseResponse baseResponse = new BaseResponse(); | ||
233 | - // 插入日志管理 | ||
234 | - logService.inLogMsg(logMsg); | ||
235 | - return baseResponse; | ||
236 | - } | ||
237 | -} |
-
Please register or login to post a comment