Semaphore/api/tasks/http.go

221 lines
5.8 KiB
Go
Raw Normal View History

package tasks
import (
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
2017-02-23 00:21:49 +01:00
"net/http"
"strconv"
"time"
2018-02-18 23:49:40 +01:00
log "github.com/Sirupsen/logrus"
"github.com/ansible-semaphore/semaphore/util"
2017-02-23 00:21:49 +01:00
"github.com/gorilla/context"
)
2018-06-14 08:20:16 +02:00
// AddTask inserts a task into the database and returns a header or returns error
func AddTask(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
user := context.Get(r, "user").(*db.User)
var taskObj db.Task
if !helpers.Bind(w, r, &taskObj) {
return
}
taskObj.Created = time.Now()
2021-08-25 17:37:19 +02:00
taskObj.Status = taskWaitingStatus
taskObj.UserID = &user.ID
2021-05-16 23:44:42 +02:00
taskObj.ProjectID = project.ID
2021-03-12 18:41:41 +01:00
newTask, err := helpers.Store(r).CreateTask(taskObj)
if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot create new task"})
w.WriteHeader(http.StatusBadRequest)
return
}
pool.register <- &task{
2020-12-04 23:22:05 +01:00
store: helpers.Store(r),
2021-03-12 18:41:41 +01:00
task: newTask,
projectID: project.ID,
}
objType := taskTypeID
2021-03-12 18:41:41 +01:00
desc := "Task ID " + strconv.Itoa(newTask.ID) + " queued for running"
_, err = helpers.Store(r).CreateEvent(db.Event{
2021-08-20 08:28:50 +02:00
UserID: &user.ID,
ProjectID: &project.ID,
ObjectType: &objType,
2021-03-12 18:41:41 +01:00
ObjectID: &newTask.ID,
Description: &desc,
})
if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Cannot write new event to database"})
w.WriteHeader(http.StatusInternalServerError)
return
}
2021-04-09 23:02:19 +02:00
helpers.WriteJSON(w, http.StatusCreated, newTask)
}
2018-06-14 08:20:16 +02:00
// GetTasksList returns a list of tasks for the current project in desc order to limit or error
2017-07-25 06:44:09 +02:00
func GetTasksList(w http.ResponseWriter, r *http.Request, limit uint64) {
project := context.Get(r, "project").(db.Project)
2021-03-12 18:41:41 +01:00
tpl := context.Get(r, "template")
2021-03-12 18:41:41 +01:00
var err error
var tasks []db.TaskWithTpl
2020-11-02 11:57:46 +01:00
2021-03-12 18:41:41 +01:00
if tpl != nil {
tasks, err = helpers.Store(r).GetTemplateTasks(project.ID, tpl.(db.Template).ID, db.RetrieveQueryParams{
Count: int(limit),
})
2020-11-02 11:57:46 +01:00
} else {
2021-03-12 18:41:41 +01:00
tasks, err = helpers.Store(r).GetProjectTasks(project.ID, db.RetrieveQueryParams{
Count: int(limit),
})
2017-07-25 06:44:09 +02:00
}
2021-03-12 18:41:41 +01:00
if err != nil {
2018-06-14 08:20:16 +02:00
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot get tasks list from database"})
w.WriteHeader(http.StatusBadRequest)
return
}
helpers.WriteJSON(w, http.StatusOK, tasks)
}
// GetAllTasks returns all tasks for the current project
func GetAllTasks(w http.ResponseWriter, r *http.Request) {
GetTasksList(w, r, 0)
2017-07-25 06:44:09 +02:00
}
// GetLastTasks returns the hundred most recent tasks
func GetLastTasks(w http.ResponseWriter, r *http.Request) {
GetTasksList(w, r, 200)
2017-07-25 06:44:09 +02:00
}
// GetTask returns a task based on its id
func GetTask(w http.ResponseWriter, r *http.Request) {
task := context.Get(r, taskTypeID).(db.Task)
helpers.WriteJSON(w, http.StatusOK, task)
}
// GetTaskMiddleware is middleware that gets a task by id and sets the context to it or panics
func GetTaskMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2021-03-12 18:41:41 +01:00
project := context.Get(r, "project").(db.Project)
taskID, err := helpers.GetIntParam("task_id", w, r)
2021-03-12 18:41:41 +01:00
if err != nil {
2021-03-12 18:41:41 +01:00
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot get task_id from request"})
w.WriteHeader(http.StatusBadRequest)
return
}
2021-03-12 18:41:41 +01:00
task, err := helpers.Store(r).GetTask(project.ID, taskID)
if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot get task from database"})
w.WriteHeader(http.StatusBadRequest)
return
}
context.Set(r, taskTypeID, task)
next.ServeHTTP(w, r)
})
}
2018-06-14 08:20:16 +02:00
// GetTaskOutput returns the logged task output by id and writes it as json or returns error
func GetTaskOutput(w http.ResponseWriter, r *http.Request) {
task := context.Get(r, taskTypeID).(db.Task)
2021-03-12 18:41:41 +01:00
project := context.Get(r, "project").(db.Project)
var output []db.TaskOutput
2021-03-12 18:41:41 +01:00
output, err := helpers.Store(r).GetTaskOutputs(project.ID, task.ID)
if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot get task output from database"})
w.WriteHeader(http.StatusBadRequest)
return
}
helpers.WriteJSON(w, http.StatusOK, output)
}
2021-08-25 17:37:19 +02:00
func StopTask(w http.ResponseWriter, r *http.Request) {
2021-08-30 21:42:11 +02:00
targetTask := context.Get(r, "task").(db.Task)
project := context.Get(r, "project").(db.Project)
2021-08-25 17:37:19 +02:00
var activeTask *task
for _, t := range pool.queue {
if t.task.ID == targetTask.ID {
activeTask = t
break
}
}
if activeTask == nil {
for _, t := range pool.runningTasks {
if t.task.ID == targetTask.ID {
activeTask = t
break
}
}
}
2021-08-30 21:42:11 +02:00
if activeTask == nil { // task not active, but exists in database
activeTask = &task{
store: helpers.Store(r),
task: targetTask,
projectID: project.ID,
2021-08-25 17:37:19 +02:00
}
2021-08-30 21:42:11 +02:00
err := activeTask.populateDetails()
if err != nil {
2021-08-25 17:37:19 +02:00
helpers.WriteError(w, err)
2021-08-30 21:42:11 +02:00
return
2021-08-25 17:37:19 +02:00
}
2021-08-30 21:42:11 +02:00
activeTask.setStatus(taskStoppedStatus)
activeTask.createTaskEvent()
} else {
if activeTask.task.Status == taskRunningStatus {
if activeTask.process == nil {
panic("running process can not be nil")
}
if err := activeTask.process.Kill(); err != nil {
helpers.WriteError(w, err)
}
}
activeTask.setStatus(taskStoppingStatus)
}
2021-08-25 17:37:19 +02:00
w.WriteHeader(http.StatusNoContent)
}
// RemoveTask removes a task from the database
func RemoveTask(w http.ResponseWriter, r *http.Request) {
task := context.Get(r, taskTypeID).(db.Task)
editor := context.Get(r, "user").(*db.User)
2021-03-12 18:41:41 +01:00
project := context.Get(r, "project").(db.Project)
if !editor.Admin {
log.Warn(editor.Username + " is not permitted to delete task logs")
w.WriteHeader(http.StatusUnauthorized)
return
}
2021-03-12 18:41:41 +01:00
err := helpers.Store(r).DeleteTaskWithOutputs(project.ID, task.ID)
if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot delete task from database"})
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
}