Toggle navigation
Toggle navigation
This project
Loading...
Sign in
ops
/
monitor-service
·
Commits
Go to a project
GitLab
Go to group
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
0
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Email Patches
Plain Diff
Browse Files
Authored by
zhengyouwei
8 years ago
Commit
a48fc331cd5a52ab47a05013c5b05875d0049176
1 parent
a1b6f196
nginx switch
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
71 additions
and
68 deletions
monitor-service-switch/src/main/java/com/monitor/switchs/NginxSwitchCtrl.java
monitor-service-switch/src/main/java/com/monitor/switchs/NginxSwitchCtrl.java
View file @
a48fc33
...
...
@@ -5,16 +5,11 @@ 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
;
...
...
@@ -26,10 +21,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.*
;
import
java.util.*
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.stream.Collectors
;
/**
* @author zhengyouwei 2016年5月12日 下午1:49:55
...
...
@@ -67,14 +60,14 @@ public class NginxSwitchCtrl {
if
(
CollectionUtils
.
isEmpty
(
awsNginxHosts
))
{
map
.
put
(
"awsList"
,
Lists
.
newArrayList
());
}
else
{
List
<
Map
<
String
,
Object
>>
awsList
=
analyseConfigFile
(
exe
(
"
view"
,
awsNginxHosts
.
get
(
0
).
getHostIp
(),
null
));
List
<
Map
<
String
,
Object
>>
awsList
=
analyseConfigFile
(
exe
(
"
-eview -n"
+
awsNginxHosts
.
get
(
0
).
getHostIp
()
));
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
));
List
<
Map
<
String
,
Object
>>
qcloudList
=
analyseConfigFile
(
exe
(
"
-eview -n"
+
qcloudNginxHosts
.
get
(
0
).
getHostIp
()
));
map
.
put
(
"qcloudList"
,
qcloudList
);
}
...
...
@@ -89,34 +82,34 @@ public class NginxSwitchCtrl {
*/
@RequestMapping
(
value
=
"viewToChangeNginxConf"
)
@ResponseBody
public
BaseResponse
viewToChangeNginxConf
(
String
cloudName
,
String
target
,
String
ips
,
String
onlineOrGray
,
HttpServletRequest
httpRequest
)
{
public
BaseResponse
viewToChangeNginxConf
(
String
cloudName
,
String
target
,
String
ips
,
String
onlineOrGray
)
{
//切线上 灰度ips不能为空
if
(
StringUtils
.
isBlank
(
ips
)
||
"null"
.
equals
(
ips
)){
List
<
HostInfo
>
hosts
;
if
(
"aws"
.
equals
(
cloudName
))
{
hosts
=
hostInfoMapper
.
selectHostInfosByTagAndCloudType
(
"upstream-switch"
,
1
);
}
else
{
hosts
=
hostInfoMapper
.
selectHostInfosByTagAndCloudType
(
"upstream-switch"
,
2
);
}
List
<
Map
<
String
,
Object
>>
mapList
=
analyseConfigFile
(
exe
(
"view"
,
hosts
.
get
(
0
).
getHostIp
(),
null
));
JSONArray
jsonArray
=
new
JSONArray
();
for
(
Map
<
String
,
Object
>
apiMap
:
mapList
)
{
if
(
StringUtils
.
equals
(
"online"
,
onlineOrGray
))
{
if
(
StringUtils
.
equals
(
"grayapigateway"
,
String
.
valueOf
(
apiMap
.
get
(
"name"
))))
{
List
<
String
>
serverList
=
(
List
<
String
>)
apiMap
.
get
(
"server"
);
jsonArray
.
addAll
(
serverList
.
stream
().
map
(
server
->
server
.
split
(
":"
)[
0
]).
collect
(
Collectors
.
toList
()));
}
}
else
{
if
(
StringUtils
.
equals
(
"apigateway"
,
String
.
valueOf
(
apiMap
.
get
(
"name"
))))
{
List
<
String
>
serverList
=
(
List
<
String
>)
apiMap
.
get
(
"server"
);
jsonArray
.
addAll
(
serverList
.
stream
().
map
(
server
->
server
.
split
(
":"
)[
0
]).
collect
(
Collectors
.
toList
()));
}
}
}
ips
=
jsonArray
.
toJSONString
();
}
// //切线上 灰度ips不能为空
// if(StringUtils.isBlank(ips) || "null".equals(ips)){
// List<HostInfo> hosts;
// if ("aws".equals(cloudName)) {
// hosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 1);
// } else {
// hosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 2);
// }
// List<Map<String, Object>> mapList = analyseConfigFile(exe("view", hosts.get(0).getHostIp(), null));
// JSONArray jsonArray = new JSONArray();
// for (Map<String, Object> apiMap : mapList) {
// if (StringUtils.equals("online", onlineOrGray)) {
// if (StringUtils.equals("grayapigateway", String.valueOf(apiMap.get("name")))) {
// List<String> serverList = (List<String>) apiMap.get("server");
// jsonArray.addAll(serverList.stream().map(server -> server.split(":")[0]).collect(Collectors.toList()));
// }
// }else {
// if (StringUtils.equals("apigateway", String.valueOf(apiMap.get("name")))) {
// List<String> serverList = (List<String>) apiMap.get("server");
// jsonArray.addAll(serverList.stream().map(server -> server.split(":")[0]).collect(Collectors.toList()));
// }
// }
// }
// ips = jsonArray.toJSONString();
//
// }
//线上切换
String
tag
=
StringUtils
.
equals
(
"gray"
,
target
)
?
"灰度"
:
"gateway"
;
...
...
@@ -130,39 +123,53 @@ public class NginxSwitchCtrl {
return
new
BaseResponse
(
500
,
cloudName
+
"在cmdb中未找到对应"
+
tag
+
"的服务器!"
);
}
Iterator
<
HostInfo
>
infoIterator
=
gatewayHosts
.
iterator
();
while
(
infoIterator
.
hasNext
())
{
while
(
infoIterator
.
hasNext
())
{
HostInfo
hostInfo
=
infoIterator
.
next
();
if
(
"gateway"
.
equals
(
tag
)){
if
(
hostInfo
.
getTags
().
indexOf
(
"灰度"
)
!=
-
1
){
if
(
"gateway"
.
equals
(
tag
))
{
if
(
hostInfo
.
getTags
().
indexOf
(
"灰度"
)
!=
-
1
)
{
infoIterator
.
remove
();
}
}
else
if
(
hostInfo
.
getTags
().
indexOf
(
"gateway"
)
==
-
1
)
{
}
else
if
(
hostInfo
.
getTags
().
indexOf
(
"gateway"
)
==
-
1
)
{
infoIterator
.
remove
();
}
}
//模板参数对象
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
);
StringBuilder
commond
=
new
StringBuilder
();
commond
.
append
(
" -elocalSwitch"
);
commond
.
append
(
" -s"
+
cloudName
);
commond
.
append
(
" -o"
+
onlineOrGray
);
commond
.
append
(
" -c"
);
for
(
HostInfo
hostInfo
:
gatewayHosts
)
{
commond
.
append
(
"server\\\\t"
+
hostInfo
.
getHostIp
()
+
":8080\\\\tmax_fails=5\\\\tfail_timeout=3s\\;\\\\n"
);
}
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
);
exe
(
commond
.
toString
());
String
result
=
exe
(
"-eviewLocalSwitch -s"
+
cloudName
);
// //模板参数对象
// 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
);
...
...
@@ -192,7 +199,7 @@ public class NginxSwitchCtrl {
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
));
return
new
BaseResponse
(
200
,
"success"
,
exe
(
"
-eswitch -n"
+
StringUtils
.
join
(
ips
,
","
)
+
" -s"
+
cloudName
));
}
/**
...
...
@@ -211,7 +218,7 @@ public class NginxSwitchCtrl {
hosts
=
hostInfoMapper
.
selectHostInfosByTagAndCloudType
(
"upstream-switch"
,
2
);
}
return
new
BaseResponse
(
200
,
"SUCCESS"
,
exe
(
"
view"
,
hosts
.
get
(
0
).
getHostIp
(),
null
));
return
new
BaseResponse
(
200
,
"SUCCESS"
,
exe
(
"
-eview -n"
+
hosts
.
get
(
0
).
getHostIp
()
));
}
...
...
@@ -220,14 +227,10 @@ public class NginxSwitchCtrl {
* 执行命令
*
* @param commod
* @param host
* @param templatePath
* @return 2016年5月17日下午3:26:29
*/
public
String
exe
(
String
commod
,
String
host
,
String
templatePath
)
{
StringBuilder
commond
=
new
StringBuilder
();
commond
.
append
(
" "
+
commod
).
append
(
" "
+
host
).
append
(
" "
+
templatePath
);
return
CommodUtil
.
exe
(
CommodUtil
.
NGINX_SWITCH_SH
,
commond
.
toString
());
public
String
exe
(
String
commod
)
{
return
CommodUtil
.
exe
(
CommodUtil
.
NGINX_SWITCH_SH
,
commod
);
}
private
List
<
Map
<
String
,
Object
>>
analyseConfigFile
(
String
rlt
)
{
...
...
Please
register
or
login
to post a comment