持续集成处理部分完成,修改表结构。
This commit is contained in:
parent
2501db94f5
commit
c4b96f9a9f
23
dao/cid.go
23
dao/cid.go
|
|
@ -11,7 +11,7 @@ type CID struct {
|
|||
Name string `gorm:"column:name"`
|
||||
Url string `gorm:"column:url"`
|
||||
Script string `gorm:"column:script"`
|
||||
End string `gorm:"column:end"`
|
||||
Token string `gorm:"column:token"` // 用于外部回调
|
||||
}
|
||||
|
||||
type CIDRunLog struct {
|
||||
|
|
@ -19,11 +19,12 @@ type CIDRunLog struct {
|
|||
CID_id int `gorm:"column:cid_id"`
|
||||
Auth_id int `form:"column:auth_id"`
|
||||
Log string `gorm:"column:log"`
|
||||
Error string `gorm:"column:error"`
|
||||
}
|
||||
|
||||
// CreateCID 创建持续集成、部署
|
||||
func CreateCID(name, url, script, end string, auth_id int) uint {
|
||||
cid := CID{Name: name, Url: url, Script: script, End: end, Auth_id: auth_id}
|
||||
func CreateCID(name, url, script, token string, auth_id int) uint {
|
||||
cid := CID{Name: name, Url: url, Script: script, Token: token, Auth_id: auth_id}
|
||||
result := DB.Debug().Create(&cid)
|
||||
if result.Error != nil {
|
||||
return 0
|
||||
|
|
@ -55,12 +56,16 @@ func FindCIDByAuthID(auth_id int) []CID {
|
|||
}
|
||||
|
||||
// UpdateCIDByID 更新持续集成、部署
|
||||
func UpdateCIDByID(id, auth_id int, name, url, script, end string) bool {
|
||||
func UpdateCIDByID(id, auth_id int, name, url, script, token string) bool {
|
||||
pd := FindCIDByID(id, auth_id)
|
||||
if pd.ID == 0 {
|
||||
return false
|
||||
}
|
||||
result := DB.Debug().Model(&CID{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(CID{Name: name, Url: url, Script: script, End: end})
|
||||
// 如果token为空,则不更新token
|
||||
if token == "" {
|
||||
token = pd.Token
|
||||
}
|
||||
result := DB.Debug().Model(&CID{}).Where("id = ? and auth_id = ?", id, auth_id).Updates(CID{Name: name, Url: url, Script: script, Token: token})
|
||||
if result.Error != nil {
|
||||
return false
|
||||
}
|
||||
|
|
@ -68,10 +73,10 @@ func UpdateCIDByID(id, auth_id int, name, url, script, end string) bool {
|
|||
}
|
||||
|
||||
// CreateRunLog,添加执行日志
|
||||
func CreateRunLog(cid_id, auth_id int, log string) uint {
|
||||
cidRunLog := CIDRunLog{CID_id: cid_id, Auth_id: auth_id, Log: log}
|
||||
err := DB.Debug().Model(cidRunLog)
|
||||
if err != nil {
|
||||
func CreateRunLog(cid_id, auth_id int, log, err string) uint {
|
||||
cidRunLog := CIDRunLog{CID_id: cid_id, Auth_id: auth_id, Log: log, Error: err}
|
||||
result := DB.Debug().Create(&cidRunLog)
|
||||
if result != nil {
|
||||
fmt.Println(err)
|
||||
return 0
|
||||
}
|
||||
|
|
|
|||
118
handler/cid.go
118
handler/cid.go
|
|
@ -1,31 +1,139 @@
|
|||
package handler
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/gin-gonic/gin"
|
||||
"os"
|
||||
"os/exec"
|
||||
"videoplayer/dao"
|
||||
"videoplayer/proto"
|
||||
)
|
||||
|
||||
type CIDCreateReq struct {
|
||||
Name string `json:"name" form:"name"`
|
||||
Url string `json:"url" form:"url"`
|
||||
Script string `json:"script" form:"script"`
|
||||
}
|
||||
|
||||
type CIDDeleteReq struct {
|
||||
ID int `json:"id" form:"id"`
|
||||
}
|
||||
|
||||
type CIDRunReq struct {
|
||||
ID int `json:"id" form:"id"`
|
||||
}
|
||||
|
||||
type CIDUpdateReq struct {
|
||||
ID int `json:"id" form:"id"`
|
||||
Name string `json:"name" form:"name"`
|
||||
Url string `json:"url" form:"url"`
|
||||
Script string `json:"script" form:"script"`
|
||||
Token string `json:"token" form:"token"`
|
||||
}
|
||||
|
||||
func SetUpCIDGroup(router *gin.Engine) {
|
||||
cidGroup := router.Group("/cid") //持续集成、部署
|
||||
cidGroup.POST("/create", GetImKey)
|
||||
cidGroup.POST("/create", CreateCID)
|
||||
cidGroup.POST("/delete", DeleteCID)
|
||||
cidGroup.POST("/update", UpdateCID)
|
||||
cidGroup.POST("/list", GetCIDList)
|
||||
cidGroup.POST("/run", RunCID)
|
||||
}
|
||||
func RunCID(c *gin.Context) {
|
||||
|
||||
var req CIDRunReq
|
||||
if err := c.ShouldBind(&req); err == nil {
|
||||
// 获取用户ID
|
||||
id, _ := c.Get("id")
|
||||
authID := int(id.(float64))
|
||||
cid := dao.FindCIDByID(req.ID, authID)
|
||||
if cid.ID == 0 {
|
||||
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
|
||||
return
|
||||
} else {
|
||||
scriptContent := ``
|
||||
str, _ := generateRandomHexString(16)
|
||||
scriptName := "" + str + `.sh`
|
||||
// 写入脚本
|
||||
err2 := os.WriteFile(scriptName, []byte(scriptContent), 0755)
|
||||
if err2 != nil {
|
||||
c.JSON(200, gin.H{"error": err2.Error(), "code": proto.OperationFailed, "message": "failed"})
|
||||
return
|
||||
} else {
|
||||
//执行脚本
|
||||
cmd := exec.Command("/bin/bash", "-c", scriptName)
|
||||
err3 := cmd.Run()
|
||||
// 使用bytes.Buffer捕获输出
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
dao.CreateRunLog(req.ID, authID, out.String(), err3.Error()) //添加执行日志
|
||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||
}
|
||||
}
|
||||
|
||||
func CreateCID(c *gin.Context) {
|
||||
var req CIDCreateReq
|
||||
if err := c.ShouldBind(&req); err == nil {
|
||||
// 获取用户ID
|
||||
id, _ := c.Get("id")
|
||||
authID := int(id.(float64))
|
||||
token, _ := generateRandomHexString(32)
|
||||
|
||||
res := dao.CreateCID(req.Name, req.Url, req.Script, token, authID)
|
||||
if res != 0 {
|
||||
c.JSON(200, gin.H{})
|
||||
} else {
|
||||
c.JSON(200, gin.H{"error": "CreateCID failed", "code": proto.OperationFailed, "message": "failed"})
|
||||
}
|
||||
} else {
|
||||
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteCID(c *gin.Context) {
|
||||
|
||||
var req CIDDeleteReq
|
||||
if err := c.ShouldBind(&req); err == nil {
|
||||
// 获取用户ID
|
||||
id, _ := c.Get("id")
|
||||
authID := int(id.(float64))
|
||||
cid := dao.DeleteCIDByID(req.ID, authID)
|
||||
if cid == false {
|
||||
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
|
||||
return
|
||||
} else {
|
||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
|
||||
}
|
||||
} else {
|
||||
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateCID(c *gin.Context) {
|
||||
var req CIDUpdateReq
|
||||
if err := c.ShouldBind(&req); err == nil {
|
||||
// 获取用户ID
|
||||
id, _ := c.Get("id")
|
||||
authID := int(id.(float64))
|
||||
cid := dao.UpdateCIDByID(req.ID, authID, req.Name, req.Url, req.Script, req.Token)
|
||||
if cid == false {
|
||||
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
|
||||
return
|
||||
} else {
|
||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
|
||||
}
|
||||
} else {
|
||||
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func GetCIDList(c *gin.Context) {
|
||||
|
||||
// 获取用户ID
|
||||
id, _ := c.Get("id")
|
||||
authID := int(id.(float64))
|
||||
cids := dao.FindCIDByAuthID(authID)
|
||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cids})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue