2017-02-23 06:12:16 +01:00
|
|
|
package db
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2021-11-02 20:30:45 +01:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
2021-10-14 22:25:12 +02:00
|
|
|
type TemplateType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
TemplateTask TemplateType = ""
|
|
|
|
TemplateBuild TemplateType = "build"
|
|
|
|
TemplateDeploy TemplateType = "deploy"
|
|
|
|
)
|
|
|
|
|
2024-01-30 10:26:11 +01:00
|
|
|
type TemplateApp string
|
|
|
|
|
|
|
|
const (
|
2024-07-16 08:35:36 +02:00
|
|
|
AppAnsible TemplateApp = "ansible"
|
|
|
|
AppTerraform TemplateApp = "terraform"
|
|
|
|
AppTofu TemplateApp = "tofu"
|
|
|
|
AppBash TemplateApp = "bash"
|
|
|
|
AppPowerShell TemplateApp = "powershell"
|
|
|
|
AppPython TemplateApp = "python"
|
|
|
|
AppPulumi TemplateApp = "pulumi"
|
2024-01-30 10:26:11 +01:00
|
|
|
)
|
|
|
|
|
2024-06-12 21:53:00 +02:00
|
|
|
func (t TemplateApp) IsTerraform() bool {
|
2024-07-16 08:35:36 +02:00
|
|
|
return t == AppTerraform || t == AppTofu
|
2024-06-12 21:53:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-19 13:44:56 +01:00
|
|
|
type SurveyVarType string
|
|
|
|
|
|
|
|
const (
|
2024-05-19 21:32:16 +02:00
|
|
|
SurveyVarStr TemplateType = ""
|
|
|
|
SurveyVarInt TemplateType = "int"
|
|
|
|
SurveyVarEnum TemplateType = "enum"
|
2022-01-19 13:44:56 +01:00
|
|
|
)
|
|
|
|
|
2024-05-19 21:32:16 +02:00
|
|
|
type SurveyVarEnumValue struct {
|
2024-10-20 23:06:51 +02:00
|
|
|
Name string `json:"name" backup:"name"`
|
|
|
|
Value string `json:"value" backup:"value"`
|
2024-05-19 21:32:16 +02:00
|
|
|
}
|
|
|
|
|
2022-01-19 13:05:48 +01:00
|
|
|
type SurveyVar struct {
|
2024-10-20 23:06:51 +02:00
|
|
|
Name string `json:"name" backup:"name"`
|
|
|
|
Title string `json:"title" backup:"title"`
|
|
|
|
Required bool `json:"required" backup:"required"`
|
|
|
|
Type SurveyVarType `json:"type" backup:"type"`
|
|
|
|
Description string `json:"description" backup:"description"`
|
|
|
|
Values []SurveyVarEnumValue `json:"values" backup:"values"`
|
2022-01-19 13:05:48 +01:00
|
|
|
}
|
|
|
|
|
2022-01-19 20:35:59 +01:00
|
|
|
type TemplateFilter struct {
|
|
|
|
ViewID *int
|
|
|
|
BuildTemplateID *int
|
|
|
|
AutorunOnly bool
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// Template is a user defined model that is used to run a task
|
2016-04-04 15:44:34 +02:00
|
|
|
type Template struct {
|
2024-10-05 22:16:25 +02:00
|
|
|
ID int `db:"id" json:"id" backup:"-"`
|
2016-04-08 21:41:20 +02:00
|
|
|
|
2024-10-05 22:16:25 +02:00
|
|
|
ProjectID int `db:"project_id" json:"project_id" backup:"-"`
|
|
|
|
InventoryID *int `db:"inventory_id" json:"inventory_id" backup:"-"`
|
|
|
|
RepositoryID int `db:"repository_id" json:"repository_id" backup:"-"`
|
|
|
|
EnvironmentID *int `db:"environment_id" json:"environment_id" backup:"-"`
|
2016-04-08 21:41:20 +02:00
|
|
|
|
2024-10-26 14:56:17 +02:00
|
|
|
// Name as described in https://github.com/semaphoreui/semaphore/issues/188
|
2022-02-03 08:05:13 +01:00
|
|
|
Name string `db:"name" json:"name"`
|
2016-04-08 21:41:20 +02:00
|
|
|
// playbook name in the form of "some_play.yml"
|
|
|
|
Playbook string `db:"playbook" json:"playbook"`
|
|
|
|
// to fit into []string
|
|
|
|
Arguments *string `db:"arguments" json:"arguments"`
|
|
|
|
// if true, semaphore will not prepend any arguments to `arguments` like inventory, etc
|
2022-01-26 20:51:20 +01:00
|
|
|
AllowOverrideArgsInTask bool `db:"allow_override_args_in_task" json:"allow_override_args_in_task"`
|
2021-08-30 21:42:11 +02:00
|
|
|
|
|
|
|
Description *string `db:"description" json:"description"`
|
2021-09-01 21:17:28 +02:00
|
|
|
|
2024-10-05 22:16:25 +02:00
|
|
|
Vaults []TemplateVault `db:"-" json:"vaults" backup:"-"`
|
2021-10-11 23:36:35 +02:00
|
|
|
|
2021-10-14 22:25:12 +02:00
|
|
|
Type TemplateType `db:"type" json:"type"`
|
2021-10-12 15:59:16 +02:00
|
|
|
StartVersion *string `db:"start_version" json:"start_version"`
|
2024-10-05 22:16:25 +02:00
|
|
|
BuildTemplateID *int `db:"build_template_id" json:"build_template_id" backup:"-"`
|
2021-10-25 20:09:46 +02:00
|
|
|
|
2024-10-05 22:16:25 +02:00
|
|
|
ViewID *int `db:"view_id" json:"view_id" backup:"-"`
|
2021-10-26 20:19:12 +02:00
|
|
|
|
2024-10-05 22:16:25 +02:00
|
|
|
LastTask *TaskWithTpl `db:"-" json:"last_task" backup:"-"`
|
2022-01-16 21:14:44 +01:00
|
|
|
|
2022-01-19 20:35:59 +01:00
|
|
|
Autorun bool `db:"autorun" json:"autorun"`
|
2022-01-18 20:17:48 +01:00
|
|
|
|
2024-09-16 21:42:05 +02:00
|
|
|
// override variables
|
|
|
|
GitBranch *string `db:"git_branch" json:"git_branch"`
|
|
|
|
|
2022-01-19 13:05:48 +01:00
|
|
|
// SurveyVarsJSON used internally for read from database.
|
|
|
|
// It is not used for store survey vars to database.
|
|
|
|
// Do not use it in your code. Use SurveyVars instead.
|
2024-10-20 23:06:51 +02:00
|
|
|
SurveyVarsJSON *string `db:"survey_vars" json:"-" backup:"-"`
|
|
|
|
SurveyVars []SurveyVar `db:"-" json:"survey_vars" backup:"survey_vars"`
|
2022-02-12 13:15:15 +01:00
|
|
|
|
|
|
|
SuppressSuccessAlerts bool `db:"suppress_success_alerts" json:"suppress_success_alerts"`
|
2024-01-30 10:26:11 +01:00
|
|
|
|
|
|
|
App TemplateApp `db:"app" json:"app"`
|
2024-06-30 09:48:36 +02:00
|
|
|
|
2024-10-05 22:16:25 +02:00
|
|
|
Tasks int `db:"tasks" json:"tasks" backup:"-"`
|
2021-10-11 23:36:35 +02:00
|
|
|
}
|
2021-10-12 12:25:43 +02:00
|
|
|
|
2021-11-02 20:30:45 +01:00
|
|
|
func (tpl *Template) Validate() error {
|
2024-04-19 19:24:46 +02:00
|
|
|
switch tpl.App {
|
2024-07-16 08:35:36 +02:00
|
|
|
case AppAnsible:
|
2024-04-19 18:47:08 +02:00
|
|
|
if tpl.InventoryID == nil {
|
|
|
|
return &ValidationError{"template inventory can not be empty"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
if tpl.Name == "" {
|
|
|
|
return &ValidationError{"template name can not be empty"}
|
2021-11-02 20:30:45 +01:00
|
|
|
}
|
|
|
|
|
2024-06-12 21:53:00 +02:00
|
|
|
if !tpl.App.IsTerraform() && tpl.Playbook == "" {
|
2021-11-02 20:30:45 +01:00
|
|
|
return &ValidationError{"template playbook can not be empty"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tpl.Arguments != nil {
|
|
|
|
if !json.Valid([]byte(*tpl.Arguments)) {
|
|
|
|
return &ValidationError{"template arguments must be valid JSON"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-12 12:25:43 +02:00
|
|
|
func FillTemplate(d Store, template *Template) (err error) {
|
2024-10-02 17:49:33 +02:00
|
|
|
var vaults []TemplateVault
|
|
|
|
vaults, err = d.GetTemplateVaults(template.ProjectID, template.ID)
|
2021-10-13 16:07:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-10-02 17:49:33 +02:00
|
|
|
template.Vaults = vaults
|
2021-10-13 16:07:22 +02:00
|
|
|
|
2023-06-13 11:35:24 +02:00
|
|
|
var tasks []TaskWithTpl
|
2024-07-19 21:38:35 +02:00
|
|
|
tasks, err = d.GetTemplateTasks(template.ProjectID, template.ID, RetrieveQueryParams{Count: 1})
|
2023-06-13 11:35:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(tasks) > 0 {
|
|
|
|
template.LastTask = &tasks[0]
|
|
|
|
}
|
2022-01-19 13:05:48 +01:00
|
|
|
|
2023-06-13 12:08:29 +02:00
|
|
|
if template.SurveyVarsJSON != nil {
|
|
|
|
err = json.Unmarshal([]byte(*template.SurveyVarsJSON), &template.SurveyVars)
|
|
|
|
}
|
|
|
|
|
2021-10-12 12:25:43 +02:00
|
|
|
return
|
|
|
|
}
|