Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
7b11e81f
"vscode:/vscode.git/clone" did not exist on "3a1ff5ce32e1ce7342178ba1d5cf298ce1de18ee"
Commit
7b11e81f
authored
Nov 19, 2019
by
zhanghouying
Browse files
添加运维系统
parent
f81b4fd3
Changes
58
Show whitespace changes
Inline
Side-by-side
eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/DeployHistoryServiceImpl.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.service.impl
;
import
cn.hutool.core.util.IdUtil
;
import
me.zhengjie.modules.mnt.domain.DeployHistory
;
import
me.zhengjie.modules.mnt.repository.DeployHistoryRepository
;
import
me.zhengjie.modules.mnt.service.DeployHistoryService
;
import
me.zhengjie.modules.mnt.service.dto.DeployHistoryDTO
;
import
me.zhengjie.modules.mnt.service.dto.DeployHistoryQueryCriteria
;
import
me.zhengjie.modules.mnt.service.mapper.DeployHistoryMapper
;
import
me.zhengjie.utils.PageUtil
;
import
me.zhengjie.utils.QueryHelp
;
import
me.zhengjie.utils.ValidationUtil
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Propagation
;
import
org.springframework.transaction.annotation.Transactional
;
import
java.util.Optional
;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Service
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
,
rollbackFor
=
Exception
.
class
)
public
class
DeployHistoryServiceImpl
implements
DeployHistoryService
{
@Autowired
private
DeployHistoryRepository
deployhistoryRepository
;
@Autowired
private
DeployHistoryMapper
deployhistoryMapper
;
@Override
public
Object
queryAll
(
DeployHistoryQueryCriteria
criteria
,
Pageable
pageable
){
Page
<
DeployHistory
>
page
=
deployhistoryRepository
.
findAll
((
root
,
criteriaQuery
,
criteriaBuilder
)
->
QueryHelp
.
getPredicate
(
root
,
criteria
,
criteriaBuilder
),
pageable
);
return
PageUtil
.
toPage
(
page
.
map
(
deployhistoryMapper:
:
toDto
));
}
@Override
public
Object
queryAll
(
DeployHistoryQueryCriteria
criteria
){
return
deployhistoryMapper
.
toDto
(
deployhistoryRepository
.
findAll
((
root
,
criteriaQuery
,
criteriaBuilder
)
->
QueryHelp
.
getPredicate
(
root
,
criteria
,
criteriaBuilder
)));
}
@Override
public
DeployHistoryDTO
findById
(
String
id
)
{
Optional
<
DeployHistory
>
deployhistory
=
deployhistoryRepository
.
findById
(
id
);
ValidationUtil
.
isNull
(
deployhistory
,
"DeployHistory"
,
"id"
,
id
);
return
deployhistoryMapper
.
toDto
(
deployhistory
.
get
());
}
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
DeployHistoryDTO
create
(
DeployHistory
resources
)
{
resources
.
setId
(
IdUtil
.
simpleUUID
());
return
deployhistoryMapper
.
toDto
(
deployhistoryRepository
.
save
(
resources
));
}
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
update
(
DeployHistory
resources
)
{
Optional
<
DeployHistory
>
optionalDeployHistory
=
deployhistoryRepository
.
findById
(
resources
.
getId
());
ValidationUtil
.
isNull
(
optionalDeployHistory
,
"DeployHistory"
,
"id"
,
resources
.
getId
());
DeployHistory
deployhistory
=
optionalDeployHistory
.
get
();
deployhistory
.
copy
(
resources
);
deployhistoryRepository
.
save
(
deployhistory
);
}
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
delete
(
String
id
)
{
deployhistoryRepository
.
deleteById
(
id
);
}
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/DeployServiceImpl.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.service.impl
;
import
cn.hutool.core.date.DatePattern
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.util.IdUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
me.zhengjie.exception.BadRequestException
;
import
me.zhengjie.modules.mnt.domain.Deploy
;
import
me.zhengjie.modules.mnt.domain.DeployHistory
;
import
me.zhengjie.modules.mnt.repository.DeployRepository
;
import
me.zhengjie.modules.mnt.service.*
;
import
me.zhengjie.modules.mnt.service.dto.*
;
import
me.zhengjie.modules.mnt.service.mapper.DeployMapper
;
import
me.zhengjie.modules.mnt.util.ExecuteShellUtil
;
import
me.zhengjie.modules.mnt.util.ScpClientUtil
;
import
me.zhengjie.modules.mnt.websocket.MsgType
;
import
me.zhengjie.modules.mnt.websocket.SocketMsg
;
import
me.zhengjie.modules.mnt.websocket.WebSocketServer
;
import
me.zhengjie.utils.PageUtil
;
import
me.zhengjie.utils.QueryHelp
;
import
me.zhengjie.utils.SecurityUtils
;
import
me.zhengjie.utils.ValidationUtil
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Propagation
;
import
org.springframework.transaction.annotation.Transactional
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Optional
;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Slf4j
@Service
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
,
rollbackFor
=
Exception
.
class
)
public
class
DeployServiceImpl
implements
DeployService
{
private
final
String
FILE_SEPARATOR
=
File
.
separatorChar
+
""
;
@Autowired
private
DeployRepository
deployRepository
;
@Autowired
private
DeployMapper
deployMapper
;
@Autowired
private
AppService
appService
;
@Autowired
private
ServerDeployService
serverDeployService
;
@Autowired
private
DeployHistoryService
deployHistoryService
;
@Autowired
private
ServerAccountService
serverAccountService
;
@Autowired
private
DatabaseService
databaseService
;
@Override
public
Object
queryAll
(
DeployQueryCriteria
criteria
,
Pageable
pageable
)
{
Page
<
Deploy
>
page
=
deployRepository
.
findAll
((
root
,
criteriaQuery
,
criteriaBuilder
)
->
QueryHelp
.
getPredicate
(
root
,
criteria
,
criteriaBuilder
),
pageable
);
return
PageUtil
.
toPage
(
page
.
map
(
deployMapper:
:
toDto
));
}
@Override
public
List
<
DeployDTO
>
queryAll
(
DeployQueryCriteria
criteria
)
{
return
deployMapper
.
toDto
(
deployRepository
.
findAll
((
root
,
criteriaQuery
,
criteriaBuilder
)
->
QueryHelp
.
getPredicate
(
root
,
criteria
,
criteriaBuilder
)));
}
@Override
public
DeployDTO
findById
(
String
id
)
{
Optional
<
Deploy
>
Deploy
=
deployRepository
.
findById
(
id
);
ValidationUtil
.
isNull
(
Deploy
,
"Deploy"
,
"id"
,
id
);
return
deployMapper
.
toDto
(
Deploy
.
get
());
}
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
DeployDTO
create
(
Deploy
resources
)
{
resources
.
setId
(
IdUtil
.
simpleUUID
());
return
deployMapper
.
toDto
(
deployRepository
.
save
(
resources
));
}
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
update
(
Deploy
resources
)
{
Optional
<
Deploy
>
optionalDeploy
=
deployRepository
.
findById
(
resources
.
getId
());
ValidationUtil
.
isNull
(
optionalDeploy
,
"Deploy"
,
"id"
,
resources
.
getId
());
Deploy
Deploy
=
optionalDeploy
.
get
();
Deploy
.
copy
(
resources
);
deployRepository
.
save
(
Deploy
);
}
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
delete
(
String
id
)
{
deployRepository
.
deleteById
(
id
);
}
@Override
public
String
deploy
(
String
fileSavePath
,
String
id
)
{
return
deployApp
(
fileSavePath
,
id
);
}
/**
* @param fileSavePath 本机路径
* @param id
* @return
*/
private
String
deployApp
(
String
fileSavePath
,
String
id
)
{
DeployDTO
deploy
=
findById
(
id
);
if
(
deploy
==
null
)
{
sendMsg
(
"部署信息不存在"
,
MsgType
.
ERROR
);
throw
new
BadRequestException
(
"部署信息不存在"
);
}
AppDTO
app
=
appService
.
findById
(
deploy
.
getAppId
());
if
(
app
==
null
)
{
sendMsg
(
"包对应应用信息不存在"
,
MsgType
.
ERROR
);
throw
new
BadRequestException
(
"包对应应用信息不存在"
);
}
int
port
=
app
.
getPort
();
//这个是服务器部署路径
String
uploadPath
=
app
.
getUploadPath
();
StringBuilder
sb
=
new
StringBuilder
();
String
msg
=
""
;
String
ip
=
deploy
.
getIp
();
ExecuteShellUtil
executeShellUtil
=
getExecuteShellUtil
(
ip
);
//判断是否第一次部署
boolean
flag
=
checkFile
(
executeShellUtil
,
app
);
//第一步要确认服务器上有这个目录
executeShellUtil
.
execute
(
"mkdir -p "
+
uploadPath
);
//上传文件
msg
=
String
.
format
(
"登陆到服务器:%s"
,
ip
);
ScpClientUtil
scpClientUtil
=
getScpClientUtil
(
ip
);
log
.
info
(
msg
);
sendMsg
(
msg
,
MsgType
.
INFO
);
msg
=
String
.
format
(
"上传文件到服务器:%s<br>目录:%s下"
,
fileSavePath
,
ip
,
uploadPath
);
sendMsg
(
msg
,
MsgType
.
INFO
);
scpClientUtil
.
putFile
(
fileSavePath
,
uploadPath
);
if
(
flag
)
{
sendMsg
(
"停止原来应用"
,
MsgType
.
INFO
);
//停止应用
stopApp
(
port
,
executeShellUtil
);
sendMsg
(
"备份原来应用"
,
MsgType
.
INFO
);
//备份应用
backupApp
(
executeShellUtil
,
ip
,
app
.
getDeployPath
(),
app
.
getName
(),
app
.
getBackupPath
(),
id
);
}
sendMsg
(
"部署应用"
,
MsgType
.
INFO
);
//部署文件,并启动应用
String
deployScript
=
app
.
getDeployScript
();
executeShellUtil
.
execute
(
deployScript
);
sendMsg
(
"启动应用"
,
MsgType
.
INFO
);
String
startScript
=
app
.
getStartScript
();
executeShellUtil
.
execute
(
startScript
);
//只有过5秒才能知道到底是不是启动成功了。
sleep
(
5
);
boolean
result
=
checkIsRunningStatus
(
port
,
executeShellUtil
);
sb
.
append
(
"服务器:"
).
append
(
ip
).
append
(
"<br>应用:"
).
append
(
app
.
getName
());
sendResultMsg
(
result
,
sb
);
return
"部署结束"
;
}
private
void
sleep
(
int
second
)
{
try
{
Thread
.
sleep
(
second
*
1000
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
private
void
backupApp
(
ExecuteShellUtil
executeShellUtil
,
String
ip
,
String
fileSavePath
,
String
appName
,
String
backupPath
,
String
id
)
{
String
deployDate
=
DateUtil
.
format
(
new
Date
(),
DatePattern
.
PURE_DATETIME_PATTERN
);
StringBuilder
sb
=
new
StringBuilder
();
if
(!
backupPath
.
endsWith
(
FILE_SEPARATOR
))
{
backupPath
+=
FILE_SEPARATOR
;
}
backupPath
+=
appName
+
FILE_SEPARATOR
+
deployDate
+
"\n"
;
sb
.
append
(
"mkdir -p "
).
append
(
backupPath
);
sb
.
append
(
"mv -f "
).
append
(
fileSavePath
);
if
(!
fileSavePath
.
endsWith
(
FILE_SEPARATOR
))
{
sb
.
append
(
FILE_SEPARATOR
);
}
sb
.
append
(
appName
).
append
(
" "
).
append
(
backupPath
);
log
.
info
(
"备份应用脚本:"
+
sb
.
toString
());
executeShellUtil
.
execute
(
sb
.
toString
());
//还原信息入库
DeployHistory
deployHistory
=
new
DeployHistory
();
deployHistory
.
setAppName
(
appName
);
deployHistory
.
setDeployDate
(
deployDate
);
deployHistory
.
setDeployUser
(
SecurityUtils
.
getUsername
());
deployHistory
.
setIp
(
ip
);
deployHistory
.
setDeployId
(
id
);
deployHistoryService
.
create
(
deployHistory
);
}
/**
* 停App
*
* @param port
* @param executeShellUtil
* @return
*/
private
void
stopApp
(
int
port
,
ExecuteShellUtil
executeShellUtil
)
{
//发送停止命令
executeShellUtil
.
execute
(
String
.
format
(
"lsof -i :%d|grep -v \"PID\"|awk '{print \"kill -9\",$2}'|sh"
,
port
));
}
/**
* 指定端口程序是否在运行
*
* @param port
* @param executeShellUtil
* @return true 正在运行 false 已经停止
*/
private
boolean
checkIsRunningStatus
(
int
port
,
ExecuteShellUtil
executeShellUtil
)
{
String
statusResult
=
executeShellUtil
.
executeForResult
(
String
.
format
(
"fuser -n tcp %d"
,
port
));
if
(
""
.
equals
(
statusResult
.
trim
()))
{
return
false
;
}
else
{
return
true
;
}
}
private
void
sendMsg
(
String
msg
,
MsgType
msgType
)
{
try
{
WebSocketServer
.
sendInfo
(
new
SocketMsg
(
msg
,
msgType
),
"deploy"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
@Override
public
String
serverStatus
(
Deploy
resources
)
{
String
ip
=
resources
.
getIp
();
String
appId
=
resources
.
getAppId
();
AppDTO
app
=
appService
.
findById
(
appId
);
StringBuffer
sb
=
new
StringBuffer
();
ExecuteShellUtil
executeShellUtil
=
getExecuteShellUtil
(
ip
);
sb
.
append
(
"服务器:"
).
append
(
ip
).
append
(
"<br>应用:"
).
append
(
app
.
getName
());
boolean
result
=
checkIsRunningStatus
(
app
.
getPort
(),
executeShellUtil
);
if
(
result
)
{
sb
.
append
(
"<br>正在运行"
);
sendMsg
(
sb
.
toString
(),
MsgType
.
INFO
);
}
else
{
sb
.
append
(
"<br>已停止!"
);
sendMsg
(
sb
.
toString
(),
MsgType
.
ERROR
);
}
log
.
info
(
sb
.
toString
());
return
"执行完毕"
;
}
private
boolean
checkFile
(
ExecuteShellUtil
executeShellUtil
,
AppDTO
appDTO
)
{
StringBuffer
sb
=
new
StringBuffer
();
sb
.
append
(
"find "
);
sb
.
append
(
appDTO
.
getDeployPath
());
sb
.
append
(
" -name "
);
sb
.
append
(
appDTO
.
getName
());
boolean
flag
=
executeShellUtil
.
executeShell
(
sb
.
toString
());
return
flag
;
}
/**
* 启动服务
*
* @param resources
* @return
*/
@Override
public
String
startServer
(
Deploy
resources
)
{
String
ip
=
resources
.
getIp
();
String
appId
=
resources
.
getAppId
();
AppDTO
app
=
appService
.
findById
(
appId
);
StringBuilder
sb
=
new
StringBuilder
();
ExecuteShellUtil
executeShellUtil
=
getExecuteShellUtil
(
ip
);
//为了防止重复启动,这里先停止应用
stopApp
(
app
.
getPort
(),
executeShellUtil
);
sb
.
append
(
"服务器:"
).
append
(
ip
).
append
(
"<br>应用:"
).
append
(
app
.
getName
());
sendMsg
(
"下发启动命令"
,
MsgType
.
INFO
);
executeShellUtil
.
execute
(
app
.
getStartScript
());
//停止3秒,防止应用没有启动完成
sleep
(
3
);
boolean
result
=
checkIsRunningStatus
(
app
.
getPort
(),
executeShellUtil
);
sendResultMsg
(
result
,
sb
);
log
.
info
(
sb
.
toString
());
return
"执行完毕"
;
}
;
/**
* 停止服务
*
* @param resources
* @return
*/
@Override
public
String
stopServer
(
Deploy
resources
)
{
String
ip
=
resources
.
getIp
();
String
appId
=
resources
.
getAppId
();
AppDTO
app
=
appService
.
findById
(
appId
);
StringBuffer
sb
=
new
StringBuffer
();
ExecuteShellUtil
executeShellUtil
=
getExecuteShellUtil
(
ip
);
sb
.
append
(
"服务器:"
).
append
(
ip
).
append
(
"<br>应用:"
).
append
(
app
.
getName
());
sendMsg
(
"下发停止命令"
,
MsgType
.
INFO
);
//停止应用
stopApp
(
app
.
getPort
(),
executeShellUtil
);
sleep
(
1
);
boolean
result
=
checkIsRunningStatus
(
app
.
getPort
(),
executeShellUtil
);
if
(
result
)
{
sb
.
append
(
"<br>关闭失败!"
);
sendMsg
(
sb
.
toString
(),
MsgType
.
ERROR
);
}
else
{
sb
.
append
(
"<br>关闭成功!"
);
sendMsg
(
sb
.
toString
(),
MsgType
.
INFO
);
}
log
.
info
(
sb
.
toString
());
return
"执行完毕"
;
}
;
@Override
public
String
serverReduction
(
DeployHistory
resources
)
{
String
deployId
=
resources
.
getDeployId
();
Deploy
deployInfo
=
deployRepository
.
findById
(
deployId
).
get
();
String
deployDate
=
resources
.
getDeployDate
();
AppDTO
app
=
appService
.
findById
(
deployInfo
.
getAppId
());
if
(
app
==
null
)
{
sendMsg
(
"应用信息不存在:"
+
resources
.
getAppName
(),
MsgType
.
ERROR
);
throw
new
BadRequestException
(
"应用信息不存在:"
+
resources
.
getAppName
());
}
String
backupPath
=
app
.
getBackupPath
();
if
(!
backupPath
.
endsWith
(
FILE_SEPARATOR
))
{
backupPath
+=
FILE_SEPARATOR
;
}
backupPath
+=
resources
.
getAppName
()
+
FILE_SEPARATOR
+
deployDate
;
//这个是服务器部署路径
String
deployPath
=
app
.
getDeployPath
();
String
ip
=
resources
.
getIp
();
ExecuteShellUtil
executeShellUtil
=
getExecuteShellUtil
(
ip
);
String
msg
=
""
;
msg
=
String
.
format
(
"登陆到服务器:%s"
,
ip
);
log
.
info
(
msg
);
sendMsg
(
msg
,
MsgType
.
INFO
);
sendMsg
(
"停止原来应用"
,
MsgType
.
INFO
);
//停止应用
stopApp
(
app
.
getPort
(),
executeShellUtil
);
//删除原来应用
sendMsg
(
"删除应用"
,
MsgType
.
INFO
);
//考虑到系统安全性,必须限制下操作目录
if
(!
deployPath
.
startsWith
(
"/opt"
))
{
throw
new
BadRequestException
(
"部署路径必须在opt目录下:"
+
deployPath
);
}
executeShellUtil
.
execute
(
"rm -rf "
+
deployPath
+
FILE_SEPARATOR
+
resources
.
getAppName
());
//还原应用
sendMsg
(
"还原应用"
,
MsgType
.
INFO
);
executeShellUtil
.
execute
(
"cp -r "
+
backupPath
+
"/. "
+
deployPath
);
sendMsg
(
"启动应用"
,
MsgType
.
INFO
);
executeShellUtil
.
execute
(
app
.
getStartScript
());
//只有过5秒才能知道到底是不是启动成功了。
sleep
(
5
);
boolean
result
=
checkIsRunningStatus
(
app
.
getPort
(),
executeShellUtil
);
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"服务器:"
).
append
(
ip
).
append
(
"<br>应用:"
).
append
(
resources
.
getAppName
());
sendResultMsg
(
result
,
sb
);
return
""
;
}
private
ExecuteShellUtil
getExecuteShellUtil
(
String
ip
)
{
ServerDeployDTO
serverDeployDTO
=
serverDeployService
.
findById
(
ip
);
if
(
serverDeployDTO
==
null
)
{
sendMsg
(
"IP对应服务器信息不存在:"
+
ip
,
MsgType
.
ERROR
);
throw
new
BadRequestException
(
"IP对应服务器信息不存在:"
+
ip
);
}
String
accountId
=
serverDeployDTO
.
getAccountId
();
ServerAccountDTO
serverAccountDTO
=
serverAccountService
.
findById
(
accountId
);
if
(
serverAccountDTO
==
null
)
{
sendMsg
(
"IP对账号信息不存在:"
+
ip
,
MsgType
.
ERROR
);
throw
new
BadRequestException
(
"IP对账号信息不存在:"
+
ip
);
}
return
new
ExecuteShellUtil
(
ip
,
serverAccountDTO
.
getAccount
(),
serverAccountDTO
.
getPassword
());
}
private
ScpClientUtil
getScpClientUtil
(
String
ip
)
{
ServerDeployDTO
serverDeployDTO
=
serverDeployService
.
findById
(
ip
);
if
(
serverDeployDTO
==
null
)
{
sendMsg
(
"IP对应服务器信息不存在:"
+
ip
,
MsgType
.
ERROR
);
throw
new
BadRequestException
(
"IP对应服务器信息不存在:"
+
ip
);
}
String
accountId
=
serverDeployDTO
.
getAccountId
();
ServerAccountDTO
serverAccountDTO
=
serverAccountService
.
findById
(
accountId
);
if
(
serverAccountDTO
==
null
)
{
sendMsg
(
"IP对账号信息不存在:"
+
ip
,
MsgType
.
ERROR
);
throw
new
BadRequestException
(
"IP对账号信息不存在:"
+
ip
);
}
return
ScpClientUtil
.
getInstance
(
ip
,
22
,
serverAccountDTO
.
getAccount
(),
serverAccountDTO
.
getPassword
());
}
public
void
sendResultMsg
(
boolean
result
,
StringBuilder
sb
)
{
if
(
result
)
{
sb
.
append
(
"<br>启动成功!"
);
sendMsg
(
sb
.
toString
(),
MsgType
.
INFO
);
}
else
{
sb
.
append
(
"<br>启动失败!"
);
sendMsg
(
sb
.
toString
(),
MsgType
.
ERROR
);
}
}
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/ServerAccountServiceImpl.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.service.impl
;
import
cn.hutool.core.util.IdUtil
;
import
me.zhengjie.modules.mnt.domain.ServerAccount
;
import
me.zhengjie.modules.mnt.repository.ServerAccountRepository
;
import
me.zhengjie.modules.mnt.service.ServerAccountService
;
import
me.zhengjie.modules.mnt.service.dto.ServerAccountDTO
;
import
me.zhengjie.modules.mnt.service.dto.ServerAccountQueryCriteria
;
import
me.zhengjie.modules.mnt.service.mapper.ServerAccountMapper
;
import
me.zhengjie.utils.PageUtil
;
import
me.zhengjie.utils.QueryHelp
;
import
me.zhengjie.utils.ValidationUtil
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Propagation
;
import
org.springframework.transaction.annotation.Transactional
;
import
java.util.Optional
;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Service
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
,
rollbackFor
=
Exception
.
class
)
public
class
ServerAccountServiceImpl
implements
ServerAccountService
{
@Autowired
private
ServerAccountRepository
serverAccountRepository
;
@Autowired
private
ServerAccountMapper
serverAccountMapper
;
@Override
public
Object
queryAll
(
ServerAccountQueryCriteria
criteria
,
Pageable
pageable
){
Page
<
ServerAccount
>
page
=
serverAccountRepository
.
findAll
((
root
,
criteriaQuery
,
criteriaBuilder
)
->
QueryHelp
.
getPredicate
(
root
,
criteria
,
criteriaBuilder
),
pageable
);
return
PageUtil
.
toPage
(
page
.
map
(
serverAccountMapper:
:
toDto
));
}
@Override
public
Object
queryAll
(
ServerAccountQueryCriteria
criteria
){
return
serverAccountMapper
.
toDto
(
serverAccountRepository
.
findAll
((
root
,
criteriaQuery
,
criteriaBuilder
)
->
QueryHelp
.
getPredicate
(
root
,
criteria
,
criteriaBuilder
)));
}
@Override
public
ServerAccountDTO
findById
(
String
id
)
{
Optional
<
ServerAccount
>
ServerAccount
=
serverAccountRepository
.
findById
(
id
);
ValidationUtil
.
isNull
(
ServerAccount
,
"ServerAccount"
,
"id"
,
id
);
return
serverAccountMapper
.
toDto
(
ServerAccount
.
get
());
}
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
ServerAccountDTO
create
(
ServerAccount
resources
)
{
resources
.
setId
(
IdUtil
.
simpleUUID
());
return
serverAccountMapper
.
toDto
(
serverAccountRepository
.
save
(
resources
));
}
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
update
(
ServerAccount
resources
)
{
Optional
<
ServerAccount
>
optionalServerAccount
=
serverAccountRepository
.
findById
(
resources
.
getId
());
ValidationUtil
.
isNull
(
optionalServerAccount
,
"ServerAccount"
,
"id"
,
resources
.
getId
());
ServerAccount
ServerAccount
=
optionalServerAccount
.
get
();
ServerAccount
.
copy
(
resources
);
serverAccountRepository
.
save
(
ServerAccount
);
}
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
delete
(
String
id
)
{
serverAccountRepository
.
deleteById
(
id
);
}
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/ServerDeployServiceImpl.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.service.impl
;
import
me.zhengjie.modules.mnt.domain.ServerDeploy
;
import
me.zhengjie.modules.mnt.repository.ServerDeployRepository
;
import
me.zhengjie.modules.mnt.service.ServerDeployService
;
import
me.zhengjie.modules.mnt.service.dto.ServerDeployDTO
;
import
me.zhengjie.modules.mnt.service.dto.ServerDeployQueryCriteria
;
import
me.zhengjie.modules.mnt.service.mapper.ServerDeployMapper
;
import
me.zhengjie.utils.PageUtil
;
import
me.zhengjie.utils.QueryHelp
;
import
me.zhengjie.utils.ValidationUtil
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Propagation
;
import
org.springframework.transaction.annotation.Transactional
;
import
java.util.Optional
;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Service
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
,
rollbackFor
=
Exception
.
class
)
public
class
ServerDeployServiceImpl
implements
ServerDeployService
{
private
ServerDeployRepository
serverDeployRepository
;
private
ServerDeployMapper
serverDeployMapper
;
public
ServerDeployServiceImpl
(
ServerDeployRepository
serverDeployRepository
,
ServerDeployMapper
serverDeployMapper
){
this
.
serverDeployRepository
=
serverDeployRepository
;
this
.
serverDeployMapper
=
serverDeployMapper
;
}
@Override
public
Object
queryAll
(
ServerDeployQueryCriteria
criteria
,
Pageable
pageable
){
Page
<
ServerDeploy
>
page
=
serverDeployRepository
.
findAll
((
root
,
criteriaQuery
,
criteriaBuilder
)
->
QueryHelp
.
getPredicate
(
root
,
criteria
,
criteriaBuilder
),
pageable
);
return
PageUtil
.
toPage
(
page
.
map
(
serverDeployMapper:
:
toDto
));
}
@Override
public
Object
queryAll
(
ServerDeployQueryCriteria
criteria
){
return
serverDeployMapper
.
toDto
(
serverDeployRepository
.
findAll
((
root
,
criteriaQuery
,
criteriaBuilder
)
->
QueryHelp
.
getPredicate
(
root
,
criteria
,
criteriaBuilder
)));
}
@Override
public
ServerDeployDTO
findById
(
String
id
)
{
Optional
<
ServerDeploy
>
server
=
serverDeployRepository
.
findById
(
id
);
ValidationUtil
.
isNull
(
server
,
"ServerDeploy"
,
"id"
,
id
);
return
serverDeployMapper
.
toDto
(
server
.
get
());
}
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
ServerDeployDTO
create
(
ServerDeploy
resources
)
{
return
serverDeployMapper
.
toDto
(
serverDeployRepository
.
save
(
resources
));
}
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
update
(
ServerDeploy
resources
)
{
Optional
<
ServerDeploy
>
optionalServer
=
serverDeployRepository
.
findById
(
resources
.
getId
());
ValidationUtil
.
isNull
(
optionalServer
,
"ServerDeploy"
,
"id"
,
resources
.
getId
());
ServerDeploy
serverDeploy
=
optionalServer
.
get
();
serverDeploy
.
copy
(
resources
);
serverDeployRepository
.
save
(
serverDeploy
);
}
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
delete
(
String
id
)
{
serverDeployRepository
.
deleteById
(
id
);
}
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/mapper/AppMapper.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.service.mapper
;
import
me.zhengjie.base.BaseMapper
;
import
me.zhengjie.modules.mnt.domain.App
;
import
me.zhengjie.modules.mnt.service.dto.AppDTO
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.ReportingPolicy
;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Mapper
(
componentModel
=
"spring"
,
uses
=
{},
unmappedTargetPolicy
=
ReportingPolicy
.
IGNORE
)
public
interface
AppMapper
extends
BaseMapper
<
AppDTO
,
App
>
{
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/mapper/DatabaseMapper.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.service.mapper
;
import
me.zhengjie.base.BaseMapper
;
import
me.zhengjie.modules.mnt.domain.Database
;
import
me.zhengjie.modules.mnt.service.dto.DatabaseDTO
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.ReportingPolicy
;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Mapper
(
componentModel
=
"spring"
,
unmappedTargetPolicy
=
ReportingPolicy
.
IGNORE
)
public
interface
DatabaseMapper
extends
BaseMapper
<
DatabaseDTO
,
Database
>
{
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/mapper/DeployHistoryMapper.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.service.mapper
;
import
me.zhengjie.base.BaseMapper
;
import
me.zhengjie.modules.mnt.domain.DeployHistory
;
import
me.zhengjie.modules.mnt.service.dto.DeployHistoryDTO
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.ReportingPolicy
;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Mapper
(
componentModel
=
"spring"
,
uses
=
{},
unmappedTargetPolicy
=
ReportingPolicy
.
IGNORE
)
public
interface
DeployHistoryMapper
extends
BaseMapper
<
DeployHistoryDTO
,
DeployHistory
>
{
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/mapper/DeployMapper.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.service.mapper
;
import
me.zhengjie.base.BaseMapper
;
import
me.zhengjie.modules.mnt.domain.Deploy
;
import
me.zhengjie.modules.mnt.service.dto.DeployDTO
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.ReportingPolicy
;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Mapper
(
componentModel
=
"spring"
,
uses
=
{},
unmappedTargetPolicy
=
ReportingPolicy
.
IGNORE
)
public
interface
DeployMapper
extends
BaseMapper
<
DeployDTO
,
Deploy
>
{
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/mapper/ServerAccountMapper.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.service.mapper
;
import
me.zhengjie.base.BaseMapper
;
import
me.zhengjie.modules.mnt.domain.ServerAccount
;
import
me.zhengjie.modules.mnt.service.dto.ServerAccountDTO
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.ReportingPolicy
;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Mapper
(
componentModel
=
"spring"
,
uses
=
{},
unmappedTargetPolicy
=
ReportingPolicy
.
IGNORE
)
public
interface
ServerAccountMapper
extends
BaseMapper
<
ServerAccountDTO
,
ServerAccount
>
{
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/mapper/ServerDeployMapper.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.service.mapper
;
import
me.zhengjie.base.BaseMapper
;
import
me.zhengjie.modules.mnt.domain.ServerDeploy
;
import
me.zhengjie.modules.mnt.service.dto.ServerDeployDTO
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.ReportingPolicy
;
/**
* @author zhanghouying
* @date 2019-08-24
*/
@Mapper
(
componentModel
=
"spring"
,
uses
=
{},
unmappedTargetPolicy
=
ReportingPolicy
.
IGNORE
)
public
interface
ServerDeployMapper
extends
BaseMapper
<
ServerDeployDTO
,
ServerDeploy
>
{
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ExecuteShellUtil.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.util
;
import
com.jcraft.jsch.*
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.BufferedReader
;
import
java.io.InputStreamReader
;
import
java.util.Vector
;
/**
* 执行shell命令
* @author: ZhangHouYing
* @date: 2019/8/10
*/
@Slf4j
public
class
ExecuteShellUtil
{
private
String
ipAddress
;
private
String
username
;
private
String
password
;
public
final
int
DEFAULT_SSH_PORT
=
22
;
private
Vector
<
String
>
stdout
;
public
ExecuteShellUtil
(
final
String
ipAddress
,
final
String
username
,
final
String
password
)
{
this
.
ipAddress
=
ipAddress
;
this
.
username
=
username
;
this
.
password
=
password
;
stdout
=
new
Vector
<
String
>();
}
public
int
execute
(
final
String
command
)
{
int
returnCode
=
0
;
JSch
jsch
=
new
JSch
();
try
{
Session
session
=
jsch
.
getSession
(
username
,
ipAddress
,
DEFAULT_SSH_PORT
);
session
.
setPassword
(
password
);
session
.
setConfig
(
"StrictHostKeyChecking"
,
"no"
);
session
.
connect
(
30000
);
Channel
channel
=
session
.
openChannel
(
"exec"
);
((
ChannelExec
)
channel
).
setCommand
(
command
);
channel
.
setInputStream
(
null
);
BufferedReader
input
=
new
BufferedReader
(
new
InputStreamReader
(
channel
.
getInputStream
()));
channel
.
connect
();
log
.
info
(
"The remote command is: "
+
command
);
String
line
;
while
((
line
=
input
.
readLine
())
!=
null
)
{
stdout
.
add
(
line
);
}
input
.
close
();
if
(
channel
.
isClosed
())
{
returnCode
=
channel
.
getExitStatus
();
}
channel
.
disconnect
();
session
.
disconnect
();
}
catch
(
JSchException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
returnCode
;
}
public
boolean
executeShell
(
String
command
)
{
int
result
=
execute
(
command
);
for
(
String
str
:
stdout
)
{
log
.
info
(
str
);
}
if
(
result
==
0
)
{
return
true
;
}
else
{
return
false
;
}
}
public
String
executeForResult
(
String
command
)
{
execute
(
command
);
StringBuilder
sb
=
new
StringBuilder
();
for
(
String
str
:
stdout
)
{
sb
.
append
(
str
);
log
.
info
(
str
);
}
return
sb
.
toString
();
}
/**
* 返回值有三条就代表成功了,2条是没启动,多余三条代表脚本有问题
* @param command
* @return
*/
public
int
checkAppStatus
(
String
command
)
{
execute
(
command
);
for
(
String
str
:
stdout
)
{
log
.
info
(
str
);
}
return
stdout
.
size
();
}
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ScpClientUtil.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.util
;
import
ch.ethz.ssh2.Connection
;
import
ch.ethz.ssh2.SCPClient
;
import
java.io.IOException
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
/**
* 远程执行linux命令
* @author: ZhangHouYing
* @date: 2019-08-10 10:06
*/
public
class
ScpClientUtil
{
static
private
ScpClientUtil
instance
;
static
synchronized
public
ScpClientUtil
getInstance
(
String
IP
,
int
port
,
String
username
,
String
passward
)
{
if
(
instance
==
null
)
{
instance
=
new
ScpClientUtil
(
IP
,
port
,
username
,
passward
);
}
return
instance
;
}
public
ScpClientUtil
(
String
IP
,
int
port
,
String
username
,
String
passward
)
{
this
.
ip
=
IP
;
this
.
port
=
port
;
this
.
username
=
username
;
this
.
password
=
passward
;
}
public
void
getFile
(
String
remoteFile
,
String
localTargetDirectory
)
{
Connection
conn
=
new
Connection
(
ip
,
port
);
try
{
conn
.
connect
();
boolean
isAuthenticated
=
conn
.
authenticateWithPassword
(
username
,
password
);
if
(!
isAuthenticated
)
{
System
.
err
.
println
(
"authentication failed"
);
}
SCPClient
client
=
new
SCPClient
(
conn
);
client
.
get
(
remoteFile
,
localTargetDirectory
);
}
catch
(
IOException
ex
)
{
Logger
.
getLogger
(
SCPClient
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
finally
{
conn
.
close
();
}
}
public
void
putFile
(
String
localFile
,
String
remoteTargetDirectory
)
{
putFile
(
localFile
,
null
,
remoteTargetDirectory
);
}
public
void
putFile
(
String
localFile
,
String
remoteFileName
,
String
remoteTargetDirectory
)
{
putFile
(
localFile
,
remoteFileName
,
remoteTargetDirectory
,
null
);
}
public
void
putFile
(
String
localFile
,
String
remoteFileName
,
String
remoteTargetDirectory
,
String
mode
)
{
Connection
conn
=
new
Connection
(
ip
,
port
);
try
{
conn
.
connect
();
boolean
isAuthenticated
=
conn
.
authenticateWithPassword
(
username
,
password
);
if
(!
isAuthenticated
)
{
System
.
err
.
println
(
"authentication failed"
);
}
SCPClient
client
=
new
SCPClient
(
conn
);
if
((
mode
==
null
)
||
(
mode
.
length
()
==
0
))
{
mode
=
"0600"
;
}
if
(
remoteFileName
==
null
)
{
client
.
put
(
localFile
,
remoteTargetDirectory
);
}
else
{
client
.
put
(
localFile
,
remoteFileName
,
remoteTargetDirectory
,
mode
);
}
}
catch
(
IOException
ex
)
{
Logger
.
getLogger
(
ScpClientUtil
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
finally
{
conn
.
close
();
}
}
private
String
ip
;
private
int
port
;
private
String
username
;
private
String
password
;
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ZipUtils.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.util
;
import
java.io.*
;
import
java.util.Enumeration
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipException
;
import
java.util.zip.ZipFile
;
import
java.util.zip.ZipInputStream
;
/**
* @author: ZhangHouYing
* @date: 2019-08-10 13:34
*/
public
class
ZipUtils
{
/**
* 解压文件
*
* @param zipFilePath 解压文件路径
* @param outputFolder 输出解压文件路径
*/
public
static
void
unZipIt
(
String
zipFilePath
,
String
outputFolder
)
{
byte
[]
buffer
=
new
byte
[
1024
];
File
folder
=
new
File
(
outputFolder
);
if
(!
folder
.
exists
())
{
folder
.
mkdir
();
}
try
{
//get the zip file content
ZipInputStream
zis
=
new
ZipInputStream
(
new
FileInputStream
(
zipFilePath
));
ZipEntry
ze
=
zis
.
getNextEntry
();
while
(
ze
!=
null
)
{
String
fileName
=
ze
.
getName
();
File
newFile
=
new
File
(
outputFolder
+
File
.
separator
+
fileName
);
System
.
out
.
println
(
"file unzip : "
+
newFile
.
getAbsoluteFile
());
//大部分网络上的源码,这里没有判断子目录
if
(
ze
.
isDirectory
())
{
newFile
.
mkdirs
();
}
else
{
new
File
(
newFile
.
getParent
()).
mkdirs
();
FileOutputStream
fos
=
new
FileOutputStream
(
newFile
);
int
len
;
while
((
len
=
zis
.
read
(
buffer
))
!=
-
1
)
{
fos
.
write
(
buffer
,
0
,
len
);
}
fos
.
close
();
}
ze
=
zis
.
getNextEntry
();
}
zis
.
closeEntry
();
zis
.
close
();
System
.
out
.
println
(
"Done"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
public
static
void
unzip
(
File
source
,
String
out
)
throws
IOException
{
try
(
ZipInputStream
zis
=
new
ZipInputStream
(
new
FileInputStream
(
source
)))
{
ZipEntry
entry
=
zis
.
getNextEntry
();
while
(
entry
!=
null
)
{
File
file
=
new
File
(
out
,
entry
.
getName
());
if
(
entry
.
isDirectory
())
{
file
.
mkdirs
();
}
else
{
File
parent
=
file
.
getParentFile
();
if
(!
parent
.
exists
())
{
parent
.
mkdirs
();
}
try
(
BufferedOutputStream
bos
=
new
BufferedOutputStream
(
new
FileOutputStream
(
file
)))
{
byte
[]
buffer
=
new
byte
[
Math
.
toIntExact
(
entry
.
getSize
())];
int
location
;
while
((
location
=
zis
.
read
(
buffer
))
!=
-
1
)
{
bos
.
write
(
buffer
,
0
,
location
);
}
}
}
entry
=
zis
.
getNextEntry
();
}
}
}
/**
* 把所有文件都直接解压到指定目录(忽略子文件夹)
*
* @param zipFile
* @param folderPath
* @throws ZipException
* @throws IOException
*/
public
static
void
upZipFile
(
File
zipFile
,
String
folderPath
)
throws
ZipException
,
IOException
{
File
desDir
=
new
File
(
folderPath
);
if
(!
desDir
.
exists
())
{
desDir
.
mkdirs
();
}
ZipFile
zf
=
new
ZipFile
(
zipFile
);
for
(
Enumeration
<?>
entries
=
zf
.
entries
();
entries
.
hasMoreElements
();
)
{
ZipEntry
entry
=
((
ZipEntry
)
entries
.
nextElement
());
InputStream
in
=
zf
.
getInputStream
(
entry
);
String
str
=
folderPath
;
File
desFile
=
new
File
(
str
,
java
.
net
.
URLEncoder
.
encode
(
entry
.
getName
(),
"UTF-8"
));
if
(!
desFile
.
exists
())
{
File
fileParentDir
=
desFile
.
getParentFile
();
if
(!
fileParentDir
.
exists
())
{
fileParentDir
.
mkdirs
();
}
}
OutputStream
out
=
new
FileOutputStream
(
desFile
);
byte
[]
buffer
=
new
byte
[
1024
*
1024
];
int
realLength
=
in
.
read
(
buffer
);
while
(
realLength
!=
-
1
)
{
out
.
write
(
buffer
,
0
,
realLength
);
realLength
=
in
.
read
(
buffer
);
}
out
.
close
();
in
.
close
();
}
}
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/websocket/MsgType.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.websocket
;
/**
* @author: ZhangHouYing
* @date: 2019-08-10 9:56
*/
public
enum
MsgType
{
CONNECT
,
CLOSE
,
INFO
,
ERROR
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/websocket/SocketMsg.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.websocket
;
import
lombok.Data
;
/**
* @author: ZhangHouYing
* @date: 2019-08-10 9:55
*/
@Data
public
class
SocketMsg
{
private
String
msg
;
private
MsgType
msgType
;
public
SocketMsg
(
String
msg
,
MsgType
msgType
)
{
this
.
msg
=
msg
;
this
.
msgType
=
msgType
;
}
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/websocket/WebSocketServer.java
0 → 100644
View file @
7b11e81f
package
me.zhengjie.modules.mnt.websocket
;
import
com.alibaba.fastjson.JSONObject
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
javax.websocket.*
;
import
javax.websocket.server.PathParam
;
import
javax.websocket.server.ServerEndpoint
;
import
java.io.IOException
;
import
java.util.concurrent.CopyOnWriteArraySet
;
/**
* @author: ZhangHouYing
* @date: 2019-08-10 15:46
*/
@ServerEndpoint
(
"/webSocket/{sid}"
)
@Slf4j
@Component
public
class
WebSocketServer
{
/**
* concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
*/
private
static
CopyOnWriteArraySet
<
WebSocketServer
>
webSocketSet
=
new
CopyOnWriteArraySet
<
WebSocketServer
>();
/**
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
*/
private
Session
session
;
/**
* 接收sid
*/
private
String
sid
=
""
;
/**
* 连接建立成功调用的方法
* */
@OnOpen
public
void
onOpen
(
Session
session
,
@PathParam
(
"sid"
)
String
sid
)
{
this
.
session
=
session
;
//如果存在就先删除一个,防止重复推送消息
for
(
WebSocketServer
webSocket:
webSocketSet
)
{
if
(
webSocket
.
sid
.
equals
(
sid
))
{
webSocketSet
.
remove
(
webSocket
);
}
}
webSocketSet
.
add
(
this
);
this
.
sid
=
sid
;
}
/**
* 连接关闭调用的方法
*/
@OnClose
public
void
onClose
()
{
webSocketSet
.
remove
(
this
);
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息*/
@OnMessage
public
void
onMessage
(
String
message
,
Session
session
)
{
log
.
info
(
"收到来"
+
sid
+
"的信息:"
+
message
);
//群发消息
for
(
WebSocketServer
item
:
webSocketSet
)
{
try
{
item
.
sendMessage
(
message
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
/**
*
* @param session
* @param error
*/
@OnError
public
void
onError
(
Session
session
,
Throwable
error
)
{
log
.
error
(
"发生错误"
);
error
.
printStackTrace
();
}
/**
* 实现服务器主动推送
*/
public
void
sendMessage
(
String
message
)
throws
IOException
{
this
.
session
.
getBasicRemote
().
sendText
(
message
);
}
/**
* 群发自定义消息
* */
public
static
void
sendInfo
(
SocketMsg
socketMsg
,
@PathParam
(
"sid"
)
String
sid
)
throws
IOException
{
String
message
=
JSONObject
.
toJSONString
(
socketMsg
);
log
.
info
(
"推送消息到"
+
sid
+
",推送内容:"
+
message
);
for
(
WebSocketServer
item
:
webSocketSet
)
{
try
{
//这里可以设定只推送给这个sid的,为null则全部推送
if
(
sid
==
null
)
{
item
.
sendMessage
(
message
);
}
else
if
(
item
.
sid
.
equals
(
sid
)){
item
.
sendMessage
(
message
);
}
}
catch
(
IOException
e
)
{
continue
;
}
}
}
}
eladmin-system/src/main/java/me/zhengjie/modules/security/config/SecurityConfig.java
View file @
7b11e81f
package
me.zhengjie.modules.security.config
;
package
me.zhengjie.modules.security.config
;
import
me.zhengjie.annotation.AnonymousAccess
;
import
me.zhengjie.annotation.AnonymousAccess
;
import
me.zhengjie.config.ElPermissionConfig
;
import
me.zhengjie.modules.security.security.JwtAuthenticationEntryPoint
;
import
me.zhengjie.modules.security.security.JwtAuthenticationEntryPoint
;
import
me.zhengjie.modules.security.security.JwtAuthorizationTokenFilter
;
import
me.zhengjie.modules.security.security.JwtAuthorizationTokenFilter
;
import
me.zhengjie.modules.security.service.JwtUserDetailsService
;
import
me.zhengjie.modules.security.service.JwtUserDetailsService
;
...
@@ -108,7 +107,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
@@ -108,7 +107,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
"/*.html"
,
"/*.html"
,
"/**/*.html"
,
"/**/*.html"
,
"/**/*.css"
,
"/**/*.css"
,
"/**/*.js"
"/**/*.js"
,
"/webSocket/**"
).
anonymous
()
).
anonymous
()
// swagger start
// swagger start
.
antMatchers
(
"/swagger-ui.html"
).
permitAll
()
.
antMatchers
(
"/swagger-ui.html"
).
permitAll
()
...
...
sql/eladmin-mnt.sql
0 → 100644
View file @
7b11e81f
-- ----------------------------
-- Records of menu
-- ----------------------------
INSERT
INTO
`menu`
VALUES
(
90
,
b
'0'
,
'运维管理'
,
''
,
0
,
920
,
'sys-tools'
,
'mnt'
,
b
'0'
,
b
'0'
,
'Mnt'
,
'2019-11-09 10:31:08'
,
NULL
,
1
);
INSERT
INTO
`menu`
VALUES
(
91
,
b
'0'
,
'账号管理'
,
'mnt/account/index'
,
90
,
100
,
'user'
,
'mnt/account'
,
b
'0'
,
b
'0'
,
'Account'
,
'2019-11-09 10:32:05'
,
'serverAccount:list'
,
1
);
INSERT
INTO
`menu`
VALUES
(
92
,
b
'0'
,
'服务器管理'
,
'mnt/serverDeploy/index'
,
90
,
200
,
'codeConsole'
,
'mnt/serverDeploy'
,
b
'0'
,
b
'0'
,
'ServerDeploy'
,
'2019-11-10 10:29:25'
,
'serverDeploy:list'
,
1
);
INSERT
INTO
`menu`
VALUES
(
93
,
b
'0'
,
'应用管理'
,
'mnt/app/index'
,
90
,
300
,
'java'
,
'mnt/app'
,
b
'0'
,
b
'0'
,
'App'
,
'2019-11-10 11:05:16'
,
'app:list'
,
1
);
INSERT
INTO
`menu`
VALUES
(
94
,
b
'0'
,
'部署管理'
,
'mnt/deploy/index'
,
90
,
400
,
'ipvisits'
,
'mnt/deploy'
,
b
'0'
,
b
'0'
,
'Deploy'
,
'2019-11-10 15:56:55'
,
'deploy:list'
,
1
);
INSERT
INTO
`menu`
VALUES
(
97
,
b
'0'
,
'部署备份管理'
,
'mnt/deployHistory/index'
,
90
,
500
,
'ipvisits'
,
'mnt/deployHistory'
,
b
'0'
,
b
'0'
,
'DeployHistory'
,
'2019-11-10 16:49:44'
,
'deployHistory:list'
,
1
);
INSERT
INTO
`menu`
VALUES
(
98
,
b
'0'
,
'数据库管理'
,
'mnt/database/index'
,
90
,
600
,
'zujian'
,
'mnt/database'
,
b
'0'
,
b
'0'
,
'Database'
,
'2019-11-10 20:40:04'
,
'database:list'
,
1
);
INSERT
INTO
`menu`
VALUES
(
99
,
b
'0'
,
'账户新增'
,
''
,
91
,
1
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-16 14:03:05'
,
'serverAccount:add'
,
2
);
INSERT
INTO
`menu`
VALUES
(
100
,
b
'0'
,
'账户编辑'
,
''
,
91
,
2
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-16 14:06:26'
,
'serverAccount:edit'
,
2
);
INSERT
INTO
`menu`
VALUES
(
101
,
b
'0'
,
'账户删除'
,
''
,
91
,
3
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-16 14:06:45'
,
'serverAccount:del'
,
2
);
INSERT
INTO
`menu`
VALUES
(
102
,
b
'0'
,
'删除'
,
''
,
97
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 09:32:48'
,
'deployHistory:del'
,
2
);
INSERT
INTO
`menu`
VALUES
(
103
,
b
'0'
,
'服务器新增'
,
''
,
92
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 11:08:33'
,
'serverDeploy:add'
,
2
);
INSERT
INTO
`menu`
VALUES
(
104
,
b
'0'
,
'服务器编辑'
,
''
,
92
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 11:08:57'
,
'serverDeploy:edit'
,
2
);
INSERT
INTO
`menu`
VALUES
(
105
,
b
'0'
,
'服务器删除'
,
''
,
92
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 11:09:15'
,
'serverDeploy:del'
,
2
);
INSERT
INTO
`menu`
VALUES
(
106
,
b
'0'
,
'应用新增'
,
''
,
93
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 11:10:03'
,
'app:add'
,
2
);
INSERT
INTO
`menu`
VALUES
(
107
,
b
'0'
,
'应用编辑'
,
''
,
93
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 11:10:28'
,
'app:edit'
,
2
);
INSERT
INTO
`menu`
VALUES
(
108
,
b
'0'
,
'应用删除'
,
''
,
93
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 11:10:55'
,
'app:del'
,
2
);
INSERT
INTO
`menu`
VALUES
(
109
,
b
'0'
,
'部署新增'
,
''
,
94
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 11:11:22'
,
'deploy:add'
,
2
);
INSERT
INTO
`menu`
VALUES
(
110
,
b
'0'
,
'部署编辑'
,
''
,
94
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 11:11:41'
,
'deploy:edit'
,
2
);
INSERT
INTO
`menu`
VALUES
(
111
,
b
'0'
,
'部署删除'
,
''
,
94
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 11:12:01'
,
'deploy:del'
,
2
);
INSERT
INTO
`menu`
VALUES
(
112
,
b
'0'
,
'数据库新增'
,
''
,
98
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 11:12:43'
,
'database:add'
,
2
);
INSERT
INTO
`menu`
VALUES
(
113
,
b
'0'
,
'数据库编辑'
,
''
,
98
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 11:12:58'
,
'database:edit'
,
2
);
INSERT
INTO
`menu`
VALUES
(
114
,
b
'0'
,
'数据库删除'
,
''
,
98
,
999
,
''
,
''
,
b
'0'
,
b
'0'
,
''
,
'2019-11-17 11:13:14'
,
'database:del'
,
2
);
-- ----------------------------
-- Table structure for mnt_app
-- ----------------------------
DROP
TABLE
IF
EXISTS
`mnt_app`
;
CREATE
TABLE
`mnt_app`
(
`id`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'应用编号'
,
`name`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'应用名称'
,
`upload_path`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'上传目录'
,
`deploy_path`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'部署路径'
,
`backup_path`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'备份路径'
,
`port`
int
(
255
)
NULL
DEFAULT
NULL
COMMENT
'应用端口'
,
`start_script`
varchar
(
4000
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'启动脚本'
,
`deploy_script`
varchar
(
4000
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'部署脚本'
,
PRIMARY
KEY
(
`id`
)
USING
BTREE
)
ENGINE
=
InnoDB
CHARACTER
SET
=
utf8
COLLATE
=
utf8_general_ci
ROW_FORMAT
=
Dynamic
;
-- ----------------------------
-- Records of mnt_app
-- ----------------------------
INSERT
INTO
`mnt_app`
VALUES
(
'04a27b0a-6570-4fac-88be-abad072028ac'
,
'eladmin-monitor-2.2.jar'
,
'/opt/upload'
,
'/opt/monitor'
,
'/opt/backup'
,
8777
,
'cd /opt/monitor/
\n
nohup java -jar eladmin-monitor-2.2.jar >nohup.out 2>&1 &
\n
'
,
'mv /opt/upload/eladmin-monitor-2.2.jar /opt/monitor/
\n
cd /opt/monitor
\n
nohup java -jar eladmin-monitor-2.2.jar >nohup.out 2>&1 &
\n
'
);
-- ----------------------------
-- Table structure for mnt_database
-- ----------------------------
DROP
TABLE
IF
EXISTS
`mnt_database`
;
CREATE
TABLE
`mnt_database`
(
`id`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'编号'
,
`name`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'名称'
,
`jdbc_url`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'jdbc连接'
,
`user_name`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'账号'
,
`pwd`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'密码'
,
PRIMARY
KEY
(
`id`
)
USING
BTREE
)
ENGINE
=
InnoDB
CHARACTER
SET
=
utf8
COLLATE
=
utf8_general_ci
ROW_FORMAT
=
Dynamic
;
-- ----------------------------
-- Table structure for mnt_deploy
-- ----------------------------
DROP
TABLE
IF
EXISTS
`mnt_deploy`
;
CREATE
TABLE
`mnt_deploy`
(
`id`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'编号'
,
`app_id`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'应用编号'
,
`ip`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'IP地址'
,
PRIMARY
KEY
(
`id`
)
USING
BTREE
)
ENGINE
=
InnoDB
CHARACTER
SET
=
utf8
COLLATE
=
utf8_general_ci
ROW_FORMAT
=
Dynamic
;
-- ----------------------------
-- Table structure for mnt_deploy_history
-- ----------------------------
DROP
TABLE
IF
EXISTS
`mnt_deploy_history`
;
CREATE
TABLE
`mnt_deploy_history`
(
`id`
varchar
(
50
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'编号'
,
`app_name`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'应用名称'
,
`deploy_date`
varchar
(
20
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'部署日期'
,
`deploy_user`
varchar
(
50
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'部署用户'
,
`ip`
varchar
(
20
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'服务器IP'
,
`deploy_id`
varchar
(
50
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'部署编号'
,
PRIMARY
KEY
(
`id`
)
USING
BTREE
)
ENGINE
=
InnoDB
CHARACTER
SET
=
utf8
COLLATE
=
utf8_general_ci
ROW_FORMAT
=
Dynamic
;
-- ----------------------------
-- Table structure for mnt_server
-- ----------------------------
DROP
TABLE
IF
EXISTS
`mnt_server`
;
CREATE
TABLE
`mnt_server`
(
`id`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'IP地址'
,
`account_id`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'账号编号'
,
PRIMARY
KEY
(
`id`
)
USING
BTREE
)
ENGINE
=
InnoDB
CHARACTER
SET
=
utf8
COLLATE
=
utf8_general_ci
ROW_FORMAT
=
Dynamic
;
-- ----------------------------
-- Table structure for mnt_server_account
-- ----------------------------
DROP
TABLE
IF
EXISTS
`mnt_server_account`
;
CREATE
TABLE
`mnt_server_account`
(
`id`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NOT
NULL
COMMENT
'编号'
,
`account`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'账户'
,
`name`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'名称'
,
`password`
varchar
(
255
)
CHARACTER
SET
utf8
COLLATE
utf8_general_ci
NULL
DEFAULT
NULL
COMMENT
'密码'
,
PRIMARY
KEY
(
`id`
)
USING
BTREE
)
ENGINE
=
InnoDB
CHARACTER
SET
=
utf8
COLLATE
=
utf8_general_ci
ROW_FORMAT
=
Dynamic
;
\ No newline at end of file
Prev
1
2
3
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment