2020-12-20 19:00:59 +01:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2024-02-04 13:46:27 +01:00
|
|
|
|
2024-02-27 12:06:05 +01:00
|
|
|
"github.com/Masterminds/squirrel"
|
2024-06-30 12:25:16 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2020-12-20 19:00:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, err error) {
|
2021-11-02 20:30:45 +01:00
|
|
|
err = template.Validate()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:52:35 +02:00
|
|
|
insertID, err := d.insert(
|
|
|
|
"id",
|
2022-01-18 22:09:42 +01:00
|
|
|
"insert into project__template (project_id, inventory_id, repository_id, environment_id, "+
|
2024-10-02 17:49:33 +02:00
|
|
|
"name, playbook, arguments, allow_override_args_in_task, description, `type`, start_version,"+
|
2024-01-30 10:26:11 +01:00
|
|
|
"build_template_id, view_id, autorun, survey_vars, suppress_success_alerts, app)"+
|
2024-10-02 17:49:33 +02:00
|
|
|
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
2020-12-20 19:00:59 +01:00
|
|
|
template.ProjectID,
|
|
|
|
template.InventoryID,
|
|
|
|
template.RepositoryID,
|
|
|
|
template.EnvironmentID,
|
2022-02-03 08:05:13 +01:00
|
|
|
template.Name,
|
2020-12-20 19:00:59 +01:00
|
|
|
template.Playbook,
|
|
|
|
template.Arguments,
|
2022-01-26 20:51:20 +01:00
|
|
|
template.AllowOverrideArgsInTask,
|
2021-09-17 00:14:35 +02:00
|
|
|
template.Description,
|
2021-10-12 13:37:51 +02:00
|
|
|
template.Type,
|
|
|
|
template.StartVersion,
|
2021-10-27 14:15:36 +02:00
|
|
|
template.BuildTemplateID,
|
2022-01-18 22:09:42 +01:00
|
|
|
template.ViewID,
|
|
|
|
template.Autorun,
|
2022-02-12 13:15:15 +01:00
|
|
|
db.ObjectToJSON(template.SurveyVars),
|
2024-01-30 10:26:11 +01:00
|
|
|
template.SuppressSuccessAlerts,
|
|
|
|
template.App)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-02 17:49:33 +02:00
|
|
|
err = d.UpdateTemplateVaults(template.ProjectID, insertID, template.Vaults)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:05:10 +02:00
|
|
|
err = db.FillTemplate(d, &newTemplate)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-20 19:00:59 +01:00
|
|
|
newTemplate = template
|
2021-08-24 19:52:35 +02:00
|
|
|
newTemplate.ID = insertID
|
2021-09-06 13:05:10 +02:00
|
|
|
|
2020-12-20 19:00:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) UpdateTemplate(template db.Template) error {
|
2021-11-02 20:30:45 +01:00
|
|
|
err := template.Validate()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-18 22:09:42 +01:00
|
|
|
_, err = d.exec("update project__template set "+
|
|
|
|
"inventory_id=?, "+
|
|
|
|
"repository_id=?, "+
|
|
|
|
"environment_id=?, "+
|
2022-02-03 08:05:13 +01:00
|
|
|
"name=?, "+
|
2022-01-18 22:09:42 +01:00
|
|
|
"playbook=?, "+
|
|
|
|
"arguments=?, "+
|
2022-01-26 20:51:20 +01:00
|
|
|
"allow_override_args_in_task=?, "+
|
2022-01-18 22:09:42 +01:00
|
|
|
"description=?, "+
|
|
|
|
"`type`=?, "+
|
|
|
|
"start_version=?,"+
|
|
|
|
"build_template_id=?, "+
|
2022-01-19 11:41:39 +01:00
|
|
|
"view_id=?, "+
|
|
|
|
"autorun=?, "+
|
2022-02-12 13:15:15 +01:00
|
|
|
"survey_vars=?, "+
|
2024-01-30 10:26:11 +01:00
|
|
|
"suppress_success_alerts=?, "+
|
|
|
|
"app=? "+
|
2022-01-31 23:35:13 +01:00
|
|
|
"where id=? and project_id=?",
|
2020-12-20 19:00:59 +01:00
|
|
|
template.InventoryID,
|
|
|
|
template.RepositoryID,
|
|
|
|
template.EnvironmentID,
|
2022-02-03 08:05:13 +01:00
|
|
|
template.Name,
|
2020-12-20 19:00:59 +01:00
|
|
|
template.Playbook,
|
|
|
|
template.Arguments,
|
2022-01-26 20:51:20 +01:00
|
|
|
template.AllowOverrideArgsInTask,
|
2021-09-09 18:50:02 +02:00
|
|
|
template.Description,
|
2021-10-12 13:37:51 +02:00
|
|
|
template.Type,
|
|
|
|
template.StartVersion,
|
|
|
|
template.BuildTemplateID,
|
2021-10-27 14:15:36 +02:00
|
|
|
template.ViewID,
|
2022-01-18 22:09:42 +01:00
|
|
|
template.Autorun,
|
2022-01-19 13:05:48 +01:00
|
|
|
db.ObjectToJSON(template.SurveyVars),
|
2022-02-12 13:15:15 +01:00
|
|
|
template.SuppressSuccessAlerts,
|
2024-01-30 10:26:11 +01:00
|
|
|
template.App,
|
2021-09-06 13:05:10 +02:00
|
|
|
template.ID,
|
2021-10-12 13:37:51 +02:00
|
|
|
template.ProjectID,
|
|
|
|
)
|
2024-10-02 17:49:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.UpdateTemplateVaults(template.ProjectID, template.ID, template.Vaults)
|
|
|
|
|
2020-12-20 19:00:59 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-01-19 20:35:59 +01:00
|
|
|
|
|
|
|
func (d *SqlDb) GetTemplates(projectID int, filter db.TemplateFilter, params db.RetrieveQueryParams) (templates []db.Template, err error) {
|
2024-07-19 21:38:35 +02:00
|
|
|
|
2024-07-19 23:11:06 +02:00
|
|
|
templates = []db.Template{}
|
|
|
|
|
2024-07-19 21:38:35 +02:00
|
|
|
type templateWithLastTask struct {
|
|
|
|
db.Template
|
|
|
|
LastTaskID *int `db:"last_task_id"`
|
|
|
|
}
|
|
|
|
|
2020-12-20 19:00:59 +01:00
|
|
|
q := squirrel.Select("pt.id",
|
|
|
|
"pt.project_id",
|
|
|
|
"pt.inventory_id",
|
|
|
|
"pt.repository_id",
|
|
|
|
"pt.environment_id",
|
2022-02-03 08:05:13 +01:00
|
|
|
"pt.name",
|
2020-12-20 19:00:59 +01:00
|
|
|
"pt.playbook",
|
|
|
|
"pt.arguments",
|
2022-01-26 20:51:20 +01:00
|
|
|
"pt.allow_override_args_in_task",
|
2024-02-04 13:46:27 +01:00
|
|
|
"pt.build_template_id",
|
|
|
|
"pt.start_version",
|
2021-10-27 21:48:51 +02:00
|
|
|
"pt.view_id",
|
2024-01-30 10:26:11 +01:00
|
|
|
"pt.`app`",
|
2024-02-04 13:46:27 +01:00
|
|
|
"pt.survey_vars",
|
2024-01-28 15:03:42 +01:00
|
|
|
"pt.start_version",
|
2024-06-30 12:25:16 +02:00
|
|
|
"pt.`type`",
|
2024-07-19 21:38:35 +02:00
|
|
|
"pt.`tasks`",
|
2024-07-20 09:08:08 +02:00
|
|
|
"(SELECT `id` FROM `task` WHERE template_id = pt.id ORDER BY `id` DESC LIMIT 1) last_task_id").
|
2022-01-31 23:35:13 +01:00
|
|
|
From("project__template pt")
|
2020-12-20 19:00:59 +01:00
|
|
|
|
2022-01-19 20:35:59 +01:00
|
|
|
if filter.ViewID != nil {
|
|
|
|
q = q.Where("pt.view_id=?", *filter.ViewID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter.BuildTemplateID != nil {
|
|
|
|
q = q.Where("pt.build_template_id=?", *filter.BuildTemplateID)
|
|
|
|
if filter.AutorunOnly {
|
|
|
|
q = q.Where("pt.autorun=true")
|
|
|
|
}
|
2021-10-27 21:48:51 +02:00
|
|
|
}
|
|
|
|
|
2020-12-20 19:00:59 +01:00
|
|
|
order := "ASC"
|
|
|
|
if params.SortInverted {
|
|
|
|
order = "DESC"
|
|
|
|
}
|
|
|
|
|
|
|
|
switch params.SortBy {
|
2022-02-03 08:05:13 +01:00
|
|
|
case "name", "playbook":
|
2020-12-20 19:00:59 +01:00
|
|
|
q = q.Where("pt.project_id=?", projectID).
|
|
|
|
OrderBy("pt." + params.SortBy + " " + order)
|
|
|
|
case "inventory":
|
|
|
|
q = q.LeftJoin("project__inventory pi ON (pt.inventory_id = pi.id)").
|
|
|
|
Where("pt.project_id=?", projectID).
|
|
|
|
OrderBy("pi.name " + order)
|
|
|
|
case "environment":
|
|
|
|
q = q.LeftJoin("project__environment pe ON (pt.environment_id = pe.id)").
|
|
|
|
Where("pt.project_id=?", projectID).
|
|
|
|
OrderBy("pe.name " + order)
|
|
|
|
case "repository":
|
|
|
|
q = q.LeftJoin("project__repository pr ON (pt.repository_id = pr.id)").
|
|
|
|
Where("pt.project_id=?", projectID).
|
|
|
|
OrderBy("pr.name " + order)
|
|
|
|
default:
|
|
|
|
q = q.Where("pt.project_id=?", projectID).
|
2022-02-03 08:05:13 +01:00
|
|
|
OrderBy("pt.name " + order)
|
2020-12-20 19:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
query, args, err := q.ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-19 21:38:35 +02:00
|
|
|
var tpls []templateWithLastTask
|
|
|
|
|
|
|
|
_, err = d.selectAll(&tpls, query, args...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-19 23:11:06 +02:00
|
|
|
taskIDs := make([]int, 0)
|
2024-07-19 21:38:35 +02:00
|
|
|
|
|
|
|
for _, tpl := range tpls {
|
2024-10-02 17:49:33 +02:00
|
|
|
tpl.Vaults, err = d.GetTemplateVaults(projectID, tpl.ID)
|
2024-07-19 21:38:35 +02:00
|
|
|
if tpl.LastTaskID != nil {
|
|
|
|
taskIDs = append(taskIDs, *tpl.LastTaskID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-20 12:43:14 +02:00
|
|
|
var tasks []db.TaskWithTpl
|
|
|
|
err = d.getTasks(projectID, nil, taskIDs, db.RetrieveQueryParams{}, &tasks)
|
2021-10-12 15:59:16 +02:00
|
|
|
|
2021-10-13 20:51:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-19 21:38:35 +02:00
|
|
|
for _, tpl := range tpls {
|
|
|
|
template := tpl.Template
|
|
|
|
|
2024-07-21 21:00:31 +02:00
|
|
|
if tpl.LastTaskID != nil {
|
|
|
|
for _, tsk := range tasks {
|
|
|
|
if tsk.ID == *tpl.LastTaskID {
|
|
|
|
err = tsk.Fill(d)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
template.LastTask = &tsk
|
|
|
|
break
|
|
|
|
}
|
2024-07-19 21:38:35 +02:00
|
|
|
}
|
|
|
|
}
|
2024-07-21 21:00:31 +02:00
|
|
|
|
2024-07-19 23:11:06 +02:00
|
|
|
templates = append(templates, template)
|
2024-07-19 21:38:35 +02:00
|
|
|
}
|
2021-10-12 15:59:16 +02:00
|
|
|
|
2020-12-20 19:00:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:17:28 +02:00
|
|
|
func (d *SqlDb) GetTemplate(projectID int, templateID int) (template db.Template, err error) {
|
|
|
|
err = d.selectOne(
|
2020-12-20 19:00:59 +01:00
|
|
|
&template,
|
2022-01-31 23:35:13 +01:00
|
|
|
"select * from project__template where project_id=? and id=?",
|
2020-12-20 19:00:59 +01:00
|
|
|
projectID,
|
|
|
|
templateID)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:17:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.FillTemplate(d, &template)
|
|
|
|
return
|
2020-12-20 19:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) DeleteTemplate(projectID int, templateID int) error {
|
2022-01-31 23:16:00 +01:00
|
|
|
_, err := d.exec("delete from project__template where project_id=? and id=?", projectID, templateID)
|
2021-08-30 21:42:11 +02:00
|
|
|
return err
|
2020-12-20 19:00:59 +01:00
|
|
|
}
|
2022-02-04 18:35:08 +01:00
|
|
|
|
|
|
|
func (d *SqlDb) GetTemplateRefs(projectID int, templateID int) (db.ObjectReferrers, error) {
|
|
|
|
return d.getObjectRefs(projectID, db.TemplateProps, templateID)
|
|
|
|
}
|