Compare commits

..

8 Commits

Author SHA1 Message Date
junleea fd71cc85c4 修改脚本仓库名称获取 2024-07-06 19:06:18 +08:00
junleea edf94cbca9 添加集成run日志脚本记录 2024-07-06 18:46:55 +08:00
junleea baae696b56 Merge branch 'refs/heads/feature-cid' 2024-07-06 10:32:11 +08:00
junleea c3c4668c26 更新脚本 2024-07-06 10:32:05 +08:00
junleea d0e9e34a65 构建home目录 2024-07-05 19:55:45 +08:00
junleea daa799dfb5 修改构建脚本 2024-07-05 19:52:18 +08:00
junleea cf3985321d 修改构建脚本 2024-07-05 19:49:19 +08:00
junleea e1e2dc5037 添加构建脚本 2024-07-05 19:48:04 +08:00
3 changed files with 32 additions and 12 deletions

7
build.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/bash
echo "Building the project"
cd /home/videoplayer
git pull
pwd
/home/lijun/go/bin/go build
echo "Build complete"

View File

@ -18,6 +18,7 @@ type CIDRunLog struct {
gorm.Model
CID_id int `gorm:"column:cid_id"`
Auth_id int `form:"column:auth_id"`
Script string `gorm:"column:script"`
Log string `gorm:"column:log"`
Error string `gorm:"column:error"`
}
@ -73,8 +74,8 @@ func UpdateCIDByID(id, auth_id int, name, url, script, token string) bool {
}
// CreateRunLog,添加执行日志
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}
func CreateRunLog(cid_id, auth_id int, script, log, err string) uint {
cidRunLog := CIDRunLog{CID_id: cid_id, Auth_id: auth_id, Log: log, Error: err, Script: script}
result := DB.Debug().Create(&cidRunLog)
if result != nil {
fmt.Println(err)
@ -85,7 +86,7 @@ func CreateRunLog(cid_id, auth_id int, log, err string) uint {
func FindRunLogByAuthID(auth_id int) []CIDRunLog {
var cidRunLogs []CIDRunLog
DB.Debug().Where(" auth_id = ?", auth_id).Find(&cidRunLogs).Order("created_at desc")
DB.Debug().Where(" auth_id = ?", auth_id).Order("created_at desc").Find(&cidRunLogs)
return cidRunLogs
}

View File

@ -5,8 +5,8 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"os/exec"
"regexp"
"strconv"
"strings"
"videoplayer/dao"
"videoplayer/proto"
)
@ -55,9 +55,12 @@ func RunCID(c *gin.Context) {
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
return
} else {
re := regexp.MustCompile(`(?i)(?:https?://|git@)[^/]+/([^/]+)/([^/]+)(?:\.git)?$`)
matches := re.FindStringSubmatch(cid.Url)
name := matches[2]
strs := strings.Split(cid.Url, "/")
name := strs[len(strs)-1]
names := strings.Split(name, ".")
name = names[0]
//脚本内容
scriptContent := `#!/bin/bash
TARGET_DIR = ` + proto.CID_BASE_DIR + `/workspace` + name + `
if [ ! -d $TARGET_DIR ]; then
@ -71,11 +74,15 @@ func RunCID(c *gin.Context) {
//执行脚本
cmd := exec.Command("/bin/bash", "-c", scriptContent)
err3 := cmd.Run()
err3_info := ""
if err3 != nil {
err3_info = err3.Error()
}
fmt.Println("bash content:", scriptContent)
// 使用bytes.Buffer捕获输出
var out bytes.Buffer
cmd.Stdout = &out
dao.CreateRunLog(req.ID, authID, out.String(), err3.Error()) //添加执行日志
dao.CreateRunLog(req.ID, authID, scriptContent, out.String(), err3_info) //添加执行日志
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
}
@ -182,9 +189,10 @@ func CIDCallback(c *gin.Context) {
res := dao.FindCIDByIDAndToken(cid, token)
if res.ID != 0 {
//从url获取仓库名称
re := regexp.MustCompile(`(?i)(?:https?://|git@)[^/]+/([^/]+)/([^/]+)(?:\.git)?$`)
matches := re.FindStringSubmatch(res.Url)
name := matches[2]
strs := strings.Split(res.Url, "/")
name := strs[len(strs)-1]
names := strings.Split(name, ".")
name = names[0]
scriptContent := `#!/bin/bash
TARGET_DIR = ` + proto.CID_BASE_DIR + `/workspace` + name + `
if [ ! -d $TARGET_DIR ]; then
@ -198,10 +206,14 @@ func CIDCallback(c *gin.Context) {
//执行脚本
cmd := exec.Command("/bin/bash", "-c", scriptContent)
err3 := cmd.Run()
err3_info := ""
if err3 != nil {
err3_info = err3.Error()
}
// 使用bytes.Buffer捕获输出
var out bytes.Buffer
cmd.Stdout = &out
dao.CreateRunLog(cid, res.Auth_id, out.String(), err3.Error()) //添加执行日志
dao.CreateRunLog(cid, res.Auth_id, scriptContent, out.String(), err3_info) //添加执行日志
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"})