Skip to content

Commit

Permalink
feat: 脚本创建时可自定义版本号名称
Browse files Browse the repository at this point in the history
  • Loading branch information
fireyun committed Feb 21, 2024
1 parent 303c7a8 commit 4f8ef33
Show file tree
Hide file tree
Showing 15 changed files with 2,689 additions and 2,643 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
该接口提供版本:v1.0.0+

创建hook脚本,为某个app设置对应的前置、后置hook脚本。
创建hook脚本时,会默认创建一个脚本版本,版本名称由系统生成
创建hook脚本时,会默认创建一个脚本版本。

#### 输入参数

Expand All @@ -15,6 +15,7 @@
| tag | string || 脚本标签 |
| memo | string || 脚本描述 |
| content | string || 脚本内容 |
| revision_name | string || 脚本版本号名称,可选项,不填时系统自动生成,生成格式为v20230904033251。最大长度128个字符,仅允许使用中文、英文、数字、下划线、中划线、点,且必须以中文、英文、数字开头和结尾 |

#### 调用示例

Expand All @@ -24,7 +25,8 @@
"type": "shell",
"tag": "自动化脚本2",
"content": "#!/bin/bash\n\nnow=$(date +'%Y-%m-%d %H:%M:%S')\necho \"hello, start at $now\"\n",
"memo": "from datetime import datetime\nprint(\"hello, end at\", datetime.now())\n"
"memo": "from datetime import datetime\nprint(\"hello, end at\", datetime.now())\n",
"revision_name": "v1"
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@


创建hook版本,新建的版本状态为“未上线”,hook下存在一个“未上线”版本时,不能创建新的版本
版本名称为系统自动生成,不需要填写

### 输入参数

| 参数名称 | 参数类型 | 必选 | 描述 |
| -------- | -------- | ---- | -------- |
| biz_id | uint32 || 业务ID |
| hook_id | uint32 || 脚本ID |
| name | string || 脚本版本号名称,可选项,不填时系统自动生成,生成格式为v20230904033251。最大长度128个字符,仅允许使用中文、英文、数字、下划线、中划线、点,且必须以中文、英文、数字开头和结尾 |
| memo | string || 版本日志 |
| content | string || 脚本内容 |

### 调用示例

```json
{
"name": "v1",
"content": "#!/bin/bash\n\nnow=$(date +'%Y-%m-%d %H:%M:%S')\necho \"hello, start at $now\"\n",
"memo": "from datetime import datetime"
}
Expand Down
11 changes: 6 additions & 5 deletions bcs-services/bcs-bscp/cmd/config-server/service/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ func (s *Service) CreateHook(ctx context.Context, req *pbcs.CreateHookReq) (*pbc
BizId: grpcKit.BizID,
},
Spec: &pbhook.HookSpec{
Name: req.Name,
Type: req.Type,
Tag: req.Tag,
Memo: req.Memo,
Content: req.Content,
Name: req.Name,
Type: req.Type,
Tag: req.Tag,
RevisionName: req.RevisionName,
Memo: req.Memo,
Content: req.Content,
},
}
rp, err := s.client.DS.CreateHook(grpcKit.RpcCtx(), r)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
pbcs "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/config-server"
pbhr "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/core/hook-revision"
pbds "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/data-service"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/tools"
)

// CreateHookRevision create hook revision with option
Expand All @@ -44,7 +43,7 @@ func (s *Service) CreateHookRevision(ctx context.Context,
HookId: req.HookId,
},
Spec: &pbhr.HookRevisionSpec{
Name: tools.GenerateRevisionName(),
Name: req.Name,
Content: req.Content,
Memo: req.Memo,
},
Expand Down
3 changes: 1 addition & 2 deletions bcs-services/bcs-bscp/cmd/data-service/service/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
pbbase "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/core/base"
pbhook "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/core/hook"
pbds "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/data-service"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/tools"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/types"
)

Expand Down Expand Up @@ -71,7 +70,7 @@ func (s *Service) CreateHook(ctx context.Context, req *pbds.CreateHookReq) (*pbd
// 2. create hook revision
revision := &table.HookRevision{
Spec: &table.HookRevisionSpec{
Name: tools.GenerateRevisionName(),
Name: req.Spec.RevisionName,
Content: req.Spec.Content,
Memo: req.Spec.Memo,
State: table.HookRevisionStatusDeployed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
pbbase "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/core/base"
hr "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/core/hook-revision"
pbds "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/data-service"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/tools"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/types"
)

Expand All @@ -43,6 +44,10 @@ func (s *Service) CreateHookRevision(ctx context.Context,
}

spec, err := req.Spec.HookRevisionSpec()
// if no revision name is specified, generate it by system
if spec.Name == "" {
spec.Name = tools.GenerateRevisionName()
}
spec.State = table.HookRevisionStatusNotDeployed
if err != nil {
logs.Errorf("get HookRevisionSpec spec from pb failed, err: %v, rid: %s", err, kt.Rid)
Expand Down
2 changes: 1 addition & 1 deletion bcs-services/bcs-bscp/cmd/data-service/service/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *Service) CreateTemplate(ctx context.Context, req *pbds.CreateTemplateRe

// 2. create template revision
spec := req.TrSpec.TemplateRevisionSpec()
spec.RevisionName = generateRevisionName()
spec.RevisionName = tools.GenerateRevisionName()
templateRevision := &table.TemplateRevision{
Spec: spec,
Attachment: &table.TemplateRevisionAttachment{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package service
import (
"context"
"fmt"
"time"

"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/dal/gen"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/dal/table"
Expand All @@ -25,6 +24,7 @@ import (
pbtr "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/core/template-revision"
pbds "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/data-service"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/search"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/tools"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/types"
)

Expand All @@ -51,7 +51,7 @@ func (s *Service) CreateTemplateRevision(ctx context.Context,
spec := req.Spec.TemplateRevisionSpec()
// if no revision name is specified, generate it by system
if spec.RevisionName == "" {
spec.RevisionName = generateRevisionName()
spec.RevisionName = tools.GenerateRevisionName()
}

// keep the revision's name and path same with template
Expand Down Expand Up @@ -238,16 +238,10 @@ func getLatestTmplRevisions(tmplRevisions []*table.TemplateRevision) map[uint32]
return latestRevisionMap
}

func generateRevisionName() string {
// Format the current time as YYYYMMDDHHMMSS
timestamp := time.Now().Format("20060102150405")
return fmt.Sprintf("v%s", timestamp)
}

func (s *Service) doCreateTemplateRevisions(kt *kit.Kit, tx *gen.QueryTx, data []*table.TemplateRevision) error {
for i := range data {
// 生成 RevisionName
data[i].Spec.RevisionName = generateRevisionName()
data[i].Spec.RevisionName = tools.GenerateRevisionName()
}
// Write template revisions table
if err := s.dao.TemplateRevision().BatchCreateWithTx(kt, tx, data); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions bcs-services/bcs-bscp/pkg/dal/table/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func (s HookSpec) ValidateCreate() error {
return err
}

if err := validator.ValidateMemo(s.Memo, false); err != nil {
return err
}

if err := s.Type.Validate(); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions bcs-services/bcs-bscp/pkg/dal/table/hook_revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (r *HookRevision) ValidateCreate() error {
// ValidateCreate validate spec when created.
func (s *HookRevisionSpec) ValidateCreate() error {

if err := validator.ValidateName(s.Name); err != nil {
if err := validator.ValidateReleaseName(s.Name); err != nil {
return err
}

Expand All @@ -115,7 +115,7 @@ func (s *HookRevisionSpec) ValidateCreate() error {
// ValidateUpdate validate spec when updated.
func (s *HookRevisionSpec) ValidateUpdate() error {

if err := validator.ValidateName(s.Name); err != nil {
if err := validator.ValidateReleaseName(s.Name); err != nil {
return err
}

Expand Down
8 changes: 8 additions & 0 deletions bcs-services/bcs-bscp/pkg/dal/table/template_revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ type TemplateRevisionSpec struct {

// ValidateCreate validate template revision spec when it is created.
func (t *TemplateRevisionSpec) ValidateCreate() error {
if err := validator.ValidateReleaseName(t.RevisionName); err != nil {
return err
}

if err := validator.ValidateMemo(t.RevisionMemo, false); err != nil {
return err
}

if err := validator.ValidateFileName(t.Name); err != nil {
return err
}
Expand Down
Loading

0 comments on commit 4f8ef33

Please sign in to comment.