2021-05-08 22:25:31 +02:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2024-07-19 22:13:01 +02:00
|
|
|
"errors"
|
2021-05-08 22:25:31 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2022-01-31 23:16:00 +01:00
|
|
|
"go.etcd.io/bbolt"
|
2021-05-08 22:25:31 +02:00
|
|
|
)
|
|
|
|
|
2021-09-01 21:17:28 +02:00
|
|
|
func (d *BoltDb) CreateTemplate(template db.Template) (newTemplate db.Template, err error) {
|
2021-11-02 20:30:45 +01:00
|
|
|
err = template.Validate()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:05:48 +01:00
|
|
|
template.SurveyVarsJSON = db.ObjectToJSON(template.SurveyVars)
|
2021-09-01 21:17:28 +02:00
|
|
|
newTpl, err := d.createObject(template.ProjectID, db.TemplateProps, template)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newTemplate = newTpl.(db.Template)
|
|
|
|
err = db.FillTemplate(d, &newTemplate)
|
|
|
|
return
|
2021-05-08 22:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) UpdateTemplate(template db.Template) error {
|
2021-11-02 20:30:45 +01:00
|
|
|
err := template.Validate()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:05:48 +01:00
|
|
|
template.SurveyVarsJSON = db.ObjectToJSON(template.SurveyVars)
|
2021-05-13 21:45:54 +02:00
|
|
|
return d.updateObject(template.ProjectID, db.TemplateProps, template)
|
2021-05-08 22:25:31 +02:00
|
|
|
}
|
|
|
|
|
2022-01-19 20:35:59 +01:00
|
|
|
func (d *BoltDb) GetTemplates(projectID int, filter db.TemplateFilter, params db.RetrieveQueryParams) (templates []db.Template, err error) {
|
|
|
|
var ftr = func(tpl interface{}) bool {
|
|
|
|
template := tpl.(db.Template)
|
|
|
|
var res = true
|
|
|
|
if filter.ViewID != nil {
|
|
|
|
res = res && template.ViewID != nil && *template.ViewID == *filter.ViewID
|
2021-10-27 21:48:51 +02:00
|
|
|
}
|
2022-01-19 20:35:59 +01:00
|
|
|
if filter.BuildTemplateID != nil {
|
|
|
|
res = res && template.BuildTemplateID != nil && *template.BuildTemplateID == *filter.BuildTemplateID
|
|
|
|
if filter.AutorunOnly {
|
|
|
|
res = res && template.Autorun
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
2021-10-27 21:48:51 +02:00
|
|
|
}
|
|
|
|
|
2022-01-19 20:35:59 +01:00
|
|
|
err = d.getObjects(projectID, db.TemplateProps, params, ftr, &templates)
|
2021-10-13 16:07:22 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-19 22:13:01 +02:00
|
|
|
templatesMap := make(map[int]*db.Template)
|
2024-07-19 21:38:35 +02:00
|
|
|
|
2024-07-19 22:13:01 +02:00
|
|
|
for i := 0; i < len(templates); i++ {
|
|
|
|
templatesMap[templates[i].ID] = &templates[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
unfilledTemplateCount := len(templates)
|
|
|
|
|
|
|
|
var errEndOfTemplates = errors.New("no more templates to filling")
|
|
|
|
|
|
|
|
err = d.apply(projectID, db.TaskProps, db.RetrieveQueryParams{}, func(i interface{}) error {
|
|
|
|
task := i.(db.Task)
|
|
|
|
|
|
|
|
tpl, ok := templatesMap[task.TemplateID]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if tpl.LastTask != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-19 21:38:35 +02:00
|
|
|
|
2024-07-19 22:13:01 +02:00
|
|
|
tpl.LastTask = &db.TaskWithTpl{
|
|
|
|
Task: task,
|
|
|
|
}
|
|
|
|
|
|
|
|
unfilledTemplateCount--
|
|
|
|
|
|
|
|
if unfilledTemplateCount <= 0 {
|
|
|
|
return errEndOfTemplates
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if errors.Is(err, errEndOfTemplates) {
|
|
|
|
err = nil
|
|
|
|
}
|
2021-10-13 16:07:22 +02:00
|
|
|
|
2021-05-08 22:25:31 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-29 10:14:42 +01:00
|
|
|
func (d *BoltDb) getRawTemplate(projectID int, templateID int) (template db.Template, err error) {
|
2021-05-13 21:45:54 +02:00
|
|
|
err = d.getObject(projectID, db.TemplateProps, intObjectID(templateID), &template)
|
2022-01-29 10:14:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) GetTemplate(projectID int, templateID int) (template db.Template, err error) {
|
|
|
|
template, err = d.getRawTemplate(projectID, templateID)
|
2021-09-01 21:17:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = db.FillTemplate(d, &template)
|
2021-05-08 22:25:31 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-31 23:16:00 +01:00
|
|
|
func (d *BoltDb) deleteTemplate(projectID int, templateID int, tx *bbolt.Tx) (err error) {
|
2022-02-04 18:35:08 +01:00
|
|
|
inUse, err := d.isObjectInUse(projectID, db.TemplateProps, intObjectID(templateID), db.TemplateProps)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if inUse {
|
|
|
|
return db.ErrInvalidOperation
|
|
|
|
}
|
|
|
|
|
2024-07-19 21:38:35 +02:00
|
|
|
tasks, err := d.GetTemplateTasks(projectID, templateID, db.RetrieveQueryParams{})
|
2022-01-31 23:16:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, task := range tasks {
|
2024-06-30 09:48:36 +02:00
|
|
|
err = d.deleteTaskWithOutputs(projectID, task.ID, true, tx)
|
2022-01-31 23:16:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
schedules, err := d.GetTemplateSchedules(projectID, templateID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, sch := range schedules {
|
|
|
|
err = d.deleteSchedule(projectID, sch.ID, tx)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.deleteObject(projectID, db.TemplateProps, intObjectID(templateID), tx)
|
|
|
|
}
|
|
|
|
|
2021-05-08 22:25:31 +02:00
|
|
|
func (d *BoltDb) DeleteTemplate(projectID int, templateID int) error {
|
2022-01-31 23:16:00 +01:00
|
|
|
return d.db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
return d.deleteTemplate(projectID, templateID, tx)
|
|
|
|
})
|
2021-05-08 22:25:31 +02:00
|
|
|
}
|
2022-02-04 18:35:08 +01:00
|
|
|
|
|
|
|
func (d *BoltDb) GetTemplateRefs(projectID int, templateID int) (db.ObjectReferrers, error) {
|
|
|
|
return d.getObjectRefs(projectID, db.TemplateProps, templateID)
|
|
|
|
}
|