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
qinchao
7 years ago
Commit
57adc6eb67a554810b07de8d1d15fadf234de542
1 parent
26d02fa4
双中心切换
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
125 additions
and
78 deletions
monitor-service-model/src/main/java/com/monitor/model/domain/CenterSwitchModel.java
monitor-service-switch/src/main/java/com/monitor/switchs/centerSwitch/ctrl/CenterSwitchCtrl.java
monitor-service-switch/src/main/java/com/monitor/switchs/centerSwitch/service/SwitchService.java
monitor-service-model/src/main/java/com/monitor/model/domain/CenterSwitchModel.java
View file @
57adc6e
...
...
@@ -21,12 +21,13 @@ public class CenterSwitchModel {
private
String
cName
;
//目标云可用区
/**
* toAws :aws云不再用了,去掉了
* 或
* toQcloud
* toQcloud
AZ1
* 或者
* toQcloudaz2
* toQcloudaz2
AZ2
*/
private
String
exe
;
...
...
monitor-service-switch/src/main/java/com/monitor/switchs/centerSwitch/ctrl/CenterSwitchCtrl.java
View file @
57adc6e
...
...
@@ -7,7 +7,6 @@ import com.monitor.switchs.centerSwitch.service.SwitchService;
import
com.yoho.ops.cmdb.qcloud.util.QcloudDnspotUtil
;
import
org.apache.commons.lang3.StringUtils
;
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
;
...
...
@@ -15,6 +14,10 @@ import org.springframework.web.bind.annotation.RestController;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.UUID
;
import
java.util.concurrent.CopyOnWriteArraySet
;
import
java.util.concurrent.atomic.AtomicInteger
;
/**
* Created by zhengyouwei on 2016/10/24.
...
...
@@ -22,6 +25,8 @@ import java.util.List;
@RestController
@RequestMapping
(
value
=
"/centerSwitch"
)
public
class
CenterSwitchCtrl
{
private
static
Set
<
String
>
SWITCH_TOKEN_SET
=
new
CopyOnWriteArraySet
<>();
@Autowired
private
SwitchService
switchService
;
...
...
@@ -31,6 +36,99 @@ public class CenterSwitchCtrl {
//@Value("${SWITCH_DB_URL}")
private
String
SWITCH_DB_URL
=
"http://10.66.4.28:8011/switchdb/switchMysql"
;
@RequestMapping
(
"/switchWithAnsible"
)
@ResponseBody
public
BaseResponse
switchWithAnsible
(
@RequestBody
CenterSwitchModel
centerSwitchModel
){
String
token
=
centerSwitchModel
.
getTokenID
();
boolean
pass
=
false
;
if
(
StringUtils
.
isNotBlank
(
token
)&&
SWITCH_TOKEN_SET
.
contains
(
token
)){
pass
=
true
;
SWITCH_TOKEN_SET
.
remove
(
token
);
}
if
(!
pass
){
return
new
BaseResponse
(
201
,
"access deny :校验不通过"
);
}
//开始调用
String
switchResult
=
switchService
.
switchWithAnsible
(
centerSwitchModel
.
getExe
());
BaseResponse
rtn
=
new
BaseResponse
();
rtn
.
setData
(
switchResult
);
return
rtn
;
}
@RequestMapping
(
"/doExeAndCheckBefore"
)
@ResponseBody
public
BaseResponse
doExeAndCheckBefore
()
{
/* String smsCode=httpRestClientService.postForObject(SWITCH_DB_URL+"/getSmsCode",null , String.class);
if(StringUtils.isBlank(smsCode)){
return new BaseResponse(201,"错误:获取的短信校验码为空");
}
switchService.sendSmsCode(smsCode);*/
switchService
.
sendSmsCode
();
return
new
BaseResponse
();
}
@RequestMapping
(
"/checkSmsCode"
)
@ResponseBody
public
BaseResponse
checkSmsCode
(
String
code
)
{
/* String token=httpRestClientService.postForObject(SWITCH_DB_URL+"/checkSmsCode?code="+code,null , String.class);
if(StringUtils.isBlank(token)||"false".equals(token.toLowerCase())){
return new BaseResponse(201,"验证码校验失败");
}else{
if (token.length()!=32){
return new BaseResponse(201,"验证码校验失败,返回token错误");
}
BaseResponse rtn= new BaseResponse();
rtn.setData(token);
return rtn;
}*/
boolean
result
=
switchService
.
checkCode
(
code
);
if
(
result
){
//产生一个token
String
token
=
UUID
.
randomUUID
().
toString
().
replaceAll
(
"-"
,
""
);
SWITCH_TOKEN_SET
.
add
(
token
);
BaseResponse
rtn
=
new
BaseResponse
();
rtn
.
setData
(
token
);
return
rtn
;
}
else
{
return
new
BaseResponse
(
201
,
"验证码校验失败"
);
}
}
/**
* 只是用来查看状态
* @param centerSwitchModel
* @return
*/
@RequestMapping
(
"/checkStatus"
)
@ResponseBody
public
BaseResponse
checkStatus
(
@RequestBody
CenterSwitchModel
centerSwitchModel
)
{
BaseResponse
baseResponse
=
new
BaseResponse
();
if
(!
"status"
.
equals
(
centerSwitchModel
.
getExe
())){
baseResponse
.
setCode
(
500
);
baseResponse
.
setMessage
(
"当前接口只用来查看状态......"
);
return
baseResponse
;
}
//以防万一
centerSwitchModel
.
setExe
(
"status"
);
String
result
=
""
;
if
(
centerSwitchModel
.
getName
().
toLowerCase
().
equals
(
"switch_mysql"
)){
//mysql的状态从另外一个sprigboot项目获取
result
=
httpRestClientService
.
postForObject
(
SWITCH_DB_URL
+
"/checkStatus"
,
null
,
String
.
class
);
}
else
if
(
centerSwitchModel
.
getName
().
toLowerCase
().
equals
(
"switch_dnspod"
)){
//dnspot调用腾讯云api
result
=
QcloudDnspotUtil
.
recordList
();
}
else
{
result
=
switchService
.
doCommond
(
centerSwitchModel
);
}
baseResponse
.
setData
(
result
==
null
?
""
:
result
);
return
baseResponse
;
}
@RequestMapping
(
"/getSwitchList"
)
@ResponseBody
public
BaseResponse
getSwitchList
()
{
...
...
@@ -102,6 +200,7 @@ public class CenterSwitchCtrl {
return
mysqlResponse
;
}
@RequestMapping
(
"/doExeAndCheck"
)
@ResponseBody
public
BaseResponse
doExeAndCheck
(
@RequestBody
CenterSwitchModel
centerSwitchModel
)
{
...
...
@@ -149,72 +248,6 @@ public class CenterSwitchCtrl {
return
baseResponse
;
}
@RequestMapping
(
"/doExeAndCheckBefore"
)
@ResponseBody
public
BaseResponse
doExeAndCheckBefore
()
{
String
smsCode
=
httpRestClientService
.
postForObject
(
SWITCH_DB_URL
+
"/getSmsCode"
,
null
,
String
.
class
);
if
(
StringUtils
.
isBlank
(
smsCode
)){
return
new
BaseResponse
(
201
,
"错误:获取的短信校验码为空"
);
}
switchService
.
sendSmsCode
(
smsCode
);
//switchService.sendSmsCode();
return
new
BaseResponse
();
}
@RequestMapping
(
"/checkSmsCode"
)
@ResponseBody
public
BaseResponse
checkSmsCode
(
String
code
)
{
String
token
=
httpRestClientService
.
postForObject
(
SWITCH_DB_URL
+
"/checkSmsCode?code="
+
code
,
null
,
String
.
class
);
if
(
StringUtils
.
isBlank
(
token
)||
"false"
.
equals
(
token
.
toLowerCase
())){
return
new
BaseResponse
(
201
,
"验证码校验失败"
);
}
else
{
if
(
token
.
length
()!=
32
){
return
new
BaseResponse
(
201
,
"验证码校验失败,返回token错误"
);
}
BaseResponse
rtn
=
new
BaseResponse
();
rtn
.
setData
(
token
);
return
rtn
;
}
/*boolean result=switchService.checkCode(code);
if(result){
return new BaseResponse();
}else{
return new BaseResponse(201,"验证码校验失败");
}*/
//return new BaseResponse();
}
/**
* 只是用来查看状态
* @param centerSwitchModel
* @return
*/
@RequestMapping
(
"/checkStatus"
)
@ResponseBody
public
BaseResponse
checkStatus
(
@RequestBody
CenterSwitchModel
centerSwitchModel
)
{
BaseResponse
baseResponse
=
new
BaseResponse
();
if
(!
"status"
.
equals
(
centerSwitchModel
.
getExe
())){
baseResponse
.
setCode
(
500
);
baseResponse
.
setMessage
(
"当前接口只用来查看状态......"
);
return
baseResponse
;
}
//以防万一
centerSwitchModel
.
setExe
(
"status"
);
String
result
=
""
;
if
(
centerSwitchModel
.
getName
().
toLowerCase
().
equals
(
"switch_mysql"
)){
//mysql的状态从另外一个sprigboot项目获取
result
=
httpRestClientService
.
postForObject
(
SWITCH_DB_URL
+
"/checkStatus"
,
null
,
String
.
class
);
}
else
if
(
centerSwitchModel
.
getName
().
toLowerCase
().
equals
(
"switch_dnspod"
)){
//dnspot调用腾讯云api
result
=
QcloudDnspotUtil
.
recordList
();
}
else
{
result
=
switchService
.
doCommond
(
centerSwitchModel
);
}
baseResponse
.
setData
(
result
==
null
?
""
:
result
);
return
baseResponse
;
}
/**
* 获取defalutDns
...
...
monitor-service-switch/src/main/java/com/monitor/switchs/centerSwitch/service/SwitchService.java
View file @
57adc6e
...
...
@@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
import
java.util.concurrent.atomic.AtomicInteger
;
/**
* Created by zhengyouwei on 2017/3/9.
...
...
@@ -31,10 +32,22 @@ public class SwitchService {
@Autowired
private
GetUsersInfoUtil
getUsersInfoUtil
;
private
static
Set
<
String
>
set
=
new
HashSet
<>();
private
static
Set
<
String
>
smsCodeSet
=
new
HashSet
<>();
private
static
AtomicInteger
count
=
new
AtomicInteger
(
0
);
private
static
Random
random
=
new
Random
();
private
final
String
ansible_switch_py
=
"ssh master@10.66.202.46 /usr/bin/python /home/master/qcloud-switch/one_key_switch.py --zone="
;
public
String
switchWithAnsible
(
String
toCloud
){
//String logId = "switch_idc_py_"+count.incrementAndGet();
String
command
=
ansible_switch_py
+
toCloud
;
String
result
=
CommodUtil
.
exe
(
command
,
"\r\n"
);
return
result
;
}
public
String
doCommond
(
CenterSwitchModel
centerSwitchModel
)
{
try
{
...
...
@@ -71,7 +84,7 @@ public class SwitchService {
public
void
sendSmsCode
()
{
String
code
=
StringUtils
.
leftPad
(
Integer
.
toString
(
random
.
nextInt
(
9999
)),
4
,
"0"
);
set
.
add
(
code
);
s
msCodeS
et
.
add
(
code
);
String
mobile_yunwei_switch_center
=
getUsersInfoUtil
.
getMobileByAlarmGroup
(
AlarmGroupContants
.
GROUP_NAME_YUNWEI_SWITCH_CENTER
);
if
(
StringUtils
.
isBlank
(
mobile_yunwei_switch_center
)){
mobile_yunwei_switch_center
=
"18751986615"
;
...
...
@@ -81,8 +94,8 @@ public class SwitchService {
new
Thread
(()
->
{
try
{
Thread
.
sleep
(
1000
*
60
);
if
(
set
.
contains
(
code
)){
set
.
remove
(
code
);
if
(
smsCodeSet
.
contains
(
code
)){
smsCodeSet
.
remove
(
code
);
}
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
...
...
@@ -91,21 +104,21 @@ public class SwitchService {
}).
start
();
}
/*
*
/*
* 从switchdb系统获取验证码,然后发送
* @param msgCode
*/
public
void
sendSmsCode
(
String
msgCode
)
{
/*
public void sendSmsCode(String msgCode) {
String mobile_yunwei_switch_center = getUsersInfoUtil.getMobileByAlarmGroup(AlarmGroupContants.GROUP_NAME_YUNWEI_SWITCH_CENTER);
if(StringUtils.isBlank(mobile_yunwei_switch_center)){
mobile_yunwei_switch_center="18751986615";
}
alarmMsgService.sendSms("switch_mobile", "即将进行双中心切换,请输入以下数字:" + msgCode+"。一分钟内有效", mobile_yunwei_switch_center);
}
}
*/
public
boolean
checkCode
(
String
code
)
{
if
(
set
.
contains
(
code
)){
set
.
remove
(
code
);
if
(
smsCodeSet
.
contains
(
code
)){
smsCodeSet
.
remove
(
code
);
return
true
;
}
return
false
;
...
...
Please
register
or
login
to post a comment