Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
holmes
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
holmes
Commits
cf25a0b7
Commit
cf25a0b7
authored
Oct 20, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加新的jenkins job构建,定时任务及接口
parent
fc72a780
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
89 additions
and
28 deletions
+89
-28
DbSyncController.java
...java/cn/qg/holmes/controller/effect/DbSyncController.java
+26
-1
JenkinsController.java
...ava/cn/qg/holmes/controller/gitlab/JenkinsController.java
+1
-1
DatabaseSyncServiceImpl.java
...g/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
+1
-1
PipelineServiceImpl.java
...cn/qg/holmes/service/gitlab/impl/PipelineServiceImpl.java
+1
-1
DbSyncTask.java
src/main/java/cn/qg/holmes/task/DbSyncTask.java
+4
-23
JenkinsService.java
src/main/java/cn/qg/holmes/utils/JenkinsService.java
+56
-1
No files found.
src/main/java/cn/qg/holmes/controller/effect/DbSyncController.java
View file @
cf25a0b7
...
@@ -49,7 +49,7 @@ public class DbSyncController {
...
@@ -49,7 +49,7 @@ public class DbSyncController {
* @param businessData 是否保留业务数据
* @param businessData 是否保留业务数据
* @return
* @return
*/
*/
@GetMapping
(
"/
new
"
)
@GetMapping
(
"/
holmes
"
)
public
JsonResult
syncDatabase
(
@RequestParam
String
namespace
,
@RequestParam
String
dbName
,
@RequestParam
String
tableName
,
@RequestParam
boolean
businessData
)
{
public
JsonResult
syncDatabase
(
@RequestParam
String
namespace
,
@RequestParam
String
dbName
,
@RequestParam
String
tableName
,
@RequestParam
boolean
businessData
)
{
try
{
try
{
long
startTime
=
System
.
currentTimeMillis
();
long
startTime
=
System
.
currentTimeMillis
();
...
@@ -149,6 +149,31 @@ public class DbSyncController {
...
@@ -149,6 +149,31 @@ public class DbSyncController {
return
JsonResult
.
buildSuccessResult
(
"创建"
+
targetIp
+
":"
+
targetPort
+
"环境表结构成功!"
);
return
JsonResult
.
buildSuccessResult
(
"创建"
+
targetIp
+
":"
+
targetPort
+
"环境表结构成功!"
);
}
}
/**
* 新-同步数据库,构建jenkins job db_sync
* @param namespace 环境
* @param targetHost 目标环境ip
* @param targetPort 目标环境端口
* @param database 数据库名称
* @param table 表名
* @param businessData 是否保留业务数据
* @return
*/
@PostMapping
(
"new"
)
public
JsonResult
dbSyncNew
(
@RequestParam
String
namespace
,
@RequestParam
String
targetHost
,
@RequestParam
String
targetPort
,
@RequestParam
String
database
,
@RequestParam
String
table
,
@RequestParam
boolean
businessData
)
{
int
buildNum
=
jenkinsService
.
buildDbSyncJobNew
(
namespace
,
targetHost
,
targetPort
,
database
,
table
,
businessData
);
if
(
buildNum
==
0
)
{
return
JsonResult
.
buildErrorStateResult
(
"数据库同步失败!"
,
0
);
}
else
{
return
JsonResult
.
buildSuccessResult
(
"开始同步数据库!"
,
buildNum
);
}
}
/**
/**
* 老数据库同步,调用Jenkins job sync_database_schema
* 老数据库同步,调用Jenkins job sync_database_schema
* @param dbName 数据库名
* @param dbName 数据库名
...
...
src/main/java/cn/qg/holmes/controller/gitlab/JenkinsController.java
View file @
cf25a0b7
...
@@ -30,7 +30,7 @@ public class JenkinsController {
...
@@ -30,7 +30,7 @@ public class JenkinsController {
@RequestParam
String
namespace
)
{
@RequestParam
String
namespace
)
{
try
{
try
{
log
.
info
(
"开始构建jenkins项目:项目类型:{}, 项目名:{}, 分支:{}, 部署至环境:{}"
,
type
,
project
,
branch
,
namespace
);
log
.
info
(
"开始构建jenkins项目:项目类型:{}, 项目名:{}, 分支:{}, 部署至环境:{}"
,
type
,
project
,
branch
,
namespace
);
jenkinsService
.
build
Jenkins
Job
(
type
,
project
,
branch
,
namespace
);
jenkinsService
.
build
Tke
Job
(
type
,
project
,
branch
,
namespace
);
return
JsonResult
.
buildSuccessResult
(
true
);
return
JsonResult
.
buildSuccessResult
(
true
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
...
...
src/main/java/cn/qg/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
View file @
cf25a0b7
...
@@ -587,7 +587,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
...
@@ -587,7 +587,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
ResultSet
dbListResultSet
=
preparedStatement
.
executeQuery
();
ResultSet
dbListResultSet
=
preparedStatement
.
executeQuery
();
while
(
dbListResultSet
.
next
())
{
while
(
dbListResultSet
.
next
())
{
String
database
=
dbListResultSet
.
getString
(
1
);
String
database
=
dbListResultSet
.
getString
(
1
);
if
(!
database
.
equals
(
"information_schema"
))
{
if
(!
database
.
equals
(
"information_schema"
)
&&
!
database
.
equals
(
"sys"
)
)
{
databaseList
.
add
(
database
);
databaseList
.
add
(
database
);
}
}
}
}
...
...
src/main/java/cn/qg/holmes/service/gitlab/impl/PipelineServiceImpl.java
View file @
cf25a0b7
...
@@ -53,7 +53,7 @@ public class PipelineServiceImpl extends ServiceImpl<PipelineMapper, Pipeline> i
...
@@ -53,7 +53,7 @@ public class PipelineServiceImpl extends ServiceImpl<PipelineMapper, Pipeline> i
if
(
pipeline
!=
null
)
{
if
(
pipeline
!=
null
)
{
if
(
pipeline
.
getEnable
()
==
1
)
{
if
(
pipeline
.
getEnable
()
==
1
)
{
log
.
info
(
"pipeline中找到对应项目,且项目是启动状态,开始构建:{}"
,
pipeline
.
toString
());
log
.
info
(
"pipeline中找到对应项目,且项目是启动状态,开始构建:{}"
,
pipeline
.
toString
());
Integer
buildNum
=
jenkinsService
.
build
Jenkins
Job
(
pipeline
.
getServiceType
(),
projectName
,
branch
,
pipeline
.
getNamespace
());
Integer
buildNum
=
jenkinsService
.
build
Tke
Job
(
pipeline
.
getServiceType
(),
projectName
,
branch
,
pipeline
.
getNamespace
());
String
markdown
=
DingdingUtils
.
buildPipelineMarkdownMsg
(
projectName
,
branch
,
pipeline
.
getServiceType
(),
author
,
message
,
commitDate
,
pipeline
.
getNamespace
(),
buildNum
.
toString
());
String
markdown
=
DingdingUtils
.
buildPipelineMarkdownMsg
(
projectName
,
branch
,
pipeline
.
getServiceType
(),
author
,
message
,
commitDate
,
pipeline
.
getNamespace
(),
buildNum
.
toString
());
DingdingUtils
.
sendToDingding
(
markdown
,
dingRobotService
.
getById
(
pipeline
.
getDingRobotId
()).
getDingUrl
());
DingdingUtils
.
sendToDingding
(
markdown
,
dingRobotService
.
getById
(
pipeline
.
getDingRobotId
()).
getDingUrl
());
}
else
{
}
else
{
...
...
src/main/java/cn/qg/holmes/task/DbSyncTask.java
View file @
cf25a0b7
package
cn
.
qg
.
holmes
.
task
;
package
cn
.
qg
.
holmes
.
task
;
import
cn.qg.holmes.
service.effect.DatabaseSync
Service
;
import
cn.qg.holmes.
utils.Jenkins
Service
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
java.util.List
;
/**
/**
* 数据库同步定时任务
* 数据库同步定时任务
*/
*/
...
@@ -17,26 +14,10 @@ import java.util.List;
...
@@ -17,26 +14,10 @@ import java.util.List;
public
class
DbSyncTask
{
public
class
DbSyncTask
{
@Autowired
@Autowired
DatabaseSyncService
databaseSyncService
;
JenkinsService
jenkinsService
;
@Value
(
"${dbsync.mysql.ip}"
)
private
String
ip
;
@Value
(
"${dbsync.mysql.port}"
)
private
String
port
;
@Value
(
"${dbsync.mysql.username}"
)
private
String
username
;
@Value
(
"${dbsync.mysql.password}"
)
private
String
password
;
// @Scheduled(cron = "0 0 24 * * ?")
@Scheduled
(
cron
=
"0 0 12 * * ?"
)
@Scheduled
(
cron
=
"0 38 15 * * ?"
)
public
void
getDbInfoFromSourceJob
()
{
public
void
getDbInfoFromSourceJob
()
{
List
<
String
>
dbList
=
databaseSyncService
.
getDatabaseList
(
ip
,
port
,
username
,
password
);
jenkinsService
.
buildDbDailyBackupJob
();
for
(
String
dbName:
dbList
)
{
databaseSyncService
.
getDbInfoFromSource
(
ip
,
port
,
username
,
password
,
dbName
);
}
}
}
}
}
src/main/java/cn/qg/holmes/utils/JenkinsService.java
View file @
cf25a0b7
...
@@ -51,7 +51,7 @@ public class JenkinsService {
...
@@ -51,7 +51,7 @@ public class JenkinsService {
* @param namespace 部署的环境
* @param namespace 部署的环境
* @return
* @return
*/
*/
public
int
build
Jenkins
Job
(
String
type
,
String
project
,
String
branch
,
String
namespace
)
{
public
int
build
Tke
Job
(
String
type
,
String
project
,
String
branch
,
String
namespace
)
{
Map
<
String
,
String
>
buildParams
=
new
HashMap
<>();
Map
<
String
,
String
>
buildParams
=
new
HashMap
<>();
buildParams
.
put
(
"GIT_REPO"
,
project
);
buildParams
.
put
(
"GIT_REPO"
,
project
);
buildParams
.
put
(
"BRANCH_NAME"
,
branch
);
buildParams
.
put
(
"BRANCH_NAME"
,
branch
);
...
@@ -154,6 +154,16 @@ public class JenkinsService {
...
@@ -154,6 +154,16 @@ public class JenkinsService {
}
}
}
}
/**
* 构建sync_database_schema
* @param dbName
* @param host
* @param port
* @param replaceDomain
* @param notDeleteBusinessData
* @param useCache
* @return build num
*/
public
int
buildDbSyncJob
(
String
dbName
,
String
host
,
String
port
,
public
int
buildDbSyncJob
(
String
dbName
,
String
host
,
String
port
,
boolean
replaceDomain
,
boolean
notDeleteBusinessData
,
boolean
useCache
)
{
boolean
replaceDomain
,
boolean
notDeleteBusinessData
,
boolean
useCache
)
{
Map
<
String
,
String
>
params
=
new
HashMap
<>();
Map
<
String
,
String
>
params
=
new
HashMap
<>();
...
@@ -188,6 +198,51 @@ public class JenkinsService {
...
@@ -188,6 +198,51 @@ public class JenkinsService {
return
result
;
return
result
;
}
}
/**
* 构建db_sync
* @param namespace
* @param host
* @param port
* @param database
* @param table
* @param businessData
* @return build num
*/
public
int
buildDbSyncJobNew
(
String
namespace
,
String
host
,
String
port
,
String
database
,
String
table
,
boolean
businessData
)
{
Map
<
String
,
String
>
params
=
new
HashMap
<>();
params
.
put
(
"namespace"
,
namespace
);
params
.
put
(
"target_db_host"
,
host
);
params
.
put
(
"target_db_port"
,
port
);
params
.
put
(
"database"
,
database
);
params
.
put
(
"table"
,
table
);
params
.
put
(
"business_data"
,
String
.
valueOf
(
businessData
));
try
{
jenkinsServer
.
getJob
(
"db_sync"
).
build
(
params
);
Build
build
=
jenkinsServer
.
getJob
(
"db_sync"
).
getLastBuild
();
return
build
.
getNumber
()
+
1
;
}
catch
(
Exception
e
)
{
log
.
info
(
"执行数据库同步脚本失败,host:{}, port: {}, dbName: {}"
,
host
,
port
,
database
);
e
.
printStackTrace
();
return
0
;
}
}
/**
* 构建db_daily_backup
* @return
*/
public
int
buildDbDailyBackupJob
()
{
try
{
jenkinsServer
.
getJob
(
"db_daily_backup"
).
build
();
Build
build
=
jenkinsServer
.
getJob
(
"db_daily_backup"
).
getLastBuild
();
return
build
.
getNumber
()
+
1
;
}
catch
(
Exception
e
)
{
log
.
info
(
"执行数据库备份失败!"
);
e
.
printStackTrace
();
return
0
;
}
}
public
static
void
main
(
String
[]
args
)
throws
IOException
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
JenkinsService
jenkinsApi
=
new
JenkinsService
();
JenkinsService
jenkinsApi
=
new
JenkinsService
();
// List<Build> buildList = jenkinsApi.getJobBuildList("tke-java", 1, "kdsp");
// List<Build> buildList = jenkinsApi.getJobBuildList("tke-java", 1, "kdsp");
...
...
Write
Preview
Markdown
is supported
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