重构执行脚本函数代码
This commit is contained in:
parent
5a87d49be9
commit
ec0a266763
|
|
@ -60,45 +60,8 @@ func RunCID(c *gin.Context) {
|
|||
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
|
||||
return
|
||||
} else {
|
||||
strs := strings.Split(cid.Url, "/")
|
||||
name := strs[len(strs)-1]
|
||||
names := strings.Split(name, ".")
|
||||
name = names[0]
|
||||
|
||||
//脚本内容,不同用户的持续集成、部署目录不同
|
||||
scriptContent := `
|
||||
echo "start"
|
||||
TARGET_DIR=` + proto.CID_BASE_DIR + username.(string) + "/" + name + `
|
||||
if [ ! -d $TARGET_DIR ]; then
|
||||
git clone ` + cid.Url + `
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Failed to clone repository."
|
||||
fi
|
||||
fi
|
||||
cd $TARGET_DIR
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Failed to change directory to $TARGET_DIR."
|
||||
fi
|
||||
git pull
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Failed to pull repository."
|
||||
fi
|
||||
` + cid.Script + `
|
||||
echo "end"`
|
||||
//执行脚本
|
||||
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()
|
||||
}
|
||||
fmt.Println("bash content:", scriptContent)
|
||||
dao.CreateRunLog(req.ID, authID, scriptContent, out.String(), err3_info) //添加执行日志
|
||||
go RunShell(username.(string), cid.Url, cid.Script, req.ID, authID)
|
||||
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"})
|
||||
|
|
@ -207,17 +170,27 @@ func CIDCallback(c *gin.Context) {
|
|||
}
|
||||
res := dao.FindCIDByIDAndToken(cid, token)
|
||||
if res.ID != 0 {
|
||||
//从url获取仓库名称
|
||||
strs := strings.Split(res.Url, "/")
|
||||
name := strs[len(strs)-1]
|
||||
names := strings.Split(name, ".")
|
||||
name = names[0]
|
||||
user := dao.FindUserByUserID(res.Auth_id)
|
||||
scriptContent := `
|
||||
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 := `
|
||||
echo "start"
|
||||
TARGET_DIR=` + proto.CID_BASE_DIR + user.Name + "/" + name + `
|
||||
TARGET_DIR=` + proto.CID_BASE_DIR + username + "/" + name + `
|
||||
if [ ! -d $TARGET_DIR ]; then
|
||||
git clone ` + res.Url + ` $TARGET_DIR
|
||||
git clone ` + url + `
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Failed to clone repository."
|
||||
fi
|
||||
|
|
@ -230,22 +203,18 @@ git pull
|
|||
if [ $? -ne 0 ]; then
|
||||
echo "Failed to pull repository."
|
||||
fi
|
||||
` + res.Script + `
|
||||
` + script + `
|
||||
echo "end"`
|
||||
//执行脚本
|
||||
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()
|
||||
}
|
||||
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"})
|
||||
return
|
||||
//执行脚本
|
||||
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()
|
||||
}
|
||||
fmt.Println("bash content:", scriptContent)
|
||||
dao.CreateRunLog(id, authID, scriptContent, out.String(), err3_info) //添加执行日志
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue