diff --git a/crontask/monkey.go b/crontask/monkey.go index c992e2c..61e71f1 100644 --- a/crontask/monkey.go +++ b/crontask/monkey.go @@ -5,6 +5,7 @@ import ( "autogo/dbsql" "autogo/models" "autogo/monkey" + "strings" ) func CheckMonkeyTasks() { @@ -18,8 +19,12 @@ func CheckMonkeyTasks() { db.Model(models.MonkeyTask{}).Where("status = ? AND is_del = 0", "WAITTING").Find(&list) for _, task := range list { + product_name := task.Product + if strings.Contains(task.Product, "-") { + product_name = strings.Split(task.Product, "-")[0] + } var device models.Device - db.Table("device").Model(models.Device{}).Where("project = ? AND status = ?", task.Project, "online").First(&device) + db.Table("device").Model(models.Device{}).Where("project = ? AND product_name = ? AND status = ?", task.Project, product_name, "online").First(&device) if device.ID < 1 { // 没有空闲设备 continue diff --git a/docs/docs.go b/docs/docs.go index 8abdecf..40e33d7 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -918,6 +918,35 @@ const docTemplate = `{ } } }, + "/api/monkey/v2/task/command": { + "get": { + "description": "通过任务id获取任务的执行命令", + "consumes": [ + "application/x-www-form-urlencoded" + ], + "tags": [ + "Monkey相关 /api/monkey/v2/" + ], + "summary": "获取Monkey任务的执行命令", + "parameters": [ + { + "type": "integer", + "description": "任务id", + "name": "task_id", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "返回执行命令", + "schema": { + "$ref": "#/definitions/models.Response" + } + } + } + } + }, "/api/monkey/v2/task/handle/set": { "post": { "description": "更新Monkey任务跟进状态", diff --git a/docs/swagger.json b/docs/swagger.json index 24bee28..fa0b753 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -906,6 +906,35 @@ } } }, + "/api/monkey/v2/task/command": { + "get": { + "description": "通过任务id获取任务的执行命令", + "consumes": [ + "application/x-www-form-urlencoded" + ], + "tags": [ + "Monkey相关 /api/monkey/v2/" + ], + "summary": "获取Monkey任务的执行命令", + "parameters": [ + { + "type": "integer", + "description": "任务id", + "name": "task_id", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "返回执行命令", + "schema": { + "$ref": "#/definitions/models.Response" + } + } + } + } + }, "/api/monkey/v2/task/handle/set": { "post": { "description": "更新Monkey任务跟进状态", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 3b4d6d1..26eb6a5 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -608,6 +608,25 @@ paths: summary: 新建Monkey任务 tags: - Monkey相关 /api/monkey/v2/ + /api/monkey/v2/task/command: + get: + consumes: + - application/x-www-form-urlencoded + description: 通过任务id获取任务的执行命令 + parameters: + - description: 任务id + in: query + name: task_id + required: true + type: integer + responses: + "200": + description: 返回执行命令 + schema: + $ref: '#/definitions/models.Response' + summary: 获取Monkey任务的执行命令 + tags: + - Monkey相关 /api/monkey/v2/ /api/monkey/v2/task/handle/set: post: consumes: diff --git a/env/env.go b/env/env.go deleted file mode 100644 index 6b8b844..0000000 --- a/env/env.go +++ /dev/null @@ -1,24 +0,0 @@ -package env - -import ( - "autogo/dbsql" - "autogo/models" - "fmt" -) - -func InitDB() { - db, err := dbsql.GetConn(dbsql.DSN) - if err != nil { - return - } - err = db.AutoMigrate(&models.Base{}) - if err != nil { - fmt.Println(err) - return - } - err = db.AutoMigrate(&models.MonkeyTask{}) - if err != nil { - fmt.Println(err) - return - } -} diff --git a/main.go b/main.go index 9c2a5dd..f6e5671 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "autogo/crontask" "autogo/dbsql" "autogo/device" - "autogo/env" "autogo/monkey" "autogo/qatool" "flag" @@ -38,7 +37,7 @@ func main() { } dbsql.DSN = dbsql.DSN_local - env.InitDB() + // env.InitDB() log.SetLevel(log.DebugLevel) log.SetReportCaller(true) @@ -73,6 +72,7 @@ func main() { // monkey v2 r.POST("/api/monkey/v2/create_task", monkey.CreateTaskV2) r.POST("/api/monkey/v2/task/handle/set", monkey.UpdateHandleStatus) + r.GET("/api/monkey/v2/task/command", monkey.GetTaskCommand) // device r.GET("/api/device/v1/list", device.GetDevices) diff --git a/models/monkey_task.go b/models/monkey_task.go index ceb9715..835c905 100644 --- a/models/monkey_task.go +++ b/models/monkey_task.go @@ -50,9 +50,9 @@ func (t *MonkeyTask) Check() error { if t.Platform == "" { return errors.New("task.Platform为空") } - if t.LaunchActivity == "" && t.Platform == "adr" { - return errors.New("task.LaunchActivity为空,platform=adr时需要传入LaunchActivity参数") - } + // if t.LaunchActivity == "" && t.Platform == "adr" { + // return errors.New("task.LaunchActivity为空,platform=adr时需要传入LaunchActivity参数") + // } if t.PackageURL == "" { return errors.New("task.PackageURL为空") } diff --git a/monkey/cli_adr.go b/monkey/cli_adr.go index 28d158b..6513726 100644 --- a/monkey/cli_adr.go +++ b/monkey/cli_adr.go @@ -38,7 +38,9 @@ func RunAndroidMonkeyCmd(task models.MonkeyTask, udid string) { log.Debug("下载完毕,安装包路径", pkg_path) } cmd_content += " -package " + task.PackageName - cmd_content += " -launch_activity " + task.LaunchActivity + if task.LaunchActivity != "" { + cmd_content += " -launch_activity " + task.LaunchActivity + } cmd_content += " -run_time " + cast.ToString(task.RunTime) if udid != "" { cmd_content += " -device_udid " + udid diff --git a/monkey/task_v2.go b/monkey/task_v2.go index 54a4d6d..6f9fcbf 100644 --- a/monkey/task_v2.go +++ b/monkey/task_v2.go @@ -8,8 +8,10 @@ import ( "fmt" "net/http" "strings" + "time" "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" "github.com/spf13/cast" "gorm.io/gorm" ) @@ -152,6 +154,57 @@ func UpdateHandleStatus(c *gin.Context) { c.JSON(http.StatusOK, rsp.Success()) } +// @Tags Monkey相关 /api/monkey/v2/ +// @Summary 获取Monkey任务的执行命令 +// @Description 通过任务id获取任务的执行命令 +// @accept x-www-form-urlencoded +// @Param task_id query int true "任务id" +// @Success 200 {object} models.Response "返回执行命令" +// @Router /api/monkey/v2/task/command [get] +func GetTaskCommand(c *gin.Context) { + rsp := controllers.NewResponse() + + task_id := cast.ToInt(c.Query("task_id")) + + if task_id < 1 { + c.JSON(http.StatusOK, rsp.Error("参数task_id错误:"+c.PostForm("task_id"))) + return + } + + var task models.MonkeyTask + db, err := dbsql.GetConn(dbsql.DSN) + if err != nil { + rsp.Error(err.Error()) + c.JSON(http.StatusOK, rsp) + return + } + defer dbsql.Close(db) + db.Where("id = ?", task_id).Find(&task) + + cmd_content := "cd /home/app/monkey && nohup python3 main_adr.py" + + " -task_id " + cast.ToString(task.Id) + if task.PackageURL != "" { + pkg_path := "/home/tmp/pkg/" + cast.ToString(time.Now().Unix()) + ".apk" + log.Debug("需要下载apk:", task.PackageURL, "-O", pkg_path) + cmd_content += " -pkg_path " + pkg_path + log.Debug("下载完毕,安装包路径", pkg_path) + } + cmd_content += " -package " + task.PackageName + if task.LaunchActivity != "" { + cmd_content += " -launch_activity " + task.LaunchActivity + } + cmd_content += " -run_time " + cast.ToString(task.RunTime) + if task.Devices != "" { + cmd_content += " -device_udid " + task.Devices + } else { + cmd_content += " -device_udid [udid] " + } + cmd_content += " >> /home/app/logs/task_output/task_" + cast.ToString(task.Id) + ".log 2>&1" + + rsp.Data = cmd_content + c.JSON(http.StatusOK, rsp.Success()) +} + // 根据id查询monkey任务信息,db传nil时会另外新建数据库链接 func getTaskById(id int, db *gorm.DB) (models.MonkeyTask, error) { var task models.MonkeyTask