Skip to content

Commit

Permalink
feat(dbm-services): 资源池匹配支持筛选os版本 TencentBlueKing#8333
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq committed Dec 3, 2024
1 parent 3a857db commit 272a46c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
10 changes: 10 additions & 0 deletions dbm-services/common/db-resource/internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (c *BackStageHandler) RegisterRouter(engine *gin.Engine) {
{
r.POST("/cc/module/check", c.RunModuleCheck)
r.POST("/cc/async", c.RunAsyncCmdb)
r.POST("/cc/sync/os/info", c.SyncOsInfo)
}
}

Expand All @@ -96,3 +97,12 @@ func (c BackStageHandler) RunAsyncCmdb(r *gin.Context) {
}
c.SendResponse(r, nil, "async success")
}

// SyncOsInfo sync os info
func (c BackStageHandler) SyncOsInfo(r *gin.Context) {
err := task.SyncOsNameInfo()
if err != nil {
logger.Error("SyncOsNameInfo failed %v", err)
}
c.SendResponse(r, nil, "async success")
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"dbm-services/common/db-resource/internal/svr/bk"
"dbm-services/common/db-resource/internal/svr/task"
"dbm-services/common/db-resource/internal/svr/yunti"
"dbm-services/common/db-resource/internal/util"
"dbm-services/common/go-pubpkg/cc.v3"
"dbm-services/common/go-pubpkg/errno"
"dbm-services/common/go-pubpkg/logger"
Expand Down Expand Up @@ -377,7 +378,7 @@ func (p ImportMachParam) transHostInfoToDbModule(h *cc.Host, bkCloudId int, labe
OsType: model.ConvertOsTypeToHuman(osType),
OsBit: h.BkOsBit,
OsVerion: h.BkOsVersion,
OsName: strings.TrimSpace(strings.ToLower(strings.ReplaceAll(h.OSName, " ", ""))),
OsName: util.CleanOsName(h.OSName),
UpdateTime: time.Now(),
CreateTime: time.Now(),
}
Expand Down
37 changes: 37 additions & 0 deletions dbm-services/common/db-resource/internal/svr/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"dbm-services/common/db-resource/internal/model"
"dbm-services/common/db-resource/internal/svr/bk"
"dbm-services/common/db-resource/internal/util"
"dbm-services/common/go-pubpkg/cc.v3"
"dbm-services/common/go-pubpkg/cmutil"
"dbm-services/common/go-pubpkg/logger"
Expand Down Expand Up @@ -215,3 +216,39 @@ func AsyncResourceHardInfo() (err error) {
}
return nil
}

// SyncOsNameInfo sync os name info
func SyncOsNameInfo() (err error) {
logger.Info("start async from cmdb ...")
var rsList []model.TbRpDetail
err = model.DB.Self.Table(model.TbRpDetailName()).Find(&rsList).Error
if err != nil {
if err == sql.ErrNoRows {
return nil
}
logger.Error("query total_storage_cap less than 0,err %w ", err)
return err
}
bizHostMap := make(map[int][]string)
for _, rs := range rsList {
bizHostMap[rs.BkBizId] = append(bizHostMap[rs.BkBizId], rs.IP)
}
for bizId, hosts := range bizHostMap {
ccInfos, _, err := bk.BatchQueryHostsInfo(bizId, hosts)
if err != nil {
logger.Warn("query machine hardinfo from cmdb failed %s", err.Error())
continue
}
for _, ccInfo := range ccInfos {
err = model.DB.Self.Table(model.TbRpDetailName()).Where("ip = ? and bk_biz_id = ? ", ccInfo.InnerIP, bizId).
Updates(map[string]interface{}{
"os_name": util.CleanOsName(ccInfo.OSName),
"os_version": ccInfo.BkOsVersion,
}).Error
if err != nil {
logger.Warn("request cmdb api failed %s", err.Error())
}
}
}
return nil
}
31 changes: 31 additions & 0 deletions dbm-services/common/db-resource/internal/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

// Package util TODO
package util

import (
"regexp"
"strings"
)

// CleanOsName clean os name
func CleanOsName(osName string) string {
tr := regexp.MustCompile(`(?i)^tencent`)
if tr.MatchString(osName) {
r := regexp.MustCompile(`\d+(.\d)+`)
return "tliunx-" + r.FindString(osName)
}
wr := regexp.MustCompile(`(?i)Windows\s*Server\s*\d\d\d\d`)
if wr.MatchString(osName) {
return strings.TrimSpace(strings.ReplaceAll(wr.FindString(osName), " ", ""))
}
return osName
}

0 comments on commit 272a46c

Please sign in to comment.