后端cid部分基本完成,待测试

This commit is contained in:
junleea 2024-07-05 11:38:59 +08:00
parent f57d9ee392
commit 77b76caf39
3 changed files with 55 additions and 37 deletions

View File

@ -3,8 +3,8 @@ package handler
import ( import (
"bytes" "bytes"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"os"
"os/exec" "os/exec"
"regexp"
"strconv" "strconv"
"videoplayer/dao" "videoplayer/dao"
"videoplayer/proto" "videoplayer/proto"
@ -54,24 +54,28 @@ func RunCID(c *gin.Context) {
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"}) c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
return return
} else { } else {
scriptContent := `` re := regexp.MustCompile(`(?i)(?:https?://|git@)[^/]+/([^/]+)/([^/]+)(?:\.git)?$`)
str, _ := generateRandomHexString(16) matches := re.FindStringSubmatch(cid.Url)
scriptName := "" + str + `.sh` name := matches[2]
// 写入脚本 scriptContent := `#!/bin/bash
err2 := os.WriteFile(scriptName, []byte(scriptContent), 0755) TARGET_DIR = ` + proto.CID_BASE_DIR + `/workspace` + name + `
if err2 != nil { if [ ! -d $TARGET_DIR ]; then
c.JSON(200, gin.H{"error": err2.Error(), "code": proto.OperationFailed, "message": "failed"}) git clone ` + cid.Url + `
return cd $TARGET_DIR/` + name + `
} else { else
//执行脚本 cd $TARGET_DIR/` + name + `
cmd := exec.Command("/bin/bash", "-c", scriptName) git pull
err3 := cmd.Run() fi
// 使用bytes.Buffer捕获输出 ` + cid.Script
var out bytes.Buffer //执行脚本
cmd.Stdout = &out cmd := exec.Command("/bin/bash", "-c", scriptContent)
dao.CreateRunLog(req.ID, authID, out.String(), err3.Error()) //添加执行日志 err3 := cmd.Run()
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"}) // 使用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 { } else {
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
@ -175,24 +179,28 @@ func CIDCallback(c *gin.Context) {
} }
res := dao.FindCIDByIDAndToken(cid, token) res := dao.FindCIDByIDAndToken(cid, token)
if res.ID != 0 { if res.ID != 0 {
scriptContent := `` //从url获取仓库名称
str, _ := generateRandomHexString(16) re := regexp.MustCompile(`(?i)(?:https?://|git@)[^/]+/([^/]+)/([^/]+)(?:\.git)?$`)
scriptName := "" + str + `.sh` matches := re.FindStringSubmatch(res.Url)
// 写入脚本 name := matches[2]
err2 := os.WriteFile(scriptName, []byte(scriptContent), 0755) scriptContent := `#!/bin/bash
if err2 != nil { TARGET_DIR = ` + proto.CID_BASE_DIR + `/workspace` + name + `
c.JSON(200, gin.H{"error": err2.Error(), "code": proto.OperationFailed, "message": "failed"}) if [ ! -d $TARGET_DIR ]; then
return git clone ` + res.Url + `
} else { cd $TARGET_DIR/` + name + `
//执行脚本 else
cmd := exec.Command("/bin/bash", "-c", scriptName) cd $TARGET_DIR/` + name + `
err3 := cmd.Run() git pull
// 使用bytes.Buffer捕获输出 fi
var out bytes.Buffer ` + res.Script
cmd.Stdout = &out //执行脚本
dao.CreateRunLog(cid, res.Auth_id, out.String(), err3.Error()) //添加执行日志 cmd := exec.Command("/bin/bash", "-c", scriptContent)
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"}) err3 := cmd.Run()
} // 使用bytes.Buffer捕获输出
var out bytes.Buffer
cmd.Stdout = &out
dao.CreateRunLog(cid, res.Auth_id, out.String(), err3.Error()) //添加执行日志
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
} else { } else {
c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"}) c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"})
return return

View File

@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
"io" "io"
"os"
"strings" "strings"
"videoplayer/dao" "videoplayer/dao"
"videoplayer/handler" "videoplayer/handler"
@ -29,6 +30,12 @@ func main() {
defer dao.Close() defer dao.Close()
defer worker.CloseRedis() defer worker.CloseRedis()
} }
func init() {
// 创建cid的目录
os.MkdirAll(proto.CID_BASE_DIR, os.ModePerm)
os.MkdirAll(proto.CID_BASE_DIR+"script", os.ModePerm)
os.MkdirAll(proto.CID_BASE_DIR+"workspace", os.ModePerm)
}
func writeLogger(c *gin.Context) { func writeLogger(c *gin.Context) {
ip := c.ClientIP() ip := c.ClientIP()

View File

@ -15,6 +15,9 @@ const (
REIDS_DB = 2 REIDS_DB = 2
TOKEN_SECRET = "mfjurnc_32ndj9dfhj" TOKEN_SECRET = "mfjurnc_32ndj9dfhj"
// 以下是持续集成、部署的配置
CID_BASE_DIR = "/home/lijun/cid/"
) )
type User struct { type User struct {