2024-07-04 18:31:04 +08:00
|
|
|
package handler
|
|
|
|
|
|
2024-07-05 10:28:05 +08:00
|
|
|
import (
|
|
|
|
|
"bytes"
|
2024-07-05 16:55:57 +08:00
|
|
|
"fmt"
|
2024-07-05 10:28:05 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"os/exec"
|
2024-07-05 10:46:02 +08:00
|
|
|
"strconv"
|
2024-07-06 19:06:18 +08:00
|
|
|
"strings"
|
2024-07-05 10:28:05 +08:00
|
|
|
"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"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 20:45:22 +08:00
|
|
|
type CIDLogReq struct {
|
|
|
|
|
ID int `json:"id" form:"id"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 10:28:05 +08:00
|
|
|
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"`
|
2024-07-05 16:55:57 +08:00
|
|
|
Token string `json:"cidtoken" form:"cidtoken"`
|
2024-07-05 10:28:05 +08:00
|
|
|
}
|
2024-07-04 18:31:04 +08:00
|
|
|
|
|
|
|
|
func SetUpCIDGroup(router *gin.Engine) {
|
|
|
|
|
cidGroup := router.Group("/cid") //持续集成、部署
|
2024-07-05 10:28:05 +08:00
|
|
|
cidGroup.POST("/create", CreateCID)
|
2024-07-04 18:31:04 +08:00
|
|
|
cidGroup.POST("/delete", DeleteCID)
|
|
|
|
|
cidGroup.POST("/update", UpdateCID)
|
|
|
|
|
cidGroup.POST("/list", GetCIDList)
|
|
|
|
|
cidGroup.POST("/run", RunCID)
|
2024-07-05 10:46:02 +08:00
|
|
|
cidGroup.POST("/log", GetCIDLogList) //获取执行日志
|
|
|
|
|
cidGroup.POST("/log/detail", GetCIDLog) //获取执行日志详情
|
|
|
|
|
cidGroup.POST("/callback", CIDCallback)
|
2024-07-04 18:31:04 +08:00
|
|
|
}
|
|
|
|
|
func RunCID(c *gin.Context) {
|
2024-07-05 10:28:05 +08:00
|
|
|
var req CIDRunReq
|
|
|
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
|
|
|
// 获取用户ID
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
authID := int(id.(float64))
|
2024-07-07 11:57:33 +08:00
|
|
|
username, _ := c.Get("username")
|
2024-07-05 10:28:05 +08:00
|
|
|
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 {
|
2024-07-08 15:35:38 +08:00
|
|
|
go RunShell(username.(string), cid.Url, cid.Script, req.ID, authID)
|
2024-07-05 11:38:59 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
|
2024-07-05 10:28:05 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
}
|
2024-07-04 18:31:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CreateCID(c *gin.Context) {
|
2024-07-05 10:28:05 +08:00
|
|
|
var req CIDCreateReq
|
|
|
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
|
|
|
// 获取用户ID
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
authID := int(id.(float64))
|
|
|
|
|
token, _ := generateRandomHexString(32)
|
2024-07-04 18:31:04 +08:00
|
|
|
|
2024-07-05 10:28:05 +08:00
|
|
|
res := dao.CreateCID(req.Name, req.Url, req.Script, token, authID)
|
|
|
|
|
if res != 0 {
|
2024-07-05 16:55:57 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": res})
|
2024-07-05 10:28:05 +08:00
|
|
|
} 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"})
|
|
|
|
|
}
|
2024-07-04 18:31:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeleteCID(c *gin.Context) {
|
2024-07-05 10:28:05 +08:00
|
|
|
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"})
|
|
|
|
|
}
|
2024-07-04 18:31:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func UpdateCID(c *gin.Context) {
|
2024-07-05 10:28:05 +08:00
|
|
|
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"})
|
|
|
|
|
}
|
2024-07-04 18:31:04 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetCIDList(c *gin.Context) {
|
2024-07-05 10:28:05 +08:00
|
|
|
// 获取用户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})
|
2024-07-04 18:31:04 +08:00
|
|
|
}
|
2024-07-05 10:46:02 +08:00
|
|
|
|
|
|
|
|
func GetCIDLog(c *gin.Context) {
|
|
|
|
|
var req CIDRunReq
|
|
|
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
|
|
|
// 获取用户ID
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
authID := int(id.(float64))
|
2024-07-06 20:45:22 +08:00
|
|
|
cidLogs := dao.FindRunLogByCIDLogID(req.ID, authID)
|
2024-07-05 10:46:02 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cidLogs})
|
|
|
|
|
} else {
|
|
|
|
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetCIDLogList(c *gin.Context) {
|
|
|
|
|
// 获取用户ID
|
|
|
|
|
id, _ := c.Get("id")
|
|
|
|
|
authID := int(id.(float64))
|
2024-07-06 20:45:22 +08:00
|
|
|
var req CIDLogReq
|
|
|
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
|
|
|
cidLogs := dao.FindRunLogByID(authID, req.ID)
|
|
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cidLogs})
|
|
|
|
|
} else {
|
|
|
|
|
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
}
|
2024-07-05 10:46:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CIDCallback(c *gin.Context) {
|
|
|
|
|
// 获取用户ID
|
|
|
|
|
token := c.Query("token")
|
|
|
|
|
cid_id := c.Query("id")
|
|
|
|
|
//将cid转换为int
|
|
|
|
|
cid, _ := strconv.Atoi(cid_id)
|
|
|
|
|
if token == "" || cid == 0 {
|
|
|
|
|
c.JSON(200, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
res := dao.FindCIDByIDAndToken(cid, token)
|
|
|
|
|
if res.ID != 0 {
|
2024-07-08 15:35:38 +08:00
|
|
|
username, _ := c.Get("username")
|
|
|
|
|
go RunShell(username.(string), res.Url, res.Script, int(res.ID), res.Auth_id)
|
|
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
|
|
|
|
|
} else {
|
|
|
|
|
c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RunShell(username, url, script string, id, authID int) {
|
|
|
|
|
strs := strings.Split(url, "/")
|
|
|
|
|
name := strs[len(strs)-1]
|
|
|
|
|
names := strings.Split(name, ".")
|
|
|
|
|
name = names[0]
|
|
|
|
|
|
|
|
|
|
//脚本内容,不同用户的持续集成、部署目录不同
|
|
|
|
|
scriptContent := `
|
2024-07-06 20:18:56 +08:00
|
|
|
echo "start"
|
2024-07-08 15:35:38 +08:00
|
|
|
TARGET_DIR=` + proto.CID_BASE_DIR + username + "/" + name + `
|
2024-07-06 19:19:00 +08:00
|
|
|
if [ ! -d $TARGET_DIR ]; then
|
2024-07-08 15:35:38 +08:00
|
|
|
git clone ` + url + `
|
2024-07-08 14:37:18 +08:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
|
echo "Failed to clone repository."
|
|
|
|
|
fi
|
|
|
|
|
fi
|
2024-07-08 15:25:11 +08:00
|
|
|
cd $TARGET_DIR
|
2024-07-08 14:37:18 +08:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
|
echo "Failed to change directory to $TARGET_DIR."
|
|
|
|
|
fi
|
|
|
|
|
git pull
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
|
echo "Failed to pull repository."
|
2024-07-06 19:19:00 +08:00
|
|
|
fi
|
2024-07-08 15:35:38 +08:00
|
|
|
` + script + `
|
2024-07-06 20:18:56 +08:00
|
|
|
echo "end"`
|
2024-07-08 15:35:38 +08:00
|
|
|
//执行脚本
|
|
|
|
|
cmd := exec.Command("/bin/bash", "-c", scriptContent)
|
|
|
|
|
// 使用bytes.Buffer捕获输出
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
err3 := cmd.Run()
|
|
|
|
|
err3_info := ""
|
|
|
|
|
if err3 != nil {
|
|
|
|
|
err3_info = err3.Error()
|
2024-07-05 10:46:02 +08:00
|
|
|
}
|
2024-07-08 15:35:38 +08:00
|
|
|
fmt.Println("bash content:", scriptContent)
|
|
|
|
|
dao.CreateRunLog(id, authID, scriptContent, out.String(), err3_info) //添加执行日志
|
2024-07-05 10:46:02 +08:00
|
|
|
}
|