From 989d3b4a8e7e1cd96c503e4dcae253856f86da28 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Wed, 10 Jul 2024 13:12:01 +0500 Subject: [PATCH] feat(apps): change log message --- api/projects/templates.go | 12 ++++++++++++ db_lib/AppFactory.go | 15 +++------------ db_lib/ShellApp.go | 6 ++++++ services/tasks/LocalJob.go | 6 ++---- services/tasks/TaskRunner.go | 6 +++--- web/src/components/EditTemplateDialog.vue | 1 + web/src/components/TemplateForm.vue | 5 ++++- 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/api/projects/templates.go b/api/projects/templates.go index 71dfe2ca..8e419500 100644 --- a/api/projects/templates.go +++ b/api/projects/templates.go @@ -2,6 +2,7 @@ package projects import ( "fmt" + "github.com/ansible-semaphore/semaphore/util" "net/http" "github.com/ansible-semaphore/semaphore/api/helpers" @@ -81,6 +82,12 @@ func AddTemplate(w http.ResponseWriter, r *http.Request) { return } + if _, ok := util.Config.Apps[string(newTemplate.App)]; !ok { + helpers.WriteErrorStatus(w, "Invalid app id", http.StatusBadRequest) + return + } + + // Check workspace and create it if required. if newTemplate.App.IsTerraform() { var inv db.Inventory @@ -138,6 +145,11 @@ func UpdateTemplate(w http.ResponseWriter, r *http.Request) { return } + if _, ok := util.Config.Apps[string(template.App)]; !ok { + helpers.WriteErrorStatus(w, "Invalid app id", http.StatusBadRequest) + return + } + // project ID and template ID in the body and the path must be the same if template.ID != oldTemplate.ID { diff --git a/db_lib/AppFactory.go b/db_lib/AppFactory.go index 55783b07..d96a5dc3 100644 --- a/db_lib/AppFactory.go +++ b/db_lib/AppFactory.go @@ -18,28 +18,19 @@ func CreateApp(template db.Template, repository db.Repository, logger task_logge Logger: logger, }, } - case db.TemplateTerraform: + case db.TemplateTerraform, db.TemplateTofu: return &TerraformApp{ Template: template, Repository: repository, Logger: logger, - Name: TerraformAppTerraform, + Name: TerraformAppName(template.App), } - case db.TemplateTofu: - return &TerraformApp{ - Template: template, - Repository: repository, - Logger: logger, - Name: TerraformAppTofu, - } - case db.TemplateBash, db.TemplatePowerShell, db.TemplatePython: + default: return &ShellApp{ Template: template, Repository: repository, Logger: logger, App: template.App, } - default: - panic("unknown app: " + template.App) } } diff --git a/db_lib/ShellApp.go b/db_lib/ShellApp.go index 1cdb5a77..c6a8fc3a 100644 --- a/db_lib/ShellApp.go +++ b/db_lib/ShellApp.go @@ -95,6 +95,12 @@ func (t *ShellApp) makeShellCmd(args []string, environmentVars *[]string) *exec. case db.TemplatePowerShell: command = "powershell" appArgs = []string{"-File"} + default: + if app, ok := util.Config.Apps[string(t.App)]; ok && app.AppPath != "" { + command = app.AppPath + } else { + command = string(t.App) + } } return t.makeCmd(command, append(appArgs, args...), environmentVars) } diff --git a/services/tasks/LocalJob.go b/services/tasks/LocalJob.go index 12197b8a..424ee150 100644 --- a/services/tasks/LocalJob.go +++ b/services/tasks/LocalJob.go @@ -165,7 +165,7 @@ func (t *LocalJob) getEnvironmentENV() (arr []string, err error) { } // nolint: gocyclo -func (t *LocalJob) getBashArgs(username string, incomingVersion *string) (args []string, err error) { +func (t *LocalJob) getShellArgs(username string, incomingVersion *string) (args []string, err error) { extraVars, err := t.getEnvironmentExtraVars(username, incomingVersion) args = append(args, t.Template.Playbook) @@ -378,10 +378,8 @@ func (t *LocalJob) Run(username string, incomingVersion *string) (err error) { args, inputs, err = t.getPlaybookArgs(username, incomingVersion) case db.TemplateTerraform, db.TemplateTofu: args, err = t.getTerraformArgs(username, incomingVersion) - case db.TemplateBash, db.TemplatePowerShell, db.TemplatePython: - args, err = t.getBashArgs(username, incomingVersion) default: - panic("unknown template app") + args, err = t.getShellArgs(username, incomingVersion) } if err != nil { diff --git a/services/tasks/TaskRunner.go b/services/tasks/TaskRunner.go index 06144b32..d52a4f58 100644 --- a/services/tasks/TaskRunner.go +++ b/services/tasks/TaskRunner.go @@ -158,7 +158,7 @@ func (t *TaskRunner) run() { err = t.job.Run(username, incomingVersion) if err != nil { - t.Log("Running playbook failed: " + err.Error()) + t.Log("Running app failed: " + err.Error()) t.SetStatus(task_logger.TaskFailStatus) return } @@ -172,7 +172,7 @@ func (t *TaskRunner) run() { AutorunOnly: true, }, db.RetrieveQueryParams{}) if err != nil { - t.Log("Running playbook failed: " + err.Error()) + t.Log("Running app failed: " + err.Error()) return } @@ -183,7 +183,7 @@ func (t *TaskRunner) run() { BuildTaskID: &t.Task.ID, }, nil, tpl.ProjectID) if err != nil { - t.Log("Running playbook failed: " + err.Error()) + t.Log("Running app failed: " + err.Error()) continue } } diff --git a/web/src/components/EditTemplateDialog.vue b/web/src/components/EditTemplateDialog.vue index 82864e28..ab9327a3 100644 --- a/web/src/components/EditTemplateDialog.vue +++ b/web/src/components/EditTemplateDialog.vue @@ -1,5 +1,6 @@