support sync container by force (#399)

* support tt automation by job

* format

* import order

* use map to avoid repetition

* add api for sync from ccp by force

* fix bug

* code refactory

* delete unused code

* delete func

Co-authored-by: dujiashu <dujiashu@didiglobal.com>
This commit is contained in:
dujiashu 2020-11-13 21:22:23 +08:00 committed by GitHub
parent ffc98f31c9
commit d4adafbcb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 100 additions and 60 deletions

View File

@ -1,15 +1,15 @@
package models
import (
"log"
"path"
"time"
"log"
"path"
"time"
"xorm.io/core"
"xorm.io/xorm"
"xorm.io/core"
"xorm.io/xorm"
"github.com/toolkits/pkg/file"
"github.com/toolkits/pkg/runner"
"github.com/toolkits/pkg/file"
"github.com/toolkits/pkg/runner"
)
type MySQLConf struct {

View File

@ -129,6 +129,7 @@ func Config(r *gin.Engine) {
userLogin.GET("/resources/bindings", resourceBindingsGet)
userLogin.GET("/resources/orphan", resourceOrphanGet)
userLogin.POST("/container/sync", containerSyncPost)
}
v1 := r.Group("/v1/rdb").Use(shouldBeService())

View File

@ -1,7 +1,6 @@
package http
import (
"github.com/didi/nightingale/src/models"
"github.com/gin-gonic/gin"
)
@ -38,57 +37,7 @@ func v1ContainersBindPost(c *gin.Context) {
bomb("items empty")
}
for i := 0; i < count; i++ {
items[i].Validate()
node := Node(items[i].NID)
if node.Leaf != 1 {
bomb("node not leaf")
}
res, err := models.ResourceGet("uuid=?", items[i].UUID)
dangerous(err)
if res != nil {
// 这个资源之前就已经存在过了,这次可能是更新了部分字段
res.Name = items[i].Name
res.Labels = items[i].Labels
res.Extend = items[i].Extend
dangerous(res.Update("name", "labels", "extend"))
} else {
// 之前没有过这个资源在RDB注册这个资源
res = new(models.Resource)
res.UUID = items[i].UUID
res.Ident = items[i].Ident
res.Name = items[i].Name
res.Labels = items[i].Labels
res.Extend = items[i].Extend
res.Cate = items[i].Cate
res.Tenant = node.Tenant()
dangerous(res.Save())
}
dangerous(node.Bind([]int64{res.Id}))
// 第二个挂载位置inner.${cate}
innerCatePath := "inner." + node.Ident
innerCateNode, err := models.NodeGet("path=?", innerCatePath)
dangerous(err)
if innerCateNode == nil {
innerNode, err := models.NodeGet("path=?", "inner")
dangerous(err)
if innerNode == nil {
bomb("inner node not exists")
}
innerCateNode, err = innerNode.CreateChild(node.Ident, node.Name, "", node.Cate, "system", 1, 1, []int64{})
dangerous(err)
}
dangerous(innerCateNode.Bind([]int64{res.Id}))
}
resourceHttpRegister(count, items)
renderMessage(c, nil)
}

View File

@ -1,6 +1,7 @@
package http
import (
"fmt"
"strings"
"github.com/gin-gonic/gin"
@ -17,6 +18,39 @@ func resourceSearchGet(c *gin.Context) {
renderData(c, list, err)
}
type containerSyncForm struct {
Name string `json:"name" binding:"required"`
Items []v1ContainersRegisterItem `json:"items"`
}
func containerSyncPost(c *gin.Context) {
var sf containerSyncForm
bind(c, &sf)
var (
uuids []string
)
list, err := models.ResourceGets("labels like ?",
fmt.Sprintf("%%,resourceName=%s%%", sf.Name))
dangerous(err)
for _, l := range list {
uuids = append(uuids, l.UUID)
}
dangerous(models.ResourceUnregister(uuids))
count := len(sf.Items)
if count == 0 {
return
}
resourceHttpRegister(count, sf.Items)
renderMessage(c, "")
}
type resourceNotePutForm struct {
Ids []int64 `json:"ids" binding:"required"`
Note string `json:"note"`
@ -28,6 +62,62 @@ func (f resourceNotePutForm) Validate() {
}
}
func resourceHttpRegister(count int, items []v1ContainersRegisterItem) {
for i := 0; i < count; i++ {
items[i].Validate()
node := Node(items[i].NID)
if node.Leaf != 1 {
bomb("node not leaf")
}
res, err := models.ResourceGet("uuid=?", items[i].UUID)
dangerous(err)
if res != nil {
// 这个资源之前就已经存在过了,这次可能是更新了部分字段
res.Name = items[i].Name
res.Labels = items[i].Labels
res.Extend = items[i].Extend
dangerous(res.Update("name", "labels", "extend"))
} else {
// 之前没有过这个资源在RDB注册这个资源
res = new(models.Resource)
res.UUID = items[i].UUID
res.Ident = items[i].Ident
res.Name = items[i].Name
res.Labels = items[i].Labels
res.Extend = items[i].Extend
res.Cate = items[i].Cate
res.Tenant = node.Tenant()
dangerous(res.Save())
}
dangerous(node.Bind([]int64{res.Id}))
// 第二个挂载位置inner.${cate}
innerCatePath := "inner." + node.Ident
innerCateNode, err := models.NodeGet("path=?", innerCatePath)
dangerous(err)
if innerCateNode == nil {
innerNode, err := models.NodeGet("path=?", "inner")
dangerous(err)
if innerNode == nil {
bomb("inner node not exists")
}
innerCateNode, err = innerNode.CreateChild(node.Ident, node.Name, "", node.Cate, "system", 1, 1, []int64{})
dangerous(err)
}
dangerous(innerCateNode.Bind([]int64{res.Id}))
}
return
}
// 游离资源页面修改备注,超级管理员,或者是租户管理员
func resourceNotePut(c *gin.Context) {
var f resourceNotePutForm

View File

@ -146,4 +146,4 @@ func roleGlobalUsersUnbind(c *gin.Context) {
func v1RoleOperationGets(c *gin.Context) {
objs, err := models.RoleOperationAll()
renderData(c, objs, err)
}
}