Merge branch 'refs/heads/local-ss' into main

This commit is contained in:
zeaslity
2024-04-29 15:22:04 +08:00
53 changed files with 835 additions and 1293 deletions

View File

@@ -0,0 +1,74 @@
package cmii
import (
"bytes"
"io.wdd.message_pusher/pusher"
"text/template"
)
type ImageSyncMessage struct {
ImageFullName string `json:"imageFullName"`
ProjectName string `json:"projectName"`
ProjectNameSpace string `json:"projectNameSpace"`
ProjectMasterIP string `json:"projectMasterIP"`
ProjectMasterTopicName string `json:"projectMasterTopicName"`
InnerWorkTopicName string `json:"innerWorkTopicName"`
CurrentProcedure string `json:"currentProcedure"`
IsSyncFinished bool `json:"isSyncFinished"`
}
const imageSyncTemplate = `
{{if .IsSyncFinished}}
镜像更新状态: 成功😍
{{- else }}
镜像更新状态: 失败👿👿👿
{{- end}}
当前步骤: {{.CurrentProcedure}}
更新镜像: {{.ImageFullName}}
项目名称: {{.ProjectName}}
项目空间: {{.ProjectNameSpace}}
项目MasterIP: {{.ProjectMasterIP}}
项目Master名称: {{.ProjectMasterTopicName}}
中转节点名称: {{.InnerWorkTopicName}}
`
const ImageSyncTopicName = "imageSync"
var ImageSyncPushOptions = []pusher.PublishOption{
pusher.WithTitle("镜像同步更新"),
pusher.WithPriority("3"),
}
func (d *ImageSyncMessage) ParseTemplate() bytes.Buffer {
// 解析模板
tmpl, err := template.New("imageSyncTemplate").Parse(imageSyncTemplate)
if err != nil {
panic(err)
}
// 应用数据并打印结果
var result bytes.Buffer
err = tmpl.Execute(&result, d)
if err != nil {
panic(err)
}
return result
}
// PublishMessage 使用默认的Client 发布更新的消息
func (d *ImageSyncMessage) PublishMessage() *pusher.Message {
parseTemplate := d.ParseTemplate()
pusher.DefaultPusherClient.ChangeTopicName(ImageSyncTopicName)
result, err := pusher.DefaultPusherClient.PublishDefault(parseTemplate, ImageSyncPushOptions)
if err != nil {
log.ErrorF("[ImageSyncMessage] - message push error %s", err.Error())
return result
}
return result
}

View File

@@ -45,6 +45,7 @@ func main() {
{
router.CMIIRouter(engine)
router.OctopusRouter(engine)
router.ImageSyncRouter(engine)
}
engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

View File

@@ -68,6 +68,10 @@ func NewDefaultClient() *Client {
return New(defaultConfig)
}
func (c *Client) ChangeTopicName(topicName string) {
c.config.DefaultTopic = topicName
}
func (c *Client) PublishDefault(message bytes.Buffer, options []PublishOption) (*Message, error) {
if c.config.DefaultTopic == "" {
return nil, errors.New("[PublishDefault] - topic empty")
@@ -135,17 +139,17 @@ func (c *Client) expandTopicURL(topic string) (string, error) {
if !topicRegex.MatchString(topic) {
return "", fmt.Errorf("invalid topic name: %s", topic)
}
return fmt.Sprintf("%s/%s", c.config.DefaultHost, topic), nil
return fmt.Sprintf("%s/%s", c.config.Host, topic), nil
}
func (c *Client) parseConfigToOption(options []PublishOption) []PublishOption {
config := c.config
if config.DefaultToken != "" {
options = append(options, WithBearerAuth(config.DefaultToken))
} else if config.DefaultUser != "" {
if *config.DefaultPassword != "" {
options = append(options, WithBasicAuth(config.DefaultUser, *config.DefaultPassword))
if config.Token != "" {
options = append(options, WithBearerAuth(config.Token))
} else if config.User != "" {
if *config.Password != "" {
options = append(options, WithBasicAuth(config.User, *config.Password))
} else {
log.ErrorF("[parseConfigToOption] - default password is empty!")
}

View File

@@ -16,13 +16,13 @@ const (
// Config is the config struct for a Client
type Config struct {
DefaultHost string `yaml:"default-host"`
DefaultUser string `yaml:"default-user"`
DefaultPassword *string `yaml:"default-password"`
DefaultToken string `yaml:"default-token"`
DefaultCommand string `yaml:"default-command"`
DefaultTopic string `yaml:"default-topic"`
Subscribe []Subscribe `yaml:"subscribe"`
Host string `yaml:"default-host"`
User string `yaml:"default-user"`
Password *string `yaml:"default-password"`
Token string `yaml:"default-token"`
DefaultCommand string `yaml:"default-command"`
DefaultTopic string `yaml:"default-topic"`
Subscribe []Subscribe `yaml:"subscribe"`
}
// Subscribe is the struct for a Subscription within Config
@@ -38,13 +38,13 @@ type Subscribe struct {
// NewDefaultConfig creates a new Config struct for a Client
func NewDefaultConfig() *Config {
return &Config{
DefaultHost: DefaultBaseURL,
DefaultUser: "",
DefaultPassword: nil,
DefaultToken: DefaultBaseToken,
DefaultTopic: DefaultTopic,
DefaultCommand: "",
Subscribe: nil,
Host: DefaultBaseURL,
User: "",
Password: nil,
Token: DefaultBaseToken,
DefaultTopic: DefaultTopic,
DefaultCommand: "",
Subscribe: nil,
}
}

View File

@@ -3,9 +3,12 @@ package router
import (
"github.com/gin-gonic/gin"
"io.wdd.message_pusher/cmii"
"io.wdd.message_pusher/pusher"
"net/http"
)
const CmiiUpdateTopicName = "cmii"
func CMIIRouter(r *gin.Engine) {
cmiiGroup := r.Group("/cmii")
@@ -37,6 +40,8 @@ func CmiiUpdate(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
pusher.DefaultPusherClient.ChangeTopicName(CmiiUpdateTopicName)
// 处理请求
upgradeMessage := messageBody.DefaultPushUpgradeMessage()

View File

@@ -0,0 +1,38 @@
package router
import (
"github.com/gin-gonic/gin"
"io.wdd.message_pusher/cmii"
"net/http"
)
func ImageSyncRouter(r *gin.Engine) {
octopusGroup := r.Group("/image")
{
octopusGroup.POST("/sync", ImageSync)
}
}
// ImageSync godoc
// @Summary 镜像同步消息
// @Schemes
// @Description response to cmii update notification
// @Tags ImageSync
// @Accept json
// @Produce json
// @Param body body cmii.ImageSyncMessage true "请求体"
// @Success 200 {object} pusher.Message
// @Router /image/sync [post]
func ImageSync(c *gin.Context) {
// 获取请求中的参数
var imageSyncMessage cmii.ImageSyncMessage
if err := c.ShouldBindJSON(&imageSyncMessage); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
// 处理请求
upgradeMessage := imageSyncMessage.PublishMessage()
// *pusher.Message
c.JSON(http.StatusOK, upgradeMessage)
}