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