2017-02-23 06:12:16 +01:00
|
|
|
package db
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2021-10-20 13:56:29 +02:00
|
|
|
import (
|
2024-11-24 18:06:05 +01:00
|
|
|
"encoding/json"
|
2024-04-19 15:05:35 +02:00
|
|
|
"fmt"
|
2021-10-20 13:56:29 +02:00
|
|
|
"time"
|
2024-04-12 12:32:54 +02:00
|
|
|
|
2024-12-06 14:53:46 +01:00
|
|
|
"github.com/go-gorp/gorp/v3"
|
|
|
|
|
2024-10-26 14:56:17 +02:00
|
|
|
"github.com/semaphoreui/semaphore/pkg/task_logger"
|
|
|
|
"github.com/semaphoreui/semaphore/util"
|
2021-10-20 13:56:29 +02:00
|
|
|
)
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2024-11-24 18:06:05 +01:00
|
|
|
type DefaultTaskParams struct {
|
2024-11-24 11:32:24 +01:00
|
|
|
}
|
|
|
|
|
2024-11-24 11:05:07 +01:00
|
|
|
type TerraformTaskParams struct {
|
|
|
|
Plan bool `json:"plan"`
|
2024-12-01 20:04:23 +01:00
|
|
|
Destroy bool `json:"destroy"`
|
2024-11-24 11:05:07 +01:00
|
|
|
AutoApprove bool `json:"auto_approve"`
|
2024-12-06 14:53:46 +01:00
|
|
|
Upgrade bool `json:"upgrade"`
|
2024-11-24 11:05:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type AnsibleTaskParams struct {
|
|
|
|
Debug bool `json:"debug"`
|
|
|
|
DryRun bool `json:"dry_run"`
|
|
|
|
Diff bool `json:"diff"`
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
// Task is a model of a task which will be executed by the runner
|
2016-04-04 15:44:34 +02:00
|
|
|
type Task struct {
|
2016-04-08 21:41:20 +02:00
|
|
|
ID int `db:"id" json:"id"`
|
|
|
|
TemplateID int `db:"template_id" json:"template_id" binding:"required"`
|
2021-05-13 15:49:32 +02:00
|
|
|
ProjectID int `db:"project_id" json:"project_id"`
|
2016-04-08 21:41:20 +02:00
|
|
|
|
2024-04-12 12:32:54 +02:00
|
|
|
Status task_logger.TaskStatus `db:"status" json:"status"`
|
2016-04-08 21:41:20 +02:00
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
Debug bool `db:"debug" json:"debug"`
|
2016-06-30 16:57:45 +02:00
|
|
|
DryRun bool `db:"dry_run" json:"dry_run"`
|
2022-10-28 05:05:13 +02:00
|
|
|
Diff bool `db:"diff" json:"diff"`
|
2016-06-30 16:57:45 +02:00
|
|
|
|
2016-04-08 21:41:20 +02:00
|
|
|
// override variables
|
2024-07-03 10:56:52 +02:00
|
|
|
Playbook string `db:"playbook" json:"playbook"`
|
|
|
|
Environment string `db:"environment" json:"environment"`
|
|
|
|
Limit string `db:"hosts_limit" json:"limit"`
|
|
|
|
Secret string `db:"-" json:"secret"`
|
|
|
|
Arguments *string `db:"arguments" json:"arguments"`
|
2024-10-19 16:16:32 +02:00
|
|
|
GitBranch *string `db:"git_branch" json:"git_branch"`
|
2016-04-17 02:20:23 +02:00
|
|
|
|
2024-07-03 10:56:52 +02:00
|
|
|
UserID *int `db:"user_id" json:"user_id"`
|
|
|
|
IntegrationID *int `db:"integration_id" json:"integration_id"`
|
|
|
|
ScheduleID *int `db:"schedule_id" json:"schedule_id"`
|
2016-06-26 00:43:59 +02:00
|
|
|
|
2016-04-17 02:20:23 +02:00
|
|
|
Created time.Time `db:"created" json:"created"`
|
|
|
|
Start *time.Time `db:"start" json:"start"`
|
|
|
|
End *time.Time `db:"end" json:"end"`
|
2021-10-11 23:36:35 +02:00
|
|
|
|
2021-10-20 13:56:29 +02:00
|
|
|
Message string `db:"message" json:"message"`
|
2021-10-14 17:13:21 +02:00
|
|
|
|
2022-01-26 20:51:20 +01:00
|
|
|
// CommitMessage is a git commit hash of playbook repository which
|
|
|
|
// was active when task was created.
|
2021-10-20 13:56:29 +02:00
|
|
|
CommitHash *string `db:"commit_hash" json:"commit_hash"`
|
|
|
|
// CommitMessage contains message retrieved from git repository after checkout to CommitHash.
|
|
|
|
// It is readonly by API.
|
2021-10-14 17:13:21 +02:00
|
|
|
CommitMessage string `db:"commit_message" json:"commit_message"`
|
2024-07-03 10:56:52 +02:00
|
|
|
BuildTaskID *int `db:"build_task_id" json:"build_task_id"`
|
2022-01-18 20:17:48 +01:00
|
|
|
// Version is a build version.
|
|
|
|
// This field available only for Build tasks.
|
|
|
|
Version *string `db:"version" json:"version"`
|
2022-01-26 20:51:20 +01:00
|
|
|
|
2024-03-10 13:13:44 +01:00
|
|
|
InventoryID *int `db:"inventory_id" json:"inventory_id"`
|
2024-11-24 11:05:07 +01:00
|
|
|
|
2024-11-24 19:28:00 +01:00
|
|
|
Params MapStringAnyField `db:"params" json:"params"`
|
2024-11-24 11:05:07 +01:00
|
|
|
}
|
|
|
|
|
2024-11-24 19:28:00 +01:00
|
|
|
func (task *Task) GetParams(target interface{}) (err error) {
|
|
|
|
content, err := json.Marshal(task.Params)
|
2024-11-24 18:06:05 +01:00
|
|
|
if err != nil {
|
2024-11-24 19:28:00 +01:00
|
|
|
return
|
2024-11-24 18:06:05 +01:00
|
|
|
}
|
2024-11-24 19:28:00 +01:00
|
|
|
err = json.Unmarshal(content, target)
|
|
|
|
return
|
2021-10-20 13:56:29 +02:00
|
|
|
}
|
|
|
|
|
2024-07-21 22:21:54 +02:00
|
|
|
func (task *Task) PreInsert(gorp.SqlExecutor) error {
|
|
|
|
task.Created = task.Created.UTC()
|
2024-11-24 19:28:00 +01:00
|
|
|
|
|
|
|
// Init params from old fields for backward compatibility
|
|
|
|
|
|
|
|
if task.Debug {
|
|
|
|
task.Params["debug"] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if task.DryRun {
|
|
|
|
task.Params["dry_run"] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if task.Diff {
|
|
|
|
task.Params["diff"] = true
|
|
|
|
}
|
|
|
|
|
2024-07-21 22:21:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (task *Task) PreUpdate(gorp.SqlExecutor) error {
|
|
|
|
if task.Start != nil {
|
|
|
|
start := task.Start.UTC()
|
|
|
|
task.Start = &start
|
|
|
|
}
|
|
|
|
|
|
|
|
if task.End != nil {
|
|
|
|
end := task.End.UTC()
|
|
|
|
task.End = &end
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-19 22:49:33 +01:00
|
|
|
func (task *Task) GetIncomingVersion(d Store) *string {
|
|
|
|
if task.BuildTaskID == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
buildTask, err := d.GetTask(task.ProjectID, *task.BuildTaskID)
|
|
|
|
|
2022-01-19 21:42:08 +01:00
|
|
|
if err != nil {
|
2022-01-19 22:49:33 +01:00
|
|
|
return nil
|
2022-01-19 21:42:08 +01:00
|
|
|
}
|
|
|
|
|
2022-01-19 22:49:33 +01:00
|
|
|
tpl, err := d.GetTemplate(task.ProjectID, buildTask.TemplateID)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2022-01-19 21:42:08 +01:00
|
|
|
}
|
2022-01-19 22:49:33 +01:00
|
|
|
|
|
|
|
if tpl.Type == TemplateBuild {
|
|
|
|
return buildTask.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildTask.GetIncomingVersion(d)
|
2021-10-20 13:56:29 +02:00
|
|
|
}
|
|
|
|
|
2024-04-19 15:05:35 +02:00
|
|
|
func (task *Task) GetUrl() *string {
|
|
|
|
if util.Config.WebHost != "" {
|
|
|
|
taskUrl := fmt.Sprintf("%s/project/%d/history?t=%d", util.Config.WebHost, task.ProjectID, task.ID)
|
|
|
|
return &taskUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-20 13:56:29 +02:00
|
|
|
func (task *Task) ValidateNewTask(template Template) error {
|
2024-11-24 18:06:05 +01:00
|
|
|
|
|
|
|
var params interface{}
|
|
|
|
switch template.App {
|
|
|
|
case AppAnsible:
|
|
|
|
params = &AnsibleTaskParams{}
|
|
|
|
case AppTerraform, AppTofu:
|
|
|
|
params = &TerraformTaskParams{}
|
|
|
|
default:
|
|
|
|
params = &DefaultTaskParams{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return task.GetParams(params)
|
2021-10-20 13:56:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 20:09:46 +02:00
|
|
|
func (task *TaskWithTpl) Fill(d Store) error {
|
2021-10-20 13:56:29 +02:00
|
|
|
if task.BuildTaskID != nil {
|
|
|
|
build, err := d.GetTask(task.ProjectID, *task.BuildTaskID)
|
2022-02-04 18:35:08 +01:00
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-20 13:56:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
task.BuildTask = &build
|
|
|
|
}
|
|
|
|
return nil
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2021-06-24 19:45:28 +02:00
|
|
|
// TaskWithTpl is the task data with additional fields
|
2021-03-12 18:41:41 +01:00
|
|
|
type TaskWithTpl struct {
|
|
|
|
Task
|
2021-10-14 22:25:12 +02:00
|
|
|
TemplatePlaybook string `db:"tpl_playbook" json:"tpl_playbook"`
|
|
|
|
TemplateAlias string `db:"tpl_alias" json:"tpl_alias"`
|
|
|
|
TemplateType TemplateType `db:"tpl_type" json:"tpl_type"`
|
2024-07-10 15:09:13 +02:00
|
|
|
TemplateApp TemplateApp `db:"tpl_app" json:"tpl_app"`
|
2021-10-14 22:25:12 +02:00
|
|
|
UserName *string `db:"user_name" json:"user_name"`
|
2021-10-25 20:09:46 +02:00
|
|
|
BuildTask *Task `db:"-" json:"build_task"`
|
2021-03-12 18:41:41 +01:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// TaskOutput is the ansible log output from the task
|
2016-04-04 15:44:34 +02:00
|
|
|
type TaskOutput struct {
|
|
|
|
TaskID int `db:"task_id" json:"task_id"`
|
2016-04-17 20:01:51 +02:00
|
|
|
Task string `db:"task" json:"task"`
|
2016-04-04 15:44:34 +02:00
|
|
|
Time time.Time `db:"time" json:"time"`
|
|
|
|
Output string `db:"output" json:"output"`
|
|
|
|
}
|
2024-06-17 20:37:45 +02:00
|
|
|
|
|
|
|
type TaskStageType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
TaskStageRepositoryClone TaskStageType = "repository_clone"
|
|
|
|
TaskStageTerraformPlan TaskStageType = "terraform_plan"
|
|
|
|
TaskStageTerraformApply TaskStageType = "terraform_apply"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TaskStage struct {
|
|
|
|
TaskID int `db:"task_id" json:"task_id"`
|
|
|
|
Start *time.Time `db:"start" json:"start"`
|
|
|
|
End *time.Time `db:"end" json:"end"`
|
|
|
|
StartOutputID *int `db:"start_output_id" json:"start_output_id"`
|
|
|
|
EndOutputID *int `db:"end_output_id" json:"end_output_id"`
|
|
|
|
Type TaskStageType `db:"type" json:"type"`
|
|
|
|
}
|