From 936c31b5b04a713fd35fe5f4d85c4ce193626826 Mon Sep 17 00:00:00 2001 From: mystarry-sky Date: Wed, 23 Apr 2025 07:51:14 +0800 Subject: [PATCH] =?UTF-8?q?openGauss=E5=AE=89=E8=A3=85=E5=8C=85=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E8=87=AA=E5=8A=A8=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ops/InstallPackageAutoUpgrade.java | 136 ++++++++++++++++++ .../src/main/resources/application.yml | 5 + 2 files changed, 141 insertions(+) create mode 100644 plugins/base-ops/src/main/java/org/opengauss/admin/plugin/service/ops/InstallPackageAutoUpgrade.java diff --git a/plugins/base-ops/src/main/java/org/opengauss/admin/plugin/service/ops/InstallPackageAutoUpgrade.java b/plugins/base-ops/src/main/java/org/opengauss/admin/plugin/service/ops/InstallPackageAutoUpgrade.java new file mode 100644 index 000000000..5d6eaccfd --- /dev/null +++ b/plugins/base-ops/src/main/java/org/opengauss/admin/plugin/service/ops/InstallPackageAutoUpgrade.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved. + * + * openGauss is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FITFOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + * ------------------------------------------------------------------------- + * + * InstallPackageAutoUpgrade.java + * + * IDENTIFICATION + * org.opengauss.admin.plugin.service.ops.InstallPackageAutoUpgrade.java + * + * ------------------------------------------------------------------------- + */ + +package org.opengauss.admin.plugin.service.ops; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; + +import cn.hutool.core.lang.Snowflake; +import cn.hutool.core.util.IdUtil; +import cn.hutool.core.util.StrUtil; +import lombok.extern.slf4j.Slf4j; + +import org.opengauss.admin.plugin.domain.entity.ops.OpsPackageManagerEntity; +import org.opengauss.admin.plugin.domain.entity.ops.OpsPackagePathDictEntity; +import org.opengauss.admin.plugin.domain.model.ops.OpsPackagePathDictVO; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +import java.util.Date; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; + +import javax.annotation.Resource; + +/** + * openGauss InstallPackageAutoUpgrade + * + * @author: wangchao + * @Date: 2025/4/22 16:27 + * @Description: PackageAutoUpgrade + * @since 7.0.0-RC1 + **/ +@Slf4j +@Component +public class InstallPackageAutoUpgrade implements ApplicationRunner { + private static final Snowflake SNOWFLAKE = IdUtil.getSnowflake(1, 1); + + @Resource + private IOpsPackageManagerV2Service opsPackageManagerV2Service; + @Resource + private IOpsPackagePathDictService opsPackagePathDictService; + @Value("${install.package.upgrade.history-version}") + private String historyVersion; + @Value("${install.package.upgrade.old-history-version}") + private String oldHistoryVersion; + @Value("${installPackage.urlPrefix}") + private String pkgUrlPrefix; + + @Override + public void run(ApplicationArguments args) throws Exception { + log.info("InstallPackageAutoUpgrade run"); + List list = opsPackagePathDictService.listPackagePathDict(); + List versionDictList = list.stream() + .filter(dict -> !StrUtil.equalsIgnoreCase(dict.getPkgTmpUseVersion(), oldHistoryVersion)) + .collect(Collectors.toList()); + upgradePackageDict(versionDictList); + upgradePackageManager(versionDictList); + log.info("InstallPackageAutoUpgrade finished"); + } + + private void upgradePackageDict(List versionDictList) { + OpsPackagePathDictVO dictVO = versionDictList.get(0); + if (Objects.equals(dictVO.getPkgTmpUseVersion(), historyVersion)) { + log.info("InstallPackageAutoUpgrade version is already latest"); + return; + } + List dictIds = versionDictList.stream().map(OpsPackagePathDictVO::getId).collect(Collectors.toList()); + opsPackagePathDictService.update(Wrappers.lambdaUpdate() + .set(OpsPackagePathDictEntity::getPkgTmpUseVersion, historyVersion) + .in(OpsPackagePathDictEntity::getId, dictIds)); + log.info("InstallPackageAutoUpgrade version dict is upgrade to: {}", historyVersion); + } + + private void upgradePackageManager(List versionDictList) { + List packageManagerList = opsPackageManagerV2Service.list(); + Set packageNumSet = packageManagerList.stream() + .map(OpsPackageManagerEntity::getPackageVersionNum) + .collect(Collectors.toSet()); + String[] historyVersions = historyVersion.split(";"); + for (String version : historyVersions) { + if (!packageNumSet.contains(version)) { + upgradePackageManager(versionDictList, version); + log.info("InstallPackageAutoUpgrade package manager version: {}", version); + } + } + } + + private void upgradePackageManager(List versionDictList, String version) { + versionDictList.forEach(dict -> { + OpsPackageManagerEntity entity = new OpsPackageManagerEntity(); + entity.setPackageId(SNOWFLAKE.nextIdStr()); + entity.setOs(dict.getOs()); + entity.setOsVersion(dict.getOsVersion()); + entity.setCpuArch(dict.getCpuArch()); + entity.setName(buildPackageManagerName(dict.getPackageNameTmp(), version)); + entity.setPackageVersion(dict.getVersion()); + entity.setPackageVersionNum(version); + entity.setPackageVersionNum(version); + entity.setType("openGauss"); + entity.setPackageUrl(dict.buildFullPackageUrl(pkgUrlPrefix, version, false)); + entity.setCreateBy("admin"); + entity.setUpdateBy("admin"); + entity.setCreateTime(new Date()); + entity.setUpdateTime(new Date()); + opsPackageManagerV2Service.save(entity); + }); + } + + private String buildPackageManagerName(String packageNameTmp, String version) { + return packageNameTmp.replace("%s", version).replace(".tar.gz", ""); + } +} diff --git a/plugins/base-ops/src/main/resources/application.yml b/plugins/base-ops/src/main/resources/application.yml index b14367f9f..3864146de 100644 --- a/plugins/base-ops/src/main/resources/application.yml +++ b/plugins/base-ops/src/main/resources/application.yml @@ -1,6 +1,11 @@ context: listener: classes: org.opengauss.admin.plugin.listener.BaseOpsPluginListener +install: + package: + upgrade: + old-history-version: 3.0.0;3.0.3;3.0.5;3.1.0;3.1.1;5.0.0;5.0.1;5.0.2;5.0.3;5.1.0;6.0.0-RC1 + history-version: 6.0.0;7.0.0-RC1 spring: resources: static-locations: classpath:resources -- Gitee