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
1cec669c
Commit
1cec669c
authored
Nov 26, 2019
by
zhanghouying
Browse files
更改命令执行方式为shell,避免因为无法获得环境变量而无法启动程序的问题
parent
0cbc6e55
Changes
4
Hide whitespace changes
Inline
Side-by-side
eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/DeployServiceImpl.java
View file @
1cec669c
...
@@ -9,8 +9,13 @@ import me.zhengjie.modules.mnt.domain.Deploy;
...
@@ -9,8 +9,13 @@ import me.zhengjie.modules.mnt.domain.Deploy;
import
me.zhengjie.modules.mnt.domain.DeployHistory
;
import
me.zhengjie.modules.mnt.domain.DeployHistory
;
import
me.zhengjie.modules.mnt.domain.ServerDeploy
;
import
me.zhengjie.modules.mnt.domain.ServerDeploy
;
import
me.zhengjie.modules.mnt.repository.DeployRepository
;
import
me.zhengjie.modules.mnt.repository.DeployRepository
;
import
me.zhengjie.modules.mnt.service.*
;
import
me.zhengjie.modules.mnt.service.DeployHistoryService
;
import
me.zhengjie.modules.mnt.service.dto.*
;
import
me.zhengjie.modules.mnt.service.DeployService
;
import
me.zhengjie.modules.mnt.service.ServerDeployService
;
import
me.zhengjie.modules.mnt.service.dto.AppDto
;
import
me.zhengjie.modules.mnt.service.dto.DeployDto
;
import
me.zhengjie.modules.mnt.service.dto.DeployQueryCriteria
;
import
me.zhengjie.modules.mnt.service.dto.ServerDeployDto
;
import
me.zhengjie.modules.mnt.service.mapper.DeployMapper
;
import
me.zhengjie.modules.mnt.service.mapper.DeployMapper
;
import
me.zhengjie.modules.mnt.util.ExecuteShellUtil
;
import
me.zhengjie.modules.mnt.util.ExecuteShellUtil
;
import
me.zhengjie.modules.mnt.util.ScpClientUtil
;
import
me.zhengjie.modules.mnt.util.ScpClientUtil
;
...
@@ -26,9 +31,8 @@ import org.springframework.data.domain.Pageable;
...
@@ -26,9 +31,8 @@ import org.springframework.data.domain.Pageable;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Propagation
;
import
org.springframework.transaction.annotation.Propagation
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.transaction.annotation.Transactional
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.sql.Timestamp
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.Set
;
...
@@ -42,7 +46,7 @@ import java.util.Set;
...
@@ -42,7 +46,7 @@ import java.util.Set;
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
,
rollbackFor
=
Exception
.
class
)
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
,
rollbackFor
=
Exception
.
class
)
public
class
DeployServiceImpl
implements
DeployService
{
public
class
DeployServiceImpl
implements
DeployService
{
private
final
String
FILE_SEPARATOR
=
File
.
separatorChar
+
""
;
private
final
String
FILE_SEPARATOR
=
"
/
"
;
private
final
DeployRepository
deployRepository
;
private
final
DeployRepository
deployRepository
;
...
@@ -163,6 +167,7 @@ public class DeployServiceImpl implements DeployService {
...
@@ -163,6 +167,7 @@ public class DeployServiceImpl implements DeployService {
boolean
result
=
checkIsRunningStatus
(
port
,
executeShellUtil
);
boolean
result
=
checkIsRunningStatus
(
port
,
executeShellUtil
);
sb
.
append
(
"服务器:"
).
append
(
deployDTO
.
getName
()).
append
(
"<br>应用:"
).
append
(
app
.
getName
());
sb
.
append
(
"服务器:"
).
append
(
deployDTO
.
getName
()).
append
(
"<br>应用:"
).
append
(
app
.
getName
());
sendResultMsg
(
result
,
sb
);
sendResultMsg
(
result
,
sb
);
executeShellUtil
.
close
();
}
}
return
"部署结束"
;
return
"部署结束"
;
}
}
...
@@ -178,7 +183,7 @@ public class DeployServiceImpl implements DeployService {
...
@@ -178,7 +183,7 @@ public class DeployServiceImpl implements DeployService {
private
void
backupApp
(
ExecuteShellUtil
executeShellUtil
,
String
ip
,
String
fileSavePath
,
String
appName
,
String
backupPath
,
Long
id
)
{
private
void
backupApp
(
ExecuteShellUtil
executeShellUtil
,
String
ip
,
String
fileSavePath
,
String
appName
,
String
backupPath
,
Long
id
)
{
String
deployDate
=
DateUtil
.
format
(
new
Date
(),
DatePattern
.
PURE_DATETIME_PATTERN
);
String
deployDate
=
DateUtil
.
format
(
new
Date
(),
DatePattern
.
PURE_DATETIME_PATTERN
);
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
if
(!
backupPath
.
endsWith
(
FILE_SEPARATOR
))
{
if
(!
backupPath
.
endsWith
(
FILE_SEPARATOR
)
&&!
backupPath
.
endsWith
(
"\\"
)
)
{
backupPath
+=
FILE_SEPARATOR
;
backupPath
+=
FILE_SEPARATOR
;
}
}
backupPath
+=
appName
+
FILE_SEPARATOR
+
deployDate
+
"\n"
;
backupPath
+=
appName
+
FILE_SEPARATOR
+
deployDate
+
"\n"
;
...
@@ -219,8 +224,8 @@ public class DeployServiceImpl implements DeployService {
...
@@ -219,8 +224,8 @@ public class DeployServiceImpl implements DeployService {
* @return true 正在运行 false 已经停止
* @return true 正在运行 false 已经停止
*/
*/
private
boolean
checkIsRunningStatus
(
int
port
,
ExecuteShellUtil
executeShellUtil
)
{
private
boolean
checkIsRunningStatus
(
int
port
,
ExecuteShellUtil
executeShellUtil
)
{
String
statusR
esult
=
executeShellUtil
.
executeForResult
(
String
.
format
(
"fuser -n tcp %d"
,
port
));
String
r
esult
=
executeShellUtil
.
executeForResult
(
String
.
format
(
"fuser -n tcp %d"
,
port
));
return
!
""
.
equals
(
statusResult
.
trim
())
;
return
result
.
indexOf
(
"/tcp:"
)>
0
;
}
}
private
void
sendMsg
(
String
msg
,
MsgType
msgType
)
{
private
void
sendMsg
(
String
msg
,
MsgType
msgType
)
{
...
@@ -248,16 +253,15 @@ public class DeployServiceImpl implements DeployService {
...
@@ -248,16 +253,15 @@ public class DeployServiceImpl implements DeployService {
sendMsg
(
sb
.
toString
(),
MsgType
.
ERROR
);
sendMsg
(
sb
.
toString
(),
MsgType
.
ERROR
);
}
}
log
.
info
(
sb
.
toString
());
log
.
info
(
sb
.
toString
());
executeShellUtil
.
close
();
}
}
return
"执行完毕"
;
return
"执行完毕"
;
}
}
private
boolean
checkFile
(
ExecuteShellUtil
executeShellUtil
,
AppDto
appDTO
)
{
private
boolean
checkFile
(
ExecuteShellUtil
executeShellUtil
,
AppDto
appDTO
)
{
String
sb
=
"find "
+
StringBuilder
sb
=
new
StringBuilder
(
"find "
).
append
(
appDTO
.
getDeployPath
()).
append
(
" -name "
).
append
(
appDTO
.
getName
());
appDTO
.
getDeployPath
()
+
String
result
=
executeShellUtil
.
executeForResult
(
sb
.
toString
());
" -name "
+
return
result
.
indexOf
(
"/tcp:"
)>
0
;
appDTO
.
getName
();
return
executeShellUtil
.
executeShell
(
sb
);
}
}
/**
/**
...
@@ -282,6 +286,7 @@ public class DeployServiceImpl implements DeployService {
...
@@ -282,6 +286,7 @@ public class DeployServiceImpl implements DeployService {
boolean
result
=
checkIsRunningStatus
(
app
.
getPort
(),
executeShellUtil
);
boolean
result
=
checkIsRunningStatus
(
app
.
getPort
(),
executeShellUtil
);
sendResultMsg
(
result
,
sb
);
sendResultMsg
(
result
,
sb
);
log
.
info
(
sb
.
toString
());
log
.
info
(
sb
.
toString
());
executeShellUtil
.
close
();
}
}
return
"执行完毕"
;
return
"执行完毕"
;
}
}
...
@@ -312,6 +317,7 @@ public class DeployServiceImpl implements DeployService {
...
@@ -312,6 +317,7 @@ public class DeployServiceImpl implements DeployService {
sendMsg
(
sb
.
toString
(),
MsgType
.
INFO
);
sendMsg
(
sb
.
toString
(),
MsgType
.
INFO
);
}
}
log
.
info
(
sb
.
toString
());
log
.
info
(
sb
.
toString
());
executeShellUtil
.
close
();
}
}
return
"执行完毕"
;
return
"执行完毕"
;
}
}
...
@@ -320,7 +326,7 @@ public class DeployServiceImpl implements DeployService {
...
@@ -320,7 +326,7 @@ public class DeployServiceImpl implements DeployService {
public
String
serverReduction
(
DeployHistory
resources
)
{
public
String
serverReduction
(
DeployHistory
resources
)
{
Long
deployId
=
resources
.
getDeployId
();
Long
deployId
=
resources
.
getDeployId
();
Deploy
deployInfo
=
deployRepository
.
findById
(
deployId
).
orElseGet
(
Deploy:
:
new
);
Deploy
deployInfo
=
deployRepository
.
findById
(
deployId
).
orElseGet
(
Deploy:
:
new
);
Timestamp
deployDate
=
resources
.
getDeployDate
();
String
deployDate
=
DateUtil
.
format
(
resources
.
getDeployDate
()
,
DatePattern
.
PURE_DATETIME_PATTERN
)
;
App
app
=
deployInfo
.
getApp
();
App
app
=
deployInfo
.
getApp
();
if
(
app
==
null
)
{
if
(
app
==
null
)
{
sendMsg
(
"应用信息不存在:"
+
resources
.
getAppName
(),
MsgType
.
ERROR
);
sendMsg
(
"应用信息不存在:"
+
resources
.
getAppName
(),
MsgType
.
ERROR
);
...
@@ -363,6 +369,7 @@ public class DeployServiceImpl implements DeployService {
...
@@ -363,6 +369,7 @@ public class DeployServiceImpl implements DeployService {
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"服务器:"
).
append
(
ip
).
append
(
"<br>应用:"
).
append
(
resources
.
getAppName
());
sb
.
append
(
"服务器:"
).
append
(
ip
).
append
(
"<br>应用:"
).
append
(
resources
.
getAppName
());
sendResultMsg
(
result
,
sb
);
sendResultMsg
(
result
,
sb
);
executeShellUtil
.
close
();
return
""
;
return
""
;
}
}
...
...
eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ExecuteShellUtil.java
View file @
1cec669c
package
me.zhengjie.modules.mnt.util
;
package
me.zhengjie.modules.mnt.util
;
import
com.jcraft.jsch.*
;
import
cn.hutool.core.io.IoUtil
;
import
com.jcraft.jsch.ChannelShell
;
import
com.jcraft.jsch.JSch
;
import
com.jcraft.jsch.Session
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.BufferedReader
;
import
java.io.*
;
import
java.io.InputStreamReader
;
import
java.util.Vector
;
import
java.util.Vector
;
/**
/**
* 执行shell命令
* 执行shell命令
*
* @author: ZhangHouYing
* @author: ZhangHouYing
* @date: 2019/8/10
* @date: 2019/8/10
*/
*/
@Slf4j
@Slf4j
public
class
ExecuteShellUtil
{
public
class
ExecuteShellUtil
{
private
String
ipAddress
;
private
String
username
;
private
String
password
;
public
final
int
DEFAULT_SSH_PORT
=
22
;
public
final
int
DEFAULT_SSH_PORT
=
22
;
private
Vector
<
String
>
stdout
;
private
Vector
<
String
>
stdout
;
public
ExecuteShellUtil
(
final
String
ipAddress
,
final
String
username
,
final
String
password
)
{
Session
session
;
this
.
ipAddress
=
ipAddress
;
this
.
username
=
username
;
this
.
password
=
password
;
stdout
=
new
Vector
<
String
>();
}
public
int
execute
(
final
String
command
)
{
public
ExecuteShellUtil
(
final
String
ipAddress
,
final
String
username
,
final
String
password
)
{
int
returnCode
=
0
;
JSch
jsch
=
new
JSch
();
try
{
try
{
Session
session
=
jsch
.
getSession
(
username
,
ipAddress
,
DEFAULT_SSH_PORT
);
JSch
jsch
=
new
JSch
();
session
=
jsch
.
getSession
(
username
,
ipAddress
,
DEFAULT_SSH_PORT
);
session
.
setPassword
(
password
);
session
.
setPassword
(
password
);
session
.
setConfig
(
"StrictHostKeyChecking"
,
"no"
);
session
.
setConfig
(
"StrictHostKeyChecking"
,
"no"
);
session
.
connect
(
3000
0
);
session
.
connect
(
3000
);
}
catch
(
Exception
e
)
{
Channel
channel
=
session
.
openChannel
(
"exec"
);
e
.
printStackTrace
(
);
((
ChannelExec
)
channel
).
setCommand
(
command
);
}
channel
.
setInputStream
(
null
);
}
BufferedReader
input
=
new
BufferedReader
(
new
InputStreamReader
(
channel
.
getInputStream
()));
public
int
execute
(
final
String
command
)
{
int
returnCode
=
0
;
ChannelShell
channel
=
null
;
PrintWriter
printWriter
=
null
;
BufferedReader
input
=
null
;
stdout
=
new
Vector
<
String
>();
try
{
channel
=
(
ChannelShell
)
session
.
openChannel
(
"shell"
);
channel
.
connect
();
channel
.
connect
();
log
.
info
(
"The remote command is: "
+
command
);
input
=
new
BufferedReader
(
new
InputStreamReader
(
channel
.
getInputStream
()));
printWriter
=
new
PrintWriter
(
channel
.
getOutputStream
());
printWriter
.
println
(
command
);
printWriter
.
println
(
"exit"
);
printWriter
.
flush
();
log
.
info
(
"The remote command is: "
);
String
line
;
String
line
;
while
((
line
=
input
.
readLine
())
!=
null
)
{
while
((
line
=
input
.
readLine
())
!=
null
)
{
stdout
.
add
(
line
);
stdout
.
add
(
line
);
System
.
out
.
println
(
line
);
}
}
input
.
close
();
if
(
channel
.
isClosed
())
{
returnCode
=
channel
.
getExitStatus
();
}
channel
.
disconnect
();
session
.
disconnect
();
}
catch
(
JSchException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
return
-
1
;
}
finally
{
IoUtil
.
close
(
printWriter
);
IoUtil
.
close
(
input
);
if
(
channel
!=
null
)
{
channel
.
disconnect
();
}
}
}
return
returnCode
;
return
returnCode
;
}
}
public
boolean
executeShell
(
String
command
)
{
public
void
close
(){
int
result
=
execute
(
command
);
if
(
session
!=
null
)
{
for
(
String
str
:
stdout
)
{
session
.
disconnect
();
log
.
info
(
str
);
}
if
(
result
==
0
)
{
return
true
;
}
else
{
return
false
;
}
}
}
}
public
String
executeForResult
(
String
command
)
{
public
String
executeForResult
(
String
command
)
{
execute
(
command
);
execute
(
command
);
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
for
(
String
str
:
stdout
)
{
for
(
String
str
:
stdout
)
{
sb
.
append
(
str
);
sb
.
append
(
str
);
log
.
info
(
str
);
}
}
return
sb
.
toString
();
return
sb
.
toString
();
}
}
/**
* 返回值有三条就代表成功了,2条是没启动,多余三条代表脚本有问题
* @param command
* @return
*/
public
int
checkAppStatus
(
String
command
)
{
execute
(
command
);
for
(
String
str
:
stdout
)
{
log
.
info
(
str
);
}
return
stdout
.
size
();
}
}
}
sql/eladmin-mnt.sql
View file @
1cec669c
-- ----------------------------
-- ----------------------------
-- Records of menu
-- Records of menu
-- ----------------------------
-- ----------------------------
INSERT
INTO
`menu`
VALUES
(
80
,
b
'0'
,
'服务监控'
,
'monitor/server/index'
,
6
,
14
,
'codeConsole'
,
'server'
,
b
'0'
,
b
'0'
,
'ServerMonitor'
,
'2019-11-07 13:06:39'
,
'server:list'
,
1
);
INSERT
INTO
`menu`
VALUES
(
80
,
b
'0'
,
'服务监控'
,
'monitor/server/index'
,
6
,
14
,
'codeConsole'
,
'server'
,
b
'0'
,
b
'0'
,
'ServerMonitor'
,
'2019-11-07 13:06:39'
,
'server:list'
,
1
);
INSERT
INTO
`menu`
VALUES
(
83
,
b
'0'
,
'图表库'
,
'components/Echarts'
,
10
,
50
,
'chart'
,
'echarts'
,
b
'1'
,
b
'0'
,
'Echarts'
,
'2019-11-21 09:04:32'
,
''
,
1
);
INSERT
INTO
`menu`
VALUES
(
83
,
b
'0'
,
'图表库'
,
'components/Echarts'
,
10
,
50
,
'chart'
,
'echarts'
,
b
'1'
,
b
'0'
,
'Echarts'
,
'2019-11-21 09:04:32'
,
''
,
1
);
INSERT
INTO
`menu`
VALUES
(
90
,
b
'0'
,
'运维管理'
,
''
,
0
,
20
,
'mnt'
,
'mnt'
,
b
'0'
,
b
'0'
,
'Mnt'
,
'2019-11-09 10:31:08'
,
NULL
,
1
);
INSERT
INTO
`menu`
VALUES
(
90
,
b
'0'
,
'运维管理'
,
''
,
0
,
20
,
'mnt'
,
'mnt'
,
b
'0'
,
b
'0'
,
'Mnt'
,
'2019-11-09 10:31:08'
,
NULL
,
1
);
...
@@ -46,7 +46,7 @@ CREATE TABLE `mnt_app` (
...
@@ -46,7 +46,7 @@ CREATE TABLE `mnt_app` (
-- ----------------------------
-- ----------------------------
-- Records of mnt_app
-- Records of mnt_app
-- ----------------------------
-- ----------------------------
INSERT
INTO
`mnt_app`
VALUES
(
1
,
'eladmin-monitor-2.3.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
'
,
'm
v /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
'
,
'2019-11-24 20:52:59'
);
INSERT
INTO
`mnt_app`
VALUES
(
1
,
'eladmin-monitor-2.3.jar'
,
'/opt/upload'
,
'/opt/monitor'
,
'/opt/backup'
,
8777
,
'cd /opt/monitor
\n
nohup java -jar eladmin-monitor-2.
3
.jar >nohup.out 2>&1 &
\n
'
,
'm
kdir -p
/opt/monitor
\n
mv -f
/opt/
upload/
eladmin-monitor-2.
3
.jar
/opt/monitor
\n
'
,
'2019-11-24 20:52:59'
);
-- ----------------------------
-- ----------------------------
-- Table structure for mnt_database
-- Table structure for mnt_database
...
...
sql/eladmin.sql
View file @
1cec669c
...
@@ -353,7 +353,7 @@ CREATE TABLE `mnt_app` (
...
@@ -353,7 +353,7 @@ CREATE TABLE `mnt_app` (
-- ----------------------------
-- ----------------------------
-- Records of mnt_app
-- Records of mnt_app
-- ----------------------------
-- ----------------------------
INSERT
INTO
`mnt_app`
VALUES
(
1
,
'eladmin-monitor-2.3.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
'
,
'm
v /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
'
,
'2019-11-24 20:52:59'
);
INSERT
INTO
`mnt_app`
VALUES
(
1
,
'eladmin-monitor-2.3.jar'
,
'/opt/upload'
,
'/opt/monitor'
,
'/opt/backup'
,
8777
,
'cd /opt/monitor
\n
nohup java -jar eladmin-monitor-2.
3
.jar >nohup.out 2>&1 &
\n
'
,
'm
kdir -p
/opt/monitor
\n
mv -f
/opt/
upload/
eladmin-monitor-2.
3
.jar
/opt/monitor
\n
'
,
'2019-11-24 20:52:59'
);
-- ----------------------------
-- ----------------------------
-- Table structure for mnt_database
-- Table structure for mnt_database
...
...
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