Search in sources :

Example 1 with IPlugin

use of io.jpom.plugin.IPlugin in project Jpom by dromara.

the class DbConfig method recoverDb.

/**
 * 恢复数据库
 */
public void recoverDb() throws Exception {
    File dbLocalPathFile = this.dbLocalPath();
    if (!FileUtil.exist(dbLocalPathFile)) {
        return;
    }
    String dbName = this.getDbName();
    File recoverBackup = FileUtil.file(dbLocalPathFile, "recover_backup", DateTime.now().toString());
    // 
    IPlugin plugin = PluginFactory.getPlugin("db-h2");
    Map<String, Object> map = new HashMap<>(10);
    map.put("dbName", dbName);
    map.put("dbPath", dbLocalPathFile);
    map.put("recoverBackup", recoverBackup);
    File backupSql = (File) plugin.execute("recoverToSql", map);
    // 清空记录
    this.clearExecuteSqlLog();
    // 记录恢复的 sql
    this.recoverSqlFile = backupSql;
}
Also used : File(java.io.File) IPlugin(io.jpom.plugin.IPlugin)

Example 2 with IPlugin

use of io.jpom.plugin.IPlugin in project Jpom by dromara.

the class DbConfig method executeRecoverDbSql.

/**
 * 恢复数据库
 *
 * @param dsFactory 数据库连接
 */
public void executeRecoverDbSql(DSFactory dsFactory) throws Exception {
    if (!FileUtil.isFile(this.recoverSqlFile)) {
        return;
    }
    // 
    IPlugin plugin = PluginFactory.getPlugin("db-h2");
    Map<String, Object> map = new HashMap<>(10);
    map.put("backupSqlPath", FileUtil.getAbsolutePath(this.recoverSqlFile));
    map.put("dataSource", dsFactory.getDataSource());
    plugin.execute("restoreBackupSql", map);
}
Also used : IPlugin(io.jpom.plugin.IPlugin)

Example 3 with IPlugin

use of io.jpom.plugin.IPlugin in project Jpom by dromara.

the class BackupInfoService method restoreWithSql.

/**
 * 根据 SQL 文件还原数据库
 * 还原数据库时只能同步,防止该过程中修改数据造成数据不一致
 *
 * @param backupSqlPath 备份 sql 文件地址
 */
public boolean restoreWithSql(String backupSqlPath) {
    try {
        long startTs = System.currentTimeMillis();
        IPlugin plugin = PluginFactory.getPlugin("db-h2");
        Map<String, Object> map = new HashMap<>(10);
        map.put("backupSqlPath", backupSqlPath);
        plugin.execute("restoreBackupSql", map);
        // h2BackupService.restoreBackupSql(backupSqlPath);
        long endTs = System.currentTimeMillis();
        DefaultSystemLog.getLog().debug("restore H2 Database backup...success...cast {} ms", endTs - startTs);
        return true;
    } catch (Exception e) {
        // 记录错误日志信息,返回数据库备份还原执行失败
        DefaultSystemLog.getLog().error("restore H2 Database backup...catch exception...message: {}, cause: {}", e.getMessage(), e.getCause());
        return false;
    }
}
Also used : HashMap(java.util.HashMap) IPlugin(io.jpom.plugin.IPlugin)

Example 4 with IPlugin

use of io.jpom.plugin.IPlugin in project Jpom by dromara.

the class MonitorItem method sendNotifyMsgToWebhook.

private void sendNotifyMsgToWebhook(MonitorNotifyLog monitorNotifyLog, NodeModel nodeModel, ProjectInfoCacheModel projectInfoCacheModel, String webhook) {
    if (StrUtil.isEmpty(webhook)) {
        return;
    }
    IPlugin plugin = PluginFactory.getPlugin("webhook");
    Map<String, Object> map = new HashMap<>(10);
    map.put("monitorId", monitorModel.getId());
    map.put("monitorName", monitorModel.getName());
    map.put("nodeId", monitorNotifyLog.getNodeId());
    map.put("nodeName", nodeModel.getName());
    map.put("runStatus", monitorNotifyLog.getStatus());
    map.put("projectId", monitorNotifyLog.getProjectId());
    if (projectInfoCacheModel != null) {
        map.put("projectName", projectInfoCacheModel.getName());
    }
    map.put("title", monitorNotifyLog.getTitle());
    map.put("content", monitorNotifyLog.getContent());
    // 
    monitorNotifyLog.setNotifyStyle(MonitorModel.NotifyType.webhook.getCode());
    monitorNotifyLog.setNotifyObject(webhook);
    // 
    dbMonitorNotifyLogService.insert(monitorNotifyLog);
    String logId = monitorNotifyLog.getId();
    ThreadUtil.execute(() -> {
        try {
            plugin.execute(webhook, map);
            dbMonitorNotifyLogService.updateStatus(logId, true, null);
        } catch (Exception e) {
            DefaultSystemLog.getLog().error("WebHooks 调用错误", e);
            dbMonitorNotifyLogService.updateStatus(logId, false, ExceptionUtil.stacktraceToString(e));
        }
    });
}
Also used : HashMap(java.util.HashMap) JSONObject(com.alibaba.fastjson.JSONObject) IPlugin(io.jpom.plugin.IPlugin)

Example 5 with IPlugin

use of io.jpom.plugin.IPlugin in project Jpom by dromara.

the class BackupInfoService method backupToSql.

/**
 * 备份数据库 SQL 文件
 *
 * @param tableNameList 需要备份的表名称列表,如果是全库备份,则不需要
 */
private void backupToSql(final List<String> tableNameList, BackupTypeEnum backupType) {
    final String fileName = LocalDateTimeUtil.format(LocalDateTimeUtil.now(), DatePattern.PURE_DATETIME_PATTERN);
    // 设置默认备份 SQL 的文件地址
    File file = FileUtil.file(DbConfig.getInstance().dbLocalPath(), Const.BACKUP_DIRECTORY_NAME, fileName + Const.SQL_FILE_SUFFIX);
    final String backupSqlPath = FileUtil.getAbsolutePath(file);
    // 数据源参数
    final String url = DbConfig.getInstance().getDbUrl();
    final String user = dbExtConfig.getUserName();
    final String pass = dbExtConfig.getUserPwd();
    JpomManifest instance = JpomManifest.getInstance();
    // 先构造备份信息插入数据库
    BackupInfoModel backupInfoModel = new BackupInfoModel();
    String timeStamp = instance.getTimeStamp();
    try {
        DateTime parse = DateUtil.parse(timeStamp);
        backupInfoModel.setBaleTimeStamp(parse.getTime());
    } catch (Exception ignored) {
    }
    backupInfoModel.setName(fileName);
    backupInfoModel.setVersion(instance.getVersion());
    backupInfoModel.setBackupType(backupType.getCode());
    backupInfoModel.setFilePath(backupSqlPath);
    this.insert(backupInfoModel);
    // 开启一个子线程去执行任务,任务完成之后修改对应的数据库备份信息
    ThreadUtil.execute(() -> {
        // 修改用的实体类
        BackupInfoModel backupInfo = new BackupInfoModel();
        BeanUtil.copyProperties(backupInfoModel, backupInfo);
        try {
            DefaultSystemLog.getLog().debug("start a new Thread to execute H2 Database backup...start");
            IPlugin plugin = PluginFactory.getPlugin("db-h2");
            Map<String, Object> map = new HashMap<>(10);
            map.put("url", url);
            map.put("user", user);
            map.put("pass", pass);
            map.put("backupSqlPath", backupSqlPath);
            map.put("tableNameList", tableNameList);
            plugin.execute("backupSql", map);
            // h2BackupService.backupSql(url, user, pass, backupSqlPath, tableNameList);
            // 修改备份任务执行完成
            backupInfo.setFileSize(FileUtil.size(file));
            backupInfo.setSha1Sum(SecureUtil.sha1(file));
            backupInfo.setStatus(BackupStatusEnum.SUCCESS.getCode());
            this.update(backupInfo);
            DefaultSystemLog.getLog().debug("start a new Thread to execute H2 Database backup...success");
        } catch (Exception e) {
            // 记录错误日志信息,修改备份任务执行失败
            DefaultSystemLog.getLog().error("start a new Thread to execute H2 Database backup...catch exception...", e);
            backupInfo.setStatus(BackupStatusEnum.FAILED.getCode());
            this.update(backupInfo);
        }
    });
}
Also used : BackupInfoModel(io.jpom.model.data.BackupInfoModel) JpomManifest(io.jpom.common.JpomManifest) HashMap(java.util.HashMap) File(java.io.File) IPlugin(io.jpom.plugin.IPlugin)

Aggregations

IPlugin (io.jpom.plugin.IPlugin)16 JSONObject (com.alibaba.fastjson.JSONObject)10 DockerInfoModel (io.jpom.model.docker.DockerInfoModel)8 ClassFeature (io.jpom.permission.ClassFeature)5 Feature (io.jpom.permission.Feature)5 MethodFeature (io.jpom.permission.MethodFeature)5 HashMap (java.util.HashMap)5 File (java.io.File)4 JpomRuntimeException (io.jpom.system.JpomRuntimeException)2 CollUtil (cn.hutool.core.collection.CollUtil)1 BetweenFormatter (cn.hutool.core.date.BetweenFormatter)1 DateUtil (cn.hutool.core.date.DateUtil)1 SystemClock (cn.hutool.core.date.SystemClock)1 FileUtil (cn.hutool.core.io.FileUtil)1 IoUtil (cn.hutool.core.io.IoUtil)1 LineHandler (cn.hutool.core.io.LineHandler)1 ResourceUtil (cn.hutool.core.io.resource.ResourceUtil)1 CharPool (cn.hutool.core.text.CharPool)1 ArrayUtil (cn.hutool.core.util.ArrayUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1