Commit cf25a0b7 authored by 黎博's avatar 黎博

增加新的jenkins job构建,定时任务及接口

parent fc72a780
...@@ -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 数据库名
......
...@@ -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.buildJenkinsJob(type, project, branch, namespace); jenkinsService.buildTkeJob(type, project, branch, namespace);
return JsonResult.buildSuccessResult(true); return JsonResult.buildSuccessResult(true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -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);
} }
} }
......
...@@ -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.buildJenkinsJob(pipeline.getServiceType(), projectName, branch, pipeline.getNamespace()); Integer buildNum = jenkinsService.buildTkeJob(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 {
......
package cn.qg.holmes.task; package cn.qg.holmes.task;
import cn.qg.holmes.service.effect.DatabaseSyncService; import cn.qg.holmes.utils.JenkinsService;
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);
}
} }
} }
...@@ -51,7 +51,7 @@ public class JenkinsService { ...@@ -51,7 +51,7 @@ public class JenkinsService {
* @param namespace 部署的环境 * @param namespace 部署的环境
* @return * @return
*/ */
public int buildJenkinsJob(String type, String project, String branch, String namespace) { public int buildTkeJob(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");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment