2016-04-17 20:01:51 +02:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2016-05-24 11:55:48 +02:00
|
|
|
database "github.com/ansible-semaphore/semaphore/db"
|
2016-04-17 20:01:51 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/models"
|
2016-05-24 11:55:48 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2016-04-17 20:01:51 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/masterminds/squirrel"
|
|
|
|
)
|
|
|
|
|
2017-02-22 23:17:36 +01:00
|
|
|
func AddTask(w http.ResponseWriter, r *http.Request) {
|
2017-02-22 23:21:52 +01:00
|
|
|
project := context.Get(r, "project").(models.Project)
|
|
|
|
user := context.Get(r, "user").(*models.User)
|
2016-04-17 20:01:51 +02:00
|
|
|
|
|
|
|
var taskObj models.Task
|
2017-02-22 23:21:52 +01:00
|
|
|
if err := mulekick.Bind(w, r, &taskObj); err != nil {
|
2016-04-17 20:01:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
taskObj.Created = time.Now()
|
|
|
|
taskObj.Status = "waiting"
|
2016-06-26 00:43:59 +02:00
|
|
|
taskObj.UserID = &user.ID
|
2016-04-17 20:01:51 +02:00
|
|
|
|
|
|
|
if err := database.Mysql.Insert(&taskObj); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.register <- &task{
|
|
|
|
task: taskObj,
|
|
|
|
projectID: project.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
objType := "task"
|
|
|
|
desc := "Task ID " + strconv.Itoa(taskObj.ID) + " queued for running"
|
|
|
|
if err := (models.Event{
|
|
|
|
ProjectID: &project.ID,
|
|
|
|
ObjectType: &objType,
|
|
|
|
ObjectID: &taskObj.ID,
|
|
|
|
Description: &desc,
|
|
|
|
}.Insert()); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(201, taskObj)
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:17:36 +01:00
|
|
|
func GetAll(w http.ResponseWriter, r *http.Request) {
|
2017-02-22 23:21:52 +01:00
|
|
|
project := context.Get(r, "project").(models.Project)
|
2016-04-17 20:01:51 +02:00
|
|
|
|
2017-02-11 09:34:57 +01:00
|
|
|
query, args, _ := squirrel.Select("task.*, tpl.playbook as tpl_playbook, user.name as user_name, tpl.alias as tpl_alias").
|
2016-04-17 20:01:51 +02:00
|
|
|
From("task").
|
|
|
|
Join("project__template as tpl on task.template_id=tpl.id").
|
2016-06-26 00:43:59 +02:00
|
|
|
LeftJoin("user on task.user_id=user.id").
|
2016-04-17 20:01:51 +02:00
|
|
|
Where("tpl.project_id=?", project.ID).
|
|
|
|
OrderBy("task.created desc").
|
|
|
|
ToSql()
|
|
|
|
|
|
|
|
var tasks []struct {
|
|
|
|
models.Task
|
|
|
|
|
2016-06-26 00:43:59 +02:00
|
|
|
TemplatePlaybook string `db:"tpl_playbook" json:"tpl_playbook"`
|
2017-02-11 09:34:57 +01:00
|
|
|
TemplateAlias string `db:"tpl_alias" json:"tpl_alias"`
|
2016-06-26 00:43:59 +02:00
|
|
|
UserName *string `db:"user_name" json:"user_name"`
|
2016-04-17 20:01:51 +02:00
|
|
|
}
|
|
|
|
if _, err := database.Mysql.Select(&tasks, query, args...); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, tasks)
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:17:36 +01:00
|
|
|
func GetTaskMiddleware(w http.ResponseWriter, r *http.Request) {
|
2016-04-17 20:01:51 +02:00
|
|
|
taskID, err := util.GetIntParam("task_id", c)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var task models.Task
|
|
|
|
if err := database.Mysql.SelectOne(&task, "select * from task where id=?", taskID); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Set("task", task)
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:17:36 +01:00
|
|
|
func GetTaskOutput(w http.ResponseWriter, r *http.Request) {
|
2017-02-22 23:21:52 +01:00
|
|
|
task := context.Get(r, "task").(models.Task)
|
2016-04-17 20:01:51 +02:00
|
|
|
|
|
|
|
var output []models.TaskOutput
|
2016-05-17 21:12:54 +02:00
|
|
|
if _, err := database.Mysql.Select(&output, "select * from task__output where task_id=? order by time asc", task.ID); err != nil {
|
2016-04-17 20:01:51 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, output)
|
|
|
|
}
|
2016-06-17 22:16:46 +02:00
|
|
|
|
2017-02-22 23:17:36 +01:00
|
|
|
func RemoveTask(w http.ResponseWriter, r *http.Request) {
|
2017-02-22 23:21:52 +01:00
|
|
|
task := context.Get(r, "task").(models.Task)
|
2016-06-17 22:16:46 +02:00
|
|
|
|
|
|
|
statements := []string{
|
|
|
|
"delete from task__output where task_id=?",
|
|
|
|
"delete from task where id=?",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, statement := range statements {
|
|
|
|
_, err := database.Mysql.Exec(statement, task.ID)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:17:36 +01:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-06-17 22:16:46 +02:00
|
|
|
}
|