From 79008d1458290baa156802e475ec1f81a93e4de0 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Sun, 24 Nov 2024 15:32:24 +0500 Subject: [PATCH] refactor(be): add local app params class --- db/Task.go | 3 +++ db_lib/AnsibleApp.go | 4 ++-- db_lib/LocalApp.go | 10 +++++++++- db_lib/ShellApp.go | 7 +++---- db_lib/TerraformApp.go | 6 +++--- services/tasks/LocalJob.go | 33 ++++++++++++++++++++++++++++----- 6 files changed, 48 insertions(+), 15 deletions(-) diff --git a/db/Task.go b/db/Task.go index 43609a59..4a33b4d6 100644 --- a/db/Task.go +++ b/db/Task.go @@ -9,6 +9,9 @@ import ( "github.com/semaphoreui/semaphore/util" ) +type ShellTaskParams struct { +} + type TerraformTaskParams struct { Plan bool `json:"plan"` AutoApprove bool `json:"auto_approve"` diff --git a/db_lib/AnsibleApp.go b/db_lib/AnsibleApp.go index 88a629c8..01bc5ecf 100644 --- a/db_lib/AnsibleApp.go +++ b/db_lib/AnsibleApp.go @@ -61,8 +61,8 @@ func (t *AnsibleApp) SetLogger(logger task_logger.Logger) task_logger.Logger { return logger } -func (t *AnsibleApp) Run(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error { - return t.Playbook.RunPlaybook(args, environmentVars, inputs, cb) +func (t *AnsibleApp) Run(args LocalAppRunningArgs) error { + return t.Playbook.RunPlaybook(args.CliArgs, args.EnvironmentVars, args.Inputs, args.Callback) } func (t *AnsibleApp) Log(msg string) { diff --git a/db_lib/LocalApp.go b/db_lib/LocalApp.go index 61b7c27a..b097430d 100644 --- a/db_lib/LocalApp.go +++ b/db_lib/LocalApp.go @@ -27,8 +27,16 @@ func getEnvironmentVars() []string { return res } +type LocalAppRunningArgs struct { + CliArgs []string + EnvironmentVars *[]string + Inputs map[string]string + TaskParams interface{} + Callback func(*os.Process) +} + type LocalApp interface { SetLogger(logger task_logger.Logger) task_logger.Logger InstallRequirements(environmentVars *[]string) error - Run(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error + Run(args LocalAppRunningArgs) error } diff --git a/db_lib/ShellApp.go b/db_lib/ShellApp.go index 10e17d41..8d295795 100644 --- a/db_lib/ShellApp.go +++ b/db_lib/ShellApp.go @@ -2,7 +2,6 @@ package db_lib import ( "fmt" - "os" "os/exec" "strings" "time" @@ -107,8 +106,8 @@ func (t *ShellApp) makeShellCmd(args []string, environmentVars *[]string) *exec. return t.makeCmd(command, append(appArgs, args...), environmentVars) } -func (t *ShellApp) Run(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error { - cmd := t.makeShellCmd(args, environmentVars) +func (t *ShellApp) Run(args LocalAppRunningArgs) error { + cmd := t.makeShellCmd(args.CliArgs, args.EnvironmentVars) t.Logger.LogCmd(cmd) //cmd.Stdin = &t.reader cmd.Stdin = strings.NewReader("") @@ -116,6 +115,6 @@ func (t *ShellApp) Run(args []string, environmentVars *[]string, inputs map[stri if err != nil { return err } - cb(cmd.Process) + args.Callback(cmd.Process) return cmd.Wait() } diff --git a/db_lib/TerraformApp.go b/db_lib/TerraformApp.go index 71d89563..73b6a1db 100644 --- a/db_lib/TerraformApp.go +++ b/db_lib/TerraformApp.go @@ -148,8 +148,8 @@ func (t *TerraformApp) Apply(args []string, environmentVars *[]string, inputs ma return cmd.Wait() } -func (t *TerraformApp) Run(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error { - err := t.Plan(args, environmentVars, inputs, cb) +func (t *TerraformApp) Run(args LocalAppRunningArgs) error { + err := t.Plan(args.CliArgs, args.EnvironmentVars, args.Inputs, args.Callback) if err != nil { return err } @@ -173,7 +173,7 @@ func (t *TerraformApp) Run(args []string, environmentVars *[]string, inputs map[ return nil case terraformReaderConfirmed: t.Logger.SetStatus(task_logger.TaskRunningStatus) - return t.Apply(args, environmentVars, inputs, cb) + return t.Apply(args.CliArgs, args.EnvironmentVars, args.Inputs, args.Callback) default: return fmt.Errorf("unknown plan result") } diff --git a/services/tasks/LocalJob.go b/services/tasks/LocalJob.go index 04c3da8a..e0704c0a 100644 --- a/services/tasks/LocalJob.go +++ b/services/tasks/LocalJob.go @@ -323,15 +323,22 @@ func (t *LocalJob) getPlaybookArgs(username string, incomingVersion *string) (ar } } - if t.Task.Debug { + var params db.AnsibleTaskParams + + err = t.Task.GetParams(¶ms) + if err != nil { + return + } + + if params.Debug { args = append(args, "-vvvv") } - if t.Task.Diff { + if params.Diff { args = append(args, "--diff") } - if t.Task.DryRun { + if params.DryRun { args = append(args, "--check") } @@ -419,20 +426,30 @@ func (t *LocalJob) Run(username string, incomingVersion *string) (err error) { var args []string var inputs map[string]string + var params interface{} switch t.Template.App { case db.AppAnsible: args, inputs, err = t.getPlaybookArgs(username, incomingVersion) + params = db.AnsibleTaskParams{} case db.AppTerraform, db.AppTofu: args, err = t.getTerraformArgs(username, incomingVersion) + params = db.TerraformTaskParams{} default: args, err = t.getShellArgs(username, incomingVersion) + params = db.ShellTaskParams{} } if err != nil { return } + err = t.Task.GetParams(params) + + if err != nil { + return + } + if t.Inventory.SSHKey.Type == db.AccessKeySSH && t.Inventory.SSHKeyID != nil { environmentVariables = append(environmentVariables, fmt.Sprintf("SSH_AUTH_SOCK=%s", t.sshKeyInstallation.SSHAgent.SocketFile)) } @@ -454,8 +471,14 @@ func (t *LocalJob) Run(username string, incomingVersion *string) (err error) { } } - return t.App.Run(args, &environmentVariables, inputs, func(p *os.Process) { - t.Process = p + return t.App.Run(db_lib.LocalAppRunningArgs{ + CliArgs: args, + EnvironmentVars: &environmentVariables, + Inputs: inputs, + TaskParams: params, + Callback: func(p *os.Process) { + t.Process = p + }, }) }